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.
[java]
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 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();
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) {
moveDir.set(camDir);
baseMoveX = 1f;
baseMoveZ = 1f;
}
}
direction.set(moveDir.addLocal(moveLeft));
player.setWalkDirection(direction);
cam.setLocation(player.getPhysicsLocation());
}
}
[/java]