Officer Vimes
Officer Vimes

Reputation: 95

Rendering a joint? [libGDX/Box2D]

How would you go about rendering a joint in box2d? For example, if you were to make a grapple hook, how do you make it so that a sprite or something follows its movements exactly? I was trying to create a Body that is on the same position as the joint but Im struggling a lot actually making it be on top of the joint at all times..

Upvotes: 0

Views: 722

Answers (1)

Draz
Draz

Reputation: 782

All you need to do is the following:

You set the origin of your desired sprite to the rotation point (e.g. bottom center) once.

sprite.setOrigin(sprite.width()/2, 0);

You determine the angle of the vector beween the two anchors of your distance Joint in oyur update method. Note that you might have to convert local anchors to worldpoints.

float angle = playerAnchorPoint.sub(ceilingAnchorPoint).angle();

and you set position and angle:

sprite.setPosition(playerAnchor.x - sprite.width()/2, playerAnchor.y);
sprite.setRotation(angle);

Upvotes: 1

Related Questions