# Elastic collision

## Recommended Posts

I read this but I need some help.

"
We begin by calculating the velocity of the center of mass for the collision.
This value is determined by adding the velocities of the two colliding objects and then dividing by the total mass of the objects.
If the two colliding objects hit each other, this velocity is the velocity that the resulting composite object would have.
"

Here is an [url="http://imageshack.us/photo/my-images/534/questiont.jpg/"]image[/url].

Questions:

1. First of all, I don't understand what is the center of mass for the collision?
2. What is this combined velocity, and having sum of velocities over mass, is that some formula I should know?

This is the code for the collision:

[code]
private void BounceAsteroids(Sprite asteroid1, Sprite asteroid2)
{
{
// both objects have mass 1, so we divide with 1 + 1 = 2
Vector2 cOfMass = (asteroid1.Velocity + asteroid2.Velocity) / 2;

Vector2 normal1 = asteroid2.Center - asteroid1.Center;
normal1.Normalize();

Vector2 normal2 = asteroid1.Center - asteroid2.Center;
normal2.Normalize();

asteroid1.Velocity -= cOfMass;
asteroid1.Velocity = Vector2.Reflect(asteroid1.Velocity, normal1);
asteroid1.Velocity += cOfMass;

asteroid2.Velocity -= cOfMass;
asteroid2.Velocity = Vector2.Reflect(asteroid2.Velocity, normal2);
asteroid2.Velocity += cOfMass;
}
}[/code]

I understand the code as a programmer, but I don't get the physics here. Why subtract cOfMass, then calculate the reflection vector and then again add cOfMass?

Thanks

#### Share this post

##### Share on other sites
japro    887
It's not the center of mass, its the velocity of the center of mass of the system. That velocity has to be conserved on impact, otherwise the collision would not conserve the impulse.

Think about the case where one asteroid doesn't move. In that case the moving asteroid would just be reflected while the other one wouldn't move at all... Even worse, if they collide at an arbitrary angle, they would simply be reflected into the opposite direction of their original movement.

#### Share this post

##### Share on other sites
What about subtracting/adding cOfMass?

#### Share this post

##### Share on other sites
japro    887
Uhm, didn't I just answer that? (Edit: well I kinda did, but it was rather "implicit" ) Subtracting the velocity of the center of mass transforms the system to an inertial system where the center of mass is at rest. Adding it transforms back...

#### Share this post

##### Share on other sites
Let's consider just this code and go slowly line by line, without physics terms like "inertial system". Just keep it common sense, I am in CS not in Physics

[code]
asteroid1.Velocity -= cOfMass;
asteroid1.Velocity = Vector2.Reflect(asteroid1.Velocity, normal1);
asteroid1.Velocity += cOfMass;
[/code]

1. Are you saying that line one makes asteroid2(the other one) basically a static object?
2. I know what reflection vector is, I am ok here.
3. I am lost on line 3.

And what is the sum of velocities over mass formula?

Edit: I consider getting some physics books for game programming, I see gamedev.net has some recommendations.

#### Share this post

##### Share on other sites
japro    887
[quote name='calculemus1988' timestamp='1314741966' post='4855646']
[code]
asteroid1.Velocity -= cOfMass;
asteroid1.Velocity = Vector2.Reflect(asteroid1.Velocity, normal1);
asteroid1.Velocity += cOfMass;
[/code]

1. Are you saying that line one makes asteroid2(the other one) basically a static object?
[/quote]
No, it makes the center of mass of the whole system static.
[quote]
2. I know what reflection vector is, I am ok here.
[/quote]

[quote]
3. I am lost on line 3.
[/quote]
this just reverses the transformation that was done in line 1.

Its mostly a style question but if I had to write that function it would look like this:
[source lang="cpp"]//change the frame of reference to one where the center of mass is at rest
asteroid1.Velocity -= cOfMass;
asteroid2.Velocity -= cOfMass;

//perform elastic collision along normal vector asteroid1.Velocity = Vector2.Reflect(asteroid1.Velocity, normal1);
asteroid2.Velocity = Vector2.Reflect(asteroid2.Velocity, normal2);

//chage back to the original frame of reference
asteroid1.Velocity += cOfMass;
asteroid2.Velocity += cOfMass;
[/source]
Kinda makes the whole intent more obvious. Also normal1 = -normal2... only calculating one and changing the sign is on less (expensive) normalization.

#### Share this post

##### Share on other sites
Look at this [url="http://imageshack.us/photo/my-images/51/70698664.jpg/"]image[/url]

The author did a mistake I think. This is what he did:

[code]Vector2 normal1 = asteroid2.Center - asteroid1.Center;
Vector2 normal2 = asteroid1.Center - asteroid2.Center;
[/code]

And this is what I think it should be:

[code]Vector2 normal2 = asteroid2.Center - asteroid1.Center;
Vector2 normal1 = asteroid1.Center - asteroid2.Center;
[/code]

Mine agrees with the image and from what I know about reflection angles. The direction of the normal matters. The reflected angle will be in the half-plane where the normal points to, not away from. Agree?

Anyways I am kinda geeky about understanding stuff I do, I will read for details in the books, there are good ones on physics for game programming, and one of them in contents has collision of two circles, similar stuff to this one.

#### Share this post

##### Share on other sites
japro    887
I don't think there is a canonical way to define the sign of those normals. It pretty much depends on how the signs in the "Reflect" methods look like. But yes, your version is consistent with the graphics.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now