- Home /
2D trajectory prediction around planets
I've created a game where the player fires a projectile between planets trying to hit a target. The planets then apply their gravitational forces to the projectile. All of this is working correctly. What I now want is to use a LineRenderer to give a prediction of where the projectile will fly. This is where my math skills fail me.
My code/scene
All projectiles originate from one location and always at the same initial speed. The only variable that changes is the direction. When a projectile is fired I apply a force to the rigidbody
rb.AddForce(origin.up * VELOCITY)
As soon as the projectile is spawned all gravitational entities in the scene starts pulling on it.
float forceMagnitude = (projectileMass * planetGravity) / Mathf.Pow(distance, 2);
Vector2 force = direction.normalized * forceMagnitude;
rb.AddForce(force);
Thoughts
I've experimented somewhat trying to solve this and have managed to get a line which loops around the planets. However it doesn't match the path the projectile flies. So something is off. I think my calculation around applying gravity and how to simulate the initial velocity.
My goal is to have something similar to the game Orbit (Steam)
Do you guys have any insight in how I can tackle this? I think I just need a nudge in the right direction.
Answer by mlepp · May 13, 2018 at 12:34 PM
Of course... Solved it almost exactly 20 minutes after asking the question... So for anyone else trying to solve the same thing, now or in the future, here is my solution:
void FixedUpdate()
{
Vector2 velocity = origin.up * 100 * Time.fixedDeltaTime;
lineRenderer.positionCount = segmentCount;
lineRenderer.SetPosition(0, origin.position);
float dt = Time.fixedDeltaTime;
for (int i = 1; i < segmentCount; i++)
{
Vector2 previous = _lineRenderer.GetPosition(i - 1);
velocity += CalculateGravity(previous) * dt;
lineRenderer.SetPosition(i, previous + velocity * dt);
}
}
what is CalculateGravity?? in (velocity += CalculateGravity(previous) * dt;)
It's been a long time and I've moved past this project. But I'm pretty sure that the CalculateGravity is my own calculation for deter$$anonymous$$ing what the gravity is at a given position. It takes previous
which is the last position, checks all (or any relevant) planets and returns what the gravity is as a sum of all possible influences. This function is very game dependent, so it is hard to give a universal CalculateGravity function.
Managed to dig up my code. This is my simple CalculateGravity method:
Vector2 CalculateGravity(Vector3 pos)
{
Vector2 velocity = Vector2.zero;
foreach (var attractor in _attractors)
{
velocity += attractor.GetForce(pos, 1);
}
return velocity;
}
Where _attractors
is a list of all different gravitational influences.
Your answer
Follow this Question
Related Questions
Golfball sphere collider "Hops" over vertex-joined planes at intersection? 1 Answer
Edit this script so that on the second jump change the gravity to 5 2 Answers
How can I make a game object move in parabolic motion as if it were under gravity? 2 Answers
Change direction of gravity for a specific instance of a prefab 1 Answer
Setting a jump force on a rigidbody 1 Answer