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 */
32package jme3test.bullet;
33
34import com.jme3.app.SimpleApplication;
35import com.jme3.bullet.BulletAppState;
36import com.jme3.bullet.PhysicsSpace;
37import com.jme3.bullet.collision.shapes.*;
38import com.jme3.bullet.control.RigidBodyControl;
39import com.jme3.math.Plane;
40import com.jme3.math.Vector3f;
41import com.jme3.scene.Node;
42import com.jme3.scene.shape.Sphere;
43
44/**
45 * This is a basic Test of jbullet-jme functions
46 *
47 * @author normenhansen
48 */
49public class TestLocalPhysics extends SimpleApplication {
50
51    private BulletAppState bulletAppState;
52
53    public static void main(String[] args) {
54        TestLocalPhysics app = new TestLocalPhysics();
55        app.start();
56    }
57
58    @Override
59    public void simpleInitApp() {
60        bulletAppState = new BulletAppState();
61        stateManager.attach(bulletAppState);
62        bulletAppState.getPhysicsSpace().enableDebug(assetManager);
63
64        // Add a physics sphere to the world
65        Node physicsSphere = PhysicsTestHelper.createPhysicsTestNode(assetManager, new SphereCollisionShape(1), 1);
66        physicsSphere.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3, 6, 0));
67        physicsSphere.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
68        rootNode.attachChild(physicsSphere);
69        getPhysicsSpace().add(physicsSphere);
70
71        // Add a physics sphere to the world using the collision shape from sphere one
72        Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, physicsSphere.getControl(RigidBodyControl.class).getCollisionShape(), 1);
73        physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(4, 8, 0));
74        physicsSphere2.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
75        rootNode.attachChild(physicsSphere2);
76        getPhysicsSpace().add(physicsSphere2);
77
78        // Add a physics box to the world
79        Node physicsBox = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(1, 1, 1)), 1);
80        physicsBox.getControl(RigidBodyControl.class).setFriction(0.1f);
81        physicsBox.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.6f, 4, .5f));
82        physicsBox.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
83        rootNode.attachChild(physicsBox);
84        getPhysicsSpace().add(physicsBox);
85
86        // Add a physics cylinder to the world
87        Node physicsCylinder = PhysicsTestHelper.createPhysicsTestNode(assetManager, new CylinderCollisionShape(new Vector3f(1f, 1f, 1.5f)), 1);
88        physicsCylinder.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2, 2, 0));
89        physicsCylinder.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
90        rootNode.attachChild(physicsCylinder);
91        getPhysicsSpace().add(physicsCylinder);
92
93        // an obstacle mesh, does not move (mass=0)
94        Node node2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new MeshCollisionShape(new Sphere(16, 16, 1.2f)), 0);
95        node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2.5f, -4, 0f));
96        node2.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
97        rootNode.attachChild(node2);
98        getPhysicsSpace().add(node2);
99
100        // the floor mesh, does not move (mass=0)
101        Node node3 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new PlaneCollisionShape(new Plane(new Vector3f(0, 1, 0), 0)), 0);
102        node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f));
103        node3.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
104        rootNode.attachChild(node3);
105        getPhysicsSpace().add(node3);
106
107        // Join the physics objects with a Point2Point joint
108//        PhysicsPoint2PointJoint joint=new PhysicsPoint2PointJoint(physicsSphere, physicsBox, new Vector3f(-2,0,0), new Vector3f(2,0,0));
109//        PhysicsHingeJoint joint=new PhysicsHingeJoint(physicsSphere, physicsBox, new Vector3f(-2,0,0), new Vector3f(2,0,0), Vector3f.UNIT_Z,Vector3f.UNIT_Z);
110//        getPhysicsSpace().add(joint);
111
112    }
113
114    @Override
115    public void simpleUpdate(float tpf) {
116        rootNode.rotate(tpf, 0, 0);
117    }
118
119    private PhysicsSpace getPhysicsSpace() {
120        return bulletAppState.getPhysicsSpace();
121    }
122}
123