#### Archived

This topic is now archived and is closed to further replies.

# Simple 2D Collision question

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

## Recommended Posts

I''m trying to figure out how to model a simple collision. Here''s the picture of the situation I''m trying to model http://www.coe.uncc.edu/~dmarkham/Collision.gif Right now I just have the two balls exchanging velocity vectors, which works only for head-on collisions and not the above scenario where the balls collide at an angle. I have no problem detecting when theres a collision, I just can''t figure out how to model the effects of the collision. Can someone please push me in the right direction?

##### Share on other sites
Follow the principle of conservation of momentum:

(m1 x v1) + (m2 x v2) = (m1+m2)vfinal

m1 = mass of first ball
v1 = velocity of first ball

m2 = mass of second ball
v2 = velocity of second ball

vfinal is the velocity of both balls after collision.

The right direction can be calculated as follows:

Project a vector from the center of the first sphere to the center of the second sphere. This vector acts as the normal vector for the collision of the second ball (youll need to flip it to get the normal for the first collision) - essential if you want to get the correct direction.

Now, the original velocity vector needs to be rotated around the normal for the collision.

The reflection vector is: V' = -V + 2(N.V)N

Keep in mind that the dot product produces a scalar which is then multiplied by the normal for that collision.

Hope this helps - if youd like a picture representation of this phenomena: http://www-scf.usc.edu/~gamedev/MOMENTUM.JPG

Email me (p_samty@yahoo.com) if the link doesnt work and ill fix it.

[edited by - psamty10 on February 8, 2004 11:44:36 PM]

[edited by - psamty10 on February 8, 2004 11:45:17 PM]

##### Share on other sites
here is a code dump to illustrate psamty10''s solution.

//------------------------------------------------------

// Check collision between boat[i] and boat [j]

//------------------------------------------------------

int GameBumperBoatRideClass::CheckBoatCollision(int i, int j)
{
//-----------------------------------------------------------

// Creates two vectors containing the velocity of the boats.

// Velocity is both the direction and speed of the boats.

//-----------------------------------------------------------

Vector2 VelocityBoatOne = Boat[i].Velocity;
Vector2 VelocityBoatTwo = Boat[j].Velocity;

//-----------------------------------------------------------

// Creates two vectors containing the x and y

//-----------------------------------------------------------

Vector2 PositionBoatOne = Boat[i].Position;
Vector2 PositionBoatTwo = Boat[j].Position;

Vector2 DistanceBetweenBoats(PositionBoatTwo - PositionBoatOne);
Vector2 VelocityDifference(VelocityBoatTwo - VelocityBoatOne);

//-----------------------------------------------------------

// calculates the relative distance, squared

//-----------------------------------------------------------

float DdotD = DistanceBetweenBoats.Dot(DistanceBetweenBoats);

//-----------------------------------------------------------

// boat didn''t collide

//-----------------------------------------------------------

return false;

//-----------------------------------------------------------

// If the code gets to this point, the boats

// have collided. :)

//-----------------------------------------------------------

double dlength = sqrt(DdotD);

//-----------------------------------------------------------

// Calculate the normal of this collision

//-----------------------------------------------------------

Vector2 Normal = DistanceBetweenBoats / dlength;

//-----------------------------------------------------------

// Calculate the depth of this collision

//-----------------------------------------------------------

float depth = TotalRadius - dlength;

//-----------------------------------------------------------

// separate the boats a bit because they are overlapping

//-----------------------------------------------------------

float sep_damping = 0.25f;

PositionBoatOne -= Normal * depth * sep_damping;
PositionBoatTwo += Normal * depth * sep_damping;

Boat[i].Position = PositionBoatOne;
Boat[j].Position = PositionBoatTwo;

//------------------------------

// collision impulse

//------------------------------

float elasticity = 0.50f; // very squishy collision

float friction = 0.15f; // very little friction

//------------------------------

// collision impact stuff

//------------------------------

//-----------------------------------------------------------

// if boats not moving towards each other, no

// collision/impulse

//-----------------------------------------------------------

Vector2 V = VelocityDifference;

float vn = V.Dot(Normal);

//-----------------------------------------------------------

// boats are separating, no impulse.

//-----------------------------------------------------------

if (vn > 0.0f)
return true;

//-----------------------------------------------------------

// the velocity component from the collision plane

//-----------------------------------------------------------

Vector2 Vn = Normal * vn;
Vector2 Vt = V - Vn;

//-----------------------------------------------------------

// apply response

//-----------------------------------------------------------

//-----------------------------------------------------------

// the collision result apply elasticity and friction

//-----------------------------------------------------------

Vn *= -(1.0f + elasticity);
Vt *= -friction;

//-----------------------------------------------------------

// the resulting velocity

//-----------------------------------------------------------

V = Vn + Vt;

//-----------------------------------------------------------

// the lighter objects will move more than the heavier one

//-----------------------------------------------------------

int Mass2 = Boat[j].Mass;
int Mass1 = Boat[i].Mass;

VelocityBoatOne -= V * Mass2 / (Mass1 + Mass2);
VelocityBoatTwo += V * Mass1 / (Mass1 + Mass2);

Boat[i].Velocity = VelocityBoatOne;
Boat[j].Velocity = VelocityBoatTwo;

return 1;
}

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632983
• Total Posts
3009714
• ### Who's Online (See full list)

There are no registered users currently online

×