Advertisement

Bouncing Algorithm

Started by December 12, 1999 04:24 PM
13 comments, last by mmarvi 25 years ago
TANSTAAFL,

Can you thoroughly explain what what the variables x1, y1, x2, and y2 stand for? Does "pi" stand for 3.141592....?

By the way, does anyone have a good pixel-perfect collision detection algorithm that is compatible with CDX and MSVC++ 5.0?

I think that x1,y1, are the start of the line, and x2,y2, and the end of the line, and yeah, bet dats pi.
Advertisement
In reply to your question about perfect pixel collision detection, I don't know where you can find one.

However, if you are detecting collision between two circles all you have to do is find the distance between the centers of the two. If that distance is less than or equal two the sum of the two radii then they are in contact.

Distance Formula:
D = sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) )
(derived from a^2 + b^2 = c^2)

Also, about the angle of incidence of the puck on the paddle. If your paddle is perfectly rectangular then the puck will bounce off in the same direction irregardless where it hits the paddle.

I am writing a simple 2-D table hockey game using CDX and MSVC 5.0 Professional, and I want some code that can demonstrate a realistic bouncing ball algorithm that I can use in such a game.
I had to figure out the same algorithm when I was making a simple pong game. I split the paddle into 7 different sections, and I made each one 9 pixels each. When the ball's y coordinate was greater than the lower board's y coordinate and the ball's x coordinate was equal to any x coordinate of the board it was about to make contact with I specified new speed variables. I did this by making the game add the x and y speed to the x and y coordinates of the ball.
Example:

Here is the coordinate plane I used:
0,0----------------50,0 (X axis)
|
|
|
|
|
0,50
(Y axis)

Paddle is the left most coordinate of the
paddle. To get the sections, all I had to
was add 9 for each section, because each section was 9 pixels long.
ball_velocity_x is the speed of the ball on the x axis
ball_velocity_y is the speed of the ball on the y axis

paddle_left = paddle + 18;

// Left of paddle, for lower paddle
if ((ball_x >= paddle && ball_x <= paddle_left) && (ball_y > paddle_y))
{
ball_velocity_x = .4;
ball_velocity_y = 2.4;
}

Here are the angles I used for my paddle:
Left = ball_velocity_x = -.4
ball_veloctiy_y = -2.4
Left Center = ball_velocity_x = -1
ball_velocity_y = -1
Center = ball_velocity_x = 0
ball_velocity_y = -1
Right Center = ball_velocity_x = 1
ball_velocity_y = -1
Right = ball_velocity_x = .4
ball_velocity_y = -2.4

For the upper paddle I just reversed those velocity settings. I didn't include the centermost left and right areas because I forgot what they were and I don't have my program to look at right now.

If you have any questions about what I just posted please ask. I hope that helped you.

This topic is closed to new replies.

Advertisement