Jump to content
  • Advertisement
Sign in to follow this  
kzar

Making balls bounce away from each other

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
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.


Thanks for your reply, replacing the trigonometry with the impulse worked. To answer your question about what the bounce does, here is the function that moves balls:


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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!