GetDistance Problem -
Sascha - 14.03.2011
Ok so..
I've created a /work cmd that creates a checkpoint at the nearest shop..
this is at my cmd:
pawn Код:
for(new n=0; n<sizeof(WorkStations); n++)
{
number++;
x = WorkStations[n][0];
y = WorkStations[n][1];
z = WorkStations[n][2];
cdist = GetDistanceToPoint(playerid, x, y, z);
if(number == 0)
{
distance = cdist;
nearest = n;
}
else if(distance > cdist)
{
distance = cdist;
nearest = n;
}
}
and that's the stock:
pawn Код:
const Float:INF_FLOAT=Float:0x7F800000;
stock Float:GetDistanceBetweenPoints(Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2)
{
return floatadd(floatadd(floatsqroot(floatpower(floatsub(x1,x2),2)),floatsqroot(floatpower(floatsub(y1,y2),2))),floatsqroot(floatpower(floatsub(z1,z2),2)));
}
stock Float:GetDistanceToPoint(playerid,Float:x1,Float:y1,Float:z1)
{
if (IsPlayerConnected(playerid))
{
new Float:x2,Float:y2,Float:z2;
GetPlayerPos(playerid,x2,y2,z2);
return GetDistanceBetweenPoints(x1,y1,z1,x2,y2,z2);
}
return INF_FLOAT;
}
however it always creates the checkpoint at the first coordinate in my "WorkingStation" list...
what's wrong?
Re: GetDistance Problem -
Mauzen - 14.03.2011
You mixed the GetDistanceBetweenPoints formula up a bit
pawn Код:
floatadd(floatadd(floatsqroot(floatpower(floatsub(x1,x2),2)),floatsqroot(floatpower(floatsub(y1,y2),2))),floatsqroot(floatpower(floatsub(z1,z2),2)));
// floatsqroot( floatpower ( a, 2 ) ) = a again
// So this is just (x1 - x2 + y1 - y2 + z1 - z2)
It should look like this ( with using * instead of floatpower, becuase it is faster)
pawn Код:
floatsqroot( (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2) )
Re: GetDistance Problem -
Sascha - 15.03.2011
so... now I've got this and it still doesn't work...
pawn Код:
stock Float:PointToPoint(Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2)
{
return floatsqroot( (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2) );
}
pawn Код:
dcmd_work(playerid, params[])
{
if(!strlen(params))
{
new Float:x1, Float:x2, Float:y1, Float:y2, Float:z1, Float:z2;
new Float:distance, nearest, Float:cdist, number;
if(IsPlayerInAnyVehicle(playerid)) GetVehiclePos(GetPlayerVehicleID(playerid), x1, y1, z1);
else GetPlayerPos(playerid, x1, y1, z1);
number = 0;
for(new n=0; n<sizeof(WorkStations); n++)
{
number++;
x2 = WorkStations[n][0];
y2 = WorkStations[n][1];
z2 = WorkStations[n][2];
cdist = PointToPoint(x1, y1, z1, x2, y2, z2);
if(number == 0)
{
distance = cdist;
nearest = n;
}
if(distance > cdist)
{
distance = cdist;
nearest = n;
}
}
SetPlayerCheckpoint(playerid, WorkStations[nearest][0], WorkStations[nearest][1], WorkStations[nearest][2], 2.00);
SendClientMessage(playerid, YELLOW, "*Drive to the checkpoint and walk into the \"Money Icon\"");
}
return 1;
}
and it still always shows the first location
AW: Re: GetDistance Problem -
Nero_3D - 15.03.2011
Can a distance be smaller than 0 ? No -> check your distance declaration
And read ******'s post again if you want to improve your code
Re: GetDistance Problem -
Sascha - 15.03.2011
oh feck... lol thanks I didn't notice...
I should've defined the distance before increasing the "number"...
thanks for this "hidden" correction:P