PDA

View Full Version : Get The Closest Dynamic Object?


Tommy_Mandaz
21/06/2011, 06:12 PM
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.

Sasino97
21/06/2011, 06:45 PM
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;
}

Babul
21/06/2011, 06:51 PM
maybe setting the...

native Streamer_VisibleItems(type, items);

.. to 1 and loop thourgh all objects if its visible? hm...
edit: nevermind >-<

Tommy_Mandaz
21/06/2011, 07:30 PM
Sasino it didn't work :( anyone help me please?

Tommy_Mandaz
25/06/2011, 06:03 PM
Bump? Anyone?

Mauzen
25/06/2011, 06:04 PM
Use one of all the GetClosest... algorithms posted all over the forum and use GetDynamicObjectPos in it

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

MyLife
25/06/2011, 06:29 PM
Try this.

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)+floatpow er(floatabs(floatsub(pz,oz)),2));
if(dist < result)
{
result = dist;
return o;
}
}
return 1;
}


Untested.

Tommy_Mandaz
25/06/2011, 06:32 PM
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.

MyLife
25/06/2011, 06:38 PM
Get distance between object ?

stock GetDistnaceBetweenObject(playerid,objectid)
{
new Float:px,Float:py,Float:pz,
Float:ox,Float:oy,Float:oz;
GetPlayerPos(playerid,px,py,pz);
GetDynamicObjectPos(objectid,ox,oy,oz);
return floatsqroot(floatpower(floatabs(floatsub(px,ox)),2 )+floatpower(floatabs(floatsub(py,oy)),2)+floatpow er(floatabs(floatsub(pz,oz)),2));

}

Tommy_Mandaz
25/06/2011, 06:47 PM
Sent you a pm about the error...

Sasino97
25/06/2011, 07:57 PM
Try this.

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)+floatpow er(floatabs(floatsub(pz,oz)),2));
if(dist < result)
{
result = dist;
return o;
}
}
return 1;
}


Untested.

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.

-.-

new objectid = GetClosestDynamicObject(playerid);

Tommy_Mandaz
25/06/2011, 09:47 PM
Not working here is what I did:

CMD:pickupcone(playerid, params[])
{
new string[128], sendername[MAX_PLAYER_NAME];
if(gPlayerInfo[playerid][pFaction] == 1)
{
if(ItemHoldingCone[playerid] == 0)
{
new objectid = GetClosestDynamicObject(playerid);
if(objectid == 1238)
{
ItemHoldingCone[playerid] = 1;
RemovePlayerAttachedObject(playerid, 0);
DestroyDynamicObject(objectid);
SetPlayerAttachedObject( playerid, 0, 1238, 6 );
GetPlayerName(playerid, sendername, sizeof(sendername));
Fmsg("%s reaches down and picks up a traffic cone", sendername);
SLM(playerid, string, 15, COLOR_PURPLE, COLOR_PURPLE);
}
}
}
return 1;
}

Sasino97
25/06/2011, 10:05 PM
Not working here is what I did:

CMD:pickupcone(playerid, params[])
{
new string[128], sendername[MAX_PLAYER_NAME];
if(gPlayerInfo[playerid][pFaction] == 1)
{
if(ItemHoldingCone[playerid] == 0)
{
new objectid = GetClosestDynamicObject(playerid);
if(objectid == 1238)
{
ItemHoldingCone[playerid] = 1;
RemovePlayerAttachedObject(playerid, 0);
DestroyDynamicObject(objectid);
SetPlayerAttachedObject( playerid, 0, 1238, 6 );
GetPlayerName(playerid, sendername, sizeof(sendername));
Fmsg("%s reaches down and picks up a traffic cone", sendername);
SLM(playerid, string, 15, COLOR_PURPLE, COLOR_PURPLE);
}
}
}
return 1;
}


Because it returns the ID of the object, not the model ....


CMD:pickupcone(playerid, params[])
{
new string[128], sendername[MAX_PLAYER_NAME];
if(gPlayerInfo[playerid][pFaction] == 1)
{
if(ItemHoldingCone[playerid] == 0)
{
new objectid = GetClosestDynamicObject(playerid);
if(GetDynamicObjectModel(objectid) == 1238)
{
ItemHoldingCone[playerid] = 1;
RemovePlayerAttachedObject(playerid, 0);
DestroyDynamicObject(objectid);
SetPlayerAttachedObject( playerid, 0, 1238, 6 );
GetPlayerName(playerid, sendername, sizeof(sendername));
Fmsg("%s reaches down and picks up a traffic cone", sendername);
SLM(playerid, string, 15, COLOR_PURPLE, COLOR_PURPLE);
}
}
}
return 1;
}

langricr
25/06/2011, 10:14 PM
stock GetClosestDynamicObject(playerid)
{
new px, py, pz, currentobject = -1, Float:distance = -1;
GetPlayerPos( playerid, px, py, pz );

for( new index = 0; index < CountDynamicObjects(); index++ )
{
if ( !IsValidDynamicObject( index ) )
continue;

Float:ox, Float:oy, Float:oz;
GetDynamicObjectPos( index, ox, oy, oz );

new Float:odist = floatsqroot(
floatpower( floatabs( floatsub( ox, px ) ), 2.0 ) +
floatpower( floatabs( floatsub( oy, py ) ), 2.0 ) +
floatpower( floatabs( floatsub( oz, pz ) ), 2.0 )
);

if ( currentobject == -1 )
{
currentobject = index;
distance = odist;
} else if ( odist < distance )
{
currentobject = index;
distance = odist;
}
}

return currentobject;
}

Edit: Just woke up, had to modify it a bit.

Tommy_Mandaz
25/06/2011, 10:15 PM
But there is no such thing as GetDynamicObjectModel, I would just get a error... Tested I got the error :/.

gangsterm
19/04/2012, 07:53 PM
try tthis 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)+floatpow er(floatabs(floatsub(pz,oz)),2));
if(dist < result)
{
result = dist;
return o;
}
}
return 1;

[Diablo]
19/04/2012, 07:58 PM
i remember topaz wrote an include for GetNearest() stuff, i'm not sure how accurate that is, but you can check the base of it - maybe it will give you an idea.

here it is (http://pastebin.com/eZDnUab1)

Jay_
19/04/2012, 09:16 PM
floatsqroot in PAWN is incredibly slow!
Considering there are no limits on the number of Dynamic objects, performing intense calculations such as the ones mentioned in this topic could be incredibly inefficient.

Writing a function for this is never going to be a small task for the server to execute, but at least use the SA-MP native IsPlayerInRangeOfPoint. Unfortunately however using this will limit the function to playerids.

Sid_Alexander
02/01/2015, 03:22 PM
CMD:yourcommandhere(playerid, params[])
{
new Float:ox, Float:oy, Float:oz;
GetDynamicObjectPos(objectid, ox, oy, oz);
if(IsPlayerInRangeOfPoint(playerid, 3.0, ox, oy, oz))
{
//do something
}
return 1;
}