# Simplest example of a working joint

Hello all,

Please can I apologise for what must seem like a stupid question.

I am trying to get a simple demo 2 boxes one on top of the other with a gap in between, the boxes are connected by a hinge.

Could some one please show me what I am doing wrong in the following code adapted from the simpleTest code. For me the top box doesn't move, it just stays there as if its a fixed joint.

The following code simply replaces the simpleInitGame() in the simpleTest example in the jmeextest package.

protected void simpleInitGame() {

display.setTitle("Simple Test");

// Set up the PhysicsWorld. It's got a couple of default

// values, e.g. Earths gravity.

PhysicsWorld.create();

PhysicsWorld.getInstance().setUpdateRate(100);

PhysicsWorld.getInstance().setStepSize(2f/100f);

// Creates the box that makes out the floor (graphics only).

Box floorGraphics = new Box("Floor", new Vector3f(0f,-0.5f,0f), 50, 0.5f, 50); //a 100x1x100 plane

rootNode.attachChild(floorGraphics);

PhysicsObject floorPhysics = new StaticPhysicsObject(floorGraphics);

//load a test box and join with another

Box box1 = new Box("Box1", new Vector3f(0,1,0), 1f, 1f, 1f); // a 2x2x2 box

rootNode.attachChild(box1);

DynamicPhysicsObject box1p = new DynamicPhysicsObject(box1, 10f);

Box box2 = new Box("Box2", new Vector3f(0f,5f,0f), 1f, 1f, 1f); // a 2x2x2 box

rootNode.attachChild(box2);

DynamicPhysicsObject box2p = new DynamicPhysicsObject(box2, 20f);

HingeJoint hinge = new HingeJoint("Joint 1", box2p, box1p);

hinge.attach();

hinge.setAnchor(new Vector3f(0.2f,3,0));

hinge.setAxis(new Vector3f(0,0,1));

//        BallJoint ball= new BallJoint("Joint 1", box2p, box1p);

//        ball.attach();

//        ball.setAnchor(new Vector3f(0.2f,3f,0f));

}

edit: It appears that setting the position of the boxes after creation using box1.getLocalTranslation().y = 0.5f; (for example) instead of using the vector3f center in the spatial constructors makes it all work.

Please why is this? Also why are two Dynamics physics objects allowed to pass through each other (as with the above, corrected, example) once they are joined with a hinge. For example the working version of the above allows the boxes to collide into and through each other, constrained only by the min and max angle set for the hinge, and not when the boxes collide. I imagine it is more convenient to allow this and use the set min and max as the constraints.

Thanks again for any advice you can give me.

1. center vs. translation:

Changing the center of a box/sphere moves the triangles in the mesh outside the origin of the spatial. For ODE this means the box/sphere collision bounds are not around the center of mass (which is at (0,0,0) relative to the body) any more. When the center of mass moves outside of the collision bounds ODE behaves really strange (becomes numerically unstable). So in most cases you would want a center of (0,0,0). The joint anchor is also relative to the spatial origin.

2. joint bodies

Joints are actually constraints for the numerical computation of ODE. Collisions/contacts constraints are internally represented by joints as well. In ODE two bodies can either have a contact joint or another joint - not both. That's why joint bodies can move though each other. ODE does not even check collisions between them. In other words: when you join two DynamicPhysicsObjects you have to care for stops/collisions yourself.

Although not an actual solution to your problem - maybe it clarifies things

Hi Irrisor,

So does this mean that when setting up the spatial which are then used to create a DPO that you have to use the translation of the spatial to position it in a scene?

For example:

I am loading a simple robot model from milkshake in and want connect the parts with joints. Each of the individual spatials for the model are there, arms limbs, spine bones, skull etc, and all in the correct place visually. But then if I output the local and world translation value for each of the spatial nodes of the loaded file, then it says they are all at (0,0,0). Therefore connecting them with joints has the same problem as the previous example.

What is the correct way to do this? To load a multiple part model and then connect the parts using joints? Is this impossible?

Cheers,

ranewc said:

So does this mean that when setting up the spatial which are then used to create a DPO that you have to use the translation of the spatial to position it in a scene?

Yes

But your model parts should have those positions after loading. Load that model in milkshape and move the pivot/rotation points of the compartments to the estimated center of mass of the individual compartment and save again. This way you should get correct part positions for the loaded Spatials.

Thank you for the help Irrisor