3rd Person and mesh direction (DirectX)

Started by
3 comments, last by GameDev.net 18 years, 3 months ago
Hi! I've been searching gamedev.net and the whole internet on information on how to make a 3rd person camera. But the information and examples are very few and often don't work. What i want to do is a camera that stays behind the mesh at all times. It should only change the x and y coordinates when the mesh rotates. Could someone please point me in the right direction. Thanks
Advertisement
There are some excellent examples on the matter and if you stick into it you might find that most of them do have a common way of creating the camera.
Toy Maker's 3RD Person camera example
The sample framework employs a totally different camera system which is enclosed in a sphere and you moving around in the sphere. It's a good start. Also take a look at how other free engines do it like irrlicht .
Here is a nice list of DirectX Engines.
With regards to actually moving behind a player. The easiest way I have found is to have your camera have an attachToEntity method and then you pass it an entity which will set the position of the camera few meters away from the entity and move with it.

I hope this helps a bit.
Take care.
Heya,

I implemented something similar for my 4e4 entry ShipHo, for which you can find the source code over here. It's in C#, but it should give you an example of how it could be done. The code in there is a bit overdone, so here's what it comes down to:

Vector3 meshPosition = new Vector3(0, 0, 0);// aligned along positive x-axisVector3 meshDirection = new Vector3(1, 0, 0);float camDistance = 50f;Vector3 cameraPosition = meshPosition - Vector3.Scale( meshDirection, camDistance );Matrix viewMatrix = Matrix.LookAtLH( cameraPosition, meshPosition, new Vector3(0,1,0));


Basically you have the camera look at the mesh from a position that is moved back a certain distance from the mesh, along the direction that mesh is currently facing. So the hardest part to implement is probably to meaningfully use this mesh direction.

In a game situation, you'll typically set this direction yourself, or it is easy to calculate if you are using a Vector3 target to where the mesh is supposed to move. From this position->target setup you can calculate the direction vector by using:

Vector3 direction = Vector3.Normalize( target - meshPosition );


So basically it comes down to using different variables for describing your mesh rotation. You can derive the actual rotation matrix for the mesh by applying the following calculation to the original direction vector of the mesh (the way the model is originally pointing) and the current direction vector:

// 2*atan2(|u-v|, |u+v|); float dirAngle = 2f * (float)Math.Atan2(    Vector3.Subtract(modelDirection, directionVector).Length(),    Vector3.Add(modelDirection, directionVector).Length() );Matrix dirMatrix = Matrix.RotationAxis( Vector3.Cross( modelDirection, directionVector ), dirAngle);


The angle calculation may seem a bit weird. I found it someplace I can't remember, but it is supposedly equivalent to the more intuitive code below except that it solves some annoying behaviour common for this approach:

float angle = (float)Math.Acos( Vector3.Dot( modelDirection, directionVector ) );


So... Hope this helps and doesn't utterly confuse you :)
Rim van Wersch [ MDXInfo ] [ XNAInfo ] [ YouTube ] - Do yourself a favor and bookmark this excellent free online D3D/shader book!


You want an 'over the shoulder' (or thru the back) view ....


Use the same forward (facing)vector of the avatar and project backwards from the avatar's XYZ head position a fixed distance (which could be adjustable by player??).

You then make your camera view using that position and the same forward vector to get the 'thru the back' view.

If you want 'over the shoulder' you would first rotate a copy of the avatar's forward vector upwards by some fixed angle (which also could be adjustable by player...). This would look down at the player. A varuation is to have the angle actually look some small distance ahead of the avatar (project forward along forward vector to get the point...).


You need to consider how to handle this type of view's problem with terrain mesh/objects behind the avatar getting in the way. Some methods are to identify them and make them wireframe or highly transparent or my personally hated one -- an auto zoom that slides the adjustment of the viewpoint/angle til its not colliding/obscured, which causes great crazy headache producing gyrations in tight terrain/interiors.


Quote:Original post by sionic
...I've been searching gamedev.net and the whole internet on information on how to make a 3rd person camera...
Thanks

Search for D3DSmartCamera.

This topic is closed to new replies.

Advertisement