get closest vehicle near me, but not my vehicle
#1

hello, im having troubles getting the closest vehicle NEAR ME (So dont check all cars in server), that is not my own vehicle!

can u help me out!
Reply
#2

what ya got so far coz you need to loop all the cars and check
and what is the value for your max cars
Reply
#3

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;
}
Just added a new conditional for your vehicle varible / array, just replace with what tells your script it's your vehicle.
Reply
#4

how this

Код:
stock FindClosestVehicle(playerid){
	new	Float:x, Float:y, Float:z,
	new closest = 100000;
	new closestid;
	for(new a = 0; a < MAX_VEHICLES; a++){
		GetVehiclePos(a, x, y, z);
		if(GetPlayerDistanceToPointEx(playerid,x,y,z) < closest  && GetPlayerVehicleID(playerid) != a)
		{
			closestid = a;
			closest = GetPlayerDistanceToPointEx(playerid,x,y,z);
		}
	}
	if(closest < 100000) return closestid;

	return INVALID_VEHICLE_ID;
}

stock GetPlayerDistanceToPointEx(playerid,Float:x,Float:y,Float:z) {
	new Float:x1,Float:y1,Float:z1;
	new Float:dis;
	GetPlayerPos(playerid,x1,y1,z1);
	dis = floatsqroot((x-x1)*(x-x1)+(y-y1)*(y-y1)+(z-z1)*(z-z1));
	return floatround(dis);
}
Reply
#5

IsPlayerInRangeOfPoint is faster than the function you supplied (due to IsPlayerInRangeOfPoint not using Floatsqroot, I think)

So my example would be much faster and without a doubt less messy.

And I believe the IsPlayerInRangeOfPoint code doesn't even use a the float functions ( I think ****** made IPIROP.. It's on his code optimizations thread)
Reply
#6

I use this like:
pawn Код:
public GetClosestVehicle(carid)
{
    new x,Float:dis,Float:dis2,car;
    car = 0;
    dis = 99999.99;
    for ( x = 0; x < MAX_VEHICLES; x++ )
    {
        if(x != carid)
        {
            dis2 = GetDistanceBetweenVehicles(x,carid);
            if(dis2 < dis && dis2 < 8.0)
            {
                dis = dis2;
                car = x;
            }
        }
    }
    return car;
}

public Float:GetDistanceBetweenVehicles(vehicleid,carid)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    GetVehiclePos(vehicleid,x1,y1,z1);
    GetVehiclePos(carid,x2,y2,z2);
    return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
Reply
#7

Your code is just doing what my code is, just slower :/..

Unless there's something to your code, which I aint found ?
Reply
#8

Quote:
Originally Posted by Joe_
Посмотреть сообщение
Your code is just doing what my code is, just slower :/..

Unless there's something to your code, which I aint found ?
what is faster 10ml seconds or 40ml seconds?
Reply
#9

4948 ms is faster than 26658 ms (10,000 iterations for each function)

Yours is the 2nd time.

I blame all of the float functions, when you can just use raw math.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)