Hey guys,
I have a problem. I need to rotate the camera around a fixed point in 3d. In other words, the camera must be able to traverse a sphere. So I have a class called CCamera and a member function like this:
GLvoid RotateAroundPoint(CVector3 vCenter, float angle, float x, float y, float z)
{
CVector3 vNewPosition;
CVector3 vPos = mPosition - vCenter;
// Calculate the sine and cosine of the angle once
float cosTheta = (float)cos(angle);
float sinTheta = (float)sin(angle);
// Find the new x position for the new rotated point
vNewPosition.x = (cosTheta + (1 - cosTheta) * x * x) * vPos.x;
vNewPosition.x += ((1 - cosTheta) * x * y - z * sinTheta) * vPos.y;
vNewPosition.x += ((1 - cosTheta) * x * z + y * sinTheta) * vPos.z;
// Find the new y position for the new rotated point
vNewPosition.y = ((1 - cosTheta) * x * y + z * sinTheta) * vPos.x;
vNewPosition.y += (cosTheta + (1 - cosTheta) * y * y) * vPos.y;
vNewPosition.y += ((1 - cosTheta) * y * z - x * sinTheta) * vPos.z;
// Find the new z position for the new rotated point
vNewPosition.z = ((1 - cosTheta) * x * z - y * sinTheta) * vPos.x;
vNewPosition.z += ((1 - cosTheta) * y * z + x * sinTheta) * vPos.y;
vNewPosition.z += (cosTheta + (1 - cosTheta) * z * z) * vPos.z;
// Now we just add the newly rotated vector to our position to set
// our new rotated position of our camera.
mPosition = vCenter + vNewPosition;
}
Well i guess when I use the key board arrow keys it works well. But when using glutMotionFunc to drag around the scene it gets bad. And the thing is in keybard func, we’d be calling the function rotate about a single axis one at a time. So it aint a problem. But i want to rotate the about the point freely using the mouse.
Heres how my motion drag func goes"
GLvoid MouseDrag(int x, int y)
{
if(!InitDrag)
{
NewDragX = x ;
NewDragY = y ;
InitDrag = true ;
}
else
{
if(y > NewDragY)
{
if(!bActivateSet1&&!bActivateSet2&&!bActivateSet3)
{
CameraObj.RotateAroundPoint(CameraObj.mView , (-CAMERASPEED/FACTOR) , 1.0f , 0.0f , 0.0f) ;
}
}
else
{
if(!bActivateSet1&&!bActivateSet2&&!bActivateSet3)
{
CameraObj.RotateAroundPoint(CameraObj.mView , (CAMERASPEED/FACTOR) , 1.0f , 0.0f , 0.0f) ;
}
}
if(x > NewDragX)
{
if(!bActivateSet1&&!bActivateSet2&&!bActivateSet3)
{
CameraObj.RotateAroundPoint(CameraObj.mView , (CAMERASPEED/FACTOR) , 0.0f , 1.0f , 0.0f) ;
}
}
else
{
if(!bActivateSet1&&!bActivateSet2&&!bActivateSet3)
{
CameraObj.RotateAroundPoint(CameraObj.mView , (-CAMERASPEED/FACTOR) , 0.0f , 1.0f , 0.0f) ;
}
}
}
glutPostRedisplay() ;
}
If anyone knows how to go about this…please let me know…write now it gives me kinda wierd motion around the point.
Yours
Siddharth