a brief look at physics joints in jME3

Added 27 Oct 2013, 6:01 p.m. edited 19 Jun 2023, 3:39 a.m.

While blocks falling and hitting a flat plane or even a landscape is great, where dynamics libraries really come into their own is joints or constrains between two bodies... For example use the right constraint between bodies - lets call them planks and you have a bridge (for the advanced user peek at the applied forces being used to solve the constraint and you can tell when the bridge is overloaded and make the appropriate joints fail....). Probably the most common use for joints would be the vehicle class provided by bullet, its the joints that create the wheels both for motors and steering. The example that you can see in the video above demonstrates two different joints a hinge joint and the very flexible 6DoF joint. The hinge joint basically works like you'd assume, however by default it is without rotational constraints on its one axis of rotation, its basically ideal to use for wheel / axle joints by default. The 6DoF joint (Six Degrees of Freedom) is the most complex but equally the most useful. The six degrees refers to being able to move on all three axes (X,Y & Z) and additionally able to rotate round the same axes. Lets look first in a little more detail at the hinge joint

        RigidBodyControl anchor1 = makeAblock(new Vector3f(0,0,0), 0);
        bob1 = makeAblock(new Vector3f(0,1f,0), 1f);

        joint = new HingeJoint(
                anchor1,                    // obj A 
                bob1,                       // obj B
                Vector3f.ZERO,              // pivot point local to A
                new Vector3f(0f, 1, 0f),    // pivot point local to B
                Vector3f.UNIT_Z,            // movement axis for A 
                Vector3f.UNIT_Z);           // movement axis for B
makeAblock is just for convenience it does nothing more than clone a mesh add a physics control to it and return the physics controller, it simply takes a position and mass for its parameters. Once you have finished one experiment with a joint and are happy with it this routine should make it real easy to add yet another joint to play with, that way you can make yourself a whole library of useful joint setups... Objects without mass rather than traveling at the speed of light as you'd expect are actually stationary - see its a dynamics library after all not a physics library ;) The joint constructor is fairly easy to follow and in the fragment above I've commented each parameter. The pivot points are both local in their frame of reference, ie when you are defining where the pivot point is in say object A, think of object A as the centre of the universe i.e. 0,0,0 The movement axes are axes that the hinge joint is working on, by changing the axes relative to each other you can have an axle / wheel joint or just make something orbit round the anchor point at a fixed distance... Obviously things get a tiny bit more complex with the 6DoF joint just alone because of its flexibility...
        anchor2 = makeAblock(new Vector3f(2,0,0), 0);
        bob2 = makeAblock(new Vector3f(2,-1f,0), 1f);

        joint2 = new SixDofJoint(
                new Vector3f(0,0,0),
                new Vector3f(0,1f,0),
        TranslationalLimitMotor tm = joint2.getTranslationalLimitMotor();

        tm.setLowerLimit(new Vector3f(0,-.5f,0));
        tm.setUpperLimit(new Vector3f(0,.5f,0));

this joint has been set up with a little more freedom than the hinge, and the constraint has a very spongy effect, it ends up behaving as if the box was on a piece of rope meaning unlike the hinge joint the distance between the two objects can change... One thing to note its that its perfectly easy to make an exploding joint that can never get into any kind of equilibrium, change just one thing at a time and persevere ! I'm including the source code, as it should make a handy means of experimenting with joints, if anyone manages to build up a wide range of different joint behaviors with this please do send it in. (see about for contact details) get the source here Advanced users note, if you are relying on an advanced feature only in the native bullet (such as feedback for example) you need to look out for this in the console:
Bullet-Native: Initializing java classes
Its entirely possible that it will carry on using jbullet despite it not being in your projects library settings. Quite what the issue is I'm not sure but I did manage to work round it by removing jbullet.jar and jME3-jbullet.jar - then add the native jars (jME3-bullet.jar and jME3-bullet-natives.jar) and try a clean / build. If this fails move jME3-bullet-natives.jar to the top of your libraries with jME3-bullet.jar just below it, you can't drag and drop your libraries to change their order in the projects window, instead open up the project properties dialog in the library section there is two buttons to move libraries up and down. Just to be paranoid do a clean again (probably not needed ;) ) and hopefully you'll see....
Bullet-Native: Initializing java classes