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 Barta 3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage jme3test.helloworld; 3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.app.SimpleApplication; 3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.asset.plugins.ZipLocator; 3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.BulletAppState; 3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.collision.shapes.CapsuleCollisionShape; 3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.collision.shapes.CollisionShape; 4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.control.CharacterControl; 4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.control.RigidBodyControl; 4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bullet.util.CollisionShapeFactory; 4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.KeyInput; 4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.controls.ActionListener; 4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.controls.KeyTrigger; 4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.light.AmbientLight; 4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.light.DirectionalLight; 4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.ColorRGBA; 4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f; 5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Node; 5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Spatial; 5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/** 5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Example 9 - How to make walls and floors solid. 5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This collision code uses Physics and a custom Action Listener. 5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * @author normen, with edits by Zathras 5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class HelloCollision extends SimpleApplication 5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta implements ActionListener { 6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private Spatial sceneModel; 6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private BulletAppState bulletAppState; 6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private RigidBodyControl landscape; 6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private CharacterControl player; 6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private Vector3f walkDirection = new Vector3f(); 6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private boolean left = false, right = false, up = false, down = false; 6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public static void main(String[] args) { 6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta HelloCollision app = new HelloCollision(); 7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta app.start(); 7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void simpleInitApp() { 7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** Set up Physics */ 7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta bulletAppState = new BulletAppState(); 7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta stateManager.attach(bulletAppState); 7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //bulletAppState.getPhysicsSpace().enableDebug(assetManager); 7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // We re-use the flyby camera for rotation, while positioning is handled by physics 8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f)); 8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta flyCam.setMoveSpeed(100); 8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta setUpKeys(); 8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta setUpLight(); 8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // We load the scene from the zip file and adjust its size. 8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta assetManager.registerLocator("town.zip", ZipLocator.class.getName()); 8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta sceneModel = assetManager.loadModel("main.scene"); 8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta sceneModel.setLocalScale(2f); 8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // We set up collision detection for the scene by creating a 9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // compound collision shape and a static RigidBodyControl with mass zero. 9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta CollisionShape sceneShape = 9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta CollisionShapeFactory.createMeshShape((Node) sceneModel); 9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta landscape = new RigidBodyControl(sceneShape, 0); 9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta sceneModel.addControl(landscape); 9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // We set up collision detection for the player by creating 9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // a capsule collision shape and a CharacterControl. 9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // The CharacterControl offers extra settings for 10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // size, stepheight, jumping, falling, and gravity. 10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // We also put the player in its starting position. 10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1); 10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta player = new CharacterControl(capsuleShape, 0.05f); 10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta player.setJumpSpeed(20); 10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta player.setFallSpeed(30); 10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta player.setGravity(30); 10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta player.setPhysicsLocation(new Vector3f(0, 10, 0)); 10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // We attach the scene and the player to the rootnode and the physics space, 11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // to make them appear in the game world. 11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.attachChild(sceneModel); 11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta bulletAppState.getPhysicsSpace().add(landscape); 11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta bulletAppState.getPhysicsSpace().add(player); 11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private void setUpLight() { 11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta // We add light so we see the scene 11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta AmbientLight al = new AmbientLight(); 11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta al.setColor(ColorRGBA.White.mult(1.3f)); 12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.addLight(al); 12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta DirectionalLight dl = new DirectionalLight(); 12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta dl.setColor(ColorRGBA.White); 12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal()); 12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.addLight(dl); 12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** We over-write some navigational key mappings here, so we can 12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * add physics-controlled walking and jumping: */ 13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private void setUpKeys() { 13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A)); 13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D)); 13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W)); 13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S)); 13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE)); 13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addListener(this, "Left"); 13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addListener(this, "Right"); 13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addListener(this, "Up"); 13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addListener(this, "Down"); 14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta inputManager.addListener(this, "Jump"); 14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** These are our custom actions triggered by key presses. 14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * We do not walk yet, we just keep track of the direction the user pressed. */ 14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void onAction(String binding, boolean value, float tpf) { 14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (binding.equals("Left")) { 14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (value) { left = true; } else { left = false; } 14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else if (binding.equals("Right")) { 14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (value) { right = true; } else { right = false; } 15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else if (binding.equals("Up")) { 15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (value) { up = true; } else { up = false; } 15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else if (binding.equals("Down")) { 15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (value) { down = true; } else { down = false; } 15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } else if (binding.equals("Jump")) { 15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta player.jump(); 15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This is the main event loop--walking happens here. 16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * We check in which direction the player is walking by interpreting 16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the camera direction forward (camDir) and to the side (camLeft). 16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * The setWalkDirection() command is what lets a physics-controlled player walk. 16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * We also make sure here that the camera moves with player. 16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void simpleUpdate(float tpf) { 16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Vector3f camDir = cam.getDirection().clone().multLocal(0.6f); 16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f); 17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta walkDirection.set(0, 0, 0); 17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (left) { walkDirection.addLocal(camLeft); } 17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (right) { walkDirection.addLocal(camLeft.negate()); } 17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (up) { walkDirection.addLocal(camDir); } 17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta if (down) { walkDirection.addLocal(camDir.negate()); } 17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta player.setWalkDirection(walkDirection); 17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta cam.setLocation(player.getPhysicsLocation()); 17759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 17859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta} 179