Jump to content
  • Advertisement
Sign in to follow this  
ryan mccabe

Help with camera bugging out.

This topic is 2609 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey again. Wondering if someone can help find the bug in this code. Its just a camera class that takes input and transforms the view matrix. The camera follows the mouse and moves with key presses. Everything works great until I move the camera while rotating, then the camera just starts doing its own thing, slipping into an accelerated turns and ignoring input. The longer I keep rotating and moving the worse the effect gets until it just ludicrously spins out of control, even for a time after all input is stopped.



void CameraClass::Render()
{
D3DXVECTOR3 vRotAxis, vDirection;
float yaw, pitch, roll;
int rotationFactor = 8;
D3DXMATRIX pitchMatrix, yawMatrix, rotationMatrix, movementMatrix;

//====================
// Rotational movement
//====================

// Set the yaw (Y axis), pitch (X axis), and roll (Z axis) rotations in radians.
yaw = m_rotationX * 0.0174532925f;
pitch = m_rotationY * 0.0174532925f;
roll = m_rotationZ * 0.0174532925f;

// Create direction (forward)
D3DXVec3Normalize(&vDirection, &(m_vLookAtPt - m_vEyePt));

//strafe vector
D3DXVec3Cross(&vRotAxis,&vDirection,&m_vUp);
D3DXVec3Normalize(&vRotAxis, &vRotAxis);

//create rotation matrices
D3DXMatrixRotationAxis(&pitchMatrix, &vRotAxis, -(pitch / rotationFactor)); //rotate around x
D3DXMatrixRotationAxis(&yawMatrix, &m_vUp, (yaw / rotationFactor)); //rotate around y

//rotate direction
D3DXVec3TransformCoord(&vDirection, &vDirection, &(pitchMatrix * yawMatrix));

//rotate up vector
D3DXVec3TransformCoord(&m_vUp, &m_vUp, &(pitchMatrix * yawMatrix));

// Translate the rotated camera position to the location of the viewer.
m_vLookAtPt = vDirection + m_vEyePt;

//=====================
// Directional movement
//=====================
if (moveForward)
{
m_vEyePt += vDirection * m_fSpeed;
m_vLookAtPt += vDirection * m_fSpeed;
}
if (moveBackward)
{
m_vEyePt -= vDirection * m_fSpeed;
m_vLookAtPt -= vDirection * m_fSpeed;
}
if (moveLeft)
{
D3DXVec3Cross(&vDirection,&vDirection,&m_vUp); //create strafe vector
D3DXVec3Normalize(&vDirection,&vDirection);
m_vEyePt += vDirection * m_fSpeed;
m_vLookAtPt += vDirection * m_fSpeed;
}
if (moveRight)
{
D3DXVec3Cross(&vDirection,&vDirection,&m_vUp); //create strafe vector
D3DXVec3Normalize(&vDirection,&vDirection);
m_vEyePt -= vDirection * m_fSpeed;
m_vLookAtPt -= vDirection * m_fSpeed;
}

// Get the movement matrix
D3DXMatrixLookAtLH(&m_viewMatrix, &m_vEyePt, &m_vLookAtPt, &m_vUp);

return;
}




Rotating is ok on its own and so is moving. Even short bursts of both work ok too. I figure since its a standard camera class it'll either check out or not.

P.s. sorry for the inconsistent naming conventions, I hacked in a lot of different camera models before getting here. . . probably should clean it up a bit.

Share this post


Link to post
Share on other sites
Advertisement
I think i see your problem, i had this at one point with my camera class too

With D3DXVec3TransformCoord() it take an output, input and matrix, but it doesnt rotate around your current, it does it around the origin.
i dont think thats the problem here but what i would say to do, instead of rotating the direction vector, rotate the lookAt vertex then build your direction vector once you have done this and moved them from the origin to your actual position :) thats how mine works too

bit of code from mine if you need a look :)

D3DXVECTOR3 tempVec(p->Pos); //Temp vector to hold current position

//move position vectors to the origin
p->Pos -= tempVec;
p->ForwardPos -= tempVec;
p->RightPos -= tempVec;
p->UpPos -= tempVec;

if(amount != 0)
{
D3DXMATRIX rotMatY;

//rotates points around the y axis
//sets up the matrix to rotate around the y axis by the amount passed
D3DXMatrixRotationY(&rotMatY, (amount / 180) * D3DX_PI);

//rotates the position vectors by this rotation matrix
D3DXVec3TransformCoord(&p->ForwardPos, &p->ForwardPos, &rotMatY);
D3DXVec3TransformCoord(&p->RightPos, &p->RightPos, &rotMatY);
}


hope that helps you

Share this post


Link to post
Share on other sites
Thank you for the code, unfortunately the problem was much more subtle. I switched from WM_MOUSEMOVE in the message loop to GetCursorPos() inside the mouse update call and the random movements are gone. On the bright side WM only checks for esc in the code now and all other key presses checked directly when they are needed, and after screwing with camera code for over a week I bet I could now write one in my sleep lol.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!