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 TestSimplePhysics extends SimpleApplication {
50
51    private BulletAppState bulletAppState;
52
53    public static void main(String[] args) {
54        TestSimplePhysics app = new TestSimplePhysics();
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        rootNode.attachChild(physicsSphere);
68        getPhysicsSpace().add(physicsSphere);
69
70        // Add a physics sphere to the world using the collision shape from sphere one
71        Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, physicsSphere.getControl(RigidBodyControl.class).getCollisionShape(), 1);
72        physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(4, 8, 0));
73        rootNode.attachChild(physicsSphere2);
74        getPhysicsSpace().add(physicsSphere2);
75
76        // Add a physics box to the world
77        Node physicsBox = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(1, 1, 1)), 1);
78        physicsBox.getControl(RigidBodyControl.class).setFriction(0.1f);
79        physicsBox.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.6f, 4, .5f));
80        rootNode.attachChild(physicsBox);
81        getPhysicsSpace().add(physicsBox);
82
83        // Add a physics cylinder to the world
84        Node physicsCylinder = PhysicsTestHelper.createPhysicsTestNode(assetManager, new CylinderCollisionShape(new Vector3f(1f, 1f, 1.5f)), 1);
85        physicsCylinder.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2, 2, 0));
86        rootNode.attachChild(physicsCylinder);
87        getPhysicsSpace().add(physicsCylinder);
88
89        // an obstacle mesh, does not move (mass=0)
90        Node node2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new MeshCollisionShape(new Sphere(16, 16, 1.2f)), 0);
91        node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2.5f, -4, 0f));
92        rootNode.attachChild(node2);
93        getPhysicsSpace().add(node2);
94
95        // the floor mesh, does not move (mass=0)
96        Node node3 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new PlaneCollisionShape(new Plane(new Vector3f(0, 1, 0), 0)), 0);
97        node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f));
98        rootNode.attachChild(node3);
99        getPhysicsSpace().add(node3);
100
101        // Join the physics objects with a Point2Point joint
102//        PhysicsPoint2PointJoint joint=new PhysicsPoint2PointJoint(physicsSphere, physicsBox, new Vector3f(-2,0,0), new Vector3f(2,0,0));
103//        PhysicsHingeJoint joint=new PhysicsHingeJoint(physicsSphere, physicsBox, new Vector3f(-2,0,0), new Vector3f(2,0,0), Vector3f.UNIT_Z,Vector3f.UNIT_Z);
104//        getPhysicsSpace().add(joint);
105
106    }
107
108    private PhysicsSpace getPhysicsSpace() {
109        return bulletAppState.getPhysicsSpace();
110    }
111}
112