Mathematics? (Peculiar Camera Rotation)
Hullo all, I've attached an image so you kind of get the idea of what I'm rendering (The actual thing is fullscreen and not so condensed)...(And on a sidenote, the textures aren't showing up on the object, which is why it's all white, but that's another problem for another day) :)
I'm trying to recreate first-person camera movement, so when you move the mouse left, the camera turns left, and so forth. I've almost gotten the x-axis to work, here's the code I use (Note, uses DirectInput):
Code:
void cDX::DIProcessMouse(void)
{
DIMOUSESTATE MouseState;
if(FAILED(DIMouseDevice->GetDeviceState(sizeof(MouseState),(LPVOID)&MouseState)))
return;
DIMouseX += MouseState.lX;
DIMouseY += MouseState.lY;
DIMouseZ += MouseState.lZ;
if(MouseState.lX != 0)
{
Camera3DYRotation += (float)(MouseState.lX / 1000.0f);
if(Camera3DYRotation > (D3DX_PI + D3DX_PI))
{
Camera3DYRotation -= (D3DX_PI + D3DX_PI);
}
else if(Camera3DYRotation < 0.0000f) //Thought numbers were getting truncated, so I tried this. Didn't fix it.
{
Camera3DYRotation += (D3DX_PI + D3DX_PI);
}
bCamera3DUpdate = true;
}
if(bCamera3DUpdate == true)
{
Camera3DRotate(0.0f, Camera3DYRotation, 0.0f);
Camera3DUpdate();
}
}
The above pretty much keeps the X Rotation number between 0 and 2Pi radians.
The Camera3DRotate() function (responsible for converting the rotation to screen coordinates) looks like this:
Code:
void cDX::Camera3DRotate(float X, float Y, float Z)
{
Camera3DLookAt.x = Camera3DFocus * sinf(Y) + Camera3DPosition.x;
Camera3DLookAt.z = Camera3DFocus * cosf(Y) + Camera3DPosition.z;
}
As you can see, it's just simple trig going on to figure out the proper (X,Z) point to focus on.
The update isn't as important, but here it is anyways so you see what I'm doing:
Code:
void cDX::Camera3DUpdate(void)
{
D3DXMatrixLookAtLH(&Camera3DMatrix, &Camera3DPosition, &Camera3DLookAt, &Camera3DUp);
D3DDevice->SetTransform(D3DTS_VIEW, &Camera3DMatrix);
bCamera3DUpdate = false;
}
Again, just a simple update to the camera.
Now, in the picture below, that's what gets rendered to the screen right when the program starts, with the mouse centered right in the middle of the ship (Yes, it's a ship...just go with it) :)
If I fling the mouse around the screen a lot, and I try to recentre the ship so that the camera is looking at (0.00,0.00), then the mouse is off-centre. And not just by a little bit. If I fling it around enough, it can be off ridiculously.
I'm wondering if anybody has any ideas what could be causing this? (Just to note, I'm using the MouseState.lX variable to update camera movement, which calculates the change in X since last call. I was very careful not to hit the edge of my screen, which would've caused the camera to still rotate, but the mouse to "stay still". So the offset isn't because of that...)
If anyone's got any ideas (or they just see something I'm doing horribly wrong, even if it's not related to this) I'm always open to suggestions :) Thanks for givin' this a read