Game Development Community

Distance between 2 objects (function included)

by Gavin Beard · in Torque Game Builder · 02/15/2009 (5:33 pm) · 4 replies

Hi all

I had spent a little time trying to work out the distance between 2 objects in a scene to find the closest object to a 3rd object, I couldnt find any function in TGB so knocked one up and thought i'd share incase anyone else needed it, It seems quick enough and is certainly small enough

Hope someone finds it useful:

function checkDistance(%obj1, %obj2)
{
   %dx = %obj1.getPosition().x - %obj2.getPosition().x;
   %dy = %obj1.getPosition().y - %obj2.getPosition().y;
   %dist = mSqrt(%dx*%dx + %dy*%dy); 
   return %dist;   
}

#1
02/15/2009 (5:43 pm)
Alternatively:

function checkDistance(%obj1, %obj2)
{
   return t2dVectorDistance(%obj1.Position, %obj2.Position);  
}
#2
02/16/2009 (3:15 am)
I completely missed that in the docs, rendring my code pointless :-)
#3
03/01/2009 (8:29 pm)
hehe... i used the code Phillip put up here for a Homming Missile about 18 months ago... i had to fiddle a lot with the math functions that comes with TS, and when i finally got it, was amazed of how much time they can save you... and since they're included in the TS, they are faster than tryin to hack your way into some things (sometimes).
#4
03/02/2009 (12:27 am)
Yeah,
Completely missed t2dVectorDistance() in the Docs which was why i created my own in script.