Get ID of vehicle in front -
CaveDweller - 07.06.2014
When a player is driving a vehicle, how can I get the ID of the vehicle in front of them within a distance of 50m (if there is a vehicle within 50m).
Thanks
Re: Get ID of vehicle in front -
Faisal_khan - 07.06.2014
https://sampforum.blast.hk/showthread.php?tid=517997
pawn Код:
stock FindClosestVehicle(playerid)
{
new
Float:x, Float:y, Float:z,
closest = -1;
for(new a = 0; a < MAX_VEHICLES; a++)
{
GetVehiclePos(a, x, y, z);
if(a != YOUR_VEHICLEID_OR_ARRAY/VARIBLE && IsPlayerInRangeOfPoint(playerid, 10.0, x, y, z))
{
closest = a;
}
}
if(closest != -1) return closest;
return INVALID_VEHICLE_ID;
}
Re: Get ID of vehicle in front -
CaveDweller - 07.06.2014
Thanks, but I don't want the closest vehicle. I want the closest vehicle that is in front of the given vehicle. The purpose is for players in police cars to be able to "read" the license plate of the vehicle infront.
Re: Get ID of vehicle in front -
Vince - 07.06.2014
Except that will check vehicles in all directions. The OP specifically asks for the front (speed radar?). The way I'd do it would be with the streamer plugin. You need to calculate these 3 points (A, B and C) with trigonometry as the input for CreateDynamicPolygon. Then use a similar loop as demonstrated above, while filtering out any vehicles not in the zone.
Re: Get ID of vehicle in front -
CaveDweller - 07.06.2014
Quote:
Originally Posted by Vince
Except that will check vehicles in all directions. The OP specifically asks for the front (speed radar?). The way I'd do it would be with the streamer plugin. You need to calculate these 3 points (A, B and C) with trigonometry as the input for CreateDynamicPolygon. Then use a similar loop as demonstrated above, while filtering out any vehicles not in the zone.
[i m g]http://puu.sh/9jovP/fa64a6d157.png[/img]
|
Thanks, Vince. However, trigonometry isn't exactly my strong point so I'd appreciate it if you could point me in the right direction here.

If I know point A, how can I work out points B, C and D (where line AC is exactly parallel with the sides of the car) based on the rotation of the car?
Re: Get ID of vehicle in front -
Threshold - 07.06.2014
pawn Код:
new Float:x, y, a;
new vid = GetPlayerVehicleID(playerid);
GetVehiclePos(vid, x, y, a);
GetVehicleZAngle(vid, a);
Point C:
x += (50.0 * floatsin(-a, degrees));
y += (50.0 * floatcos(-a, degrees));
Point B:
x += (50.0 * floatsin(-(a - 15), degrees));
y += (50.0 * floatcos(-(a - 15), degrees));
Point D:
x += (50.0 * floatsin(-(a + 15), degrees));
y += (50.0 * floatcos(-(a + 15), degrees));
That's what I would do.
EDIT: Alternatively, you can add a 'Z' coord check to see whether the other vehicle is within a range of 10 or so units in height.
Re: Get ID of vehicle in front -
CaveDweller - 10.06.2014
Thanks guys, it works great!
https://www.youtube.com/watch?v=K9jM3UopcIM