How to rotate the camera like a blender NUMPAD 2-4-6-8

The code I’m trying to use:

[java]

void rotateCamera(float tpf, Vector3f axis){

float rotationSpeed = 2;



Matrix3f mat = new Matrix3f();

mat.fromAngleNormalAxis(rotationSpeed * tpf, axis);



Vector3f upDir = cam.getUp();

Vector3f leftDir = cam.getLeft();

Vector3f dir = cam.getDirection();



mat.mult(upDir, upDir);

mat.mult(leftDir, leftDir);

mat.mult(dir, dir);



Quaternion q = new Quaternion();

q.fromAxes(leftDir, upDir, dir);

q.normalizeLocal();



cam.setAxes(q);



dir = cam.getDirection();

dir = dir.mult(20.0f);

Vector3f invDir = new Vector3f(-dir.x, -dir.y, -dir.z);

Vector3f position = invDir.add(getFocus());

cam.setLocation(position);

}[/java]



should rotate the camera, but the result is not what I expected

stupid math :frowning:

Check wikipedia on “Gimbal Lock” and also maybe check this: http://hub.jmonkeyengine.org/groups/graphics/forum/topic/trackball-samples/

It is necessary that the object is rotated relative to the camera rather than the coordinate axes (like in blender: object or camera rotation) :frowning:



But thanks anyway

The same wisdom applies. Use direction vectors instead of messing with rotations and your life will be much easier.

How this can be done in your example?

http://hub.jmonkeyengine.org/groups/graphics/forum/topic/trackball-samples/