I am making a 2D Newtonian physics game. One of the main aspects of the game is orbits. I want to have a projection line which will project the players trajectory. I have one set up but it does not accurately predict the orbital pattern. Instead, the prediction line shoots off and doesn’t circle the body.
Here is a photograph of my predicament. The magenta line is the orbit (it was traced out by the Debug). The dark teal line at the bottom right quadrant that juts out is the prediction. Notice that the prediction is sort of on point at first but does not encircle the planet, instead jutting out into infinite away from the planet
Here is my code for the prediction line:
public class OrbitPredictionLine : MonoBehaviour
{
private Vector2 uV;
public void DrawTraject (Vector2 startPos, Vector2 startVelocity )
{
//line rendering..
int verts = 2000;
LineRenderer line = GetComponent<LineRenderer>();
line.SetVertexCount(verts);
//input vars
Vector2 pos = startPos;
Vector2 vel = startVelocity;
GameObject orbitedPlanet = GameObject.FindGameObjectWithTag("BeingOrbited");
uV = orbitedPlanet.GetComponent<PlanetaryGravity>().uV;
Vector2 grav = uV;
for (var i = 0; i < verts; i++)
{
line.SetPosition(i, new Vector3(pos.x, pos.y, 0));
vel = vel + grav * Time.fixedDeltaTime;
pos = pos + vel * Time.fixedDeltaTime;
}
}
void Update () {
Vector2 wotanPos = GameObject.Find("Wotan").GetComponent<Rigidbody2D>().transform.position;
DrawTraject(wotanPos, GameObject.Find("Wotan").GetComponent<Rigidbody2D>().velocity);
}
}
Wotan is the name of the ship. uV is the gravitational force vector.
