# Cant get camera to stay behind object

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

## Recommended Posts

I have an object in space with Position and Rotation vectors, and I have a camera object that has a Position vector only. I've tried for hours to find a solution but i'm stumped and this is my last resort. How can i get the camera to stay behind the object, looking at the object, regardless of which way the object is facing? edit: figured I would say what i'm already doing that doesnt work. DX.Vector3 rotation = boundEntity.Rotation; DX.Matrix translationFirst = DX.Matrix.Translation(ePos); DX.Matrix rotationCam = DX.Matrix.RotationZ(rotation.Z); DX.Matrix final = translationFirst * rotationCam; DX.Vector3 finalValue = DX.Vector3.TransformCoordinate(DX.Vector3.Empty,final); basically this is my final attempt, there were others which were equally unsuccessful. what i tried doing was getting a rotation and translation matrix and then transforming an empty (0 for all axes) vector by it. its behaving erratically though.

##### Share on other sites
Is the rotation vector normalized??

##### Share on other sites
The rotation vector is just a Vector3 holding the rotation values in radians for each axes. Dont see how it could be normalized.

##### Share on other sites
I do this by using Sin and Cos to calculate the position of the camera, treating the position like a circle around the object, the radius of this circle representing the distance away from the object. The same can then be done for the camera target, only at a different radius and the same Angle + 180 degrees (so that the camera is looking to the other side of the object).

If you really need I can post an example, but its rather specific to my camera class so may be hard to understand.

##### Share on other sites
try multiplying the camera world matrix my the objects world matrix

##### Share on other sites
Use the D3DXMatrixLookAtLH() function to set your camera based on your object. Your object must have a world position, look vector, and up vector (just like your camera must have). Using the object, set the camera's position (pEye) using the object's look vector.

// position the camera 5 units behind the object
CameraPos = ObjectPos - ObjectLookVector * 5;

// position it 2 units above the object (if you want)
CameraPos = ObjectPos + ObjectUpVector * 2;

Then you can use the D3DXMatrixLookAtLH() function, using the CameraPos as pEye, the object's look vector as pAt, and the object's up vector as pUp.

• 48
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631374
• Total Posts
2999653
×