Ok, so I have a camera with four vectors attached to it: up, right, forward and position. I want to be able to use it as a full function camera, but I'm getting weird problems (see bottom).
Here's what I have so far:
First, my orientation matrix has a general rotate function:
void rot(float x, float y, float z)
{
/////////// Do the x rotations//////////////
if(x!=0.0f)
{
Vector3 vTemp=getForward();
vTemp.rotVect(x, getRight());
setForward(vTemp);
setUp(getRight().cross(getForward())); // Set the up vector
}
if(y!=0.0f)
{
Vector3 vTemp=getForward();
vTemp.rotVect(y, getUp());
setForward(vTemp);
setRight(getForward().cross(getUp()));
}
if(z!=0.0f)
{
Vector3 vTemp=getUp();
vTemp.rotVect(z, getForward());
setUp(vTemp);
setRight(getForward().cross(getUp()));
}
}
Then the vector rotation function is here:
void rotVect(float angle, Vector3 coord)
{
// 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
x = (cosTheta + (1 - cosTheta) * coord.x * coord.x) * x;
x += ((1 - cosTheta) * coord.x * coord.y - coord.z * sinTheta) * y;
x += ((1 - cosTheta) * coord.x * coord.z + coord.y * sinTheta) * z;
// Find the new y position for the new rotated point
y = ((1 - cosTheta) * coord.x * coord.y + coord.z * sinTheta) * x;
y += (cosTheta + (1 - cosTheta) * coord.y * coord.y) * y;
y += ((1 - cosTheta) * coord.y * coord.z - coord.x * sinTheta) * z;
// Find the new z position for the new rotated point
z = ((1 - cosTheta) * coord.x * coord.z - coord.y * sinTheta) * x;
z += ((1 - cosTheta) * coord.y * coord.z + coord.x * sinTheta) * y;
z += (cosTheta + (1 - cosTheta) * coord.z * coord.z) * z;
}
And finally I have a camera function to rotate from mouse movement:
void CCamera::SetViewByMouse()
{
POINT mousePos; // This is a window structure that holds an X and Y
int middleX = 800 >> 1; // This is a binary shift to get half the width
int middleY = 600 >> 1; // This is a binary shift to get half the height
float angleY = 0.0f; // This is the direction for looking up or down
float angleX = 0.0f; // This will be the value we need to rotate around the Y axis (Left and Right)
static float currentRotX = 0.0f;
// Get the mouse's current X,Y position
GetCursorPos(&mousePos);
// If our cursor is still in the middle, we never moved... so don't update the screen
if( (mousePos.x == middleX) && (mousePos.y == middleY) ) return;
// Set the mouse position to the middle of our window
SetCursorPos(middleX, middleY);
// Get the direction the mouse moved in, but bring the number down to a reasonable amount
angleY = (float)( (middleX - mousePos.x) ) / 500.0f;
angleX = (float)( (middleY - mousePos.y) ) / 500.0f;
// Here we keep track of the current rotation (for up and down) so that
// we can restrict the camera from doing a full 360 loop.
currentRotX -= angleX;
// If the current rotation (in radians) is greater than 1.0, we want to cap it.
if(currentRotX > 1.0f)
currentRotX = 1.0f;
// Check if the rotation is below -1.0, if so we want to make sure it doesn't continue
else if(currentRotX < -1.0f)
currentRotX = -1.0f;
// Otherwise, we can rotate the view around our position
else
{
orientation.rot(angleX,0, 0);
}
// Rotate around the y axis no matter what the currentRotX is
orientation.rot(0, angleY, 0);
}
Ok, what's happening is that when I rotate, the scene gets squeezed until it becomes a little vertical line and disappears, leaving offly the clear color. If I rotate at all using the mouse, in any direction, it does that.
What's going off?
[edited by - sirSolarius on March 24, 2004 7:59:07 PM]