Monday, June 27, 2011

ball bounce

public void move (Rectangle bounds) I
// Add velocity values dx/dy to position to
get ,
// ball s new position
x +_ dx;
y +° dy;
// Check for collision with left edge
if (x < bounds.x && dx < 0)
dx = -dx;
x -= 2 * (x - bounds.x);
// Check for collision with right edge
else if ((x + size) ) (bounds.x + bounds.width) &&
d x > 0' )
dx = -dx;
x -= 2 * ((x + size) - (bounds.x + bounds.width));
// Check for collision with top edge
if (y < bounds.y && dy < 0) I
dy = -dy;
y -° 2 * (y - bounds.y);
// Check for collision with bottom edge
else i f (( y + size) > ( bounds.y + bounds.height) &&
dy = -dy;
y -= 2 * ((y + size) - (bounds.y + bounds.height));































No comments:

Post a Comment