Rotating the Camera up an down around the Origin

Started by Knuffi, April 08, 2012, 12:42:34 PM

Previous topic - Next topic

Knuffi

I have Objects siting at the origin and want to rotate the camera around that point.

Rotating left and right is no problem, as it is always a rotation around the y-axis. But Rotating up and down is quite difficult, as it is no rotation around a fixed axis. I looked into the methods of the camera, the SimpleVector and the Matrixc, but did not really find something to work with.

Can anyone give me a tip?

Knuffi

#1
Ok, I'm blind - just found the RotateCameraAxis() Method. :)

Edit: Nope, that's not it. The camera rotates around itself instead around the origin.  :(

Knuffi

Ok, got it. I'm sure, there is a easier way ;)
I used a rotation matrix that rotates the camera position around a normalized cross-vector of the camera position and the y-axis. That stops working when I'm directly above the origin, but that does not matter to me.  :D

public static void rotateupdown(float v){
SimpleVector cam = world.getCamera().getPosition();
SimpleVector y = new SimpleVector(0,1,0);
SimpleVector rot = cam.calcCross(y);
rot = rot.normalize();
float n1 = rot.x;
float n2 = rot.y;
float n3 = rot.z;
float r = v*(float)Math.PI/30;
float c =(float) Math.cos(r);
float s =(float) Math.sin(r);
Matrix m = new Matrix();
m.set(0, 0, n1*n1*(1-c) + c);
m.set(0,1, n1*n2*(1-c) - n3*s);
m.set(0,2, n1*n3*(1-c) + n2*s);
m.set(1,0, n1*n2*(1-c) + n3*s);
m.set(1,1, n2*n2*(1-c) + c);
m.set(1,2, n2*n3*(1-c)- n1*s);
m.set(2,0, n1*n3*(1-c)- n2*s);
m.set(2,1, n2*n3*(1-c) + n1*s);
m.set(2,2, n3*n3*(1-c) + c);

cam.rotate(m);

world.getCamera().setPosition(cam);
world.getCamera().lookAt(new SimpleVector(0,0,0));

buffer.clear(java.awt.Color.DARK_GRAY);
world.renderScene(buffer);
world.drawWireframe(buffer, Color.WHITE);
buffer.update();
}

EgonOlsen

Actually rotateAxis(...) is the way to go. If you want to rotate the camera around some object at the origin, move the camera to the origin, do the rotation around the axis and move it back...like so...


cam.moveCamera(Camera.CAMERA_MOVEIN, distance);
cam.rotateAxis(axis, angle);
cam.moveCamera(Camera.CAMERA_MOVEOUT, distance);


But if you got it working in another way...

Knuffi

Ok, that is much shorter, and the axis could be set as in my source by a cross product.