GPS Calculation - Professional Scripter required -
Trooper[Y] - 05.12.2010
Hey there,
im actually doing a GPS System,more advanced than its ever been.
After i created a net of waypoints all over San Fierro,
i now need to script the gps itself.
I already tried things like this:
pawn Код:
new Float:gpsp[8],tmpoutput3[128];
GetPlayerPos(playerid,gpsp[0],gpsp[1],gpsp[2]);
gpsp[6] = GetDistance(gpsp[0],gpsp[1],gpsp[2],gx,gy,gz);
gpsp[3]=gx-gpsp[0]/(gpsp[6]*50);
gpsp[4]=gy-gpsp[1]/(gpsp[6]*50);
for(new gc=25;gc<=150;gc+=25)
{
format(mysqlquery[playerid],256,"SELECT * FROM streetview_lv WHERE x BETWEEN '%f' AND '%f' && y BETWEEN '%f' AND '%f' ORDER BY x ASC",gpsp[3]-gc,gpsp[3]+gc,gpsp[4]-gc,gpsp[4]+gc);
mysql_query(mysqlquery[playerid]);
mysql_store_result();
if(mysql_fetch_field("x",tmpoutput3)) break;
}
SetPVarFloat(playerid,"knotx",floatstr(tmpoutput3));
mysql_fetch_field("y",tmpoutput3);
SetPVarFloat(playerid,"knoty",floatstr(tmpoutput3));
mysql_fetch_field("z",tmpoutput3);
SetPVarFloat(playerid,"knotz",floatstr(tmpoutput3));
mysql_free_result();
but they aint working.
So, all informations in a few points:
*Given starting point (xyz 1)
*Given target (xyz 2)
*Given waypoints (xyz n)
What i now want to do is to calculate the closest waypoint from the position of the player (starting point) in the direction of the target.
As im not a genious mathematic, and discussed with other good scripter about this more than an hour, and tried it more than 3 hours, i think, i can now try to get an answer from you.
Regards,
Trooper
Re: GPS Calculation - Professional Scripter required -
Think - 05.12.2010
First you need to get the distance between the player and the waypoints, then you loop throught the waypoints and check which one is closest.. this one is for players but will work the same with your GPS system;
pawn Код:
stock Float:GetDistanceBetweenPlayers(p1,p2)
{
new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
if(!IsPlayerConnected(p1) || !IsPlayerConnected(p2))
{
return -1.00;
}
GetPlayerPos(p1,x1,y1,z1);
GetPlayerPos(p2,x2,y2,z2);
return floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
}
forward GetClosestPlayer(p1);
public GetClosestPlayer(p1)
{
new x,Float:dis,Float:dis2,player;
player = -1;
dis = 99999.99;
for (x=0;x<MAX_PLAYERS;x++)
{
if(IsPlayerConnected(x))
{
if(x != p1)
{
dis2 = GetDistanceBetweenPlayers(x,p1);
if(dis2 < dis && dis2 != -1.00)
{
dis = dis2;
player = x;
}
}
}
}
return player;
}
You could edit this to fit your GPS system.. look through the code and you will understand how it works and you can copy it or improve it by yourself..
AW: GPS Calculation - Professional Scripter required -
Trooper[Y] - 05.12.2010
Thanks for responding,
but it shouldґve got clear in the startpost,
that i have really ALOT of waypoints.
If i calculate the distance to everyone of them,
not depending on the distance to whatever,
i can trash the server...
Re: GPS Calculation - Professional Scripter required -
Ash. - 05.12.2010
How many waypoints are there exactly?
AW: GPS Calculation - Professional Scripter required -
Trooper[Y] - 06.12.2010
At the moment ~350, going variabel upwards up to ~1200
MySQL dont have problems with the time, but SA:MP Servers have.
As thats not enough,
your response was not even useful for my question,
as i need the closest waypoint IN A DIRECTION...
Re: GPS Calculation - Professional Scripter required -
RyDeR` - 06.12.2010
Great idea.
Re: GPS Calculation - Professional Scripter required -
randomkid88 - 06.12.2010
You should look into Dijkstra's algorithm. It finds the shortest path to a point. Someone already started making one
here, you might be able to talk to them for more info about it.
AW: GPS Calculation - Professional Scripter required -
Trooper[Y] - 06.12.2010
That linked GPS system is something like i planned,
but this algorhythm seems to require calculations from each point,
what seems to be not possible to this amount of waypoints...
But thanks, i will take a look at it
AW: GPS Calculation - Professional Scripter required -
Trooper[Y] - 06.12.2010
pawn Код:
angle=-GetAngleBetweenPoints(gpsp[0],gpsp[1],gx,gy);
gpsp[3]=gpsp[0]+(100 * floatsin(-angle, degrees));
gpsp[4]=gpsp[1]+(100 * floatcos(-angle, degrees));
for(new gc=20;gc<=80;gc+=20)
{
format(mysqlquery[playerid],256,"SELECT * FROM streetview_lv WHERE x BETWEEN '%f' AND '%f' && y BETWEEN '%f' AND '%f' ORDER BY x ASC",gpsp[3]-gc,gpsp[3]+gc,gpsp[4]-gc,gpsp[4]+gc);
mysql_query(mysqlquery[playerid]);
mysql_store_result();
if(mysql_fetch_field("x",tmpoutput3)) break;
}
mysql_fetch_field("x",tmpoutput3);
SetPVarFloat(playerid,"knotx",floatstr(tmpoutput3));
mysql_fetch_field("y",tmpoutput3);
SetPVarFloat(playerid,"knoty",floatstr(tmpoutput3));
mysql_fetch_field("z",tmpoutput3);
SetPVarFloat(playerid,"knotz",floatstr(tmpoutput3));
mysql_free_result();
SetPlayerRaceCheckpoint(playerid,3,GetPVarFloat(playerid,"knotx"),GetPVarFloat(playerid,"knoty"),GetPVarFloat(playerid,"knotz"),0,0,0,3);
Float:GetAngleBetweenPoints(Float:X1,Float:Y1,Float:X2,Float:Y2)
{
new Float:angle=atan2(X2-X1,Y2-Y1);
if(angle>360)angle-=360;
if(angle<0)angle+=360;
return angle;
}
Tested it multiple times,
looks like its working...
![Smiley](images/smilies/smile.png)
Thought i share it with you,
as you tried to help me.
Now i only have another problem, but thats a new topic