# [SOLVED] Camera rotation problem. Need math help please

The setup:

I have a cockpit mesh in a space scene. A camera node at local translation 0,0,0. The user needs to be able to look left, right, up and down in the cockpit with the mouse while he is flying the ship with the keyboard. Ship flight controls work fine. I even have a roll on Z axis control for the ship. Ship moves, camera node follows ship no problem.

The problem:

Using the mouse to rotate the camera on the Y axis works fine. As soon as I rotate the camera on the X axis (look up) and then rotate the camera on the Y axis, it rotates relative to the camera nodeâ€™s Y axis therefore twisting the Z axis relative to the ship.

The Code:
if (name.equals(â€śmouseLeftâ€ť)) {

``````        Quaternion q = new Quaternion();

cameraNode.setLocalRotation(cameraNode.getLocalRotation().mult(q.fromAngleAxis(value, Vector3f.UNIT_Y)));

} else if (name.equals("mouseRight")) {

Quaternion q = new Quaternion();
cameraNode.setLocalRotation(cameraNode.getLocalRotation().mult(q.fromAngleAxis(value * -1f, Vector3f.UNIT_Y)));

} else if (name.equals("Up")) {
Quaternion q = new Quaternion();
cameraNode.setLocalRotation(cameraNode.getLocalRotation().mult(q.fromAngleAxis(tpf , Vector3f.UNIT_X)));
} else if (name.equals("Down")) {
Quaternion q = new Quaternion();
cameraNode.setLocalRotation(cameraNode.getLocalRotation().mult(q.fromAngleAxis(tpf * -1 , Vector3f.UNIT_X)));
}
``````

The screen shots:

Sequence is, rotate right, rotate up, rotate left.

height=â€ś389â€ť>

I basically need to rotate the camera node on the COCKPITâ€™S Y axis instead of the camera nodeâ€™s Y axis but I canâ€™t get my head around the math for it.

You probably donâ€™t want to use angle axis.

You probably want to keep yaw and pitch separately and recreate the quaternion using fromAngles() whenever they change.

Either way, you will need to track the yaw and pitch rotations separately and apply them in a specific orderâ€¦ thatâ€™s the only way youâ€™ll get â€śquake cameraâ€ť style rotation ordering.

pspeed Once again you saved the day. I never thought of that trick and it works perfectly. Here is the new code:

private float yaw;
private float pitch;
private float rotationalDegreesPerSecond = 90f;

``````@Override
public void onAnalog(String name, float value, float tpf) {
if (inputManager.isCursorVisible() == true) {
return;
}

if (name.equals("mouseLeft")) {

yaw = yaw + (FastMath.DEG_TO_RAD * rotationalDegreesPerSecond * tpf);

} else if (name.equals("mouseRight")) {
yaw = yaw - (FastMath.DEG_TO_RAD * rotationalDegreesPerSecond * tpf);

} else if (name.equals("Up")) {
pitch = pitch + (FastMath.DEG_TO_RAD * rotationalDegreesPerSecond * tpf);
} else if (name.equals("Down")) {
pitch = pitch - (FastMath.DEG_TO_RAD * rotationalDegreesPerSecond * tpf);
} else if (name.equals("ZoomIn")) {

} else if (name.equals("ZoomOut")) {

}

Quaternion q = new Quaternion();
q.fromAngles(pitch, yaw, 0f);
cameraNode.setLocalRotation(q);
}``````

Nice cockpit btw

Thank you. Itâ€™s one of 7 different ones in the game â€¦ and now thanks to pspeed you can actually fly it

1 Like