1/*
2 * Copyright (c) 2009-2010 jMonkeyEngine
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 *   notice, this list of conditions and the following disclaimer in the
14 *   documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 *   may be used to endorse or promote products derived from this software
18 *   without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33package jme3test.bullet;
34
35import com.jme3.app.SimpleApplication;
36import com.jme3.bullet.BulletAppState;
37import com.jme3.bullet.PhysicsSpace;
38import com.jme3.bullet.collision.shapes.BoxCollisionShape;
39import com.jme3.bullet.control.RigidBodyControl;
40import com.jme3.bullet.joints.HingeJoint;
41import com.jme3.input.KeyInput;
42import com.jme3.input.controls.AnalogListener;
43import com.jme3.input.controls.KeyTrigger;
44import com.jme3.math.Vector3f;
45import com.jme3.scene.Node;
46
47public class TestPhysicsHingeJoint extends SimpleApplication implements AnalogListener {
48    private BulletAppState bulletAppState;
49    private HingeJoint joint;
50
51    public static void main(String[] args) {
52        TestPhysicsHingeJoint app = new TestPhysicsHingeJoint();
53        app.start();
54    }
55
56    private void setupKeys() {
57        inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_H));
58        inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));
59        inputManager.addMapping("Swing", new KeyTrigger(KeyInput.KEY_SPACE));
60        inputManager.addListener(this, "Left", "Right", "Swing");
61    }
62
63    public void onAnalog(String binding, float value, float tpf) {
64        if(binding.equals("Left")){
65            joint.enableMotor(true, 1, .1f);
66        }
67        else if(binding.equals("Right")){
68            joint.enableMotor(true, -1, .1f);
69        }
70        else if(binding.equals("Swing")){
71            joint.enableMotor(false, 0, 0);
72        }
73    }
74
75    @Override
76    public void simpleInitApp() {
77        bulletAppState = new BulletAppState();
78        stateManager.attach(bulletAppState);
79        bulletAppState.getPhysicsSpace().enableDebug(assetManager);
80        setupKeys();
81        setupJoint();
82    }
83
84    private PhysicsSpace getPhysicsSpace(){
85        return bulletAppState.getPhysicsSpace();
86    }
87
88    public void setupJoint() {
89        Node holderNode=PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f( .1f, .1f, .1f)),0);
90        holderNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f,0,0f));
91        rootNode.attachChild(holderNode);
92        getPhysicsSpace().add(holderNode);
93
94        Node hammerNode=PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f( .3f, .3f, .3f)),1);
95        hammerNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f,-1,0f));
96        rootNode.attachChild(hammerNode);
97        getPhysicsSpace().add(hammerNode);
98
99        joint=new HingeJoint(holderNode.getControl(RigidBodyControl.class), hammerNode.getControl(RigidBodyControl.class), Vector3f.ZERO, new Vector3f(0f,-1,0f), Vector3f.UNIT_Z, Vector3f.UNIT_Z);
100        getPhysicsSpace().add(joint);
101    }
102
103    @Override
104    public void simpleUpdate(float tpf) {
105
106    }
107
108
109}