Go Back   RunUO - Ultima Online Emulation > RunUO > Script Support

Script Support Get support for modifying RunUO Scripts, or writing your own!

Reply
 
Thread Tools Display Modes
Old 08-06-2008, 11:56 AM   #1 (permalink)
Forum Expert
 
vermillion2083's Avatar
 
Join Date: Jun 2005
Location: Lansing, MI
Age: 25
Posts: 1,016
Send a message via ICQ to vermillion2083 Send a message via MSN to vermillion2083
Default Finding the distance between two mobiles

Good afternoon all,

The distance between two mobiles has been a value that I have found myself needing several times in the past, so I figured its due time I figured it out. I made an attempt using the Pythagorean Theorem but I am not entirely sure if this will function or not (I am at work so I am unable to test it). I've seen several users show their amazing capabilities with mathematic formulas here on the RunUO forums, so I figured I would post my first attempt in hopes that someone will be able to either confirm that this method will function, or lead me in a better direction.

Code:
	public static int CalculateDistance( Mobile from, Mobile target )
	{
		int distance = Math.Sqrt( Math.Pow( from.X - from.Y, 2 ) + Math.Pow( target.X - target.Y, 2 ) );
		return distance;
	}
The formula I was attempting to follow (although it has been so long since I’ve taken a math class only god know how far off I am) can be found here:

The Distance Formula

Any assistance with this method would be greatly appreciated. Thank you all in advance!
__________________
Father Time
Life's journey is not to arrive at the grave safely in a well preserved body, but rather to skid in sideways, totally worn out, shouting "Holy Hell......What a ride!!!"

Server: UO: Extinction
ICQ: 146563794
FatherTime@UOExtinction.com
UO: Extinction homepage
UO: Extinction forum
vermillion2083 is online now   Reply With Quote
Old 08-06-2008, 12:12 PM   #2 (permalink)
Forum Expert
 
mordero's Avatar
 
Join Date: Nov 2003
Location: Illinois, USA
Age: 22
Posts: 2,911
Default

Almost, but not quite. Its:

Code:
    public static int CalculateDistance( Mobile from, Mobile target )
    {
        int distance = Math.Sqrt( Math.Pow( from.X - target.X, 2 ) + Math.Pow( from.Y - target.Y, 2 ) );
        return distance;
    }
mordero is offline   Reply With Quote
Old 08-06-2008, 12:25 PM   #3 (permalink)
Forum Expert
 
vermillion2083's Avatar
 
Join Date: Jun 2005
Location: Lansing, MI
Age: 25
Posts: 1,016
Send a message via ICQ to vermillion2083 Send a message via MSN to vermillion2083
Default

Ahh *smacks his head*, thanks for the catch. It's good to know it's this simple, thanks a million mordero. I'm excited to get home and fiddle around with this and see what I can do with it.
__________________
Father Time
Life's journey is not to arrive at the grave safely in a well preserved body, but rather to skid in sideways, totally worn out, shouting "Holy Hell......What a ride!!!"

Server: UO: Extinction
ICQ: 146563794
FatherTime@UOExtinction.com
UO: Extinction homepage
UO: Extinction forum
vermillion2083 is online now   Reply With Quote
Old 08-07-2008, 05:12 AM   #4 (permalink)
Forum Expert
 
arul's Avatar
 
Join Date: Jan 2005
Location: Hic sunt leones ...
Age: 21
Posts: 1,280
Send a message via MSN to arul
Default

You can use the api:

double distance = mobile1.GetDistanceToSqrt(mobile2);

Anyway, if you want to compare the distance against some fixed value, rather use a power-of-two of the fixed value since square root calculations are quite expensive.

So if the original code looked like:

Code:
    public static int CalculateDistance( Mobile from, Mobile target )
    {
        int distance = Math.Sqrt( Math.Pow( from.X - target.X, 2 ) + Math.Pow( from.Y - target.Y, 2 ) );
        return distance;
    }
....
   if(CalculateDistance( m1, m2 ) > 7)
  {
     DoSomething();
  }
Then the optimized code would look like:

Code:
public static int CalculateDistanceSansSqrt(Mobile from, Mobile target)
{
     int dx= from.X - target.X;
     int dy= from.Y - target.Y;

     return (dx*dx) + (dy*dy);
}

   if(CalculateDistanceSansSqrt( m1, m2 ) > ( 7*7 )/*49*/)
  {
     DoSomething();
  }
Without the square root it turns out to be like 3.5 times faster.
__________________
Angels are falling the very last time, down they're burning in hate and decline, unfaithful and violent we're breaking the spell, we're god, we're scissor, in heaven and hell!

Last edited by arul; 08-07-2008 at 09:41 AM.
arul is offline   Reply With Quote
Old 08-07-2008, 10:40 AM   #5 (permalink)
Forum Expert
 
vermillion2083's Avatar
 
Join Date: Jun 2005
Location: Lansing, MI
Age: 25
Posts: 1,016
Send a message via ICQ to vermillion2083 Send a message via MSN to vermillion2083
Default

Terrific information arul, thank you for the input.
__________________
Father Time
Life's journey is not to arrive at the grave safely in a well preserved body, but rather to skid in sideways, totally worn out, shouting "Holy Hell......What a ride!!!"

Server: UO: Extinction
ICQ: 146563794
FatherTime@UOExtinction.com
UO: Extinction homepage
UO: Extinction forum
vermillion2083 is online now   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5