[SOLVED] Cannot handle more than 1 joint - Physical Hinges and Joints

Have you read this?

Maybe something here will help. I just don’t have time myself to dig into this yet.

https://wiki.jmonkeyengine.org/jme3/advanced/ragdoll.html

No, I haven’t read that, but I will. By the way.

Try to run this code, it’s supposed to be Newton’s cradle.

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.joints.HingeJoint;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Node;

/**
 * This is the Main Class of your Game. You should only do initialization here.
 * Move your Logic into AppStates or Controls
 * @author normenhansen
 */
public class Main extends SimpleApplication {
    private BulletAppState bulletAppState; 
    private Node node1;
    private Node node2;
    private Node node3;
    private Node node4;
    private Node dyn1;
    private Node dyn2;
    private Node dyn3;
    private Node dyn4;
    private HingeJoint joint1;
    private HingeJoint joint2;
    private HingeJoint joint3;
    private HingeJoint joint4;

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        bulletAppState.setDebugEnabled(true);
        setupStaticalNodes();
        setupDynamicalNodes();
        setupJoints();
        
    }
    
    private void setupStaticalNodes(){
        // Static nodes
        node1 = createControl(new BoxCollisionShape(new Vector3f(0.1f, 0.1f, 0.1f)), 0);
        node2 = createControl(new BoxCollisionShape(new Vector3f(0.1f, 0.1f, 0.1f)), 0);
        node3 = createControl(new BoxCollisionShape(new Vector3f(0.1f, 0.1f, 0.1f)), 0);
        node4 = createControl(new BoxCollisionShape(new Vector3f(0.1f, 0.1f, 0.1f)), 0);
        
        // Place out the static nodes
        node1.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, 0, 0f));
        node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(1f, 0, 0f));
        node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2f, 0, 0f));
        node4.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3f, 0, 0f));
        
        // Attach them to physical space
        bulletAppState.getPhysicsSpace().add(node1);
        bulletAppState.getPhysicsSpace().add(node2);
        bulletAppState.getPhysicsSpace().add(node3);
        bulletAppState.getPhysicsSpace().add(node4);
        
        // Attach the static nodes to the rootNode
        rootNode.attachChild(node1);
        rootNode.attachChild(node2);
        rootNode.attachChild(node3);
        rootNode.attachChild(node4);
        
    }
    
    private void setupDynamicalNodes(){
        // Static nodes
        dyn1 = createControl(new BoxCollisionShape(new Vector3f(0.5f, 0.5f, 0.5f)), 100);
        dyn2 = createControl(new BoxCollisionShape(new Vector3f(0.5f, 0.5f, 0.5f)), 100);
        dyn3 = createControl(new BoxCollisionShape(new Vector3f(0.5f, 0.5f, 0.5f)), 100);
        dyn4 = createControl(new BoxCollisionShape(new Vector3f(0.5f, 0.5f, 0.5f)), 100);
        
        // Place out the static nodes
        dyn1.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(-10f, -5, 0f));
        dyn2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(1f, -5, 0f));
        dyn3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2f, -5, 0f));
        dyn4.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3f, -5, 0f));
        
        // Attach them to physical space
        bulletAppState.getPhysicsSpace().add(dyn1);
        bulletAppState.getPhysicsSpace().add(dyn2);
        bulletAppState.getPhysicsSpace().add(dyn3);
        bulletAppState.getPhysicsSpace().add(dyn4);
        
        // Attach the static nodes to the rootNode
        rootNode.attachChild(dyn1);
        rootNode.attachChild(dyn2);
        rootNode.attachChild(dyn3);
        rootNode.attachChild(dyn4);
    }
    
    private void setupJoints(){
        // Joints
        
        joint1 = new HingeJoint(node1.getControl(RigidBodyControl.class), // A
                     dyn1.getControl(RigidBodyControl.class), // B
                     Vector3f.ZERO,  // pivot point local to A
                     new Vector3f(0f, 5, 0),  // pivot point local to B
                     Vector3f.UNIT_Z,           // DoF Axis of A (Z axis)
                     Vector3f.UNIT_Z);        // DoF Axis of B (Z axis)
        
        joint2 = new HingeJoint(node2.getControl(RigidBodyControl.class), // A
                     dyn2.getControl(RigidBodyControl.class), // B
                     Vector3f.ZERO,  // pivot point local to A
                     new Vector3f(0f, 5, 0),  // pivot point local to B
                     Vector3f.UNIT_Z,           // DoF Axis of A (Z axis)
                     Vector3f.UNIT_Z);        // DoF Axis of B (Z axis)
        
        joint3 = new HingeJoint(node3.getControl(RigidBodyControl.class), // A
                     dyn3.getControl(RigidBodyControl.class), // B
                     Vector3f.ZERO,  // pivot point local to A
                     new Vector3f(0f, 5, 0),  // pivot point local to B
                     Vector3f.UNIT_Z,           // DoF Axis of A (Z axis)
                     Vector3f.UNIT_Z);        // DoF Axis of B (Z axis)
        
        joint4 = new HingeJoint(node4.getControl(RigidBodyControl.class), // A
                     dyn4.getControl(RigidBodyControl.class), // B
                     Vector3f.ZERO,  // pivot point local to A
                     new Vector3f(0f, 5, 0),  // pivot point local to B
                     Vector3f.UNIT_Z,           // DoF Axis of A (Z axis)
                     Vector3f.UNIT_Z);        // DoF Axis of B (Z axis)
        
        bulletAppState.getPhysicsSpace().add(joint1);
        bulletAppState.getPhysicsSpace().add(joint2);
        bulletAppState.getPhysicsSpace().add(joint3);
        bulletAppState.getPhysicsSpace().add(joint4);
        
        
        
        
    }
    
    // Add control to a node. 
    // Static nodes has 0 mass and dynamicla nodes hass mass > 0
    private Node createControl(BoxCollisionShape shape, float mass){
        Node node = new Node();
        RigidBodyControl rigidBodyControl = new RigidBodyControl(shape, mass);
        node.addControl(rigidBodyControl);
        return node;
    }

    boolean flag = true;
    @Override
    public void simpleUpdate(float tpf) {
        
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }
}

