Game Development Community

Quick Way to Get Vector Distance?

by John Kanalakis · in Torque X 2D · 10/21/2007 (6:04 pm) · 1 replies

Is there a quick way to get the distance between to vector points? TGB has t2dVectorDistance and it seems like the T2DVectorUtil class would be a great place stick this. I'm able to get by with my own Pythagorian check for now, but I'm wondering if there's a faster way. Currently, I'm calculating this...

homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/BASICMAT/img24.gif
by coding this...

float xDist = System.Math.Abs(_sceneObject.Position.X - _targetObject.Position.X);
float yDist = System.Math.Abs(_sceneObject.Position.Y - _targetObject.Position.Y);
double Distance = System.Math.Sqrt((xDist * xDist) + (yDist * yDist));

John K.

About the author

John Kanalakis is the owner of EnvyGames, an independent game development studio in Silicon Valley that produces games and tools for Xbox 360, Windows, and the Web.


#1
10/21/2007 (11:42 pm)
Ouch... I found this hiding in plain sight... a static Distance method hanging off of the Vector2 class. In fact, there's a goldmine of great vector math functions. Now, I can replace the code above with one line of code:

float Distance = Vector2.Distance( _sceneObject.Position, _targetObject.Position);

John K.