Sign in to follow this  
DanielDoyle

Third person camera, how to stop it swaying?

Recommended Posts

DanielDoyle    102
Hi
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:
[code]
ThirdPersonCamera::ThirdPersonCamera(AObject* ObjectToFollow, D3DXVECTOR3 offset)
{

attachedObject = ObjectToFollow;

offsetPos = offset;
}
[/code]

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:
[code]
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

D3DXMatrixIdentity(&cM);

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);
UpdateVectors©;
PreviousData = *C;
}
[/code]

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this