Archived

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

davidadams

How to get the velocities after collided

Recommended Posts

There are two balls(B1 and B2). balls'' radius is R. Vector V10 is the original velocity of B1, and Vector V20 is the original velocity of B2, After collided, the velocities become V11 and V21 . Is there any sample function to get them?

Share this post


Link to post
Share on other sites
If two objects undergo an elastic 1D collision, then the impulse experienced by each one is equal to the difference between their momentums (is that the right word?). In 2D, the impulse will be the projection of that vector onto the vector of collision.

The following code is tested, but unoptimised. It is intended for educational purposes only, and you should probably rewrite it once you understand it.
  
typedef struct
{
float PosX, PosY;
float VelX, VelY;
float Mass;
} Object2D;

void DoCollision(Object2D* Obj1, Object2D* Obj2)
{
// The Impulse expected from a direct collision would be...

float DImpulseX = (Obj2->VelX*Obj2->Mass - Obj1->VelX*Obj1->Mass);
float DImpulseY = (Obj2->VelY*Obj2->Mass - Obj1->VelY*Obj1->Mass);

// Normalise this impulse vector.

float DImpulseL = sqrt(DImpulseX*DImpulseX + DImpulseY*DImpulseY);
DImpulseX /= DImpulseL;
DImpulseY /= DImpulseL;

// DiffVec (Difference Vector) is the vector from Obj2 to Obj1.

float DiffVecX = Obj2->PosX - Obj1->PosX;
float DiffVecY = Obj2->PosY - Obj1->PosY;

// Normalise the difference vector

float DiffVecL = sqrt(DiffVecX*DiffVecX + DiffVecY*DiffVecY);
DiffVecX /= DiffVecL;
DiffVecY /= DiffVecL;

// Take the dot product of the unit vectors

float UnitDot = (DiffVecX*DImpulseX + DiffVecY*DImpulseY);

// The impulse is then equal to:

float ImpulseX = DiffVecX * UnitDot * DImpulseL;
float ImpulseY = DiffVecY * UnitDot * DImpulseL;

// Apply the impulse

Obj1->VelX += ImpulseX / Obj1->Mass;
Obj1->VelY += ImpulseY / Obj1->Mass;
Obj2->VelX -= ImpulseX / Obj2->Mass;
Obj2->VelY -= ImpulseY / Obj2->Mass;
}

Share this post


Link to post
Share on other sites