Log in

View Full Version : Walking to the closest Point?



Mat
03-21-2012, 07:05 AM
I have say 10 SPS Points and the character spawns around the whole area, how would I have it walk to the closest one to it?
Point (1)

Point(2)
You(2.7)
Point(3)
you'd want it to go to three, is this possible?
Thanks
Mat

Flight
03-21-2012, 07:39 AM
Gimme a second and I'll whip something up for ya.

Edit:
Give this a whirl:

Function SPS_GetClosestPoint(PArr: TPointArray): TPoint;
Var
Me: TPoint;
begin
Me := SPS_GetMyPos;
SortTPAFrom(PArr, Me);
Result := PArr[0];
end;

Daniel
03-21-2012, 07:44 AM
Gimme a second and I'll whip something up for ya.

One second is well over.

Flight
03-21-2012, 07:47 AM
One second is well over.

Atleast it wouldn't take me an hour. :garfield: I'm gonna get on the IRC, we can fight there noob.

Mat
03-21-2012, 01:18 PM
Gimme a second and I'll whip something up for ya.

Edit:
Give this a whirl:

Function SPS_GetClosestPoint(PArr: TPointArray): TPoint;
Var
Me: TPoint;
begin
Me := SPS_GetMyPos;
SortTPAFrom(PArr, Me);
Result := PArr[0];
end;

Can you expalin how it works? please <3

Er1k
03-21-2012, 01:40 PM
SortTPAFrom returns the sorted TPointArray, closest to your position first. Array[0] is the answer to your question

Flight
03-21-2012, 01:51 PM
SortTPAFrom returns the sorted TPointArray, closest to your position first. Array[0] is the answer to your question

Yep pretty much.

First, we define our own player's position on the MM (Me := SPS_GetMyPos), this will be a TPoint. Next, we use the nifty little procedure "SortTPAFrom" which has 2 parameters: the first is the given TPointArray. Two steps are done here, first you feed your TPA into the procedure and in that same parameter your TPA will be given back to you, but in a sorted (by distance) form. The second parameter is the point you want to start the sorting from. In this case we use our player's location (Me).

At this point, our TPointArray has been sorted by distance from our player. And as you know, every array starts at 0, with 0 being the first in the array. So... PArr[0] must be the closest point to our player because its first on our sorted array. The second closest point would be PArr[1], third would be PArr[2], ect... You get the idea. ;)

Sin
03-21-2012, 01:54 PM
Woo, thanks Flight, unknowingly you solved the final part of my puzzle in SPS walking for my Frost Dragons script <3

Mat
03-21-2012, 02:56 PM
Sweet thanks flight this is gonna come in handy for changing the way my abyss script works <3

Flight
03-21-2012, 04:15 PM
Sweet thanks flight this is gonna come in handy for changing the way my abyss script works <3

I look forward to seeing that. I noticed you removed it from your thread so I posted a reply as why you removed it. I was hoping to use it and perhaps help you speed it up where possible.

Mat
03-21-2012, 07:20 PM
I look forward to seeing that. I noticed you removed it from your thread so I posted a reply as why you removed it. I was hoping to use it and perhaps help you speed it up where possible.
Sure I'll figure out how to add this function and then re update.

Wyn
03-21-2012, 07:26 PM
Woo, thanks Flight, unknowingly you solved the final part of my puzzle in SPS walking for my Frost Dragons script <3

Don't you release it lol.


Seriously, Don't.

Mat
03-21-2012, 08:02 PM
Lol^^ Flight the main Problem is the object finding inside abyss I am finding them but because I'm having to check for all of them its rather slow, any idea's of speeding it up?
Well Prob not you need to see the code :P