# Spatial move regarding the camera direction

First: I hope you understand my english, if not, let me know

Well, i have a spatial and a ChaseCamera.

The ChaseCamera follows the spatial.

I move the spatial with w,a,s,d

The spatials (and so the cameras) rotation is controlled by the mouse.

The code snippet:

[java]private AnalogListener analogListener = new AnalogListener() {

public void onAnalog(String name, float value, float tpf) {

if (name.equals(â€śMOVE_FORWARDâ€ť)) {

Vector3f v = playerMaterial.getLocalTranslation();

playerMaterial.setLocalTranslation(v.x, v.y, v.z + value * speed);

}

if (name.equals(â€śMOVE_BACKWARDâ€ť)) {

Vector3f v = playerMaterial.getLocalTranslation();

playerMaterial.setLocalTranslation(v.x, v.y, v.z - value * speed);

}

if (name.equals(â€śMOVE_RIGHTâ€ť)) {

Vector3f v = playerMaterial.getLocalTranslation();

playerMaterial.setLocalTranslation(v.x - value * speed, v.y, v.z);

}

if (name.equals(â€śMOVE_LEFTâ€ť)) {

Vector3f v = playerMaterial.getLocalTranslation();

playerMaterial.setLocalTranslation(v.x + value * speed, v.y, v.z);

}

}

};

private AnalogListener analogListener2 = new AnalogListener() {

public void onAnalog(String name, float value, float tpf) {

if (name.equals(â€śRotateRightâ€ť)) {

playerMaterial.rotate(0, 0.1f, 0);

chaseCam.setDefaultHorizontalRotation(chaseCam.getHorizontalRotation() - .1f);

}

if (name.equals(â€śRotateLeftâ€ť)) {

playerMaterial.rotate(0, -0.1f, 0);

chaseCam.setDefaultHorizontalRotation(chaseCam.getHorizontalRotation() + .1f);

}

}

};[/java]

That works pretty good.

The problem is: Normally in games the player presses W and the figur moves forward, whatever the camera rotation is.

In this example, when the camera rotated, from the users point of view the figur doesnâ€™t run forward any moreâ€¦

How do i calculate the direction the figur has to run forward, regarding the camera position?

Any ideas?

[java]

playermove(Vector3f movement)

{

node.setLocalTranslation( node.localToWorld(movement,movement));

}

public void onAnalog(String name, float value, float tpf)

{

float amount = 5 * value ;

if (name.equals("StrafeLeft"))

{

Vector3f movement = new Vector3f(amount,0,0);

playermove( movement );

}

else if (name.equals("StrafeRight"))

{

Vector3f movement = new Vector3f(-amount,0,0);

playermove( movement );

}

else if (name.equals("Forward"))

{

Vector3f movement = new Vector3f(0,0,amount);

playermove( movement );

}

else if (name.equals("Backward"))

{

Vector3f movement = new Vector3f(0,0,-amount);

playermove( movement );

}

}

[/java]

1 Like

Problem solved,

THANKS A LOT!

How would i do this if my model was continously spinning, like the TARDIS from doctor who, itâ€™s continously spinning but i want it to fly in the direction that the camera facing and if i do this:

[java]private Node Player;