Hello all, I’ve been trying to get the cam from simpleApplication to follow my PlayerNode which contains a model of a gun. But for some reason I can’t get the cam to move to the location of the playernode. I know I’m missing something, but I just can’t figure out what exactly I’m missing.
public class GameManager extends AbstractAppState {
private SimpleApplication simApp;
private Node rootNode;
private AssetManager assetManager;
private AppStateManager stateManager;
private InputManager inputManager;
private ViewPort viewPort;
private BulletAppState physics;
private Spatial Scene;
private RigidBodyControl sceneColl;
private BetterCharacterControl playerControl;
private Spatial playerSpatial;
private Node playerNode;
//Player Walking directions
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
//Temporary vectors used in each frame, initialized so we don't have to init each frame
private Vector3f camDir = new Vector3f();
private Vector3f camLeft = new Vector3f();
//Action Listener
private ActionListener actionListener = new ActionListener() {
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;
} else if (binding.equals("Jump")) {
if (isPressed) { playerControl.jump(); }
}
}
};
@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
//TODO: initialize your AppState, e.g. attach spatials to rootNode
//this is called on the OpenGL thread after the AppState has been attached
this.simApp = (SimpleApplication)app;
this.rootNode = this.simApp.getRootNode();
this.assetManager = this.simApp.getAssetManager();
this.stateManager = this.simApp.getStateManager();
this.inputManager = this.simApp.getInputManager();
this.viewPort = this.simApp.getViewPort();
this.physics = this.stateManager.getState(BulletAppState.class);
//this.simApp.getFlyByCamera().setEnabled(false);
//Init Scene and load the collision model
this.Scene = this.assetManager.loadModel("Scenes/Sandbox.j3o");
Node sceneNode = (Node) this.Scene;
CollisionShape SceneCollShape = CollisionShapeFactory.createMeshShape(sceneNode.getChild("Textures/Scene/Scene.blend"));
this.sceneColl = new RigidBodyControl(SceneCollShape, 0);
this.Scene.addControl(sceneColl);
this.rootNode.attachChild(Scene);
//Set up the keys
this.setUpKeys();
//Set up the player collision
this.playerSpatial = this.assetManager.loadModel("Models/Weapons/M81/M81.j3o");
this.playerNode = new Node();
playerNode.attachChild(playerSpatial);
playerSpatial.move(0,4.5f,0);
this.playerControl = new BetterCharacterControl(1.5f, 6f, 1f);
playerNode.addControl(this.playerControl);
//Add basic physical properties
this.playerControl.setJumpForce(new Vector3f(0, 5f, 0));
this.playerControl.setGravity(new Vector3f(0, 1f, 0));
this.playerControl.warp(new Vector3f(5, 10, 5));
this.rootNode.attachChild(playerNode);
this.physics.getPhysicsSpace().add(this.playerControl);
this.physics.getPhysicsSpace().addAll(playerSpatial);
this.physics.getPhysicsSpace().addAll(Scene);
}
private void setUpKeys(){
this.inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
this.inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
this.inputManager.addMapping("Up", new KeyTrigger((KeyInput.KEY_W)));
this.inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));
this.inputManager.addListener(this.actionListener,"Left");
this.inputManager.addListener(this.actionListener,"Right");
this.inputManager.addListener(this.actionListener,"Up");
this.inputManager.addListener(this.actionListener, "Down");
}
@Override
public void update(float tpf) {
this.camDir.set(this.simApp.getCamera().getDirection()).multLocal(0.6f);
this.camLeft.set(this.simApp.getCamera().getLeft()).multLocal(0.4f);
this.walkDirection.set(0, 0, 0);
if(left){
this.walkDirection.addLocal(camLeft);
}
if(right){
this.walkDirection.addLocal(camLeft.negate());
}
if(up){
this.walkDirection.addLocal(camDir);
}
if(down){
this.walkDirection.addLocal(camDir.negate());
}
this.playerControl.setWalkDirection(walkDirection);
this.simApp.getCamera().setLocation(this.playerNode.getLocalTranslation());
}
@Override
public void cleanup() {
super.cleanup();
//TODO: clean up what you initialized in the initialize method,
//e.g. remove all spatials from rootNode
//this is called on the OpenGL thread after the AppState has been detached
}
}