Jump to content
  • Advertisement
Sign in to follow this  

Third person camera, how to stop it swaying?

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

As the topic title says, im having a problem with a third person camera swaying behind the object it is following when the object moves (sways left and right on axis when moving left or right, completly buggers up when the object goes up or down.
At the moment all the camera does it, follow an object that is passed to it via an offset also passed (from constructor or attatchToObject() call) like so:

ThirdPersonCamera::ThirdPersonCamera(AObject* ObjectToFollow, D3DXVECTOR3 offset)

attachedObject = ObjectToFollow;

offsetPos = offset;

in the cameras update function, i construct a a matrix from the offset and multiply it against the attatchedObjects position matrix (gives me the position to move the camera too, it's lookAt is of course the attatchedObject, but i think the problem is the up vector (the game is 3d (all floating and no gravity etc)) so what im assuming is that the cameras UP vector wants to be the same as the objects, because if the object is upside down, i want the camera to be upside down too.

so i work out the up like so:

void ThirdPersonCamera::updateCamera(LPDIRECT3DDEVICE9 gd3dDevice)
//used to update cameras pos
bChanged = true;
PositionData* P = &attachedObject->getPositionData();
PositionData* C = &this->getPositionData();
D3DXMATRIX cM; //offset matrix for the cameras pos


cM._41 = offsetPos.x;
cM._42 = offsetPos.y;
cM._43 = offsetPos.z;

cM = cM * P->PositionMatrix;

//calculate the up vector:
// move from cams pos along P's Up vector
D3DXVECTOR3 temp(P->Pos - P->UpPos), p(C->Pos);
D3DXVec3Normalize(&temp, &temp);
temp *= 1.0f;
p += temp;
C->UpPos = p;

C->PositionMatrix = cM;
C->Pos = D3DXVECTOR3(cM._41, cM._42, cM._43);
PreviousData = *C;

The Base class version of updateCamera, Camera::UpdateCamera(LPDIRECT3DDEVICE9 gd3dDevice) gets called in my engines begin scene call (which i dont get access to (in a lib file))
and that builds the view matrix from the cameras vectors.

So, basically what im asking is. am i cocking up somewhere in this bit of code? the base camera class works perfectly, it's just the swaying and iffy up vectors stuff im having problems with here

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!