PDA

View Full Version : Rotating around arbitry point troubles



earthbanana
03-24-2013, 06:16 AM
Okay so this is a pretty specific question having to do with my camera transform in OpenGL. When the view opens the rotation point is around the origin - and when you click on another hexagon tile the rotation point changes to that tiles position. So the transform in pseudo code is -rotationPointTranslationMatrix * rotationMatrix * rotationPointTranslationMatrix.

Well I want the rotation point to change from the origin to the hexagon tile you click without the camera position appearing to change. I would post pictures to show what I'm talking about but I'm new and it won't let me. The problems I am having are strange. So first I also have a camera position - ill call the this transform cameraPositionTranslationMatrix.

All of the tiles are sitting flat along the z=0 plane.

So when I get the transform with this code -


return cameraPositionTranslationMatrix * rotationMatrix * rotationPointTranslationMatrix

it works - the rotation point changes to the correct tile. But the camera position also moves so that the newly selected tile is the center of the screen again. And when I do this...


return cameraPositionTranslationMatrix * -rotationPointTranslationMatrix * rotationMatrix * rotationPointTranslationMatrix


it also works - and now the camera doesn't move and the rotation point changes to the new point exactly like I want. When I rotate the camera it will rotate around that hex tile. However - If I rotate the camera first (for example 30 degrees about the z axis) and then move the point of rotation to another tile - all of the tiles will move in a direction in their coordinate space that is proportional to the angle over the axis. For example - with a 30 degree rotation about the z axis - changing the rotation point from 0,0,0 to 0,10,0 will cause all of the tiles to move a certain amount in their y direction (which makes the camera look like its moving up and to the left).

I can't figure out what in the world is going on here..
I think there must be something wrong with my transformation code - so here it is.



NSMatrix4Df NSMatrix4Df::getRotationMatrix(float xAxisAng, float yAxisAng, float zAxisAng)
{
float xRad = DegreesToRadians(xAxisAng);
float yRad = DegreesToRadians(yAxisAng);
float zRad = DegreesToRadians(zAxisAng);

NSMatrix4Df xRotMat;
NSMatrix4Df yRotMat;
NSMatrix4Df zRotMat;

xRotMat.value(1,1) = cosf(xRad); xRotMat.value(1,2) = -sinf(xRad);
xRotMat.value(2,1) = sinf(xRad); xRotMat.value(2,2) = cosf(xRad);

yRotMat.value(0,0) = cosf(yRad); yRotMat.value(0,2) = -sinf(yRad);
yRotMat.value(2,0) = sinf(yRad); yRotMat.value(2,2) = cosf(yRad);

zRotMat.value(0,0) = cosf(zRad); zRotMat.value(0,1) = -sinf(zRad);
zRotMat.value(1,0) = sinf(zRad); zRotMat.value(1,1) = cosf(zRad);

return zRotMat * yRotMat * xRotMat;
}

NSMatrix4Df NSMatrix4Df::getTranslationMatrix(float xMove, float yMove, float zMove)
{
NSMatrix4Df identity;
// set the fourth column of the matrix to the translation amount
// the rest of the matrix is the identity matrix
identity.setColumn(NSVec4Df(xMove,yMove,zMove,1),3 );
return identity;
}


NSMatrix4Df is just a class to hold a 4x4 matrix in a 2 dimensional array ([4][4]). It is initialized with a 4x4 identity matrix. NSVec4Df is a class that holds a four dimensional vector with x,y,z, and w. Everything is done using floats.

rotationPointTranslationMatrix and cameraPositionTranslationMatrix are filled using the NSMatrix4Df::getTranslationMatrix function and rotationMatrix is filled using NSMatrix4Df::getRotationMatrix - the rotationPoint and position are both a NSVec3Df (same as 4D but no w) in world space coordinates and orientaton is a NSVec3Df with the camera angles around each axis.. (x,y, and z). Please let me know if you see anything wrong with my transformations.. I can't figure out what is going wrong. Any help would be greatly appreciated. Thanks for your time!

tonyo_au
03-24-2013, 05:44 PM
When you rotate around a object you have to decide what is happening to your taget point as well as the eye. You can choose to rotate the target point the same amount as the eye or you can leave it alone. They give you quite different results. I rotate the target point.