Author Topic: Rotating the Camera up an down around the Origin  (Read 3088 times)

Offline Knuffi

  • byte
  • *
  • Posts: 19
    • View Profile
Rotating the Camera up an down around the Origin
« on: April 08, 2012, 12:42:34 pm »
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?

Offline Knuffi

  • byte
  • *
  • Posts: 19
    • View Profile
Re: Rotating the Camera up an down around the Origin
« Reply #1 on: April 08, 2012, 06:36:28 pm »
Ok, I'm blind - just found the RotateCameraAxis() Method. :)

Edit: Nope, that's not it. The camera rotates around itself instead around the origin.  :(
« Last Edit: April 08, 2012, 06:47:32 pm by Knuffi »

Offline Knuffi

  • byte
  • *
  • Posts: 19
    • View Profile
Re: Rotating the Camera up an down around the Origin
« Reply #2 on: April 08, 2012, 07:25:41 pm »
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

Code: [Select]
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();
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Rotating the Camera up an down around the Origin
« Reply #3 on: April 08, 2012, 09:09:39 pm »
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...

Code: [Select]
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...

Offline Knuffi

  • byte
  • *
  • Posts: 19
    • View Profile
Re: Rotating the Camera up an down around the Origin
« Reply #4 on: April 08, 2012, 09:16:30 pm »
Ok, that is much shorter, and the axis could be set as in my source by a cross product.