# Magus' explanatory document about Coders Strike Back

Hi, I’ve been reading the document Magus published about “Coders Strike Back” but I can’t understand the calculations he does in the method called “bounce”, I have also read about conservation of the linear momentum but I can’t see the relation between the calculations he does and what conservation of the momentum says. Could anybody give me a clue please?

I know and i use the formula but i can’t really say i understand it myself. @pb4608 could explain it far better than me.

1 Like

Most of the idea of the computation can be found here on wikipedia : https://en.wikipedia.org/wiki/Elastic_collision#Two-dimensional_collision_with_two_moving_objects (If you go at the end of this section, you will have an angle free formulation of the collision).

You can see the collision as a two parts process :
1- You find the direction of the velocity vectors after collision
2- You find the norm of these vector to scale them accordingly to the speed of the pods

The first part is “just” trigonometry and vector manipulation.

In the second part, if you have an elastic collision, then you must ensure two quantities are conserved : the kinetic energy (`1/2 * (m1*v1**2 + m2*v2**2)`), and the momentum (`m1v1 + m2v2`). By conserved we mean that the value of these two quantities will be the same before and after the collision. The equation in wikipedia comes from the combination of the first point (trig) and these two conservation equations (you can find a derivation here : http://vobarian.com/collisions/2dcollisions2.pdf )

Now if you take a look at the last formula in Wikipedia, you will see that the right hand terms correspond to `fx/m1` and `fy/m1` (or /m2 if you consider the second pod). One thing differs there : the wikipedia formula has a 2 that is not present in the code. But, if you take a look at the code, you will see that the impact is applied twice (we have twice the line `this.vx -= fx / m1;`). There is our two. The only difference left now is that between the two applications of the impact, the vector is renormalized to be at least 120.

This point is stated in the rules : the minimum impulsion is 120. That means that if two pods collide at very slow speed, they will be still repulsed instead of getting “glued” together. The vector is thus normalized if it is less that 120. There is still one mysterious thing here : why the normalization is only applied for the second part of the impact. This, I guess `pb` will have a better answer

Hope that helps.

5 Likes

I wouldn’t dream of giving a better answer than yours, which is already very detailed !

As Aveuh said, the formulas used in Magus’ article are direct application of physical principles for elastic collisions.

On the minimum impulsion : this is the only deviation in CG’s engine when compared to elastic collisions. The minimum impulse results in non conservation of the kinetic energy. I understand that this minimum impulse is a design feature to prevent pods from racing very close to each other. If the relative velocities of the pods is too small, they will be ejected away one from the other.

As for why the minimum impulse would be applied to only half of the impact : I have no idea. I believe the calculation steps are not exactly the same in CG’s engine and it made sense for them to apply it to one of their calculation step. The minimum impulse being a non-physical feature, I don’t see any value in understanding the “why”. What’s important is getting the “how” right to re-implement it the same way in our simulation engines.

4 Likes

Thank you, it is very helpful. I didn’t know about minimum impulsion rule.
I found a reading showing how to resolve the two equations ( kinetic energy and momentum ) and it turns out in two simple formulas:

the problem here I guess, is that it does not take into account the minimum impulse, does it?

Without having reverse-engineered the whole thing, the most natural way of implementing the minimal impulse with these formulae would be to use the formulae for the direction of the vectors, and then increase their length to a minimum of 120.

Be careful. What you have here is the expression of the elastic collision in 1D. It does not give you a direction.

In any case, you will not find the minimum impulsion anywhere on the net. The reason is the following : most of the documents you will find will document the physical phenomenon of elastic collision. But here, we are in an unrealistic case where “real life physics” does not really apply. For the little story, there is another reason why you won’t find these stories about minimum impulsion : this trick does not conserve neither kinetic energy nor momentum at low speeds. If you think about it, the minimum impulse adds quantity of movement to the pods when they collide at slow speed. So we get out of the field of elastic collision. End of parenthesis

Skywalker is right : the best way to solve the problem is simply what Magus does in his code : taking the vector, measuring its length. If the length is less than 120, we renormalize it so it is exactly 120, otherwise we keep it as it is.

1 Like

Ok I see, I just have one last question.

if it was a realistic case why couldn’t apply those formulas for 2D collisions? As I see if we take v1 and v2 as vectors that formula would be magnitude * vector + magnitude * vector = vector, am I missing something?

1 Like

Yes sorry, you were just expressing the magnitude of the velocities that’s why I said you have to be careful. You still have to compute the direction of the vectors, but then, that’s simpler to use the formula of Wikipedia (well, I guess), or Magus’.

I don’t really get which vector is what on your diagram ? Can you explain it ? Is Vi the initial velocity and Vf the velocity after impact ?

1 Like

I guess the letters represent the initial and final speeds.

Then there is a big problem with the vectors ^^. It looks like vf1 is not going in the right direction.

1 Like

The ball 1 is way heavier than the ball 2.

Oh you’re right … I didn’t see the masses. I thought we were still talking about CSB …

1 Like

yes it’s correct:
m1= mass of the object 1
m2= mass of the object 2
Vi1= Initial velocity of object 1
Vi2= Initial velocity of object 2
Vf1= Final velocity (after the impact) of object 1
Vf2= Final velocity (after the impact) of object 2

The example were just to know if the formulas I posted before, could be apply to 2d collision if the environment were more realistic (No for CSB as it uses a minimum impulse rule just like @Aveuh explained)