How to set collision control for the player who is being viewed in third person view??
I tried the below code. But the player is not moving
public class Main extends SimpleApplication {
public static void main(String[] args) {
Main app = new Main();
app.start();
}
private AnimControl control;
private AnimChannel channel;
CameraNode camNode;
private RigidBodyControl landscape;
private BulletAppState bulletAppState;
ChaseCamera chaseCam;
private CharacterControl character;
Node player;
Spatial scene;
Node city;
@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(20);
flyCam.setEnabled(true);
bulletAppState = new BulletAppState();
bulletAppState.setThreadingType(BulletAppState.ThreadingType.PARALLEL);
stateManager.attach(bulletAppState);
guiNode.detachAllChildren();
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText helloText = new BitmapText(guiFont, false);
helloText.setSize(guiFont.getCharSet().getRenderedSize());
helloText.setText("Hello World");
helloText.setLocalTranslation(300, helloText.getLineHeight(), 0);
guiNode.attachChild(helloText);
city =(Node) assetManager.loadModel("Models/city.j3o");
city.setLocalScale(500, 600, 700);
city.addControl(new RigidBodyControl(0));
bulletAppState.getPhysicsSpace().addAll(city);
initLight();
CapsuleCollisionShape capsule = new CapsuleCollisionShape(0.2f, 6, 1);
character = new CharacterControl(capsule, 0.05f);
player = (Node) assetManager.loadModel("Models/tex/Cube.mesh.xml");
player.setMaterial( assetManager.loadMaterial("Textures/newMaterial.j3m"));
player.setLocalScale(0.5f);
player.setLocalTranslation(-50f, 4, 0);
player.addControl(character);
bulletAppState.getPhysicsSpace().add(character);
// camNode.lookAt(player.getLocalTranslation(), Vector3f.UNIT_Y)*/
camNode = new CameraNode("CamNode", cam);
camNode.setControlDir(ControlDirection.SpatialToCamera);
player.attachChild(camNode);
camNode.setLocalTranslation(new Vector3f(0, 20, -50));
camNode.lookAt(player.getLocalTranslation(), Vector3f.UNIT_Y);
control = player.getControl(AnimControl.class);
channel = control.createChannel();
channel.setAnim("stand");
initLight();
initLight();
initKeys();
rootNode.attachChild(player);
rootNode.attachChild(city);
// rootNode.attachChild(scene);
}
public void initLight(){
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.White.mult(2.0f));
rootNode.addLight(al);
DirectionalLight dl = new DirectionalLight();
dl.setColor(ColorRGBA.White);
dl.setDirection(new Vector3f(-5f, -5f, -5f).normalizeLocal());
rootNode.addLight(dl);
}
public void initKeys(){
inputManager.addMapping("Move_fwd", new KeyTrigger(keyInput.KEY_T));
inputManager.addMapping("Move_bwd", new KeyTrigger(keyInput.KEY_G));
inputManager.addMapping("Move_left", new KeyTrigger(keyInput.KEY_F));
inputManager.addMapping("Move_right", new KeyTrigger(keyInput.KEY_H));
inputManager.addListener(analog, "Move_fwd", "Move_bwd", "Move_left", "Move_right");
inputManager.addListener(action, "Move_fwd","Move_bwd", "Move_left", "Move_right");
}
private ActionListener action = new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if(name.equals("Move_fwd")){
channel.setAnim("walk");
// channel.setLoopMode(LoopMode.DontLoop);
}
if(name.equals("Move_bwd")){
channel.setAnim("walk");
//channel.setLoopMode(LoopMode.DontLoop);
}
if(name.equals("Move_left")){
channel.setAnim("walk");
//channel.setLoopMode(LoopMode.DontLoop);
}
if(name.equals("Move_right")){
channel.setAnim("walk");
// channel.setLoopMode(LoopMode.DontLoop);
}
}
};
private AnalogListener analog = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if(name.equals("Move_fwd")){
Vector3f v1 = player.getLocalTranslation();
player.setLocalTranslation(v1.x, v1.y, v1.z-value*5);
}
if(name.equals("Move_bwd")){
Vector3f v1 = player.getLocalTranslation();
player.setLocalTranslation(v1.x, v1.y, v1.z+value*5);
}
if(name.equals("Move_left")){
Vector3f v1 = player.getLocalTranslation();
player.setLocalTranslation(v1.x-value*5, v1.y, v1.z);
}
if(name.equals("Move_right")){
Vector3f v1 = player.getLocalTranslation();
player.setLocalTranslation(v1.x+value*5, v1.y, v1.z);
}
}
};
@Override
public void simpleUpdate(float tpf) {
//TODO: add update code
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}