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

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: http://gamedevelopment.tutsplus.com/tutorials/how-to-create-a-custom-2d-physics-engine-the-basics-and-impulse-resolution--gamedev-6331

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!

• What I did to fix my soft body physics implementation was to add/increase linear and rotational damping. If you don't have damping implemented, it might be something to look into. – mythos Jul 19 '16 at 17:15
• @sakul_ca Well I don't even know how to implement joint basis, damping shouldn't be related I guess. – Peter Lenkefi Jul 19 '16 at 18:13

## 1 Answer

I'm facing a similar issue, and here is what I've come up with so far:

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

This is just my hypothesis, and I'm still working to find out a solution.