Public Group

getting the direction vector

This topic is 4324 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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
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);
}



Share on other sites
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.

Share on other sites
it is a 3D game so would that still apply?

Share on other sites
direction from A to B is

d = Normalize( B position - A position )

Share on other sites
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:

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• Forum Statistics

• Total Topics
633732
• Total Posts
3013584
×