Friday, June 21, 2019

How to create an extensible rope in Box2D?



Let's say I'm trying to create a ninja lowering himself down a rope, or pulling himself back up, all whilst he might be swinging from side to side or hit by objects. Basically like http://ninja.frozenfractal.com/ but with Box2D instead of hacky JavaScript.


Ideally I would like to use a rope joint in Box2D that allows me to change the length after construction. The standard Box2D RopeJoint doesn't offer that functionality.


I've considered a PulleyJoint, connecting the other end of the "pulley" to an invisible kinematic body that I can control to change the length, but PulleyJoint is more like a rod than a rope: it constrains maximum length, but unlike RopeJoint it constrains the minimum as well.


Re-creating a RopeJoint every frame using a new length is rather inefficient, and I'm not even sure it would work properly in the simulation.


I could create a "chain" of bodies connected by RotationJoints but that is also less efficient, and less robust. I also wouldn't be able to change the length arbitrarily, but only by adding and removing a whole number of links, and it's not obvious how I would connect the remainder without violating existing joints.


This sounds like something that should be straightforward to do. Am I overlooking something?


Update: I don't care whether the rope is "deformable", i.e. whether it actually behaves like a rope, or whether it collides with other geometry. Just a straight rope will do. I can do graphical gimmicks while rendering; they don't need to exist inside the physics engine. I just want something a RopeJoint whose length I can change at will.



Answer



Ok, I naively assumed that LibGDX wrapped all of Box2D, so this would be purely a Box2D problem.


It turns out that vanilla Box2D, at least in trunk, has a function called b2RopeJoint::SetMaxLength. I've added it and got a pull request merged within minutes. It is now available (and working) in LibGDX nightlies.



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...