I jused the jme3- jbullet library and not the jme3-bullet and jme3-bullet-native.

Markering_065

Not really what I expected, but it’s bullet physics.
I expected a real simulation of newton’s cradle.

I think restitution defaults to zero to save on math. Try setting it to somewhere near 1 on all of your physicscontrols. It is mentioned here.

https://wiki.jmonkeyengine.org/jme3/advanced/physics.html#specify-physical-properties

1 Like

Thank you. Now it looks like a real newton cradle. Not 100%, but more like 85% of behavior.

Hi again!

Why can’t I set the gravity at the RigidBodyControl?

I try to sett some random vectors at the dynamical nodes, but nothing happens. Why?

dyn2.getControl(RigidBodyControl.class).setGravity(new Vector3f(10f, 0, 0));
dyn3.getControl(RigidBodyControl.class).setGravity(new Vector3f(100f, 0, 0));
dyn4.getControl(RigidBodyControl.class).setGravity(new Vector3f(1000f, 0, 0));
dyn5.getControl(RigidBodyControl.class).setGravity(new Vector3f(10000f, 0, 0));

maybe try setting the Y value instead?

Yes, But still nothing happens. It’s just a easy code of line and I don’t understand why the code won’t change the gravity of a individual spatials.

Setting the global gravity works.

bulletAppState.getPhysicsSpace().setGravity(new Vector3f(0, -10f, 0));

I set gravity on a RigidBodyControl, and it seemed to work. What evidence convinced you that nothing is happening?

Because you are doing it before you add it to the physics space… which will then set the gravity.

2 Likes

Ok! Not it’s working! Thank you.

Is there a way to fix the inner balls so they stand still, but still can give forces to the outer balls?

Markering_068

Edit:

Found the solution:

rigidBodyControl.setAngularDamping(20f);
1 Like