Closest Dynamic Object
#1

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

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);
Reply
#3

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.
Reply
#4

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
Reply
#5

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

Come on! rookie mistake
Reply
#6

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

Ye already ahead of that, fixed the rest aswell.

Needed to add a new on Floatx, Floaty, Floatz;
Reply
#8

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.
Reply
#9

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


Forum Jump:


Users browsing this thread: 3 Guest(s)