I'm writing a simple 2D physics engine to help me understand the inner workings of physics engines. I'm following Randy Gaul's tutorial which can be found here and It's been extremely useful and interesting. I'm now on the fourth and final article in the series which covers rotation, but I've spent weeks trying to figure it out and I've hit a brick wall. I've looked all over for other tutorials or explanations, but I have yet to find any that help. No matter how I impliment it, to the point of translating the source code of the tutorial to C# I seem to get completely counter intuative behavour.

The first cause of confusion is that the tutorial states that the collision resolution impulse for two bodies according to the tutorial is the first of the equations below, but the source code accompanying the tutorials appears to be using the second. These two don't appear to be equivalent to me, but I may be wrong.

The first equation is listed on the tutorial, the second is used in the source code

I'm using the second equation in my code, as implemented in the example source code. I alter the angular velocity as follows:

angularVelocity += ColumnVector2.crossProduct(contactVector, impulse);

Assuming neither of these are the problem, then I must be doing something fundamentally wrong. Can anyone explain, or point me to a good explanation of how to calculate the angular components of my physics engine because I am completely stuck!

Thanks in advance (and sorry for the lengthy question), Adam


You're in luck. I did a full translation of Randy Gaul's 2D physics engine into C# and XNA. He hasn't really explained things well for beginners like me.

For your answer, you should just multiply the cross product with the inverse of the inertia of the body. This is from my translation:

angularVelocity += inverseInertia * Vector2D.Cross(contactVector, impulse);

It works fine for me (just as Fahim Ali Zain said in his answer). I am anyway going to release a series of tutorials properly explaining the code underlying physics engines and introduce extra features like Raycasting, a few weeks in the future maybe.

  • \$\begingroup\$ Thanks so much for the feedback! His tutorials have been invaluable in getting me this far, but I agree that some parts are confusing for complete beginners like me. As far as I can tell this fixed one of the problems I was having, the other being that unlike with linear momentum, friction and air resistance don't appear to be slowing angular momentum down. I'm looking into this now. I really look forward to seeing your tutorials, where will they be posted when they're done? \$\endgroup\$ – acernine May 3 '15 at 9:40
  • \$\begingroup\$ @acernine I will post them on CodeProject. I'm doing the first article right now which covers everything except rotation, then in the next one I'll have rotation and joints. After that maybe I'll have accumulated impulses, raycasting and finally buoyancy. \$\endgroup\$ – EvilTak May 3 '15 at 16:37
  • \$\begingroup\$ I look forward to it! A feature I added that was not in the tutorials was air resistance, which works nicely (A falling rectangle 'landscape' will fall slower than 'portrait'). \$\endgroup\$ – acernine May 3 '15 at 21:22
  • \$\begingroup\$ That's nice! Can you show me the snippet on how to do so? If you are fine with it, I will include it in my tutorials and of course give you credit. \$\endgroup\$ – EvilTak May 4 '15 at 4:16
  • 1
    \$\begingroup\$ Sure, I'll try and send it to you later today :) The basic idea was that you project the shape perpendicular to its normalized velocity, and the larger the projected shape the more air resistance it will encounter. Once you know that, NASA has several good articles on air resistance and the related formulae. \$\endgroup\$ – acernine May 4 '15 at 7:52

Whatever collision be, angular momentum is conserved. ie Iw = constant with the coefficient of restitution (in translation, i dont know if its said the same in rotation) u define, and with the moment of inertia, you should be able to figure it out.

And i think this would similar to collisions in 1D, since only one axis is used :)

Goodluck :)


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.