Game Development Community

Simulating Latitude/Longitude

by Rodney (OldRod) Burns · in Torque Game Engine · 02/04/2003 (2:29 am) · 6 replies

I'm looking for a way to implement a latitude/longitude function that can carry across multiple terrain files. This will allow me to simulate a larger world than just one terrain for example. For instance, when you leave one terrain and enter another, the coordinates should flow between them as if they were connected.

What I thought of doing is maintaining an offset variable (for x, y, z) for each terrain file and then using the current client position, modified by this offset, to come up with the current player latitude/longitude/elevation. This information would be displayed as a gui element as part of a compass.

Has anyone tried this? Is there a better way to do it than this? Are there any problems with doing it this way?

#1
02/04/2003 (6:50 pm)
I would suggest using the terrain manager (see www.gorpe.com) and then finding a way to map from world-space to latitude/longitude space. Probably just scale the current xy co-ordinates of the player...
#2
02/05/2003 (6:52 am)
last I checked, x,y,z was fro mthe origina of the map, even if you were on another tile?

This is the snippet I use to echo position to the console:

function serverCmdpositionEcho(%client)
{
// position echo
%pos_x = getWords(%client.player.getTransform(), 0, 0);
%pos_y = getWords(%client.player.getTransform(), 1, 1);
%pos_z = getWords(%client.player.getTransform(), 2, 2);
messageClient(%client.player.client,"","X = " @ %pos_x @ " Y = " @ %pos_y @ " Z = " @ %pos_z);

//speed echo
%speed = getWords(getControlObjectSpeed(), 0, 0);
messageClient(%client.player.client,"","speed = " @ %speed);

}
#3
02/05/2003 (7:28 am)
Edward, I saw that code in another thread. Is there a way to bind that to a GUI element?
#4
02/05/2003 (10:33 am)
One could use the same logic that TerrainManager uses to determine what terrain square it's dealing with given a set of world corrdinates.

Basicly treat all of the terrain blocks, with their own 256x256 grids, as part of a larger metagrid. That way if you know they xy location of an object on a terrain block, and you know the terrain block's xy position within the grid of terrin then you can calcuate the position of the object on the metagrid.
#5
02/06/2003 (8:18 am)
//For what its worth, here's some code to convert
//between spherical and cartesian coordinates.

double radians(double a)
{ return a*(3.14159265359/180.0); }

double degrees(double a)
{ return a*(180.0/3.14159265359); }

void FromSpherical(
double r, double lon, double lat,
double& x, double& y, double& z
)
{
// origin of coordinate system is at longitude
// zero and latitude zero

// coordinates north of the equator or west of
// the prime meridian are negative
// EG: W113 N49 would be represented as -133 -49

// coordinates south of the equator or east of
// the prime meridian are positive
// EG: E30 S43 would be represented as 30 43

// convert input lon and lat degrees to radians
double rlat = radians(lat);
double rlon = radians(lon));
double cp = cos(rlat);
double sp = sin(rlat);
double ct = cos(rlon);
double st = sin(rlon);
//output coordinates in radius (r) units
x = r * ct * sp;
y = r * cp;
z = -r * st * sp;
}

void ToSpherical(
double x, double y, double z, double r,
double& lon, double& lat, double& alt
)
{
// input coordinates in radius (r) units
// r is assumed to include an altitude above the
// base spherical radius

// estimate geocentric altitude
alt = sqrt(x*x + y*y + z*z);
// output lon and lat in degrees
double rlat = acos(y / alt);
lat = -degrees(rlat);
double sp = sin(rlat);
double st = z / (-alt * sp);
double rlon = asin(st);
lon = -(180.0 - degrees(rlon));
alt = alt - r;
}
#6
02/06/2003 (8:19 am)
Thanks :)