Getting nearest vehicle in range - 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: Getting nearest vehicle in range (
/showthread.php?tid=491485)
Getting nearest vehicle in range -
Jimmy0wns - 30.01.2014
I've been trying to make a function that gets the nearest vehicle within 10 meters, so far i only got the nearest vehicle, but don't know how to add the range to it.
Here's the current function:
pawn Код:
stock GetNearestVehicle(playerid)
{
new Float:dist = 1000.0;
new targetid = INVALID_PLAYER_ID;
new Float:x1,Float:y1,Float:z1;
new Float:x2,Float:y2,Float:z2;
new Float:tmpdis;
GetPlayerPos(playerid,x1,y1,z1);
for(new i=0;i<MAX_PLAYERS;i++)
{
if(i == playerid) continue;
GetPlayerPos(i,x2,y2,z2);
tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
if(tmpdis < dist)
{
dist = tmpdis;
targetid = i;
}
}
return targetid;
}
Re: Getting nearest vehicle in range -
AlonzoTorres - 30.01.2014
Why not loop through all vehicles and see if the player is close to any of them? You would just loop through all possible IDs (2000) and then do
https://sampwiki.blast.hk/wiki/GetPlayerDistanceFromPoint with
https://sampwiki.blast.hk/wiki/GetVehiclePos.
Re: Getting nearest vehicle in range -
MP2 - 31.01.2014
pawn Код:
stock GetNearestVehicle(playerid, Float:max_range = 99999.0)
{
new Float:closestDist = max_range;
new closestVehicle = 0;
new Float:thisDist;
new Float:x, Float:y, Float:z;
GetPlayerPos(playerid, x, y, z);
for(new i=1; i<=MAX_VEHICLES; i++)
{
thisDist = GetVehicleDistanceFromPoint(i, x, y, z);
if(thisDist < max_range && thisDist < closestDist)
{
closestDist = thisDist;
closestVehicle = i;
}
}
return closestVehicle;
}
Not tested, but it compiles, and it should work.
If it returns 0 (invalid vehicle ID as they start at 1) it means there's no vehicles within range.
Re: Getting nearest vehicle in range -
Jimmy0wns - 31.01.2014
Quote:
Originally Posted by MP2
pawn Код:
stock GetNearestVehicle(playerid, Float:max_range = 99999.0) { new Float:closestDist = max_range; new closestVehicle = 0; new Float:thisDist; new Float:x, Float:y, Float:z; GetPlayerPos(playerid, x, y, z); for(new i=1; i<=MAX_VEHICLES; i++) { thisDist = GetVehicleDistanceFromPoint(i, x, y, z); if(thisDist < max_range && thisDist < closestDist) { closestDist = thisDist; closestVehicle = i; } } return closestVehicle; }
Not tested, but it compiles, and it should work.
If it returns 0 (invalid vehicle ID as they start at 1) it means there's no vehicles within range.
|
It give's me an invalid vehicle ID (28 in my case) but there are over 10 vehicles in range of it.