Making balls bounce away from each other

This topic is 4882 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, in my game I need to make balls bounce away from each other. I want the balls to bounce away an unrealisticly large distance, and I don't want that distance to change relative to the balls speed or anything. So far I have got the collision check code working and the code to handle bouncing is almost there. My problem is how to split the bounce vector between the two balls to cause them to bounce away from each other. (If you get it the wrong way around the fly through each other) Here is my handle_collision function, I *think* its fine except the "Split the bounce vector between the balls" section. (By the way, to be clear the balls have two seperate velocity's, vel is controlled by the mouse and bounce is set here and decayed over time)
void handle_collision(struct Sball *ball, struct Sball *ball2)
{
float impulse_x, impulse_y;
float distance_x, distance_y, distance_length;
float angle, bounce_x, bounce_y;

/* Calculate the impulse vector */
distance_x = ball->pos.x - ball2->pos.x;
distance_y = ball->pos.y - ball2->pos.y;
distance_length = sqrt((distance_x * distance_x) + (distance_y * distance_y));

impulse_x = distance_x / distance_length;
impulse_y = distance_y / distance_length;

/* Figure out the angle */
angle = atan(impulse_x / impulse_y);

/* Figure out the total bounce vector */
bounce_x = BOUNCE_AMOUNT * sin(angle);
bounce_y = BOUNCE_AMOUNT * cos(angle);

/* Split the bounce vector between the balls */
ball->bounce.x += (bounce_x / 2);
ball->bounce.y += (bounce_y / 2);
ball2->bounce.x -= (bounce_x / 2);
ball2->bounce.y -= (bounce_y / 2);

/* Zero the input velocity */
ball->vel.x = 0;
ball->vel.y = 0;
ball2->vel.x = 0;
ball2->vel.y = 0;
}


What do you think? Thanks, David [Edited by - kzar on September 11, 2005 9:22:04 AM]

Share on other sites
How does bounce affect the position of a ball? What you are doing is really a lot different than normal real world physics, so really it is hard to comment on it.

Also the way you are performing the math, I believe impulse_x is equal to sin(angle) and impulse_y is equal to cos(angle) (possibly with some minor rounding error). So you can avoid the trig functions and simultaneously avoid a division by 0 case where distance_y is 0.

It might make the code a bit more robust if you check for the case where the distance between the objects is close to 0, to avoid a division by close to 0 there (and then possibly use some other method to determine the bounce direction, maybe based on the velocities or arbitrarily pick a direction).

The code looks right, other than that you're using a physics model that isn't based on real physics so I can't tell what you're doing with the bounce quantity. In a real world physics based model, you'd be applying forces to the colliding objects which would then be used to calculate accelerations which would alter the velocities. And in a realistic collision model, the forces would be in the same directions that you are applying the bounce, but they would be based on the momentums and elasticity of the colliding objects.

Actually I just noticed that using atan is probably screwing up the collision direction half the time. So taking out the trig stuff and replacing cos(angle) and sin(angle) with the impulse_y and impulse_x will probably fix your problem.

Share on other sites
Quote:
 Original post by Anonymous PosterHow does bounce affect the position of a ball? What you are doing is really a lot different than normal real world physics, so really it is hard to comment on it.Also the way you are performing the math, I believe impulse_x is equal to sin(angle) and impulse_y is equal to cos(angle) (possibly with some minor rounding error). So you can avoid the trig functions and simultaneously avoid a division by 0 case where distance_y is 0.It might make the code a bit more robust if you check for the case where the distance between the objects is close to 0, to avoid a division by close to 0 there (and then possibly use some other method to determine the bounce direction, maybe based on the velocities or arbitrarily pick a direction).The code looks right, other than that you're using a physics model that isn't based on real physics so I can't tell what you're doing with the bounce quantity. In a real world physics based model, you'd be applying forces to the colliding objects which would then be used to calculate accelerations which would alter the velocities. And in a realistic collision model, the forces would be in the same directions that you are applying the bounce, but they would be based on the momentums and elasticity of the colliding objects.Actually I just noticed that using atan is probably screwing up the collision direction half the time. So taking out the trig stuff and replacing cos(angle) and sin(angle) with the impulse_y and impulse_x will probably fix your problem.

void move_ball(struct Sball *ball){       ball->pos.x += ((ball->vel.x + ball->bounce.x) * time_passed * DELTA);    ball->pos.y += ((ball->vel.y + ball->bounce.y) * time_passed * DELTA);}

Is there a more normal way of achieving the same effect that I have done? I would like to make my physics in a more standard way and I have been trying to read my copy of "Physics for games programmers" but its a bit over my head at the moment :(

Anyway thanks again

Share on other sites
The effect you want isn't based on real world physics, so there's not really a way to model it using real world physics. Which means what you're doing is fine.

It looks like bounce is used in the same way as velocity. I'm wondering if you can just combine them together and add some sort of friction that acts on the velocity instead. It depends on what effect you're trying to get.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• Forum Statistics

• Total Topics
634135
• Total Posts
3015754
×