making a rope bridge (jME3 and physics)

I previously alluded to the idea that constrains (joints) could be used to make a bridge.  Most of the code should be reasonably familiar to someone who’s had some basic experience with jME3, so I won’t go into major detail.

Setting up the individual planks of the bridge is straight forward, but its worth mentioning that the objects are in array, the index of the array is related to the position of the plank so the joints can be added from left to right in order.

        for (int i = 0; i < 7; i++) {
            parts[i] = geom.clone();
            if (i == 0 || i == 6) {
                partsPhys[i] = new RigidBodyControl(0);
                parts[i].setLocalTranslation(7.5f - 2.5f * i, 0, 0);
            } else {
                partsPhys[i] = new RigidBodyControl(1);
                parts[i].setLocalTranslation(7.5f - 2.5f * i, 2, 0);
            }
            parts[i].addControl(partsPhys[i]);
            partsPhys[i].setSleepingThresholds(0.01f, 0.01f);
            getPhysicsSpace().add(parts[i]);
            rootNode.attachChild(parts[i]);
        }

The first and last plank are static (zero mass) which allows the bridge to be anchored.

When setting up the joints we have to lock the rotation on the x axis – the direction of the bridge. You could use two chains of joints, however with the “slop” in the joints it looks okay with just a single chain of joints going down the centre of the bridge.

        Vector3f p1 = new Vector3f(-1.25f, 0, 0);
        Vector3f p2 = new Vector3f(1.25f, 0, 0);
        for (int i = 0; i < 6; i++) {
            joints[i] = new SixDofJoint(partsPhys[i], partsPhys[i + 1], p1, p2, true);
            getPhysicsSpace().add(joints[i]);
            TranslationalLimitMotor tm = joints[i].getTranslationalLimitMotor();
            tm.setLimitSoftness(.1f);
            tm.setRestitution(0.1f);
            tm.setLowerLimit(new Vector3f(0, -.2f, 0));
            tm.setUpperLimit(new Vector3f(0, .2f, 0));
            RotationalLimitMotor rlm = joints[i].getRotationalLimitMotor(0);
            rlm.setHiLimit(0); // fix this axis
            rlm.setLoLimit(0);
            rlm.setBounce(0.5f);
        }

The transitional limit motor allows the “rope” a little elasticity, as ever if you find objects suddenly vanishing or things going generally going wild don’t loose heart, there are plenty of possible unstable states for a joint a probably fewer stable conditions! Looking at the c++ blender examples to see just how the joints should be used is always an option.

Grab the full project here PhysicsBridge

Leave a Reply

Your e-mail address will not be published. Required fields are marked *