I was finally successful at removing the default controls so that I could customize my own controls. The thing that I need help with now is adding rotation back into my code so that when the right key is pressed, it rotates to the right (in a look around way) and I’m trying to do the same with the left key (in reverse).
I left comments in the sections that I’m trying to fix so that the things could be easier to find. Simply go to the commented sections. I just need help with those parts. Thanks!
P.S. Anybody can feel free to use my code below in case anybody is trying to disable the default controls in order to add in their own controls. I’m all for helping people out. Again, it’s only missing rotation. Thanks again!
package mygame;
import com.jme3.app.SimpleApplication;
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.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
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.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
public class Main extends SimpleApplication
implements ActionListener {
private Geometry Player;
private Spatial sceneModel;
private BulletAppState bulletAppState;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false, rotateRight = false,
rotateLeft = false;
private Vector3f camDir = new Vector3f();
private Vector3f camLeft = new Vector3f();
public static void main(String[] args) {
Main app = new Main();
app.start();
}
public void simpleInitApp() {
/** Set up Physics */
this.flyCam.setEnabled(false);
Player = new Geometry("Player", new Box(1,1,1));
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));
flyCam.setMoveSpeed(100);
setUpKeys();
setUpLight();
ChaseCamera chaseCam = new ChaseCamera(getCamera(), Player, getInputManager());
chaseCam.setSmoothMotion(true);
chaseCam.setRotationSensitivity(5f);
sceneModel = assetManager.loadModel("Scenes/HenryScene.j3o");
sceneModel.setLocalScale(2f);
CollisionShape sceneShape =
CollisionShapeFactory.createMeshShape(sceneModel);
landscape = new RigidBodyControl(sceneShape, 0);
sceneModel.addControl(landscape);
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));
rootNode.attachChild(sceneModel);
bulletAppState.getPhysicsSpace().add(landscape);
bulletAppState.getPhysicsSpace().add(player);
}
private void setUpLight() {
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);
}
private void setUpKeys() {
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addListener(this, "Left");
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addListener(this, "Right");
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addListener(this, "Up");
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
inputManager.addListener(this, "Down");
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, "Jump");
//-------------------------------------------------------------------------
//This is the part that I'm trying to fix. I want to add rotation back into my code
inputManager.addMapping("RotateRight", new KeyTrigger(KeyInput.KEY_RIGHT));
inputManager.addListener(this, "RotateRight");
inputManager.addMapping("RotateLeft", new KeyTrigger(KeyInput.KEY_LEFT));
inputManager.addListener(this, "RotateLeft");
//-------------------------------------------------------------------------
}
public void onAction(String binding, boolean isPressed, float tpf) {
if (binding.equals("Left")) {
left = isPressed;
} else if (binding.equals("Right")) {
right= isPressed;
} else if (binding.equals("Up")) {
up = isPressed;
} else if (binding.equals("Down")) {
down = isPressed;
//------------------------------------------------------
//Here is the second part of my rotation code that I wanted to fix
} else if (binding.equals("RotateRight")) {
rotateRight = isPressed;
} else if (binding.equals("RotateLeft")) {
rotateRight = isPressed;
//--------------------------------------------------------
} else if (binding.equals("Jump")) {
if (isPressed) { player.jump(); }
}
}
@Override
public void simpleUpdate(float tpf) {
camDir.set(cam.getDirection()).multLocal(0.6f);
camLeft.set(cam.getLeft()).multLocal(0.4f);
walkDirection.set(0, 0, 0);
if (left) {
walkDirection.addLocal(camLeft);
}
if (right) {
walkDirection.addLocal(camLeft.negate());
}
if (up) {
walkDirection.addLocal(camDir);
}
if (down) {
walkDirection.addLocal(camDir.negate());
}
player.setWalkDirection(walkDirection);
cam.setLocation(player.getPhysicsLocation());
}
private AnalogListener analogListener = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
//------------------------------------------------------
//What do I add to put my rotation back into my code?
if (rotateRight) {
}
if (rotateLeft){
}
// That's all for now. I just need help with adding rotation back into my code
//After I set the flycame to false so that I could customize my controls.
//------------------------------------------------------
}
};
}