Home | History | Annotate | Download | only in bullet
      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 package jme3test.bullet;
     33 
     34 import com.jme3.app.SimpleApplication;
     35 import com.jme3.bullet.BulletAppState;
     36 import com.jme3.bullet.PhysicsSpace;
     37 import com.jme3.bullet.collision.shapes.BoxCollisionShape;
     38 import com.jme3.bullet.collision.shapes.SphereCollisionShape;
     39 import com.jme3.bullet.control.GhostControl;
     40 import com.jme3.bullet.control.PhysicsControl;
     41 import com.jme3.bullet.control.RigidBodyControl;
     42 import com.jme3.bullet.joints.HingeJoint;
     43 import com.jme3.input.KeyInput;
     44 import com.jme3.input.controls.AnalogListener;
     45 import com.jme3.input.controls.KeyTrigger;
     46 import com.jme3.math.Vector3f;
     47 import com.jme3.scene.Node;
     48 
     49 /**
     50  * Tests attaching ghost nodes to physicsnodes via the scenegraph
     51  * @author normenhansen
     52  */
     53 public class TestAttachGhostObject extends SimpleApplication implements AnalogListener {
     54 
     55     private HingeJoint joint;
     56     private GhostControl ghostControl;
     57     private Node collisionNode;
     58     private Node hammerNode;
     59     private Vector3f tempVec = new Vector3f();
     60     private BulletAppState bulletAppState;
     61 
     62     public static void main(String[] args) {
     63         TestAttachGhostObject app = new TestAttachGhostObject();
     64         app.start();
     65     }
     66 
     67     private void setupKeys() {
     68         inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_H));
     69         inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_K));
     70         inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
     71         inputManager.addListener(this, "Lefts", "Rights", "Space");
     72     }
     73 
     74     public void onAnalog(String binding, float value, float tpf) {
     75         if (binding.equals("Lefts")) {
     76             joint.enableMotor(true, 1, .1f);
     77         } else if (binding.equals("Rights")) {
     78             joint.enableMotor(true, -1, .1f);
     79         } else if (binding.equals("Space")) {
     80             joint.enableMotor(false, 0, 0);
     81         }
     82     }
     83 
     84     @Override
     85     public void simpleInitApp() {
     86         bulletAppState = new BulletAppState();
     87         stateManager.attach(bulletAppState);
     88         bulletAppState.getPhysicsSpace().enableDebug(assetManager);
     89         setupKeys();
     90         setupJoint();
     91     }
     92 
     93     private PhysicsSpace getPhysicsSpace() {
     94         return bulletAppState.getPhysicsSpace();
     95     }
     96 
     97     public void setupJoint() {
     98         Node holderNode = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(.1f, .1f, .1f)), 0);
     99         holderNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, 0, 0f));
    100         rootNode.attachChild(holderNode);
    101         getPhysicsSpace().add(holderNode);
    102 
    103         Node hammerNode = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(.3f, .3f, .3f)), 1);
    104         hammerNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -1, 0f));
    105         rootNode.attachChild(hammerNode);
    106         getPhysicsSpace().add(hammerNode);
    107 
    108         //immovable
    109         collisionNode = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(.3f, .3f, .3f)), 0);
    110         collisionNode.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(1.8f, 0, 0f));
    111         rootNode.attachChild(collisionNode);
    112         getPhysicsSpace().add(collisionNode);
    113 
    114         //ghost node
    115         ghostControl = new GhostControl(new SphereCollisionShape(0.7f));
    116 
    117         hammerNode.addControl(ghostControl);
    118         getPhysicsSpace().add(ghostControl);
    119 
    120         joint = new HingeJoint(holderNode.getControl(RigidBodyControl.class), hammerNode.getControl(RigidBodyControl.class), Vector3f.ZERO, new Vector3f(0f, -1, 0f), Vector3f.UNIT_Z, Vector3f.UNIT_Z);
    121         getPhysicsSpace().add(joint);
    122     }
    123 
    124     @Override
    125     public void simpleUpdate(float tpf) {
    126         if (ghostControl.getOverlappingObjects().contains(collisionNode.getControl(PhysicsControl.class))) {
    127             fpsText.setText("collide");
    128         }
    129     }
    130 }
    131