#1: Creating the full view matrix.- Code: Select all
`// Create a forward vector. Looking at the enemy.`

CVector3 vTarget = vEnemyPos - vYourPos;

// Get the up and right vectors. Get the right vector by using the world-axis up vector.

CVector3 vUp( 0.0f, 1.0f, 0.0f );

CVector3 vRight = vTarget.Cross( vUp );

// The up vector is the cross of the right and forward.

vUp = vRight.Cross( vTarget );

// Normalize all of the vectors.

vTarget.Normalize();

vUp.Normalize();

vRight.Normalize();

// Create the view matrix.

mMatrix._11 = vRight.x;

mMatrix._12 = vRight.y;

mMatrix._13 = vRight.z;

mMatrix._14 = 0.0f;

mMatrix._21 = vUp.x;

mMatrix._22 = vUp.y;

mMatrix._23 = vUp.z;

mMatrix._24 = 0.0f;

mMatrix._31 = vTarget.x;

mMatrix._32 = vTarget.y;

mMatrix._33 = vTarget.z;

mMatrix._34 = 0.0f;

mMatrix._41 = vYourPos.x;

mMatrix._42 = vYourPos.y;

mMatrix._43 = vYourPos.z;

mMatrix._44 = 1.0f;

If the perspective matrix causes the view to look down -Z instead of Z, you will have to invert the target vector using the following as the first line instead:

- Code: Select all
`CVector3 vTarget = vYourPos - vEnemyPos;`

The matrix is in DirectX format (row-major). Transpose for OpenGL. Y is assumed to be up.

#2: Just modifying your player’s facing direction.You have 2 values representing the direction your player is facing. These are Cartesian coordinates. The values you have are useless. The only thing we need to know is that the player has Cartesian values to describe the direction it is facing.

Again you need to create a vector to look at the target. But instead of converting it to a matrix we convert it to Cartesian coordinates. As you remember from 3rd grade elementary school, you can convert a directional vector to Cartesian coordinates via the following (assuming Y is up).

- Code: Select all
`void VectorToCartesian( const CVector3 &_vVector, float &_fHor, float &_fVert ) {`

_fHor = ::fatan2( _vVector.z, _vVector.x );

float fLen = _vVector.Len();

if ( fLen == 0.0f ) {

_fVert = 1.5707963267948966192313216916398f; // Half PI.

}

else {

_fVert = ::acosf( _vVector.y / fLen );

}

}

With that elementary utility function it is pre-school to perform this task.

- Code: Select all
`// Create a forward vector. Looking at the enemy.`

CVector3 vTarget = vEnemyPos - vYourPos;

// Convert to Cartesian to overwrite the RAM in the player block.

float fH, fV;

VectorToCartesian( vTarget, fH, fV );

fH and fV will be the values to overwrite in your player block.

Again it may cause you to look away from the target, in which case you reverse the first line of code (again).

Again Y = up.

L. Spiro

Our songs remind you of songs you’ve never heard.