17.02.2013, 21:24
I found this function on the forums, but it doesn't seem to work for me. I couldn't figure out what the problem was in the code. However, instead of the function returning the NEAREST object, it returns the FARTHEST object.
Ideas...?
pawn Код:
stock GetNearestDynObject(playerid)
{
new
Float: px, Float: py, Float: pz,
currentobject = -1,
Float:distance = -1
;
GetPlayerPos( playerid, px, py, pz );
for( new index = 0; index < CountDynamicObjects(); index++ )
{
if ( !IsValidDynamicObject( index ) ) continue;
new
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;
}