How to Create a pin-Joint on a body in Box2D iphone?

By | February 3, 2011

Here mybody is a body to which I am connecting a pinpoint.
The pin joint is between the world and the body. So it will appear as hanging in the wall if the gravity is downwards. ‘The ground’ is the other body to which the my_body is connected.
First you create a big ground body with 3 or more fixtures and try this.
Happy coding.

//myBody
bodyDef.position.Set(10.920371f, 6.620342f);
bodyDef.userData = my_sprite;
bodyDef.angle = 0.000000f;
b2Body* myBody = world->CreateBody(&bodyDef;);
initVel.Set(0.000000f, 0.000000f);
myBody->SetLinearVelocity(initVel);
myBody->SetAngularVelocity(0.000000f);

b2Vec2 myBody_vertices[4];
myBody_vertices[0].Set(-2.245807f, -0.266091f);
myBody_vertices[1].Set(2.245807f, -0.266091f);
myBody_vertices[2].Set(2.245807f, 0.266091f);
myBody_vertices[3].Set(-2.245807f, 0.266091f);
shape.Set(myBody_vertices, 4);
fd.shape = &shape;
fd.density = 0.015000f;
fd.friction = 0.300000f;
fd.restitution = 0.600000f;
fd.filter.groupIndex = int16(0);
fd.filter.categoryBits = uint16(65535);
fd.filter.maskBits = uint16(65535);

myBody->CreateFixture(&fd;);


//Revolute joints - creating the pin joint.
pos.Set(8.940655f, 6.620342f);
revJointDef.Initialize(myBody, ground, pos);
revJointDef.collideConnected = false;
world->CreateJoint(&revJointDef;);

Here mybody is a body to which I am connecting a pinpoint.
The pin joint is between the world and the body. So it will appear as hanging in the wall if the gravity is downwards. ‘The ground’ is the other body to which the my_body is connected.
First you create a big ground body with 3 or more fixtures and try this.
Happy coding.

//myBody
bodyDef.position.Set(10.920371f, 6.620342f);
bodyDef.userData = my_sprite;
bodyDef.angle = 0.000000f;
b2Body* myBody = world->CreateBody(&bodyDef;);
initVel.Set(0.000000f, 0.000000f);
myBody->SetLinearVelocity(initVel);
myBody->SetAngularVelocity(0.000000f);

b2Vec2 myBody_vertices[4];
myBody_vertices[0].Set(-2.245807f, -0.266091f);
myBody_vertices[1].Set(2.245807f, -0.266091f);
myBody_vertices[2].Set(2.245807f, 0.266091f);
myBody_vertices[3].Set(-2.245807f, 0.266091f);
shape.Set(myBody_vertices, 4);
fd.shape = &shape;
fd.density = 0.015000f;
fd.friction = 0.300000f;
fd.restitution = 0.600000f;
fd.filter.groupIndex = int16(0);
fd.filter.categoryBits = uint16(65535);
fd.filter.maskBits = uint16(65535);

myBody->CreateFixture(&fd;);


//Revolute joints - creating the pin joint.
pos.Set(8.940655f, 6.620342f);
revJointDef.Initialize(myBody, ground, pos);
revJointDef.collideConnected = false;
world->CreateJoint(&revJointDef;);

Leave a Reply

Your email address will not be published. Required fields are marked *