# balls colliding...

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

## Recommended Posts

So, this is the situation... I have like 20 - 30 balls going in all directions..., I control their movement using 2 values..., angle and speed..., and using basic trig, I calculate the new X and Y of the sprite...(I'm using this in 2D) Now, I've figured out the right equations to handle the bouncing against the edges of the screen, but when it comes to collison of balls against balls.., I'm a little lost about how to calculate the new angle and speed of every ball after they collide..... Any body has the equations?? that would make my life reeeeeeeeeeally easy... And to make things easier... all balls have the same mass.... Well, thanks in advance!! Haora

##### Share on other sites
You have angles... Yes, we could work out the equations for you, but are you sure angles are the way forward? What I'm trying to say is, it is much more common, and much simpler, to use two velocity components, one in the X direction and one in the Y direction.

Have you chosen to use a speed and angle instead for a particular reason, or is it because that is the way which occured to you and you went with it? Either way is fine, and kudos for obviously getting this far without just copying everyone elses code, but if it's the latter I would be happier proposing an alternative system than giving you equations for the one you are using [smile].

##### Share on other sites
That's the way it occured to me in the first place...and I went with it, then I realized that I could do the same with vectors, but didn't give it much thought...

I would really apretiate your help in using an alternative system....

Thanks a lot!

Haora

##### Share on other sites
Cool - OK.

With vectors, basically your ball is at (Px,Py) and has velocities (Vx,Vy).
Then you don't need any trig. for the movement, you just add Vx to Px and Vy to Py. Bouncing from a vertical wall Vy = -Vy. Bouncing from a horizontal wall Vx = -Vx.

Can I assume you know the basic vector maths? adding, subtracting, getting lengths, doing dot products, etc? The following is not psuedo code - it's an explanation of the process.

Bouncing from another ball goes like this:

First, find out if you are colliding. This will also entail obtaining a vector which goes from one ball to the other. (gap = pos2 - pos1)

Normalize that vector so that it is unit length. (norm = normal(gap))

Now - the dot product of a balls velocity with that vector is the magnitude of it's velocity in that direction. For a perfectly elastic collision of equal mass balls, all that will happen is that they will exchange this component with each other. So:

// get the components:
component_of_vel_1 = vel_1 <dot> norm
component_of_vel_2 = vel_2 <dot> -norm
// exchange them:
vel_1 = vel_1 - component_of_vel_1 + component_of_vel_2
vel_2 = vel_2 - component_of_vel_2 + component_of_vel_1

If you need help with the basic idea of vector maths, just say... or search google for a tutorial... I hope this shows that life is much simpler this way than when trigonometry and angles are involved :o)

##### Share on other sites
Wow, thanks!
I haven't tryed it yet, but I'll let you know how it turns out...

Thanks again!!!

Haora

##### Share on other sites
Well, I did it the way to told me..., very nice...,thanks again!, everything works allmost great..., there's only one problem..
if I use this formula to exchange the speed between the balls:
Quote:
 // exchange them:vel_1 = vel_1 - component_of_vel_1 + component_of_vel_2vel_2 = vel_2 - component_of_vel_2 + component_of_vel_1

they get accelerated every time they collide, and it gets out of control...
To solve this I exchanged them like this:

aux = vel_1
vel_1 = vel_2
vel_2 = aux;

Now, I'm having another problem, but I don't know if it is because of what I just said, or 'cause of something else..., but sometimes, 2 balls, after their collision, will get the same speed vector, and move togheter..., do you have any ideas about what could be the problem???

Haora

##### Share on other sites
Quote:
 Original post by haoraNow, I'm having another problem, but I don't know if it is because of what I just said, or 'cause of something else..., but sometimes, 2 balls, after their collision, will get the same speed vector, and move togheter..., do you have any ideas about what could be the problem???

probably they get stuck inside eachother.

try pushing the balls outward by the collision normal to prevent this from happening.

##### Share on other sites
Quote:
 probably they get stuck inside eachother.try pushing the balls outward by the collision normal to prevent this from happening.

Thanks for the reply, but I'm not that great with vectors..., what is the collision normal??, the normalized vector I calculated like Squirm said??? or is it something else?

Thanks again!

Haora

##### Share on other sites
Yup, it's the normalized vector... but you need to play with it a little first.

You have a distance between the centers of the balls, and you have the sum of their radii, and you find that it collides because the radii sum is greater than the distance. You want:

Now, there are various things you can do, at various levels of complexity, with better or worse results. For now, ignore ball2, and just move ball1 as :

ball1.pos = ball1.pos - (norm * depth)

A better approach is to move both balls by half the depth, and better still is to take their differnt velocities into account, but for the time being that solution will do, and noone is likely to notice. Another post follows this one.

##### Share on other sites
I don't understand why you need a temporary variable ... the velocities don't swap completely, only the components of them which are in the direction of the normal... for example, a moving ball which just glances off the edge of a stationary ball should barely change direction atall, and the stationary one should gain very little speed from it. They will exchange only a small amount of their velocity, and because these components are in separate variables, no 'aux' should be required ?

• ### What is your GameDev Story?

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

• 14
• 14
• 45
• 22
• 27
• ### Forum Statistics

• Total Topics
634044
• Total Posts
3015211
×