import com.jme3.light.AmbientLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
+import com.jme3.math.Vector3f;
import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
space.add(sphereGeometry);
}
+
+ public static void createPhysicsTestWorldSoccer(Node rootNode, AssetManager assetManager, PhysicsSpace space) {
+ AmbientLight light = new AmbientLight();
+ light.setColor(ColorRGBA.LightGray);
+ rootNode.addLight(light);
+
+ Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
+ material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
+
+ Box floorBox = new Box(140, 0.25f, 140);
+ Geometry floorGeometry = new Geometry("Floor", floorBox);
+ floorGeometry.setMaterial(material);
+ floorGeometry.setLocalTranslation(0, -0.25f, 0);
+// Plane plane = new Plane();
+// plane.setOriginNormal(new Vector3f(0, 0.25f, 0), Vector3f.UNIT_Y);
+// floorGeometry.addControl(new RigidBodyControl(new PlaneCollisionShape(plane), 0));
+ floorGeometry.addControl(new RigidBodyControl(0));
+ rootNode.attachChild(floorGeometry);
+ space.add(floorGeometry);
+
+ //movable spheres
+ for (int i = 0; i < 5; i++) {
+ Sphere sphere = new Sphere(16, 16, .5f);
+ Geometry ballGeometry = new Geometry("Soccer ball", sphere);
+ ballGeometry.setMaterial(material);
+ ballGeometry.setLocalTranslation(i, 2, -3);
+ //RigidBodyControl automatically uses Sphere collision shapes when attached to single geometry with sphere mesh
+ ballGeometry.addControl(new RigidBodyControl(.001f));
+ ballGeometry.getControl(RigidBodyControl.class).setRestitution(1);
+ rootNode.attachChild(ballGeometry);
+ space.add(ballGeometry);
+ }
+
+ //immovable Box with mesh collision shape
+ Box box = new Box(1, 1, 1);
+ Geometry boxGeometry = new Geometry("Box", box);
+ boxGeometry.setMaterial(material);
+ boxGeometry.setLocalTranslation(4, 1, 2);
+ boxGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(box), 0));
+ rootNode.attachChild(boxGeometry);
+ space.add(boxGeometry);
+
+ }
/**
* creates a box geometry with a RigidBodyControl
/*\r
- * Copyright (c) 2009-2010 jMonkeyEngine\r
- * All rights reserved.\r
- *\r
+ * Copyright (c) 2009-2010 jMonkeyEngine All rights reserved. <p/>\r
* Redistribution and use in source and binary forms, with or without\r
- * modification, are permitted provided that the following conditions are\r
- * met:\r
- *\r
- * * Redistributions of source code must retain the above copyright\r
- * notice, this list of conditions and the following disclaimer.\r
- *\r
- * * Redistributions in binary form must reproduce the above copyright\r
- * notice, this list of conditions and the following disclaimer in the\r
- * documentation and/or other materials provided with the distribution.\r
- *\r
- * * Neither the name of 'jMonkeyEngine' nor the names of its contributors\r
- * may be used to endorse or promote products derived from this software\r
- * without specific prior written permission.\r
- *\r
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED\r
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
+ * modification, are permitted provided that the following conditions are met:\r
+ * \r
+ * * Redistributions of source code must retain the above copyright notice,\r
+ * this list of conditions and the following disclaimer. <p/> * Redistributions\r
+ * in binary form must reproduce the above copyright notice, this list of\r
+ * conditions and the following disclaimer in the documentation and/or other\r
+ * materials provided with the distribution. <p/> * Neither the name of\r
+ * 'jMonkeyEngine' nor the names of its contributors may be used to endorse or\r
+ * promote products derived from this software without specific prior written\r
+ * permission. <p/> THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND\r
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT\r
+ * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A\r
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR\r
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;\r
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR\r
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\r
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
*/\r
package jme3test.bullet;\r
\r
import com.jme3.app.SimpleApplication;\r
import com.jme3.bullet.PhysicsSpace;\r
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;\r
-import com.jme3.bullet.collision.shapes.SphereCollisionShape;\r
import com.jme3.bullet.control.CharacterControl;\r
import com.jme3.input.KeyInput;\r
import com.jme3.input.MouseInput;\r
import com.jme3.input.controls.ActionListener;\r
import com.jme3.input.controls.KeyTrigger;\r
import com.jme3.input.controls.MouseButtonTrigger;\r
-import com.jme3.material.Material;\r
import com.jme3.math.Vector3f;\r
import com.jme3.renderer.RenderManager;\r
+import com.jme3.scene.CameraNode;\r
+import com.jme3.scene.Node;\r
import com.jme3.scene.Spatial;\r
-import com.jme3.scene.shape.Sphere;\r
-import com.jme3.scene.shape.Sphere.TextureMode;\r
+import com.jme3.scene.control.CameraControl.ControlDirection;\r
\r
/**\r
- *\r
- * @author normenhansen\r
+ * A walking physical character followed by a 3rd person camera. (No animation.)\r
+ * @author normenhansen, zathras\r
*/\r
public class TestPhysicsCharacter extends SimpleApplication implements ActionListener {\r
\r
- private BulletAppState bulletAppState;\r
- private CharacterControl physicsCharacter;\r
- private Vector3f walkDirection = new Vector3f();\r
- private Material mat;\r
- private Sphere bullet;\r
- private SphereCollisionShape bulletCollisionShape;\r
+ private BulletAppState bulletAppState;\r
+ private CharacterControl physicsCharacter;\r
+ private Node characterNode;\r
+ private CameraNode camNode;\r
+ boolean rotate = false;\r
+ private Vector3f walkDirection = new Vector3f(0,0,0);\r
+ private Vector3f viewDirection = new Vector3f(0,0,0);\r
+ boolean leftStrafe = false, rightStrafe = false, forward = false, backward = false, \r
+ leftRotate = false, rightRotate = false;\r
\r
- public static void main(String[] args) {\r
- TestPhysicsCharacter app = new TestPhysicsCharacter();\r
- app.start();\r
- }\r
+ public static void main(String[] args) {\r
+ TestPhysicsCharacter app = new TestPhysicsCharacter();\r
+ app.start();\r
+ }\r
\r
private void setupKeys() {\r
- inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_H));\r
- inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_K));\r
- inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_U));\r
- inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_J));\r
- inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));\r
- inputManager.addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));\r
- inputManager.addListener(this, "shoot");\r
- inputManager.addListener(this, "Lefts");\r
- inputManager.addListener(this, "Rights");\r
- inputManager.addListener(this, "Ups");\r
- inputManager.addListener(this, "Downs");\r
- inputManager.addListener(this, "Space");\r
- inputManager.addMapping("gc", new KeyTrigger(KeyInput.KEY_X));\r
- inputManager.addListener(this, "gc");\r
+ inputManager.addMapping("Strafe Left", \r
+ new KeyTrigger(KeyInput.KEY_Q), \r
+ new KeyTrigger(KeyInput.KEY_Z));\r
+ inputManager.addMapping("Strafe Right", \r
+ new KeyTrigger(KeyInput.KEY_E),\r
+ new KeyTrigger(KeyInput.KEY_X));\r
+ inputManager.addMapping("Rotate Left", \r
+ new KeyTrigger(KeyInput.KEY_A), \r
+ new KeyTrigger(KeyInput.KEY_LEFT));\r
+ inputManager.addMapping("Rotate Right", \r
+ new KeyTrigger(KeyInput.KEY_D), \r
+ new KeyTrigger(KeyInput.KEY_RIGHT));\r
+ inputManager.addMapping("Walk Forward", \r
+ new KeyTrigger(KeyInput.KEY_W), \r
+ new KeyTrigger(KeyInput.KEY_UP));\r
+ inputManager.addMapping("Walk Backward", \r
+ new KeyTrigger(KeyInput.KEY_S),\r
+ new KeyTrigger(KeyInput.KEY_DOWN));\r
+ inputManager.addMapping("Jump", \r
+ new KeyTrigger(KeyInput.KEY_SPACE), \r
+ new KeyTrigger(KeyInput.KEY_RETURN));\r
+ inputManager.addMapping("Shoot", \r
+ new MouseButtonTrigger(MouseInput.BUTTON_LEFT));\r
+ inputManager.addListener(this, "Strafe Left", "Strafe Right");\r
+ inputManager.addListener(this, "Rotate Left", "Rotate Right");\r
+ inputManager.addListener(this, "Walk Forward", "Walk Backward");\r
+ inputManager.addListener(this, "Jump", "Shoot");\r
}\r
+ @Override\r
+ public void simpleInitApp() {\r
+ // activate physics\r
+ bulletAppState = new BulletAppState();\r
+ stateManager.attach(bulletAppState);\r
\r
- @Override\r
- public void simpleInitApp() {\r
- bulletAppState = new BulletAppState();\r
- stateManager.attach(bulletAppState);\r
- bullet = new Sphere(32, 32, 0.4f, true, false);\r
- bullet.setTextureMode(TextureMode.Projected);\r
- bulletCollisionShape = new SphereCollisionShape(0.4f);\r
- PhysicsTestHelper.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());\r
+ // init a physical test scene\r
+ PhysicsTestHelper.createPhysicsTestWorldSoccer(rootNode, assetManager, bulletAppState.getPhysicsSpace());\r
+ setupKeys();\r
\r
- setupKeys();\r
+ // Add a physics character to the world\r
+ physicsCharacter = new CharacterControl(new CapsuleCollisionShape(0.5f, 1.8f), .1f);\r
+ physicsCharacter.setPhysicsLocation(new Vector3f(0, 1, 0));\r
+ characterNode = new Node("character node");\r
+ Spatial model = assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");\r
+ model.scale(0.25f);\r
+ characterNode.addControl(physicsCharacter);\r
+ getPhysicsSpace().add(physicsCharacter);\r
+ rootNode.attachChild(characterNode);\r
+ characterNode.attachChild(model);\r
\r
- // Add a physics character to the world\r
- physicsCharacter = new CharacterControl(new CapsuleCollisionShape(0.5f,1.8f), .1f);\r
- physicsCharacter.setPhysicsLocation(new Vector3f(3, 6, 0));\r
+ // set forward camera node that follows the character\r
+ camNode = new CameraNode("CamNode", cam);\r
+ camNode.setControlDir(ControlDirection.SpatialToCamera);\r
+ camNode.setLocalTranslation(new Vector3f(0, 1, -5));\r
+ camNode.lookAt(model.getLocalTranslation(), Vector3f.UNIT_Y);\r
+ characterNode.attachChild(camNode);\r
\r
- Spatial model = assetManager.loadModel("Models/Sinbad/Sinbad.mesh.xml");\r
- model.scale(0.25f);\r
- model.addControl(physicsCharacter);\r
- getPhysicsSpace().add(physicsCharacter);\r
- rootNode.attachChild(model);\r
- }\r
+ //disable the default 1st-person flyCam (don't forget this!!)\r
+ flyCam.setEnabled(false);\r
\r
- private PhysicsSpace getPhysicsSpace() {\r
- return bulletAppState.getPhysicsSpace();\r
- }\r
+ }\r
\r
- @Override\r
+ @Override\r
public void simpleUpdate(float tpf) {\r
+ Vector3f camDir = cam.getDirection().mult(0.2f);\r
+ Vector3f camLeft = cam.getLeft().mult(0.2f);\r
+ camDir.y = 0;\r
+ camLeft.y = 0;\r
+ viewDirection.set(camDir);\r
+ walkDirection.set(0, 0, 0);\r
+ if (leftStrafe) {\r
+ walkDirection.addLocal(camLeft);\r
+ } else\r
+ if (rightStrafe) {\r
+ walkDirection.addLocal(camLeft.negate());\r
+ }\r
+ if (leftRotate) {\r
+ viewDirection.addLocal(camLeft.mult(0.02f));\r
+ } else\r
+ if (rightRotate) {\r
+ viewDirection.addLocal(camLeft.mult(0.02f).negate());\r
+ }\r
+ if (forward) {\r
+ walkDirection.addLocal(camDir);\r
+ } else\r
+ if (backward) {\r
+ walkDirection.addLocal(camDir.negate());\r
+ }\r
physicsCharacter.setWalkDirection(walkDirection);\r
- physicsCharacter.setViewDirection(walkDirection);\r
- cam.lookAt(physicsCharacter.getPhysicsLocation(), Vector3f.UNIT_Y);\r
- }\r
-\r
- @Override\r
- public void simpleRender(RenderManager rm) {\r
- //TODO: add render code\r
+ physicsCharacter.setViewDirection(viewDirection);\r
}\r
\r
public void onAction(String binding, boolean value, float tpf) {\r
- if (binding.equals("Lefts")) {\r
+ if (binding.equals("Strafe Left")) {\r
if (value) {\r
- walkDirection.addLocal(new Vector3f(-.1f, 0, 0));\r
+ leftStrafe = true;\r
} else {\r
- walkDirection.addLocal(new Vector3f(.1f, 0, 0));\r
+ leftStrafe = false;\r
}\r
- } else if (binding.equals("Rights")) {\r
+ } else if (binding.equals("Strafe Right")) {\r
if (value) {\r
- walkDirection.addLocal(new Vector3f(.1f, 0, 0));\r
+ rightStrafe = true;\r
} else {\r
- walkDirection.addLocal(new Vector3f(-.1f, 0, 0));\r
+ rightStrafe = false;\r
}\r
- } else if (binding.equals("Ups")) {\r
+ } else if (binding.equals("Rotate Left")) {\r
if (value) {\r
- walkDirection.addLocal(new Vector3f(0, 0, -.1f));\r
+ leftRotate = true;\r
} else {\r
- walkDirection.addLocal(new Vector3f(0, 0, .1f));\r
+ leftRotate = false;\r
}\r
- } else if (binding.equals("Downs")) {\r
+ } else if (binding.equals("Rotate Right")) {\r
if (value) {\r
- walkDirection.addLocal(new Vector3f(0, 0, .1f));\r
+ rightRotate = true;\r
} else {\r
- walkDirection.addLocal(new Vector3f(0, 0, -.1f));\r
+ rightRotate = false;\r
}\r
- } else if (binding.equals("Space")) {\r
+ } else if (binding.equals("Walk Forward")) {\r
+ if (value) {\r
+ forward = true;\r
+ } else {\r
+ forward = false;\r
+ }\r
+ } else if (binding.equals("Walk Backward")) {\r
+ if (value) {\r
+ backward = true;\r
+ } else {\r
+ backward = false;\r
+ }\r
+ } else if (binding.equals("Jump")) {\r
physicsCharacter.jump();\r
}\r
- if (binding.equals("gc") && !value) {\r
- System.gc();\r
- }\r
}\r
+\r
+ private PhysicsSpace getPhysicsSpace() {\r
+ return bulletAppState.getPhysicsSpace();\r
+ }\r
+\r
+ @Override\r
+ public void simpleRender(RenderManager rm) {\r
+ //TODO: add render code\r
+ }\r
}\r