Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problems with algorithm for bouncing between two circles

This thread is locked; no one can reply to it. rss feed Print
Problems with algorithm for bouncing between two circles
Loki66
Member #17,089
May 2019

Hi everyone
Problems with algorithm for bouncing between two circles

sorry for the bad english

source code
// bounce calculation
void fv_bounce(double fst_sp_x, double sec_sp_x, double fst_sp_y, double sec_sp_y, double fst_m, double sec_m) {

/*newVelX1 = (firstBall.speed.x * (firstBall.mass – secondBall.mass) +
(2 * secondBall.mass * secondBall.speed.x)) / (firstBall.mass + secondBall.mass);*/
double ld_dx1 = (fst_sp_x * (fst_m - sec_m) + (2 * sec_m * sec_sp_x)) / (fst_m + sec_m);

/*newVelY1 = (firstBall.speed.y * (firstBall.mass – secondBall.mass) +
(2 * secondBall.mass * secondBall.speed.y)) / (firstBall.mass + secondBall.mass);*/
double ld_dy1 = (fst_sp_y * (fst_m - sec_m) + (2 * sec_m * sec_sp_y)) / (fst_m + sec_m);

/*newVelX2 = (secondBall.speed.x * (secondBall.mass – firstBall.mass) +
(2 * firstBall.mass * firstBall.speed.x)) / (firstBall.mass + secondBall.mass);*/
double ld_dx2 = (sec_sp_x * (sec_m - fst_m) + (2 * fst_m * fst_sp_x)) / (fst_m + sec_m);

/*newVelY2 = (secondBall.speed.y * (secondBall.mass – firstBall.mass) +
(2 * firstBall.mass * firstBall.speed.y)) / (firstBall.mass + secondBall.mass);*/
double ld_dy2 = (sec_sp_y * (sec_m - fst_m) + (2 * fst_m * fst_sp_y)) / (fst_m + sec_m);

d_enemy_dx = ld_dx1, d_enemy_dy = ld_dy1;
d_dsk_dx = ld_dx2, d_dsk_dy = ld_dy2;
}

Explanation:
this algorithm is found on the site: https://gamedevelopment.tutsplus.com/tutorials/when-worlds-collide-simulating-circle-circle-collisions--gamedev-769

the part that deals with the bounce is this:

newVelX1 = (firstBall.speed.x * (firstBall.mass – secondBall.mass) + (2 * secondBall.mass * secondBall.speed.x)) / (firstBall.mass + secondBall.mass);
newVelY1 = (firstBall.speed.y * (firstBall.mass – secondBall.mass) + (2 * secondBall.mass * secondBall.speed.y)) / (firstBall.mass + secondBall.mass);
newVelX2 = (secondBall.speed.x * (secondBall.mass – firstBall.mass) + (2 * firstBall.mass * firstBall.speed.x)) / (firstBall.mass + secondBall.mass);
newVelY2 = (secondBall.speed.y * (secondBall.mass – firstBall.mass) + (2 * firstBall.mass * firstBall.speed.y)) / (firstBall.mass + secondBall.mass);

The problem is that this works if both circles move, if one is stopped the calculation is wrong.
Example:
newVelX1 = (firstBall.speed.x * (firstBall.mass – secondBall.mass) +
(2 * secondBall.mass * secondBall.speed.x)) / (firstBall.mass + secondBall.mass);

If the two circles are equal, they have the same mass (firstBall.mass and secondBall.mass)
(firstBall.speed.x * (firstBall.mass – secondBall.mass) = 0

If the circle two does not move secondBall.speed.x = 0
(2 * secondBall.mass * secondBall.speed.x) = 0

Am I not understanding or does this algorithm not work?
What do you suggest I do?
Thank you all

MikiZX
Member #17,092
June 2019

Seeing that even the algorithm you present does not work with super-fast moving circles(because between two frames of collision calculation it can happen that circles that should have collided end up going "through" each-other without a collision detection) - I will go again and suggest this long video:

video

This video also has part 2 and that can be a help if you wish to expand this proposed collision later on.

If you will have super-fast moving circles on your screen then possibly you will have to use some sort of circle-circle sweep collision solutions (but I am sorry - I do not know of a good tutorial for this).

Loki66
Member #17,089
May 2019

I can't understand spoken English, and I think videos are a waste of time.
Please, if you don't understand what I write, don't answer !!!
I just need a bounce algorithm that works.

Johan Halmén
Member #1,550
September 2001

Check this thread. It's only 12 years old.
https://www.allegro.cc/forums/thread/591703

In short: When two circles collide, draw a wall through the collision point. The wall becomes a tangent to both circles. Divide each circle's velocity vector into a tangent component and a normal component (tangent component is parallel to the wall, normal component is perpendicular). Exchange the normal components between the circles. Add the components for each circle to get their new velocities. Before adding, you might want to reduce the normal components by a percentage (energy dissipation).

A circle which stands still will therefore continue perpendicular to the wall, while the other circle continueas in the wall direction. This is a well known fact among pool players: The que ball continues 90 degrees away from the ball it hit.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Loki66
Member #17,089
May 2019

Thanks, that's what I need.
I apologize, I didn't know he was only twelve

piccolo
Member #3,163
January 2003
avatar

On YouTube you can use capsions so you can read what they are saying

wow
-------------------------------
i am who you are not am i

Loki66
Member #17,089
May 2019

thank you all
I tried to calculate the bounce suggested by Johan Halmén: works well.
Check this thread.
https://www.allegro.cc/forums/thread/591703

I also tried the algorithm suggested by gary_ramsgate and this works well too.
problem solved!

Go to: