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 33package jme3test.bullet; 34 35 36import com.jme3.app.SimpleApplication; 37import com.jme3.bullet.BulletAppState; 38import com.jme3.bullet.PhysicsSpace; 39import com.jme3.bullet.collision.shapes.*; 40import com.jme3.bullet.control.RigidBodyControl; 41import com.jme3.bullet.joints.HingeJoint; 42import com.jme3.export.binary.BinaryExporter; 43import com.jme3.export.binary.BinaryImporter; 44import com.jme3.math.Plane; 45import com.jme3.math.Vector3f; 46import com.jme3.renderer.RenderManager; 47import com.jme3.scene.Node; 48import com.jme3.scene.shape.Sphere; 49import java.io.ByteArrayInputStream; 50import java.io.ByteArrayOutputStream; 51import java.io.IOException; 52import java.util.logging.Level; 53import java.util.logging.Logger; 54 55/** 56 * This is a basic Test of jbullet-jme functions 57 * 58 * @author normenhansen 59 */ 60public class TestPhysicsReadWrite extends SimpleApplication{ 61 private BulletAppState bulletAppState; 62 private Node physicsRootNode; 63 public static void main(String[] args){ 64 TestPhysicsReadWrite app = new TestPhysicsReadWrite(); 65 app.start(); 66 } 67 68 @Override 69 public void simpleInitApp() { 70 bulletAppState = new BulletAppState(); 71 stateManager.attach(bulletAppState); 72 bulletAppState.getPhysicsSpace().enableDebug(assetManager); 73 physicsRootNode=new Node("PhysicsRootNode"); 74 rootNode.attachChild(physicsRootNode); 75 76 // Add a physics sphere to the world 77 Node physicsSphere = PhysicsTestHelper.createPhysicsTestNode(assetManager, new SphereCollisionShape(1), 1); 78 physicsSphere.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(3, 6, 0)); 79 rootNode.attachChild(physicsSphere); 80 getPhysicsSpace().add(physicsSphere); 81 82 // Add a physics sphere to the world using the collision shape from sphere one 83 Node physicsSphere2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, physicsSphere.getControl(RigidBodyControl.class).getCollisionShape(), 1); 84 physicsSphere2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(4, 8, 0)); 85 rootNode.attachChild(physicsSphere2); 86 getPhysicsSpace().add(physicsSphere2); 87 88 // Add a physics box to the world 89 Node physicsBox = PhysicsTestHelper.createPhysicsTestNode(assetManager, new BoxCollisionShape(new Vector3f(1, 1, 1)), 1); 90 physicsBox.getControl(RigidBodyControl.class).setFriction(0.1f); 91 physicsBox.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(.6f, 4, .5f)); 92 rootNode.attachChild(physicsBox); 93 getPhysicsSpace().add(physicsBox); 94 95 // Add a physics cylinder to the world 96 Node physicsCylinder = PhysicsTestHelper.createPhysicsTestNode(assetManager, new CylinderCollisionShape(new Vector3f(1f, 1f, 1.5f)), 1); 97 physicsCylinder.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2, 2, 0)); 98 rootNode.attachChild(physicsCylinder); 99 getPhysicsSpace().add(physicsCylinder); 100 101 // an obstacle mesh, does not move (mass=0) 102 Node node2 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new MeshCollisionShape(new Sphere(16, 16, 1.2f)), 0); 103 node2.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(2.5f, -4, 0f)); 104 rootNode.attachChild(node2); 105 getPhysicsSpace().add(node2); 106 107 // the floor mesh, does not move (mass=0) 108 Node node3 = PhysicsTestHelper.createPhysicsTestNode(assetManager, new PlaneCollisionShape(new Plane(new Vector3f(0, 1, 0), 0)), 0); 109 node3.getControl(RigidBodyControl.class).setPhysicsLocation(new Vector3f(0f, -6, 0f)); 110 rootNode.attachChild(node3); 111 getPhysicsSpace().add(node3); 112 113 // Join the physics objects with a Point2Point joint 114 HingeJoint joint=new HingeJoint(physicsSphere.getControl(RigidBodyControl.class), physicsBox.getControl(RigidBodyControl.class), new Vector3f(-2,0,0), new Vector3f(2,0,0), Vector3f.UNIT_Z,Vector3f.UNIT_Z); 115 getPhysicsSpace().add(joint); 116 117 //save and load the physicsRootNode 118 try { 119 //remove all physics objects from physics space 120 getPhysicsSpace().removeAll(physicsRootNode); 121 physicsRootNode.removeFromParent(); 122 //export to byte array 123 ByteArrayOutputStream bout=new ByteArrayOutputStream(); 124 BinaryExporter.getInstance().save(physicsRootNode, bout); 125 //import from byte array 126 ByteArrayInputStream bin=new ByteArrayInputStream(bout.toByteArray()); 127 BinaryImporter imp=BinaryImporter.getInstance(); 128 imp.setAssetManager(assetManager); 129 Node newPhysicsRootNode=(Node)imp.load(bin); 130 //add all physics objects to physics space 131 getPhysicsSpace().addAll(newPhysicsRootNode); 132 rootNode.attachChild(newPhysicsRootNode); 133 } catch (IOException ex) { 134 Logger.getLogger(TestPhysicsReadWrite.class.getName()).log(Level.SEVERE, null, ex); 135 } 136 137 } 138 139 private PhysicsSpace getPhysicsSpace(){ 140 return bulletAppState.getPhysicsSpace(); 141 } 142 143 @Override 144 public void simpleUpdate(float tpf) { 145 //TODO: add update code 146 } 147 148 @Override 149 public void simpleRender(RenderManager rm) { 150 //TODO: add render code 151 } 152 153} 154