[HELP] - DirectX Camera Rotation

Technical Discussions not Related Directly to MHS. For Example, Coding, Hex Editing, General Hacking, Etc.

Moderators: g3nuin3, SpeedWing, WhiteHat

[HELP] - DirectX Camera Rotation

Postby denispn » Tue May 11, 2010 6:43 am

Hi, folks!

Let's suppose, in a game, that i have my player's (x,y,z) position, the angles (x,y) that my player's looking at and an enemy position.
I want to rotate my player's camera to look at this enemy.

Is there any .DLL that will return the angles needed to rotate a camera to look at a specific target? Is there any directx or opengl function that will return these angles?

Hacker Smacker
Posts: 43
Joined: Wed Dec 26, 2007 9:45 am

Re: [HELP] - DirectX Camera Rotation

Postby L. Spiro » Thu May 13, 2010 8:25 am

#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.

// 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.
User avatar
L. Spiro
L. Spiro
Posts: 3128
Joined: Mon Jul 17, 2006 10:14 pm
Location: Tokyo, Japan

Re: [HELP] - DirectX Camera Rotation

Postby denispn » Thu May 13, 2010 8:50 am

Thanks you very much L. Spiro!

I will try to implement your second method in asm, as it seems to be a lot easier.
I will be back! :-)

Hacker Smacker
Posts: 43
Joined: Wed Dec 26, 2007 9:45 am

Return to Technical Unrelated

Who is online

Users browsing this forum: No registered users and 2 guests