Sign in to follow this  
deathtrap

Cant get camera to stay behind object

Recommended Posts

deathtrap    364
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 this post


Link to post
Share on other sites
Silkjc    219
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 this post


Link to post
Share on other sites
DXnut    262
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.

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