Adding Sprint, and auto-run toggle?

Hi, I’m new here and new to jMonkeyEngine. I saw it and wanted to try it out and make a small game for a school project. I’m using the HelloCollision and a bit of SC that I found on this site for the walking and scenery.

However, I have been having some trouble when trying to add “Sprinting” or “Auto-Run”.

The auto run works, but I’m not sure how to toggle it on or off, and as for sprinting, I don’t really know how to increase the movement speed.

If you can help me, I would greatly appreciate it.


package src;


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 walkDirection = new Vector3f();

    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();



    public void simpleInitApp() {

    * Sets up Physics. /

    bulletAppState = new 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));


    * Loads the scene from the zip file and adjust its size. /

    assetManager.registerLocator("", ZipLocator.class.getName());

    sceneModel = assetManager.loadModel("main.scene");


    * 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);


    * 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.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.








    private void setUpLight() {

    * Adds light so we see the scene. /

    AmbientLight al = new AmbientLight();



    DirectionalLight dl = new DirectionalLight();


    dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());



    * 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) {



    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.



    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]) {


    if (speedZ < maxMoveZ)

    speedZ += addMoveZ;


    else if (move[1]) {



    if (speedZ < maxMoveZ)

    speedZ += addMoveZ;


    else {

    speedZ = baseMoveZ;


    if (move[2]) {


    if (speedX < maxMoveX)

    speedX += addMoveX;


    else if (move[3]) {



    if (speedX < maxMoveX)

    speedX += addMoveX;


    else {

    speedX = baseMoveZ;


    if (move[4]) {

    if (autoRun = true) {


    addMoveX = 1f;

    addMoveZ = 1f;



    if (move[5]) {

    if (sprint = true) {


    baseMoveX = 1f;

    baseMoveZ = 1f;









First I see some assignments in the if-functions. That is often not what you might wanna do. I am talking about if (autoRun = true) and if (sprint = true). That should be t if (autoRun == true) and if (sprint == true).

To toggle the autorun on/off you can do something like this: autoRun = !autoRun;

It is really hard for me to see for me what the move array does in the simpleUpdate function. One solution is to use moveDir.mult(extraspeed); whenever you wanna sprint. Maybe player have some kind of setMovementSpeed function or something. It seems that you use a old version so I have no idea really.