Advertisement Jump to content
Sign in to follow this  

Spectator Camera

This topic is 3106 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

I am trying to write a spectator camera for my editor. I can control the camera's position via the WASD keys. I would the user to hold down the right mouse button and move the mouse around to control the camera's orientation. I almost have the latter part working but I am a little stuck on the math. The following is what I have thus far.

void manageMouseInput()
unsigned int iWidth, iHeight, iDepth;
int iTop, iLeft;
m_pRenderWindow->getMetrics(iWidth, iHeight, iDepth, iLeft, iTop);

const OIS::MouseState &ms = m_pMouse->getMouseState();
ms.width = iWidth;
ms.height = iHeight;
Vector3 vecForward = m_pCamera->getUp().normalisedCopy().crossProduct(m_pCamera->getRight().normalisedCopy());

m_bRightMouseDown = false;
m_bRightMouseDown = true;

if(m_bRightMouseDown == true)
//m_pCamera->setPosition(m_pCamera->getPosition() + (vecForward * 0.02f));
Vector3 vecDirection;
vecDirection = vecForward;
float fAxX = ms.X.abs - m_msOld.X.abs;
float fAxY = ms.Y.abs - m_msOld.Y.abs;

fAxX = fAxX * 0.010f;
fAxY = fAxY * 0.010f;


vecDirection.x += 0;
vecDirection.y -= fAxY;
vecDirection.z += fAxX;


m_msOld = ms;


I am confused with regards to calculating a new direction vector based on the delta's in the mouse movement. Could someone explain it to me? Thanks!

Share this post

Link to post
Share on other sites
I don't quite understand what sort of end-result you're looking for. Do you want the camera to move like an FPS camera when you have the right mouse button held?

Share this post

Link to post
Share on other sites
The general approach for mouse input in an FPS camera goes something like this.

1.) Build a Vector3 representing rotational velocity. In general, we have

// Declare, update, and/or pass in these variables in from elsewhere.
Vector3 RotVel;
Vector2 newMouse;
Vector2 oldMouse;
Vector2 deltaMouse;
const float rotScalar = 0.01f;

deltaMouse = newMouse - oldMouse;
// Note: The x ( roll ) component is general 0 in FPS.
// Flight sims reintroduce it with input from keyboard or fancy joy sticks.
RotVel.y = DeltaMouse.y * rotScalar;
RotVel.z = DeltaMouse.z * rotScalar;

Your code looks pretty good in this regard. I don't know about taking absolute values though (ie. ms.X.abs ect.).

2.) Add your rotational velocity into the current roll, pitch, and yaw values.

// Again, for FPS we are going to ignore roll.
// These variables are probably going to be member variables in your class.
float mPitch;
float mYaw;
mPitch += RotVel.y;
mYaw += RotVel.z;
// Note: Some camera abstractions clamp the pitch value to straight up or straight down. Most people can't tie their neck in knots to look behind them.

3.) Figure out where the camera is looking based on the new Euler angles.

Matrix cameraRot;
MathLib::MatrixRotationYPR( &cameraRot, mYaw, mPitch, 0 ); //roll zero

Vector3 cameraUp, cameraLookAt;
Vector3 up, lookAt;
up = (0,1,0);
lookAt = (0,0,1);
MathLib::VectorTransform( &cameraUp, &up, &cameraRot );
MathLib::VectorTransform( &cameraLookAt, &lookAt, &cameraRot );

I believe this is what you were asking. However, there's 1 more thing.

4.) Recalculate the camera's view matrix based on the transformed vectors.

// These should be member variables in your camera class.
Vector3 mLookAt;
Vector3 mEye;
Matrix mView;

// This is the overall goal. Change where the camera is looking based on rotations from the mouse.
mLookAt = mEye + cameraLookAt;

// Finally, update the view matrix.
MathLib::MatrixLookAtLH( &mView, &mEye, &mLookAt, &cameraUp );
// Becareful of the forth param. It's the up vector of the current rotation of the camera and not the default up of the world.

Well, that should get you going. If you want to do a different camera, like a model viewing camera or an over the shoulder camera ( World of Warcraft ), the logic of steps 2-4 is very similar. However, figuring out the rotational velocity is more complex. Generally, you need a an arc ball class leveraging quaternions or something similar.

Share this post

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

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!