How would joints work in an impulse-based physics engine?

by Peter Lenkefi   Last Updated July 11, 2018 21:13 PM

I want to learn about physics engines, especially impulse-based ones. So I've followed along this tutorial to create some simple rigid-body physics:

I've got this working and it seems pretty solid to me. So I wanted to add revolute (fixed) joints. I've started off with a simple structure:

struct Joint
    RigidBody* A;       // One body
    Vec2 AnchorPointA;  // A's anchor point's offset (0,0 if center)
    RigidBody* B;       // The other body
    Vec2 AnchorPointB;  // B's anchor point's offset (0,0 if center)    

I'm guessing this would do the job. Now... I'm not too good with physics (hence I started to code this) so I started out what I thought would work:

RigidBody* A = joint->A;
RigidBody* B = joint->B;

// Rotate the anchor points based on the body's rotation
Vec2 ancha = A->BodyShape->Transform * joint->AnchorPointA;
Vec2 anchb = B->BodyShape->Transform * joint->AnchorPointB;

// The relative positions of the 2 points
Vec2 relativeVel = (B->Position + B->Velocity + anchb) - (A->Position + A->Velocity + ancha);

// Apply a force towards eachother, pulling at the anchor points
B->ApplyImpulse(-relativeVel, anchb);
A->ApplyImpulse(relativeVel, ancha);

This approach simply pulls the bodies together. And it works... Kind of. After the bodies slide together as the joints are set up, it looks ok, but when the bodies fall they tend to spin away from eachother or start to freak out (keep rotating). After messing around with the code (which was getting worse and worse as I was changing it) I've tried to look up solutions on the internet or at least some hints. I've found this: How can I implement revolute (hinge) joints in a 2d physics system?

But this approach does not apply impulses, it fixes position. Wouldn't be an impulse-based system better? How can I fix it? I don't even know how big forces should I apply (I guess it should be based of the distance or something like that and also for the masses of the bodies). Thanks for any help!

Answers 1

I have same question , so my hypothesis is 1. joint must fix position between two body but it can still rotating. 2. joint can transfer force or impulse in x,y axis 3. rotating object apply centripetal force to joint but still working to find out solution

Dhamdhawach Horsuwan
Dhamdhawach Horsuwan
July 11, 2018 20:55 PM

Related Questions