8
\$\begingroup\$

I'm pretty new to C++ and DirectX9 and I want to Implement a Third Person Camera. After some reading about Quaternion-based Cameras I decided to try Implementing it. But it really blow my mind, so I ask here what is the easiest way to Implement a TP-Camera?

Here are the things the Camera should do:

  • Follow coordinates stored in a 3DVector (x, y, z)

  • Rotate around the Object (Smooth)

  • Maybe some type of 'spring' = Slowly moving back to the normal Pos behind the Object after Rotating

Apologise for my bad english: I'm german and still in school!

Thanks in Advance!

EDIT:

After some trying I've got it.

The biggest problem I considered was the right extracting of the "direction" Vector from the World-Matrix.

During my research I've found mostly something like this:

D3DXVECTOR3 direction;
direction.x = worldMatrix->_13;
direction.y = worldMatrix->_23;
direction.z = worldMatrix->_33;
D3DXVec3Normalize(&direction, &direction);

That does NOT work for me! Instead I did this:

D3DXVECTOR3 direction;
direction.x = worldMatrix->_13;
// Consider the .y after the .z!
direction.z = worldMatrix->_23;
direction.y = worldMatrix->_33;
D3DXVec3Normalize(&direction, &direction);

For scaling:

// The tenfold of the average distance
D3DXVec3Scale(&direction, &direction, 10.0f);

Setting the Positions:

cameraPos = targetPos - direction;
// For a top-view
cameraPos.y = targetPos.y + 5;

For Rotations you should create a temporary world-Matrix (from the position of the target) and rotate it how you want and extract the direction.

The Smoothing of the Rotation really depends on YOUR game!

This sort of 3rd Person Camera is really easy to make and it does it's job!

Really GREAT thanks to "jheriko" who gave me the right answer!

\$\endgroup\$
2
  • \$\begingroup\$ +1 for actually describing what you need, that is rare (even though it could be more detailed, for example the 3rd point is confusing - weren't you implying it is rotating freely?). \$\endgroup\$
    – wondra
    Commented Oct 31, 2015 at 16:00
  • \$\begingroup\$ Yes, it should rotate freely but I want it to go slowly back to the Point in the back of the player after the Rotation is done! \$\endgroup\$ Commented Oct 31, 2015 at 16:06

1 Answer 1

1
\$\begingroup\$

What will help you is having the transform matrix for the object you want to follow. You can extract the foward vector from this matrix - depending on your coordinate system and matrix handedness this will be one of the rows or columns with the 4th component removed (assuming you use 4x4 transform matrices, with no scale factors - scale factors are horrible for many reasons... but if you have scale factors you can normalise the row/column after removing the 4th component.)

Once you have this you can position the camera directly behind the object by subtracting this from the object position. To move the camera in and out you can scale the vector, and to rotate around the object you can make an x and y rotation (or xz/zy etc. depending on handedness etc.) which you apply to a copy of the object matrix before extracting the forward direction.

Finally to smooth the movement the simplest solution is to track the previous offset and angles and lerp towards them by some framerate dependent amount described by a^(b.dt) where dt is the delta time and a, b are parameters you choose. a = 0.9 and b = 30 gives you the effect of moving 90% towards the position as if you were doing it every frame at 30 fps. this is called an 'infinite impulse response' filter and is a classical hack for this sort of smoothing.

this might not be ideal, you may want to pick a minimum distance and constrain the angles too, otherwise the camera may pass through the object. if you are inside of a dense world clipping against or fading intervening objects will become important too.

sorry this is long, but its not a trivial problem, as you seem to have discovered.

hope this helps. i can provide more depth of detail if necessary.

(p.s. no quaternions necessary - they really don't help for this problem)

\$\endgroup\$
3
  • \$\begingroup\$ Thank you very much! You gave me some ideas how to do this. But I would appreciate some Code examples, if you don't mind. And why not use quaternions? I read that they are commonly used for doing this kind of stuff (en.wikipedia.org/wiki/Quaternion). \$\endgroup\$ Commented Nov 6, 2015 at 16:22
  • \$\begingroup\$ Tried to do it like you described, but still have some Issues. The Camera isn't in the back of the Player proberly, but at least somewhere in the side of him! I don't understand the part with the X and Y rotations: What should be rotated? The world matrix, the direction vector or a copy of the player position vector? And how to rotate? Hope you can help me! \$\endgroup\$ Commented Nov 7, 2015 at 17:25
  • \$\begingroup\$ you rotate the direction vector, or the matrix you extract it from (the object's transform matrix, not the world or view matrix) to rotate you construct axis angle rotation matrices and multiply them, or use trigonometry... \$\endgroup\$
    – jheriko
    Commented Nov 8, 2015 at 0:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .