Search code examples
libgdxbox2d

How can I make a body rotatable but not movable in box2d?


I need to make a body to not move around, like a static body, but i do need it to be rotatable, is there any way to do so?


Solution

  • Create normal dynamic body and static body - put them in the place when you want to have rotatable body lay and joint them using RevoluteJoint - it is kind of joint that you can imagine as a 'pin'

    BodyDef dynamicBodyDef, staticBodyDef;
    //define type, position of bodies...
    
    Body dynamicBody = world.createBody(dynamicBodyDef);
    Body staticBody= world.createBody(staticBodyDef);
    
    RevoluteJointDef jointDef = new RevoluteJointDef();
    jointDef .bodyA = dynamicBody;
    jointDef .bodyB = staticBody;
    
    world.createJoint(jointDef );
    

    You can define if bodies can collide, what is the maximum angle etc - take a look at RevoluteJointDef class definition