Spatial move regarding the camera direction

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

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;

Player = assetManager.loadModel(“Models/tardis.j3o”);

or

Player = (Node) assetManager.loadModel(“Models/tardis.j3o”); [/java]

it throws an error. If i do it with a spatial, whenever i press to move, the tardis flies around in a circle.