Jump to content
  • Advertisement


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


How to get the velocities after collided

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

If you intended to correct an error in the post then please contact us.

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!