# Third person camera, how to stop it swaying?

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

## Recommended Posts

Hi (reposting here as it seems more suitable because its a math based problem)
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

D3DXMatrixIdentity(&cM);

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

cM = cM * P->PositionMatrix;
//Tried lots of different methods rather than the below bit (all same result)

//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

as another note, if i do
cM = P->PositionMatrix * cM; the camera follow without sitting behind the objects, just facing in z axis, so that works at least

Double post.

##### Share on other sites
browser timed out clicking add xD

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 27
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631737
• Total Posts
3001944
×