0
votes

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..

1
Show what you got so far, some code - Draz
Heres my rope class, ATM I have it so that a bunch of bodies connected through distance joints form a chain-like thing, thats supposed to be the rope that connectes the player to the cloud. This doesnt work because joints in box2d suck and start spazzing out at the least amount of force. pastebin.com/3qQMQ0Xw I wanted to try to make it so a single big segments connects both player and ceiling to see if its more stable ( and then just add a sprite to that body to render it), Im currently away from home so cant try it, but hopefully you see what my goal is. - Officer Vimes
Do you want to have collisions enabled on the rope? If yes, then use revolute Joints on your rope Body. If not, then you don't Need a Body to draw a rope. - Draz
No I dont need collision detection. If I could I would like to just have the super simple single distance joint but how am I supposed to draw it on the screen? Joints dont have any methos that tell you the angle and stuff like that to feed to a sprite. - Officer Vimes

1 Answers

1
votes

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);