We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Pong Example
Page Index Toggle Pages: 1
Pong Example (Read 4862 times)
Pong Example
Oct 17th, 2005, 3:36pm
 
Hello,

I am using the Pong example

http://www.processing.org/learning/examples/collision.html

to help me better understand how collisions work in processing and I have been trying to recreate this program so the paddle hit the ball from left>right instead of the original right>left.

I think the problem is probably something to do with my math but I am not sure. Right now I am kind of stuck and I would appreciate any help. Thanks.

<code>

// Global variables for the ball
float ball_x;
float ball_y;
float ball_dir = 1;
float ball_size = 5;  // Radius
float dy = 0;  // Direction

// Global variables for the paddle
int paddle_width = 5;
int paddle_height = 20;

int dist_wall = 15;

void setup()
{
 size(200, 200);
 rectMode(CENTER_RADIUS);
 ellipseMode(CENTER_RADIUS);
 noStroke();
 ball_y = height/2;
 ball_x = -1;
 framerate(30);
}

void draw()
{
 background(51);
 
 ball_x += ball_dir * 2;
 ///int z=200;
 //if(z>0){
 //z--;}
// ball_x=z;
 ball_y += dy;
 if(ball_x > width+ball_size) {
   ball_x = -width/2-ball_size;
   ball_y = random(0, height);
   dy = 0;
 }
 
 // Constrain paddle to screen
 float paddle_y = constrain(mouseY, paddle_height, height-paddle_height);

 // Test to see if the ball is touching the paddle
 float py = dist_wall-paddle_width-ball_size;
 if(ball_x == py
    && ball_y > paddle_y - paddle_height - ball_size
    && ball_y < paddle_y + paddle_height + ball_size) {
   ball_dir *= 1;
   if(mouseY != pmouseY) {
     dy = (mouseY-pmouseY)/2.0;
     if(dy <  5) { dy =  5; }
     if(dy > -5) { dy = -5; }
   }
 }
 
 // If ball hits paddle or back wall, reverse direction
 if(ball_x < ball_size && ball_dir == -1) {
   ball_dir *= -1;
 }
 
 // If the ball is touching top or bottom edge, reverse direction
 if(ball_y > height-ball_size) {
   dy = dy * -1;
 }
 if(ball_y < ball_size) {
   dy = dy * -1;
 }

 // Draw ball
 fill(255);
 ellipse(ball_x, ball_y, ball_size, ball_size);
 
 // Draw the paddle
 fill(153);
 rect(dist_wall, paddle_y, paddle_width, paddle_height);  
}
</code>
Re: Pong Example
Reply #1 - Oct 22nd, 2005, 1:08am
 
Don't think everybody is angry with you Wink... The time is a rare treasure, and I think everybody are too busy.
I hope this help you.

Quote:


// Collision (Pong)
// by REAS <http://reas.com>

// Move the mouse up and down to move the paddle.

// Updated 13 January 2003 by K Pfeiffer


// Global variables for the ball
float ball_x;
float ball_y;
float ball_dir = 1;
float ball_size = 5;  // Radius
float dy = 0;  // Direction

// Global variables for the paddle
int paddle_width = 5;
int paddle_height = 20;

int dist_wall = 15;

void setup()
{
 size(200, 200);
 rectMode(CENTER_RADIUS);
 ellipseMode(CENTER_RADIUS);
 noStroke();
 ball_y = height/2;
 ball_x = 1;
 framerate(30);
}

void draw()
{
 background(51);
 
 ball_x += ball_dir * 2;
 ball_y += dy;
 if(ball_x < -ball_size) { //<<<<<<<<<<<<1
   ball_x = width+ball_size;
   ball_y = random(0, height);
   dy = 0;
 }
 
 // Constrain paddle to screen
 float paddle_y = constrain(mouseY, paddle_height, height-paddle_height);

 // Test to see if the ball is touching the paddle
 float py = dist_wall+paddle_width+ball_size; //<<<<<<<<<<<<2
 if(ball_x == py
    && ball_y > paddle_y - paddle_height - ball_size
    && ball_y < paddle_y + paddle_height + ball_size) {
   ball_dir *= -1;
   if(mouseY != pmouseY) {
     dy = (mouseY-pmouseY)/2.0;
     if(dy >  5) { dy =  5; }
     if(dy < -5) { dy = -5; }
   }
 }
 
 // If ball hits paddle or back wall, reverse direction
 if((ball_x > 200-ball_size) && ball_dir == 1) { //<<<<<<<<<<<<3
   ball_dir *= -1;
 }
 
 // If the ball is touching top or bottom edge, reverse direction
 if(ball_y > height-ball_size) {
   dy = dy * -1;
 }
 if(ball_y < ball_size) {
   dy = dy * -1;
 }

 // Draw ball
 fill(255);
 ellipse(ball_x, ball_y, ball_size, ball_size);
 
 // Draw the paddle
 fill(153);
 rect(dist_wall, paddle_y, paddle_width, paddle_height); //<<<<<<<<<<<<4
}

Page Index Toggle Pages: 1