SA-MP Forums Archive
Get The Closest Dynamic Object? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Get The Closest Dynamic Object? (/showthread.php?tid=263363)



Get The Closest Dynamic Object? - Tommy_Mandaz - 21.06.2011

Hello, im using Incognito Streamer and I wanted to know of a way to get the closest dynamicobject to a player, is this possible? Thanks.


Re: Get The Closest Dynamic Object? - Sasino97 - 21.06.2011

pawn Code:
for(new i=0;i<MAX_DYNAMIC_OBJECTS;i++)
{
  new Float:oX, Float:oY, Float:oZ;
  GetDynamicObjectPos(oX, oY, oZ);
  if(IsPlayerInRangeOfPoint(playerid, oX, oY, oZ))
  {
    //Do the things
    break;
  }
  continue;
}



Re: Get The Closest Dynamic Object? - Babul - 21.06.2011

maybe setting the...
Code:
native Streamer_VisibleItems(type, items);
.. to 1 and loop thourgh all objects if its visible? hm...
edit: nevermind >-<


Re: Get The Closest Dynamic Object? - Tommy_Mandaz - 21.06.2011

Sasino it didn't work anyone help me please?


Re: Get The Closest Dynamic Object? - Tommy_Mandaz - 25.06.2011

Bump? Anyone?


Re: Get The Closest Dynamic Object? - Mauzen - 25.06.2011

Use one of all the GetClosest... algorithms posted all over the forum and use GetDynamicObjectPos in it


Re: Get The Closest Dynamic Object? - Tommy_Mandaz - 25.06.2011

Tried searching about getting closest object, seems my search isn't finding anything :/. Maybe im searching the wrong tag?


Re: Get The Closest Dynamic Object? - MyLife - 25.06.2011

Try this.
pawn Code:
stock GetClosestDynamicObject(playerid)
{
    new Float:px,Float:py,Float:pz,
    Float:ox,Float:oy,Float:oz,Float:dist,result = 12345;
    GetPlayerPos(playerid,px,py,pz);
    for(new o; o < CountDynamicObjects(); o++)
    {
        GetDynamicObjectPos(o,ox,oy,oz);
        dist = floatsqroot(floatpower(floatabs(floatsub(px,ox)),2)+floatpower(floatabs(floatsub(py,oy)),2)+floatpower(floatabs(floatsub(pz,oz)),2));
        if(dist < result)
        {
            result = dist;
            return o;
        }
    }
    return 1;
}
Untested.


Re: Get The Closest Dynamic Object? - Tommy_Mandaz - 25.06.2011

Is there a way to add the object id? So like GetClosestDynamicObject(playerid, objectid); or something? So that way I can find out which object is the closest.


Re: Get The Closest Dynamic Object? - Sasino97 - 25.06.2011

Quote:
Originally Posted by MyLife
View Post
Try this.
pawn Code:
stock GetClosestDynamicObject(playerid)
{
    new Float:px,Float:py,Float:pz,
    Float:ox,Float:oy,Float:oz,Float:dist,result = 12345;
    GetPlayerPos(playerid,px,py,pz);
    for(new o; o < CountDynamicObjects(); o++)
    {
        GetDynamicObjectPos(o,ox,oy,oz);
        dist = floatsqroot(floatpower(floatabs(floatsub(px,ox)),2)+floatpower(floatabs(floatsub(py,oy)),2)+floatpower(floatabs(floatsub(pz,oz)),2));
        if(dist < result)
        {
            result = dist;
            return o;
        }
    }
    return 1;
}
Untested.
Quote:
Originally Posted by Tommy_Mandaz
View Post
Is there a way to add the object id? So like GetClosestDynamicObject(playerid, objectid); or something? So that way I can find out which object is the closest.
-.-

pawn Code:
new objectid = GetClosestDynamicObject(playerid);