159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/* 259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * To change this template, choose Tools | Templates 359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * and open the template in the editor. 459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage jme3test.bullet; 659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.app.SimpleApplication; 859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.BulletAppState; 959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.PhysicsSpace; 1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.collision.shapes.MeshCollisionShape; 1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.collision.shapes.PlaneCollisionShape; 1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.collision.shapes.SphereCollisionShape; 1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.control.RigidBodyControl; 1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Plane; 1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f; 1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Node; 1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.shape.Sphere; 1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/** 2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * 2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author Nehon 2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class TestKinematicAddToPhysicsSpaceIssue extends SimpleApplication { 2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public static void main(String[] args) { 2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta TestKinematicAddToPhysicsSpaceIssue app = new TestKinematicAddToPhysicsSpaceIssue(); 2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta app.start(); 2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta BulletAppState bulletAppState; 3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void simpleInitApp() { 3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta bulletAppState = new BulletAppState(); 3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta stateManager.attach(bulletAppState); 3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta bulletAppState.getPhysicsSpace().enableDebug(assetManager); 3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Add a physics sphere to the world 3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Node physicsSphere = PhysicsTestHelper.createPhysicsTestNode(assetManager, new SphereCollisionShape(1), 1); 3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta physicsSphere.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3, 6, 0)); 4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.attachChild(physicsSphere); 4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //Setting the rigidBody to kinematic before adding it to the physic space 4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta physicsSphere.getControl(RigidBodyControl.class).setKinematic(true); 4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //adding it to the physic space 4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta getPhysicsSpace().add(physicsSphere); 4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //Making it not kinematic again, it should fall under gravity, it doesn't 4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta physicsSphere.getControl(RigidBodyControl.class).setKinematic(false); 4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // Add a physics sphere to the world using the collision shape from sphere one 5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new SphereCollisionShape(1), 1); 5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(5, 6, 0)); 5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.attachChild(physicsSphere2); 5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //Adding the rigid body to physic space 5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta getPhysicsSpace().add(physicsSphere2); 5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //making it kinematic 5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta physicsSphere2.getControl(RigidBodyControl.class).setKinematic(false); 5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //Making it not kinematic again, it works properly, the rigidbody is affected by grvity. 5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta physicsSphere2.getControl(RigidBodyControl.class).setKinematic(false); 6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // an obstacle mesh, does not move (mass=0) 6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Node node2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new MeshCollisionShape(new Sphere(16, 16, 1.2f)), 0); 6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2.5f, -4, 0f)); 6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.attachChild(node2); 6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta getPhysicsSpace().add(node2); 6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // the floor mesh, does not move (mass=0) 7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Node node3 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new PlaneCollisionShape(new Plane(new Vector3f(0, 1, 0), 0)), 0); 7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f)); 7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.attachChild(node3); 7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta getPhysicsSpace().add(node3); 7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private PhysicsSpace getPhysicsSpace() { 7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return bulletAppState.getPhysicsSpace(); 7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta} 81