Archived

This topic is now archived and is closed to further replies.

Moh_Abed

3rd person camera

Recommended Posts

Q1: how can i make a 3rd person camera based on some object (character) using directx or so. Q2: i read the zen of direct3d, windows game programming guru, and programming rpg with directx. so what next book u suggest?, i need to learn some more advanced topics like (advanced occlusion culling(mask shadow, pvs, ...) and advanced animation and AI/Physics. i heard about 3d game engine design. what about it. or what u suggest at all.

Share this post


Link to post
Share on other sites
I''ve posted this before, but it is a copy of my camera class that can either free float, or follow another object around. Works and is simple, but could use a little clean up and the Follow function needs more work when following rotating objects. I just have not fully sorted out the math yet.

Also, for books, I really liked the Game Programming Gems series.


  
/// <summary>

/// Camera class that supports a camera that can follow an entity or can

/// fly on its own.

/// </summary>

public class Camera
{
private bool m_bNeedsUpdate = false;
private Vector3 m_vPosition = new Vector3(0,0,0);
private Matrix m_matView = new Matrix();
private Quaternion m_quatOrientation = new Quaternion();

public Camera()
{
LookAt(new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f,0.0f,1.0f), new Vector3(0.0f,1.0f,0.0f));
Update();
}
/// <summary>

/// Look at a point in space.

/// </summary>

/// <param name="Eye">Viewer location</param>

/// <param name="At">Where viewer is looking</param>

/// <param name="Up">Up Vector of viewer</param>

public void LookAt(Vector3 Eye, Vector3 At, Vector3 Up)
{
Matrix matTemp;

matTemp = Matrix.LookAtLH( Eye, At, Up );

m_vPosition = Eye;

// Get the orientation

m_quatOrientation = Quaternion.RotationMatrix(matTemp);

m_bNeedsUpdate = true;
}

private void Update()
{
Matrix matTranslation = Matrix.Translation(-m_vPosition.X, -m_vPosition.Y, -m_vPosition.Z);
Matrix matRotation = Matrix.RotationQuaternion(m_quatOrientation);
m_matView = matTranslation * matRotation;

m_bNeedsUpdate = false;
}

public Matrix GetViewMatrix()
{
if(m_bNeedsUpdate) Update();
return m_matView;
}

public void MoveForward(float Dist)
{
Matrix matTemp;
Vector3 Forward = new Vector3(0.0f,0.0f,-1.0f);

matTemp = Matrix.RotationQuaternion(m_quatOrientation);
matTemp.Transpose(matTemp);
Forward.TransformCoordinate(matTemp);

// update location

m_vPosition -= Forward * Dist;
m_bNeedsUpdate = true;
}

public void RotateRel(Vector3 DeltaRotation)
{
// This will rotate about the camera''s current rotational position.

Quaternion quat = new Quaternion();
quat.RotateYawPitchRoll(DeltaRotation.Y, DeltaRotation.X, DeltaRotation.Z);

// Now rotate current orientation by new rotation.

m_quatOrientation *= quat;
m_bNeedsUpdate = true;
}

public void Follow(Quaternion ObjOrientation, Vector3 Position, float Yaw, float Pitch, float Distance)
{
Vector3 v2, vUp, vForward;
Quaternion quat1 = new Quaternion();
Quaternion quat2 = new Quaternion();
Matrix matTemp = new Matrix();

//System.Diagnostics.Debug.WriteLine(Yaw + " " + Pitch);

quat1.RotateYawPitchRoll(Yaw, Pitch, 0.0f);
quat2 = quat1 * ObjOrientation;

matTemp.RotateQuaternion(quat2);

vForward.X = matTemp.M31; vForward.Y = matTemp.M32; vForward.Z = matTemp.M33;
vUp.X = matTemp.M21; vUp.Y = matTemp.M22; vUp.Z = matTemp.M23;

v2 = Position - vForward * Distance;

LookAt(v2, Position, vUp);
m_bNeedsUpdate = true;
}

}

Share this post


Link to post
Share on other sites