This is a third person game, i am trying to implement horizontal movement of the character with the mouse. i am able to capture the input and rotate the camera but i dont know how to turn the character. When mouse moves right, the character should turn right
` @Override
public void simpleInitApp() {
mouseInput.setCursorVisible(false);
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//bulletAppState.setDebugEnabled(true);
Node sceneNode = (Node) assetManager.loadModel("Scenes/scene.j3o");
rootNode.attachChild(sceneNode);
RigidBodyControl scenePhy = new RigidBodyControl(0f);
sceneNode.addControl(scenePhy);
bulletAppState.getPhysicsSpace().add(sceneNode);
playerNode = new Node("player");
playerNode.setLocalTranslation(new Vector3f(0, 0, 0));
rootNode.attachChild(playerNode);
playerControl1 = new BetterCharacterControl(.9f, 3, 30f);
playerControl1.setJumpForce(new Vector3f(0, 150, 0));
playerControl1.setGravity(new Vector3f(0, -10, 0));
playerNode.addControl(playerControl1);
Spatial modelp = assetManager.loadModel("Models/Oto.mesh.xml");
modelp.scale(0.25f);
modelp.setLocalTranslation(0, 1.1f, 0);
playerNode.attachChild(modelp);
bulletAppState.getPhysicsSpace().add(playerControl1);
camNode = new CameraNode("CamNode", cam);
camNode.setControlDir(ControlDirection.SpatialToCamera);
camNode.setLocalTranslation(new Vector3f(0, 5, -8));
Quaternion quat = new Quaternion();
quat.lookAt(Vector3f.UNIT_Z, Vector3f.UNIT_Y);
camNode.setLocalRotation(quat);
camNode.rotate(0.4f, 0f, 0);
playerNode.attachChild(camNode);
camNode.setEnabled(true);
//disable the default 1st-person flyCam (don't forget this!!)
flyCam.setEnabled(false);
}
@Override
public void simpleUpdate(float tpf) {
// Get current forward and left vectors of the playerNode:
Vector3f modelForwardDir =
playerNode.getWorldRotation().mult(Vector3f.UNIT_Z);
Vector3f modelLeftDir =
playerNode.getWorldRotation().mult(Vector3f.UNIT_X);
// Determine the change in direction
walkDirection.set(0, 0, 0);
if (forward) {
walkDirection.addLocal(modelForwardDir.mult(speed));
} else if (backward) {
walkDirection.addLocal(modelForwardDir.mult(speed).
negate());
}
playerControl1.setWalkDirection(walkDirection); // walk!
// Determine the change in rotation
if (rotateLeft) {
Quaternion rotateL = new Quaternion().
fromAngleAxis(FastMath.PI * tpf, Vector3f.UNIT_Y);
rotateL.multLocal(viewDirection);
} else if (rotateRight) {
Quaternion rotateR = new Quaternion().
fromAngleAxis(-FastMath.PI * tpf, Vector3f.UNIT_Y);
rotateR.multLocal(viewDirection);
}
playerControl1.setViewDirection(viewDirection); // turn!
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
private void setUpKeys() {
inputManager.addMapping("Forward",
new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Back",
new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Rotate Left",
new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Rotate Right",
new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Jump",
new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, "Rotate Left",
"Rotate Right");
inputManager.addListener(this, "Forward", "Back", "Jump");
inputManager.addMapping("MouseMoved",
new MouseAxisTrigger(MouseInput.AXIS_X, false),
new MouseAxisTrigger(MouseInput.AXIS_X, true));
inputManager.addListener(new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
inputManager.getCursorPosition();
centredX = inputManager.getCursorPosition().x - 0.5f * settings.getWidth();
float centredY = inputManager.getCursorPosition().y - 0.5f * settings.getHeight();
Quaternion quat = new Quaternion();
quat.fromAngles(-(0.002f * centredY), -(0.002f * centredX), 0);
camNode.setLocalRotation(quat);
}
}, "MouseMoved");
}
public void onAction(String binding, boolean isPressed, float tpf) {
if (binding.equals("Rotate Left")) {
rotateLeft = isPressed;
} else if (binding.equals("Rotate Right")) {
rotateRight = isPressed;
} else if (binding.equals("Forward")) {
forward = isPressed;
} else if (binding.equals("Back")) {
backward = isPressed;
} else if (binding.equals("Jump")) {
playerControl1.jump();
}
}