0

So here's my scenario: I have a spaceship game, each spaceship can target another ship and fire torpedoes and the torpedo needs a directional vector to travel along, the vector needs to be independent of the distance.

I'm looking to create a method that returns a D3DXVECTOR3 constructed like so:

D3DXVECTOR3 TargetVector(D3DXVECTOR3 *target, D3DXVECTOR3 *firer)

Has anyone got experience in this matter? It would be great if anyone could even point me towards any decent, easy to understand D3D mathematics tutorials as all I have found so far is based on rendering images rather than mathematical equations.

Thanks!

-Ryan

TotalJargon
  • 147
  • 2
  • 2
  • 14

1 Answers1

2

If you want a direction vector that starts from firer and points at target just subtract firer from target ie :

direction = target - firer;

There is D3DXVec3Subtract for it in D3DX lib.

If you want to have a unit normal, which is a vector that has a length of 1 then normalize it with D3DXVec3Normalize.

So you will have :

D3DXVECTOR3 TargetVector(D3DXVECTOR3 *target, D3DXVECTOR3 *firer)
{
    D3DXVECTOR3 direction;
    D3DXVec3Subtract(&direction, target, firer);

    D3DXVec3Normalize(&direction, &direction);
    return direction;
}
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
  • Built without the ' & ' on target and firer, and the normalization will allow me to translate the torpedo like: ' torpedo[i].posVec3 += (torpedo[i].directionVec * torpedo[i].speed); ' right? – TotalJargon Apr 11 '13 at 15:59
  • Yes assuming the `directionVec` has the right operator to multiply with a scalar. – Barış Uşaklı Apr 11 '13 at 16:00
  • directionVec is a D3DXVECTOR3, so I'm guessing that's a no-no? Also how to I use the code highlights in comments? It's been bugging me for a while now xD – TotalJargon Apr 11 '13 at 16:03
  • Just use ` around the word to quote. There is probably a method in D3DX to multiply a vector and scalar. http://msdn.microsoft.com/en-us/library/windows/desktop/bb205518(v=vs.85).aspx – Barış Uşaklı Apr 11 '13 at 16:05
  • Consider moving to [DirectXMath](http://blogs.msdn.com/b/chuckw/archive/2012/03/27/introducing-directxmath.aspx) rather than continuing to use the legacy D3DX math library. You could also use [XNAMath](http://blogs.msdn.com/b/chuckw/archive/2011/02/23/xna-math-version-2-04.aspx) if you are not yet using the Windows 8.x SDK. – Chuck Walbourn Aug 25 '14 at 17:49