SA-MP Forums Archive
[help] distance between object and player - 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: [help] distance between object and player (/showthread.php?tid=482454)



[help] distance between object and player - OTACON - 21.12.2013

Why this code does not work properly?

pawn Код:
if(DistanceBetweenObjectAndPlayer(playerid) < 3.5){
//this close to the object
}else{
//not close to the object
}
pawn Код:
stock DistanceBetweenObjectAndPlayer(playerid){
    new Float:pos1[3],Float:pos2[3],Float:distance;
    for(new slots=1,idx=MAX_SLOTS;slots!=idx;slots++){
        GetObjectPos(VarObject[slots], pos1[0], pos1[1], pos1[2]);
        GetPlayerPos(playerid, pos2[0], pos2[1], pos2[2]);
        distance = floatsqroot(floatpower(floatabs(floatsub(pos1[0], pos2[0])),2)+floatpower(floatabs(floatsub(pos1[1], pos2[1])),2)+floatpower(floatabs(floatsub(pos1[2], pos2[2])),2));
    } return floatround(distance);
}
How can I do it correctly?


Re: [help] distance between object and player - Ada32 - 21.12.2013

just use https://sampwiki.blast.hk/wiki/GetPlayerDistanceFromPoint


Respuesta: [help] distance between object and player - OTACON - 21.12.2013

I did the following, but only responds to the latest slots.

something I'm doing wrong.

pawn Код:
stock DistanceBetweenObjectAndPlayer(playerid, Float:dist){
    new Float:pos[3],Float:distance;
    for(new slots=1,idx=MAX_SLOTS;slots!=idx;slots++){
        GetObjectPos(VarObject[slots], pos[0], pos[1], pos[2]);
        distance = GetPlayerDistanceFromPoint(playerid ,pos[0], pos[1], pos[2]);}
    if(distance < dist) return true;
    return false;
}

if(DistanceBetweenObjectAndPlayer(playerid, 3.5)){
//this close to the object
}else{
//not close to the object
}



Re: [help] distance between object and player - Ada32 - 21.12.2013

if your trying to get the distance between a single object and the player why are you looping? (hence the function's name?) and the reason what you're trying to do won't work is because you don't check which object is the nearest (3.5) IN the function.

loop, get objects distance from player, check if < 3.5, return objectid


Re: [help] distance between object and player - RajatPawar - 21.12.2013

pawn Код:
stock IsPlayerCloseToAnyObject(playerid, _MAXIMUM_OBJECTS, fProximity = 5.0)
{
    new Float: fpX, Float: fpY, Float: fpZ,
    Float: fOX, Float: fOY, Float: fOZ,
    Float: fDist = 0.0;
   
    GetPlayerPos( playerid, fpX, fpY, fpZ );
   
    for( new i = 0 ; i < _MAXIMUM_OBJECTS; i++ )
    {
       
        if( IsValidObject( i ) )
        {
            GetObjectPos( i, fOX, fOY, fOZ );
           
            fDist = floatsqroot( (floatpower( floatabs((fpX - fOX)), 2) + floatpower( floatabs((fpY - fOY)), 2 ) + floatpower( floatabs((fpZ - fOZ)), 2 )) ) ;
           
            if( fDist < fProximity )
                return i;
            else continue;
        }
    }
    return INVALID_OBJECT_ID;
}
I just made this right now, try if it works!
pawn Код:
if( IsValidObject( IsPlayerCloseToAnyObject(playerid, MAX_OBJECTS, 5.7) ) )



AW: [help] distance between object and player - Nero_3D - 21.12.2013

If you just want to check if one object is in range of the player
pawn Код:
IsPlayerInRangeOfObject(playerid, objectid, Float: range) {
    new
        Float: X,
        Float: Y,
        Float: Z
    ;
    return GetObjectPos(objectid, X, Y, Z) && IsPlayerInRangeOfPoint(playerid, range, X, Y, Z);
}

IsPlayerInRangeOfVarObject(playerid, Float: range, & slot = 1) {
    slot = 1;

    while(slot < MAX_SLOTS) {
        if(IsPlayerInRangeOfObject(playerid, VarObject[slot], range)) {
            return true;
        }
        slot++;
    }
    return false;
}
If you want to get the nearest object and the exact distance
pawn Код:
Float: GetPlayerDistanceFromObject(playerid, objectid) {
    new
        Float: X,
        Float: Y,
        Float: Z
    ;
    if(GetObjectPos(objectid, X, Y, Z)) {
        return GetPlayerDistanceFromPoint(playerid, X, Y, Z);
    }
    return 0.0;
}

GetClosestVarObjectFromPlayer(playerid, & Float: distance = 0.0) {
    new
        slot = 1,
        Float: tmp,
        object = slot
    ;
    distance = GetPlayerDistanceFromObject(playerid, VarObject[object])

    while(++slot < MAX_SLOTS) {
        tmp = GetPlayerDistanceFromObject(playerid, VarObject[slot]);

        if(tmp < distance) {
            object = slot;
            distance = tmp;
        }
    }
    return object;
}