I am trying to add penalty forces between two hard body objects, so when they collide they move in a realistic way. What I have so far is this:

var totalMass = aMass + bMass;
var temp = (aMass / totalMass * aVelocity) + (bMass / totalMass * bVelocity);
newVelocityB = (bMass / totalMass * bVelocity) + (aMass / totalMass * aVelocity);
newVelocityA = temp;

Originally I thought this was correct and it seems to work for the most part. However, this doesn't seem to be flawless. For example, if the collided object is a vertical wall (represented with a mass of infinity), the wall's velocity of (0,0) will be the final result. So, for an object moving diagonally with a velocity of (10,10) and hitting the wall, will result in a velocity of (0,0). In my case I would expect that the y component of the velocity remains 10, and it keeps sliding up the wall.

Can anyone tell me if I'm on the right path, and how I can modify my formula to make sure that the y-velocity does not become 0?

I have considered that the collision normal of the wall could be important (in this example [-1, 0]), or the perpendicular of the normal, however I still have not figured out how to apply it in this formula. Multiplying by the perpendicular of the normal was my first idea, but this isn't the correct answer.

  • \$\begingroup\$ Usually we compute these types of interactions with impulses, not a weighted average of velocities as shown here. Since the collision impulse acts along the collision normal, a diagonal collision with a wall doesn't stop the motion parallel to the wall. \$\endgroup\$ – DMGregory Aug 23 '20 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.