159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * To change this template, choose Tools | Templates
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * and open the template in the editor.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage jme3test.bullet;
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.app.SimpleApplication;
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.BulletAppState;
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.PhysicsSpace;
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.control.RigidBodyControl;
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.joints.ConeJoint;
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.joints.PhysicsJoint;
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.controls.ActionListener;
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.controls.MouseButtonTrigger;
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f;
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Node;
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/**
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author normenhansen
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class TestRagDoll extends SimpleApplication implements ActionListener {
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private BulletAppState bulletAppState = new BulletAppState();
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private Node ragDoll = new Node();
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private Node shoulders;
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private Vector3f upforce = new Vector3f(0, 200, 0);
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private boolean applyForce = false;
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static void main(String[] args) {
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        TestRagDoll app = new TestRagDoll();
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        app.start();
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void simpleInitApp() {
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        bulletAppState = new BulletAppState();
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        stateManager.attach(bulletAppState);
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        bulletAppState.getPhysicsSpace().enableDebug(assetManager);
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        inputManager.addMapping("Pull ragdoll up", new MouseButtonTrigger(0));
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        inputManager.addListener(this, "Pull ragdoll up");
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        PhysicsTestHelper.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        createRagDoll();
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private void createRagDoll() {
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        shoulders = createLimb(0.2f, 1.0f, new Vector3f(0.00f, 1.5f, 0), true);
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node uArmL = createLimb(0.2f, 0.5f, new Vector3f(-0.75f, 0.8f, 0), false);
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node uArmR = createLimb(0.2f, 0.5f, new Vector3f(0.75f, 0.8f, 0), false);
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node lArmL = createLimb(0.2f, 0.5f, new Vector3f(-0.75f, -0.2f, 0), false);
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node lArmR = createLimb(0.2f, 0.5f, new Vector3f(0.75f, -0.2f, 0), false);
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node body = createLimb(0.2f, 1.0f, new Vector3f(0.00f, 0.5f, 0), false);
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node hips = createLimb(0.2f, 0.5f, new Vector3f(0.00f, -0.5f, 0), true);
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node uLegL = createLimb(0.2f, 0.5f, new Vector3f(-0.25f, -1.2f, 0), false);
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node uLegR = createLimb(0.2f, 0.5f, new Vector3f(0.25f, -1.2f, 0), false);
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node lLegL = createLimb(0.2f, 0.5f, new Vector3f(-0.25f, -2.2f, 0), false);
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node lLegR = createLimb(0.2f, 0.5f, new Vector3f(0.25f, -2.2f, 0), false);
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        join(body, shoulders, new Vector3f(0f, 1.4f, 0));
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        join(body, hips, new Vector3f(0f, -0.5f, 0));
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        join(uArmL, shoulders, new Vector3f(-0.75f, 1.4f, 0));
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        join(uArmR, shoulders, new Vector3f(0.75f, 1.4f, 0));
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        join(uArmL, lArmL, new Vector3f(-0.75f, .4f, 0));
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        join(uArmR, lArmR, new Vector3f(0.75f, .4f, 0));
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        join(uLegL, hips, new Vector3f(-.25f, -0.5f, 0));
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        join(uLegR, hips, new Vector3f(.25f, -0.5f, 0));
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        join(uLegL, lLegL, new Vector3f(-.25f, -1.7f, 0));
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        join(uLegR, lLegR, new Vector3f(.25f, -1.7f, 0));
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ragDoll.attachChild(shoulders);
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ragDoll.attachChild(body);
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ragDoll.attachChild(hips);
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ragDoll.attachChild(uArmL);
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ragDoll.attachChild(uArmR);
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ragDoll.attachChild(lArmL);
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ragDoll.attachChild(lArmR);
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ragDoll.attachChild(uLegL);
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ragDoll.attachChild(uLegR);
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ragDoll.attachChild(lLegL);
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ragDoll.attachChild(lLegR);
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(ragDoll);
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        bulletAppState.getPhysicsSpace().addAll(ragDoll);
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private Node createLimb(float width, float height, Vector3f location, boolean rotate) {
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        int axis = rotate ? PhysicsSpace.AXIS_X : PhysicsSpace.AXIS_Y;
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        CapsuleCollisionShape shape = new CapsuleCollisionShape(width, height, axis);
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node node = new Node("Limb");
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        RigidBodyControl rigidBodyControl = new RigidBodyControl(shape, 1);
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        node.setLocalTranslation(location);
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        node.addControl(rigidBodyControl);
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return node;
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private PhysicsJoint join(Node A, Node B, Vector3f connectionPoint) {
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f pivotA = A.worldToLocal(connectionPoint, new Vector3f());
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Vector3f pivotB = B.worldToLocal(connectionPoint, new Vector3f());
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        ConeJoint joint = new ConeJoint(A.getControl(RigidBodyControl.class), B.getControl(RigidBodyControl.class), pivotA, pivotB);
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        joint.setLimit(1f, 1f, 0);
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return joint;
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void onAction(String string, boolean bln, float tpf) {
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if ("Pull ragdoll up".equals(string)) {
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (bln) {
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                shoulders.getControl(RigidBodyControl.class).activate();
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                applyForce = true;
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            } else {
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                applyForce = false;
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void simpleUpdate(float tpf) {
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (applyForce) {
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            shoulders.getControl(RigidBodyControl.class).applyForce(upforce, Vector3f.ZERO);
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
125