How to know where the player is the most closed to 5 point?
#1

Hello, i have 5 points in my map;
* one in San Fierro
* one in Los Santos
* one in Las Venturas
* one in The Desert
* one in The Middle Of Nowhere

How to know where a player is the closest to?
Reply
#2

Use PlayerToPoint.
Reply
#3

Quote:
Originally Posted by MenaceX^
Use PlayerToPoint.
That doesn't make any sence, because if i do that it just brink me to the point i first scripted, not the closest to.
Reply
#4

It's a bit hard to explain with words, so i will just post basically the method i use in my gamemode:

Define points at the top of script:
pawn Код:
enum
{
  INVALID_POINT = -1,
  POINT_1,
  POINT_2,
  POINT_3
}

static Float:Points[][] =
{
  { 10.0, 10.0, 10.0 }, //POINT_1
  { 0.0, 0.0, 0.0 }, //POINT_2
  { -10.0, -10.0, -10.0 } //etc
};
Then, the main function, which return an index of the Points array, and you can get the distance in 'd':
pawn Код:
GetClosestPoint(Float:x, Float:y, Float:z, &Float:d = 0.0)
{
  new
    c = INVALID_POINT,
    Float:x2,
    Float:y2,
    Float:z2,
    Float:d2 = 70000.0
  ;

  for (new p = 0; p < sizeof Points; p ++)
  {
    x2 = x - Points[p][0];
    x2 *= x2;
       
    y2 = y - Points[p][1];
    y2 *= y2;
       
    z2 = z - Points[p][2];
    z2 *= z2;
       
    d = floatsqroot(x2 + y2 + z2);

    if (d < d2)
    {
      d2 = d;
      c = p;
    }
  }

  d = d2;

  return c;
}
pawn Код:
GetPlayerClosestPoint(playerid)
{
  new
    Float:x,
    Float:y,
    Float:z
  ;

  GetPlayerPos(playerid, x, y, z);

  return GetClosestPoint(x, y, z);
}
So basically now all you have to do is:
pawn Код:
switch (GetPlayerClosestPoint(playerid))
{
  case POINT_1 : //bla
  case POINT_2 : //etc
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)