# Legacy GMCollision between balls with physical effect

#### Bhreno

##### Member
In my current project, there are balls that randomly come out of the map (without gravity) and on the way they can collide with 3 different things:
> solid blocks where they hit and deflect.
> breakable blocks, where they destroy and deflect.
> other balls, they hit and deflect each other.

I would like to make sure that when the balls collide, the one with the highest speed would end up acting at the speed and direction of the other.
Example: Two boys play ball and each is kicking one. They kick both in the same direction. One is with speed force 10, the other with force 20. The higher force will logically "push" and change the direction of the other when both collide. But I can't do it in GMS. The Bounce functions I try to use always end up just "reversing the course" after the crash, rather than the actual physics effect.
can you help me?

#### Relic

##### Member
Is this a physics room? I’m guessing not.

Are the balls restricted to one dimensional movement? You have made it sound like it is.

For elastic collisions (no loss of energy) two objects of identical mass swap velocities along the dimension of the collision.

Your collision code will have to:
1) get the speed and direction of both balls.
2) swap the speed and direction

#### 2Dcube

##### Member
Have you considered using game maker's built in physics system? The balls would behave very naturally.

Alternatively, you can do the calculations yourself when 2 balls collide. You'd need the x and y velocity of each and the angle of impact between them.

Here is a tutorial that explains ball collision math: https://www.emanueleferonato.com/2007/08/19/managing-ball-vs-ball-collision-with-flash/
It's for Flash Action Script but should be similar to GML.

#### Bhreno

##### Member
Look, each ball has its speed and direction set and can even make a collision with physics. However, when they collide, sometimes they just cross another. Already tried to change the mask, precise, rectangular, full image... Nothing of them worked. The collision still causes one ball to lock on the other several times.
What can I do to prevent this from happening? Like a check steps or something...

Last edited: