Camera Rotation

Hi trying to get my head around the mechanics of how to rotate and trasform a camera usgin xith.

can’t find many tutorials on it and got some code from a few posts in this forum but still i don’t really understand about multipling the matrices.

I want to be able to move tha camera like in the game Black and White which im sure ye all know, so basically the camera is above the chess board (making a chess game) with its view origin towards the center of the board. when the user moves mouse to the left or right edge of the screen and clicks and drags the camera will rotate around the central axis of the board.

likewise with the top and bottom edge but the camera will tilt around the central axis of the board. also want the camera to zoom in and out from the board using the mouse wheel.

hope thats a good description of what i want to do, sorry about the none tecnical language. I dont mind doing the research myself just can’t find any good tutorials on doing this sort of thing in xith or jogl, so if anyone can suggest code or web sites that would be great :slight_smile:

Here is the code I’m using that does what you described:


// these are global vars
float xrot = 0; // rotation around the right axis
float yrot = 0; // rotation around the up axis
float distance = 250; // the distance of the camera from the origin

...
// this goes in the game loop
// (Mouse.dx, Mouse.dy) is the mouse movement since last frame
if (Mouse.isButtonDown(0)) {
  // update rotation when left mouse button is dragged
  xrot += Mouse.dy*0.01f;
  float max = (float)(Math.PI/2.0*0.9);
  xrot = Math.min(max, Math.max(-max, xrot));
  yrot += Mouse.dx*0.01f;
} else if (Mouse.isButtonDown(1)) {
  // update distance when right mouse button is dragged
  distance -= distance * Mouse.dy/100;
}

// calculate position of camera using cos and sin
float cos = (float)Math.cos(xrot);
float x = cos*(float)Math.cos(yrot) * distance;
float y =     (float)Math.sin(xrot) * distance;
float z = cos*(float)Math.sin(yrot) * distance;

// feed the position to the lookAt function. We are looking at the center of the world
GLU.gluLookAt(x, y, z, 0, 0, 0, 0, 1, 0);

Just replace GLU.gluLookAt with view.getTransform().lookAt()

It is possible to extend this so that also can move on the viewing plane. That is move right, left, up and down. But it’s a little bit more complicated. But I can give you the details if you need it.

I think thats all i need i’ll try it out tonight thanks very much :slight_smile:

tom tom tom go for it :slight_smile: