PDA

View Full Version : Rotating around an object



thorsan
02-22-2004, 05:10 AM
Hello

Imhaving a little problem with rotating around a cube. First tried using glRotatef ariund the three different vectors. this seemed to work nice, but... When i have rotated 90 degrees around the y axis and then want to rotate over the cube( "standing" along the x axis ) and want to rotate "over the cube" the cube spins like a wheel. this is so because, when i push the up button i increase the rotation around the x axis. but when im not standing orthogonally on the x axis, i must also rotate around the z axis too...

i have tried building up a projection matrix this way:
void buildMatrix()
{
float phi = mat.xgrad;
float kappa = mat.ygrad;
float omega = mat.zgrad;

mat.trans[0] = cos(kappa)*cos(omega);
mat.trans[1] = -sin(omega);/*-cos(kappa)*-sin(omega);*/
mat.trans[2] = sin(kappa);

mat.trans[3] = 0.0f;

mat.trans[4] = sin(omega);/*sin(phi)*sin(kappa)*cos(omega) + cos(phi)*sin(omega);*/
mat.trans[5] = cos(phi) * cos(omega);/*-sin(phi)*sin(kappa)*sin(omega) + cos(phi)*cos(omega);*/
mat.trans[6] = -sin(phi);/**cos(kappa);*/

mat.trans[7] = 0.0f;

mat.trans[8] = -sin(kappa); /*-cos(phi)*sin(kappa)*cos(omega) + sin(phi)*sin(omega);*/
mat.trans[9] = sin(phi); /*cos(phi)*sin(kappa)*sin(omega) + sin(phi)*cos(omega);*/
mat.trans[10] = cos(phi)*cos(kappa);

mat.trans[11] = 0.0f;

mat.trans[12] = 0.0f;//mat.xgrad;
mat.trans[13] = 0.0f;//mat.ygrad;
mat.trans[14] = 0.0f;//mat.zgrad;

mat.trans[15] = 1.f;

}

this doesnt work at all. i need tips! how do i freely move around an object?

Clockwork
02-25-2004, 12:57 PM
I use a vector to hold 3 floats to represent Polar coordinates (Rho, Theta, Phi). The anchor vector here represents the postion that the camera is rotating around. The mouse interacts with the polar vector to move the camera. The equations below convert the polar coordinates into cartesian coordinates to get a real location in 3d space. Once these are done, I use gluLookAt with Position.x,y,z Anchor.x,y,z and 0,0,1 as the global up vector.

double tempTheta = polar.y * (float) Math.PI/180;
double tempPhi = polar.z * (float) Math.PI/180;

position.x = anchor.x + polar.x * (float) Math.cos( tempPhi ) * (float) Math.cos( tempTheta );
position.y = anchor.y + polar.x * (float) Math.cos( tempPhi ) * (float) Math.sin( tempTheta );
position.z = anchor.z + polar.x * (float) Math.sin( tempPhi );

Feel free to shoot me an email if you've got problems with it. Position is the location of the camera, and anchor is the direction it's pointed. You can use those and a global up vector to get some billboard coordinates pretty quickly... Some day I'll put that together in a tutorial.