Answer the question
In order to leave comments, you need to log in
How to draw a geodesic line between two points?
Hello toaster, I've been poring over the issue for a couple of months.
There is a sphere at the origin, ie 0,0,0
It has two points given in spherical and Cartesian coordinates (the meshes are attached to them).
It is necessary to build a geodesic line (the shortest distance between points on a sphere.
It is logical that intermediate points are needed to draw a line.
I tried to apply trigonometry and make a formula with a step, but the bagels did not work.
Tell me how to draw this line either by an algorithm or a crutch from the Unity engine, a lot in advance thank you.
Answer the question
In order to leave comments, you need to log in
You can sculpt a quaternion to find intermediate points. I won’t say anything about the optimality of the path, but it looks correct. First find the local coordinates of the objects, then normalize them and get the direction. You create a rotation from one direction to another and apply it in steps to calculate points. The easiest way to draw is with the LineRenderer.
using UnityEngine;
public class LineExample : MonoBehaviour
{
public LineRenderer lineRenderer;
public Transform a;
public Transform b;
public float sphereRadius = 0.5f;
public int positionCount = 10;
private void Awake()
{
Vector3 aDirection = transform.InverseTransformPoint(a.position).normalized;
Vector3 bDirection = transform.InverseTransformPoint(b.position).normalized;
var fromRotation = Quaternion.identity;
var toRotation = Quaternion.FromToRotation(aDirection, bDirection);
lineRenderer.positionCount = positionCount;
for (int i = 0; i < positionCount; i++)
{
float t = i/(positionCount - 1f);
Quaternion rotation = Quaternion.Lerp(fromRotation, toRotation, t);
Vector3 point = rotation*aDirection*sphereRadius;
lineRenderer.SetPosition(i, point);
}
}
}
Let the endpoints have Cartesian coordinates {x0,y0,z0} and {x1,y1,z1}
Take a line segment connecting the endpoints (passing inside the sphere); the coordinates of its points will be:
{x0+(x1-x0)*k,y0+(z1-z0)*k,z0+(z1-z0)*k}, where k runs through all values from zero to one.
We translate these coordinates into a spherical system, and then forcibly make the radius equal to the radius of the circle (the angles do not change). That's it, we have all the points you need arcs of a circle.
How to use it, i.e. which points you need (from the infinite set that I gave) - decide for yourself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question