159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met:
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer.
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer in the
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   documentation and/or other materials provided with the distribution.
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   may be used to endorse or promote products derived from this software
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   without specific prior written permission.
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage jme3test.bullet;
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.app.SimpleApplication;
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.BulletAppState;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.PhysicsSpace;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.collision.shapes.*;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.control.RigidBodyControl;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Plane;
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f;
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Node;
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.shape.Sphere;
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/**
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This is a basic Test of jbullet-jme functions
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author normenhansen
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class TestLocalPhysics extends SimpleApplication {
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private BulletAppState bulletAppState;
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static void main(String[] args) {
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        TestLocalPhysics app = new TestLocalPhysics();
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        app.start();
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void simpleInitApp() {
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        bulletAppState = new BulletAppState();
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        stateManager.attach(bulletAppState);
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        bulletAppState.getPhysicsSpace().enableDebug(assetManager);
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // Add a physics sphere to the world
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node physicsSphere = PhysicsTestHelper.createPhysicsTestNode(assetManager, new SphereCollisionShape(1), 1);
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        physicsSphere.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3, 6, 0));
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        physicsSphere.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(physicsSphere);
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        getPhysicsSpace().add(physicsSphere);
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // Add a physics sphere to the world using the collision shape from sphere one
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, physicsSphere.getControl(RigidBodyControl.class).getCollisionShape(), 1);
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(4, 8, 0));
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        physicsSphere2.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(physicsSphere2);
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        getPhysicsSpace().add(physicsSphere2);
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // Add a physics box to the world
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node physicsBox = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(1, 1, 1)), 1);
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        physicsBox.getControl(RigidBodyControl.class).setFriction(0.1f);
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        physicsBox.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.6f, 4, .5f));
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        physicsBox.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(physicsBox);
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        getPhysicsSpace().add(physicsBox);
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // Add a physics cylinder to the world
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node physicsCylinder = PhysicsTestHelper.createPhysicsTestNode(assetManager, new CylinderCollisionShape(new Vector3f(1f, 1f, 1.5f)), 1);
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        physicsCylinder.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2, 2, 0));
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        physicsCylinder.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(physicsCylinder);
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        getPhysicsSpace().add(physicsCylinder);
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // an obstacle mesh, does not move (mass=0)
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node node2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new MeshCollisionShape(new Sphere(16, 16, 1.2f)), 0);
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2.5f, -4, 0f));
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        node2.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(node2);
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        getPhysicsSpace().add(node2);
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // the floor mesh, does not move (mass=0)
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Node node3 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new PlaneCollisionShape(new Plane(new Vector3f(0, 1, 0), 0)), 0);
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f));
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        node3.getControl(RigidBodyControl.class).setApplyPhysicsLocal(true);
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(node3);
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        getPhysicsSpace().add(node3);
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // Join the physics objects with a Point2Point joint
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//        PhysicsPoint2PointJoint joint=new PhysicsPoint2PointJoint(physicsSphere, physicsBox, new Vector3f(-2,0,0), new Vector3f(2,0,0));
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//        PhysicsHingeJoint joint=new PhysicsHingeJoint(physicsSphere, physicsBox, new Vector3f(-2,0,0), new Vector3f(2,0,0), Vector3f.UNIT_Z,Vector3f.UNIT_Z);
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//        getPhysicsSpace().add(joint);
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void simpleUpdate(float tpf) {
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.rotate(tpf, 0, 0);
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private PhysicsSpace getPhysicsSpace() {
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return bulletAppState.getPhysicsSpace();
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
123