Hi,
I'd like to share with you the results of my experiments with implementing a "STRONG" rope.
Below you'll find a code snippet in Java.
You can see the results here:
http://www.dustedpixels.com/download/webstart/RopeDemo.jnlpThe rope on the left is the "strong" one, the rope on the right is the "ordinary" one.
As a point of reference, the box connected to the rope is 500x heavier than every of the links.
Code:
private static final int COUNT = 16;
private static final boolean STRONG = false;
// Create links. Last link will be a very heavy box.
List<Body> links = new ArrayList<Body>();
for (int i = 1; i <= COUNT; i++) {
BodyDef bodyDef = new BodyDef();
bodyDef.position = anchor.add(new Vec2(0, i * 0.25f));
Body link = world.createBody(bodyDef);
if (i != COUNT) {
// A link
CircleDef circleDef = new CircleDef();
circleDef.radius = 0.025f;
circleDef.density = 1f;
link.createShape(circleDef);
} else {
// A box attached at the end of the rope
PolygonDef polygonDef = new PolygonDef();
polygonDef.setAsBox(0.5f, 0.3f, new Vec2(0, 0.3f), 0);
polygonDef.density = 1f;
link.createShape(polygonDef);
}
link.setMassFromShapes();
links.add(link);
}
// Connects links together. First link is connected directly to the "ceiling".
Body lastLink = null;
for (Body link : links) {
DistanceJointDef distanceJointDef = new DistanceJointDef();
if (lastLink == null) {
distanceJointDef.initialize(link, world.getGroundBody(), link.getPosition(), anchor);
} else {
distanceJointDef.initialize(link, lastLink, link.getPosition(), lastLink.getPosition());
}
world.createJoint(distanceJointDef);
lastLink = link;
}
if (STRONG) {
// To make rope stronger, connect each link directly to the ceiling.
for (Body link : links) {
BodyDef bodyDef = new BodyDef();
bodyDef.position = anchor;
Body attachement = world.createBody(bodyDef);
MassData massData = new MassData();
massData.mass = link.getMass();
massData.I = link.getInertia();
attachement.setMass(massData);
RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
revoluteJointDef.initialize(attachement, world.getGroundBody(), attachement.getPosition());
world.createJoint(revoluteJointDef);
PrismaticJointDef prismaticJointDef = new PrismaticJointDef();
prismaticJointDef.initialize(attachement, link, link.getPosition(), new Vec2(0, -1));
prismaticJointDef.enableLimit = true;
prismaticJointDef.lowerTranslation = 0;
prismaticJointDef.upperTranslation = (link.getPosition().y - anchor.y);
world.createJoint(prismaticJointDef);
}
}
Regards,
Michal.