Coordinate Systems?

Not sure if this is the right place to post this but i am currently trying to use longitude and latitude coordinates to create a race track (based on SilverStone) in unity. I’ve converted the coordinates into cartesian to make it easier to use in unity. The problem is that it displays the circuit as a straight line instead of the shape of the actually circuit. I’m not sure if its a problem with the conversion, the scaling or anything else. Does anyone have any ideas?

An example of the longitude, latitude coordinates is this (-1.012135, 52.078918).

This is the code:

using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
public class RaceTrackCreator : MonoBehaviour
{
public static List coordinates;
public float width = 10f;
public Material lineMaterial; // Assign a material in the Inspector
private void Start()
{
coordinates = new List(); // Clear coordinates
string filePath = “SilverStone_Coords.txt”;
string jsonContent = File.ReadAllText(filePath);
if (string.IsNullOrEmpty(jsonContent))
{
Debug.LogError("Failed to read file: " + filePath);
return;
}
SilverStoneArray coordsArray = JsonUtility.FromJson(jsonContent);
if (coordsArray == null)
{
Debug.LogError(“Failed to deserialize JSON data”);
return;
}
foreach (SilverStone coord in coordsArray.coords)
{
// Convert latitude and longitude to Cartesian coordinates
Vector3 cartesianCoord = LatLonToCartesian(coord.y, coord.x, coord.z);
coordinates.Add(cartesianCoord);
}
CreateRaceTrack();
}
private Vector3 LatLonToCartesian(float lat, float lon, float alt)
{
double a = 6378137.0; // semi-major axis in meters
double e = 0.081819190842622; // eccentricity
double latRad = DegToRad(lat);
double lonRad = DegToRad(lon);
double N = a / Mathf.Sqrt(1 - Mathf.Pow((float)(e * Mathf.Sin((float)latRad)), 2));
double X = (N + alt) * Mathf.Cos((float)latRad) * Mathf.Cos((float)lonRad);
double Y = (N + alt) * Mathf.Cos((float)latRad) * Mathf.Sin((float)lonRad);
double Z = ((1 - e * e) * N + alt) * Mathf.Sin((float)latRad);
// Adjust ECEF to Unity coordinates
float unityX = (float)X / 100f;
float unityY = (float)Z / 100f;
float unityZ = (float)Y / 100f;
return new Vector3(unityX, unityY, unityZ);
}
private double DegToRad(double degrees)
{
return degrees * Mathf.Deg2Rad;
}
private void CenterCoordinates()
{
// Calculate the centroid
Vector3 centroid = Vector3.zero;
foreach (Vector3 coord in coordinates)
{
centroid += coord;
}
centroid /= coordinates.Count;
// Center the coordinates around the origin
for (int i = 0; i < coordinates.Count; i++)
{
coordinates -= centroid;
}
}
private void CreateRaceTrack()
{

// Create an empty GameObject to hold the race track
GameObject raceTrackObject = new GameObject(“RaceTrack”);
// Set the position of the raceTrackObject to (0, 0, 0)
raceTrackObject.transform.position = Vector3.zero;
// Create the LineRenderer component
LineRenderer lineRenderer = raceTrackObject.AddComponent();
lineRenderer.material = lineMaterial;
lineRenderer.startWidth = width;
lineRenderer.endWidth = width;
lineRenderer.useWorldSpace = true;
lineRenderer.enabled = true;
// Create the line points
Vector3[ ] linePoints = new Vector3[coordinates.Count];
for (int i = 0; i < coordinates.Count; i++)
{
linePoints = coordinates*;*
}
// Set the line points
lineRenderer.positionCount = linePoints.Length;
lineRenderer.SetPositions(linePoints);
}
[System.Serializable]
public class SilverStone
{
public float x;
public float y;
public float z;
}
[System.Serializable]
public class SilverStoneArray
{
public SilverStone[ ] coords;
}
}

Hi!

If you share your SilverStone_Coords.txt I can have a look.
However, you might have more luck converting to UTM instead of ECEF, I find ECEF pretty awkward to work with.
Ordnance survey have info on algorithms:
https://www.ordnancesurvey.co.uk/documents/resources/guide-coordinate-systems-great-britain.pdf (Appendix C)

You could maybe batch-convert your file to National Grid:

National Grid should be good for Silverstone, probably no good for stuff outside the UK, I’m not sure.

I bet there is a C# Lat,lon > UTM thing somewhere and/or a batch conversion program (I thiink NovAtel have one), but I thought I’d best link stuff I was familiar with.

I don’t think it’ll be causing your problem, but I think I’d use double-precision everywhere, at least until you get as far as the final output points. Using double-precision for the dataset, then applying some offset to make the co-ordinates relative to the start point, and then switching to floats is maybe nicest. I think UTM runs out of precision otherwise.

Code’s easier to read if you use code tags, info here: