post #1 of 1
Thread Starter 
I'm working on a 3-D game in java for my AP CompSci class and i''ve run into a little problem.
I'm trying to add an auto run and sprint option but i'm not exactly sure how to make it so that while the key is being pressed auto-run and when its not, stop.
I've tried putting a while keyPressed but that just seems to crash the game :/

If you guys have any ideas, ill post the SC but it will be impossible to test unless you have jMonkeyEngine 3.

Code:
package src;

import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.ZipLocator;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;

/**
 * @author ...
 */
public class HelloNewCollision extends SimpleApplication {
    
    private Spatial sceneModel;
    private BulletAppState bulletAppState;
    private RigidBodyControl landscape;
    private CharacterControl player;
    private Vector3f direction = new Vector3f();
    
    private boolean[] move = new boolean[6];
    private boolean autoRun = false, sprint = false;
    
    private float baseMoveX = .2f;
    private float baseMoveZ = .2f;
    
    private float addMoveX = .001f;
    private float addMoveZ = .001f;
    
    private float maxMoveX = .5f;
    private float maxMoveZ = .5f;
    
    private float speedX = baseMoveX;
    private float speedZ = baseMoveZ;
    
public static void main(String[] args) {
        HelloNewCollision app = new HelloNewCollision();
        app.start();
}

public void simpleInitApp() {
    /** Sets up Physics. */
    bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    
    /** Re-Uses the flyby camera control for rotation, while positioning is handled by physics. */
    viewPort.setBackgroundColor(new ColorRGBA(0.7f,0.8f,1f,1f));
    flyCam.setMoveSpeed(50);
        
    /** Loads the scene from the zip file and adjust its size. */
    assetManager.registerLocator("town.zip", ZipLocator.class.getName());
    sceneModel = assetManager.loadModel("main.scene");
    sceneModel.setLocalScale(2f);
    
    /** Sets up collision detection for the scene by creating a
     *  compound collision shape and a static physics node with mass zero.
     */
    CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) sceneModel);
    landscape = new RigidBodyControl(sceneShape, 0);
    sceneModel.addControl(landscape);
    
    /** Sets up collision detection for the player by creatin
     *  a capsule collision shape and a physics character node.
     *  The physics character node offers extra settings for
     *  size, stepheight, jumping, falling, and gravity.
     *  Also puts the player in its starting position.
     */
    CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
    player = new CharacterControl(capsuleShape, 0.05f);
    
    player.setJumpSpeed(20);
    player.setFallSpeed(30);
    player.setGravity(30);
    player.setPhysicsLocation(new Vector3f(0, 10, 0));
    
    /** Attaches the scene and the player to the rootnode and the physics space,
     *  to make them appear in the game world.
     */
    rootNode.attachChild(sceneModel);
    bulletAppState.getPhysicsSpace().add(landscape);
    bulletAppState.getPhysicsSpace().add(player);
    
    keyBindings();
    setUpLight();
}

private void setUpLight() {
    /** Adds light so we see the scene. */
    AmbientLight al = new AmbientLight();
    al.setColor(ColorRGBA.White.mult(1.3f));
    rootNode.addLight(al);
    DirectionalLight dl = new DirectionalLight();
    dl.setColor(ColorRGBA.White);
    dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());
    rootNode.addLight(dl);
}

/** Over-Writes some navigational key mappings here, for 
 *  physics-controlled walking and jumping: 
 */
private void keyBindings()  {
    inputManager.addMapping("forward",     new KeyTrigger(KeyInput.KEY_W));
    inputManager.addMapping("backward",    new KeyTrigger(KeyInput.KEY_S));
    inputManager.addMapping("strafeLeft",  new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping("strafeRight", new KeyTrigger(KeyInput.KEY_D));
    
    inputManager.addMapping("jump", new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addMapping("autoRun", new KeyTrigger(KeyInput.KEY_R));    
    inputManager.addMapping("sprint", new KeyTrigger(KeyInput.KEY_LSHIFT));
       
    inputManager.addListener(actionListener, new String[] 
        { "forward", "backward", "strafeLeft", "strafeRight", "jump", "sprint", "autoRun" });
}

/** These are our custom actions triggered by key presses.
 *  We do not walk yet, we just keep track of the direction the user pressed. 
 */
private ActionListener actionListener = new ActionListener()    {
    public void onAction(String name, boolean keyPressed, float tpf)    {
        if (name.equals("forward")) {
            move[0] = keyPressed;
        }
        else if (name.equals("backward"))   {
            move[1] = keyPressed;
        }
        if (name.equals("strafeLeft"))  {
            move[2] = keyPressed;
        }
        else if (name.equals("strafeRight"))    {
            move[3] = keyPressed;
        }
        else if (name.equals("jump") && keyPressed) {
            player.jump();
        }
        if (name.equals("autoRun") && keyPressed)   {
            move[4] = keyPressed;
            autoRun = true; 
        }
        if (name.equals("sprint") && keyPressed) {
            move[5] = keyPressed;
            sprint = true;
        }
    }
};
  
/**
 * This is the main event loop--walking happens here.
 * Checks in which direction the player is walking by interpreting
 * the camera direction forward (camDir) and to the side (camLeft).
 * The setWalkDirection() command is what lets a physics-controlled player walk.
 * Also makes sure here that the camera moves with player.
 */
    @Override
public void simpleUpdate(float tpf) {
    Vector3f moveDir = new Vector3f(0, 0, 0);
    Vector3f moveLeft = new Vector3f(0, 0, 0);
    Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
    Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
    direction.set(0, 0, 0);
        if (move[0])    {
            moveDir.set(camDir);
        if (speedZ < maxMoveZ)
            speedZ += addMoveZ;
        }
        else if (move[1])   {
            camDir.negateLocal();
            moveDir.set(camDir);
        if (speedZ < maxMoveZ)
            speedZ += addMoveZ;
        }
        else    {
            speedZ = baseMoveZ;
        }
        if (move[2])    {
            moveLeft.set(camLeft);
        if (speedX < maxMoveX)
            speedX += addMoveX;
        }
        else if (move[3])   {
            camLeft.negateLocal();
            moveLeft.set(camLeft);
        if (speedX < maxMoveX)
            speedX += addMoveX;
        }
        else    {
            speedX = baseMoveZ;
        }
        if (move[4]) {
            if (autoRun = true) {
                moveDir.set(camDir);     
                    addMoveX = 1f;
                    addMoveZ = 1f;   
            }
        }
        if (move[5]) {
            if (sprint = true) {
                    speedX += 2f;
                    speedZ += 2f;  
            }
        }
        direction.set(moveDir.addLocal(moveLeft));
        player.setWalkDirection(direction);
        cam.setLocation(player.getPhysicsLocation());
}
}
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply
Supra
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7-2600K 4.0GHz @ 1.160V MSI P67A-GD65 (B3) XFX Radeon HD 6950 2GB Unlocked @ 950/1450 XFX Radeon HD 6950 2GB Unlocked @ 950/1450 
RAMHard DriveHard DriveHard Drive
Mushkin Enhanced Silverline 8GB (2 x 4GB) Corsair Nova SSD 32GB 1TB Seagate 6.0GB/s SATA 1TB Seagate 6.0GB/s SATA 
Optical DriveCoolingOSMonitor
Lite-On DVD Burner Stock Windows 7 Ultimate x64 Acer S231HL LED Monitor 
MonitorMonitorKeyboardPower
Acer S231HL LED Monitor Acer A23IH LCD Monitor eMachines Generic Antec 750W Modular 
CaseMouseMouse Pad
Antec 1200 Gigabyte Ghost 6000 dpi Desk 
  hide details  
Reply