SA-MP Forums Archive
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: Closest Dynamic Object (/showthread.php?tid=311743)



Closest Dynamic Object - Oh - 18.01.2012

Is there a way to get the closest dynamic object near you?


Re: Closest Dynamic Object - T0pAz - 18.01.2012

pawn Код:
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;
}
Example
pawn Код:
new objectid = GetClosestDynamicObject(playerid);



Re: Closest Dynamic Object - T0pAz - 18.01.2012

Simplified version
pawn Код:
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: It will return the closest object id.


Re: Closest Dynamic Object - Oh - 18.01.2012

Quote:
Originally Posted by T0pAz
Посмотреть сообщение
Simplified version
pawn Код:
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: It will return the closest object id.
Thanks, +repped.

GetPlayerPos(playerid, px, py, pz);
warning 213: tag mismatch


Re: Closest Dynamic Object - PrawkC - 18.01.2012

new px, py, pz,
to
new Float: px, Float: py, Float: pz,

Come on! rookie mistake


Re: Closest Dynamic Object - T0pAz - 18.01.2012

oops sorry about that
pawn Код:
stock GetClosestDynamicObject(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;

        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;
}



Re: Closest Dynamic Object - Oh - 18.01.2012

Ye already ahead of that, fixed the rest aswell.

Needed to add a new on Floatx, Floaty, Floatz;


Re: Closest Dynamic Object - T0pAz - 18.01.2012

Quote:
Originally Posted by Oh
Посмотреть сообщение
Ye already ahead of that, fixed the rest aswell.

Needed to add a new on Floatx, Floaty, Floatz;
I was in quick mode.


Re: Closest Dynamic Object - Oh - 18.01.2012

I owe you big time, if you ever need anything hit me up.