Thanks @BigBob it is working now. I Used the ChaseCamera instead of CameraNode.
The Next Thing I need to figure out is how to attach a weapon. I tried attaching to the CharacterNode but when I look up or down it stays in a fixed position and does not move with the camera. Since its a ChaseCamera I am not sure how to attached the weapon to the Camera.
This is what I did if anyone else needs it.
[java]
package mygame;
import com.jme3.app.DebugKeysAppState;
import com.jme3.app.SimpleApplication;
import com.jme3.app.StatsAppState;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.terrain.geomipmap.TerrainQuad;
public class TestTwo extends SimpleApplication implements ActionListener {
private BulletAppState bulletAppState;
private BetterCharacterControl physicsCharacter;
private TerrainQuad terrain;
private ChaseCamera chaseCam;
private Node characterNode;
boolean leftStrafe = false, rightStrafe = false;
private boolean left = false, right = false, up = false, down = false;
private Vector3f walkDirection = new Vector3f();
private Vector3f camDir = new Vector3f();
private Vector3f camLeft = new Vector3f();
public final float SPEED = 5.0f;
public TestTwo() {
//Skipping Fly Cam
super(new StatsAppState(), new DebugKeysAppState());
}
public static void main(String[] args) {
TestTwo testTwo = new TestTwo();
testTwo.start();
}
@Override
public void simpleInitApp() {
this.setDisplayFps(false);
this.setDisplayStatView(false);
this.setShowSettings(false);
this.inputManager.setCursorVisible(false);
// activate physics
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
initTerrian();
initPlayer();
initWeapon();
initLight();
initChaseCamera();
bulletAppState.setDebugEnabled(true);
setUpKeys();
}
private void initLight() {
AmbientLight ambient = new AmbientLight();
ambient.setColor(ColorRGBA.Blue);
rootNode.addLight(ambient);
DirectionalLight sun = new DirectionalLight();
sun.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalizeLocal());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
}
private void initTerrian() {
Spatial level = assetManager.loadModel("Scenes/LevelOne.j3o");
rootNode.attachChild(level);
terrain = (TerrainQuad) ((Node) rootNode.getChild("New Scene")).getChild("terrain-LevelOne");
terrain.addControl(new RigidBodyControl(0));
bulletAppState.getPhysicsSpace().add(terrain);
}
private void initPlayer() {
// Create a node for the character model
characterNode = new Node("character node");
characterNode.setLocalTranslation(new Vector3f(4, 5, 2));
// Add a character control to the node so we can add other things and
// control the model rotation
physicsCharacter = new BetterCharacterControl(0.3f, 2.0f, 8f);
characterNode.addControl(physicsCharacter);
bulletAppState.getPhysicsSpace().add(physicsCharacter);
// Load model, attach to character node
Node model = (Node) assetManager.loadModel("Models/Man.mesh.j3o");
model.setLocalScale(0.25f);
model.setLocalTranslation(new Vector3f(0, 1.1f, 0));
model.setName("Player");
characterNode.attachChild(model);
// Add character node to the rootNode
rootNode.attachChild(characterNode);
}
private void initWeapon(){
Node weapon = (Node) assetManager.loadModel("Models/Cube/Cube.mesh.j3o");
weapon.setLocalScale(.1f);
weapon.setLocalTranslation(new Vector3f(-3f, 0f, 5f));
characterNode.attachChild(weapon);
}
private void initChaseCamera() {
chaseCam = new ChaseCamera(cam, characterNode.getChild("Player"), inputManager);
chaseCam.setMinDistance(0.1f);
chaseCam.setMaxDistance(0.1f);
chaseCam.setInvertVerticalAxis(true);
chaseCam.setDragToRotate(false);
}
@Override
public void simpleUpdate(float tpf) {
camDir.set(cam.getDirection()).multLocal(SPEED, 0.0f, SPEED);
camLeft.set(cam.getLeft()).multLocal(SPEED);
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());
}
physicsCharacter.setWalkDirection(walkDirection.multLocal(1));
physicsCharacter.setViewDirection(camDir);
}
private void setUpKeys() {
inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("Click", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addListener(this, "Up");
inputManager.addListener(this, "Down");
inputManager.addListener(this, "Left");
inputManager.addListener(this, "Right");
inputManager.addListener(this, "Click");
inputManager.addListener(this, "Jump");
}
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("Down")) {
down = isPressed;
} else if (binding.equals("Up")) {
up = isPressed;
}else if (binding.equals("Jump")) {
physicsCharacter.jump();
}
}
}
[/java]