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;
}
}