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.MeshCollisionShape;
     39 import com.jme3.bullet.collision.shapes.SphereCollisionShape;
     40 import com.jme3.bullet.control.RigidBodyControl;
     41 import com.jme3.input.MouseInput;
     42 import com.jme3.input.controls.ActionListener;
     43 import com.jme3.input.controls.MouseButtonTrigger;
     44 import com.jme3.material.Material;
     45 import com.jme3.math.ColorRGBA;
     46 import com.jme3.math.Vector3f;
     47 import com.jme3.renderer.RenderManager;
     48 import com.jme3.renderer.queue.RenderQueue.ShadowMode;
     49 import com.jme3.scene.Geometry;
     50 import com.jme3.scene.Node;
     51 import com.jme3.scene.shape.Box;
     52 import com.jme3.scene.shape.Sphere;
     53 import com.jme3.scene.shape.Sphere.TextureMode;
     54 
     55 /**
     56  *
     57  * @author normenhansen
     58  */
     59 public class TestCcd extends SimpleApplication implements ActionListener {
     60 
     61     private Material mat;
     62     private Material mat2;
     63     private Sphere bullet;
     64     private SphereCollisionShape bulletCollisionShape;
     65     private BulletAppState bulletAppState;
     66 
     67     public static void main(String[] args) {
     68         TestCcd app = new TestCcd();
     69         app.start();
     70     }
     71 
     72     private void setupKeys() {
     73         inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
     74         inputManager.addMapping("shoot2", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
     75         inputManager.addListener(this, "shoot");
     76         inputManager.addListener(this, "shoot2");
     77     }
     78 
     79     @Override
     80     public void simpleInitApp() {
     81         bulletAppState = new BulletAppState();
     82         stateManager.attach(bulletAppState);
     83         bulletAppState.getPhysicsSpace().enableDebug(assetManager);
     84         bullet = new Sphere(32, 32, 0.4f, true, false);
     85         bullet.setTextureMode(TextureMode.Projected);
     86         bulletCollisionShape = new SphereCollisionShape(0.1f);
     87         setupKeys();
     88 
     89         mat = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
     90         mat.getAdditionalRenderState().setWireframe(true);
     91         mat.setColor("Color", ColorRGBA.Green);
     92 
     93         mat2 = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
     94         mat2.getAdditionalRenderState().setWireframe(true);
     95         mat2.setColor("Color", ColorRGBA.Red);
     96 
     97         // An obstacle mesh, does not move (mass=0)
     98         Node node2 = new Node();
     99         node2.setName("mesh");
    100         node2.setLocalTranslation(new Vector3f(2.5f, 0, 0f));
    101         node2.addControl(new RigidBodyControl(new MeshCollisionShape(new Box(Vector3f.ZERO, 4, 4, 0.1f)), 0));
    102         rootNode.attachChild(node2);
    103         getPhysicsSpace().add(node2);
    104 
    105         // The floor, does not move (mass=0)
    106         Node node3 = new Node();
    107         node3.setLocalTranslation(new Vector3f(0f, -6, 0f));
    108         node3.addControl(new RigidBodyControl(new BoxCollisionShape(new Vector3f(100, 1, 100)), 0));
    109         rootNode.attachChild(node3);
    110         getPhysicsSpace().add(node3);
    111 
    112     }
    113 
    114     private PhysicsSpace getPhysicsSpace() {
    115         return bulletAppState.getPhysicsSpace();
    116     }
    117 
    118     @Override
    119     public void simpleUpdate(float tpf) {
    120         //TODO: add update code
    121     }
    122 
    123     @Override
    124     public void simpleRender(RenderManager rm) {
    125         //TODO: add render code
    126     }
    127 
    128     public void onAction(String binding, boolean value, float tpf) {
    129         if (binding.equals("shoot") && !value) {
    130             Geometry bulletg = new Geometry("bullet", bullet);
    131             bulletg.setMaterial(mat);
    132             bulletg.setName("bullet");
    133             bulletg.setLocalTranslation(cam.getLocation());
    134             bulletg.setShadowMode(ShadowMode.CastAndReceive);
    135             bulletg.addControl(new RigidBodyControl(bulletCollisionShape, 1));
    136             bulletg.getControl(RigidBodyControl.class).setCcdMotionThreshold(0.1f);
    137             bulletg.getControl(RigidBodyControl.class).setLinearVelocity(cam.getDirection().mult(40));
    138             rootNode.attachChild(bulletg);
    139             getPhysicsSpace().add(bulletg);
    140         } else if (binding.equals("shoot2") && !value) {
    141             Geometry bulletg = new Geometry("bullet", bullet);
    142             bulletg.setMaterial(mat2);
    143             bulletg.setName("bullet");
    144             bulletg.setLocalTranslation(cam.getLocation());
    145             bulletg.setShadowMode(ShadowMode.CastAndReceive);
    146             bulletg.addControl(new RigidBodyControl(bulletCollisionShape, 1));
    147             bulletg.getControl(RigidBodyControl.class).setLinearVelocity(cam.getDirection().mult(40));
    148             rootNode.attachChild(bulletg);
    149             getPhysicsSpace().add(bulletg);
    150         }
    151     }
    152 }
    153