[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.

<img src="/uploads/default/original/2X/6/6f8c2757898bf47d777e99df6a08590ca6991e04.png" width=“690”

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 :smile:

1 Like