|
||
|
|||||||
| Script Support Get support for modifying RunUO Scripts, or writing your own! |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Forum Expert
|
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 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 |
|
|
|
|
|
#2 (permalink) |
|
Forum Expert
Join Date: Nov 2003
Location: Illinois, USA
Age: 22
Posts: 2,911
|
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;
}
__________________
Useful links (Use them or die in a fire!!!): Ultimate Little Guide, C# Tutorials & Docs, RunUO Basic Scripts, Run UO How to..., Configure server for connections, Scripting for Dummies, Common Problem Solutions, FAQ Forum, RunUO Wiki, Basic Generics, Xml Tutorial |
|
|
|
|
|
#3 (permalink) |
|
Forum Expert
|
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 |
|
|
|
|
|
#4 (permalink) |
|
Forum Expert
|
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();
}
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();
}
__________________
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. |
|
|
|
|
|
#5 (permalink) |
|
Forum Expert
|
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|