getting the direction vector

Started by
3 comments, last by Driv3MeFar 17 years, 2 months ago
I have run into a little problem i need to get the direction of which way my tower faces, how can i get the direction vector?

const float TankTower::ANGLE_RATE = 1.0f; 
const float TankTower::MAX_ANGLE = 45.0f; 

void TankTower::getWorldPosition(D3DXVECTOR3 &position) 
{ 
   position.x = finalMatrix.m[3][0]; 
   position.y = finalMatrix.m[3][1]; 
   position.z = finalMatrix.m[3][2]; 
} 
/******************************************************************** 
* update() 
********************************************************************/ 
void TankTower::update(float dtime, D3DXVECTOR3 TankTowerOffSetPos,bool Left, bool Right) 
{    
   positionVector.x = TankTowerOffSetPos.x; 
   positionVector.y = TankTowerOffSetPos.y; 
   positionVector.z = TankTowerOffSetPos.z; 
   //Turn the Tower of the Tank 

   if (Right) 
   { 
      if (TurningAngle <= MAX_ANGLE) 
      { 
         TurningAngle += ANGLE_RATE; 
      } 
      else 
         TurningAngle = MAX_ANGLE; 
   } 
   if (Left) 
   { 
      if (TurningAngle >= -MAX_ANGLE) 
      { 
         TurningAngle += -ANGLE_RATE; 
      } 
      else 
         TurningAngle = -MAX_ANGLE; 
   } 
} 
/******************************************************************** 
* render() 
********************************************************************/ 
void TankTower::render(LPDIRECT3DDEVICE9 device) 
{ 
   D3DXMatrixTranslation(&transMatrix, positionVector.x, positionVector.y, positionVector.z); 
   // Turn the TankTower to a steering angle 
   D3DXMatrixRotationY(&rotMatrix,D3DXToRadian(TurningAngle)); 
   D3DXMatrixScaling(&scaleMatrix, size, size, size); 
   D3DXMatrixMultiply(&transMatrix, &rotMatrix, &transMatrix); 
   D3DXMatrixMultiply(&transMatrix, &scaleMatrix, &transMatrix);    
   // multiply the TankTower matrix by the Tank matrix to set it relative to the Tank 
   D3DXMatrixMultiply(&finalMatrix, &transMatrix, &pTank->transMatrix); 
   device->SetTransform(D3DTS_WORLD, &finalMatrix); 
   Model->render(device); 
} 
 

Advertisement
Assuming this is in 2D (since your turret orientation is represented by a single angle), you can get the x coord with a cos, and the y with a sin.
it is a 3D game so would that still apply?
direction from A to B is

d = Normalize( B position - A position )
Does the turret rotate in 2D or 3D? As long as it just rotates in the XZ (or whatever) plane, it should work fine. Just substitute z for y in my earlier post.

Heres how it works in the D3D coord system:

This topic is closed to new replies.

Advertisement