|
Author |
Topic: couple small bugs in "collision.pde" demo (Read 996 times) |
|
kevinP
|
couple small bugs in "collision.pde" demo
« on: Jan 13th, 2004, 9:49pm » |
|
While playing with one of the demo scripts (collision.pde, the "pong" one) I found a couple small bugs (in the script, not processing). Perhaps they've long been fixed as I haven't read much here yet, but in case not... No guarantees on my solutions. -K // Collision (Pong) // by REAS <http://www.groupc.net> // Move the mouse up and down to move the paddle. // Updated 21 August 2002 // Modified 13 Jan 2004 - K Pfeiffer // Fixed bug that the first serve was not detected by the paddle due to ball_x incrementing by 2 // Fixed bug that paddle was constrained to edge of frame, but detection area moved half outside // 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; // Was 0, but would miss 1st serve - see comment below framerate(30); } void loop() { background(51); ball_x += ball_dir * 2; 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 = width - dist_wall - paddle_width - ball_size; if(ball_x == py // Careful, as ball_x increments by 2! // && ball_y > mouseY - paddle_height - ball_size // && ball_y < mouseY + paddle_height + ball_size) { && 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 paddles and constrain to the screen // float paddle_y = constrain(mouseY, paddle_height, height-paddle_height); // This was moved up so that paddle detection also uses same constraint on paddle position! fill(153); rect(width-dist_wall, paddle_y, paddle_width, paddle_height); }
|
« Last Edit: Jan 13th, 2004, 9:50pm by kevinP » |
|
Kevin Pfeiffer
|
|
|
REAS
|
Re: couple small bugs in "collision.pde" demo
« Reply #1 on: Jan 14th, 2004, 3:16am » |
|
thank you for taking the time to do this. i've noticed problems with this example and haven't made fixing them a priority. you've made fixing it very easy. i've corrected it now and it will be posted with the next release of the software. + casey
|
|
|
|
|