I have a celestial sphere with a plane in the centre and spheres (stars) aligned around the centre with a fixed radius.
In order to show the stars visible from a certain latitude and longitude from Earth, I need to rotate the plane accordingly.
To rotate the plane properly, I converted the latitude and longitude into 3D vectors, then utilised the LookAt()
method to point the plane at this vector.
The following is the code which calculated the vectors from the coordinates, and utilises the LookAt()
Method:
public void positionLand(double latitude, double longitude)
{
double latitude_rad = (math.PI / 180) * latitude;
double longitude_rad = (math.PI / 180) * longitude;
Vector3 markerPositioning = new Vector3();
markerPositioning.x = (float)-((math.cos(latitude_rad)) * (math.cos(longitude_rad)));
markerPositioning.y = (float)((math.cos(latitude_rad)) * (math.sin(longitude_rad)));
markerPositioning.z = (float)((math.sin(latitude_rad)));
groundPlane.transform.LookAt(markerPositioning);
}
To test if this would work, I used the coordinates of London, UK, latitude: 51.509865 and longitude: -0.118092. And this was the result:
With the LookAt()
method changed to the following: groundPlane.transform.LookAt(markerPositioning, Vector3.forward);
This is the result:
Replacing groundPlane.transform.LookAt(markerPositioning);
with groundPlane.transform.up = markerPositioning;
results in the following:
As you can see, the plane is not properly rotating. Initially, I thought maybe the LookAt()
method is using the incorrect forward vector of the plane gameobject, but this isn't the case as there is no connection between the way the plane rotated and the coordinates of London as the plane is near vertical at 89 degrees.
What am I doing wrong and how can I fix this?
groundPlane.transform.up = markerPositioning;
? \$\endgroup\$