G
G
Georgy Pelageykin2016-09-24 23:39:33
Mathematics
Georgy Pelageykin, 2016-09-24 23:39:33

How to calculate the trajectory, knowing the acceleration function?

Hello. It is necessary for the game to make a calculation of the trajectory of the ship. Known immutable data:

  • Start coordinates
  • starting speed
  • Target coordinates
  • Developed acceleration
  • The function of the "autopilot" - takes as input a vector (targetPos - currentPos) and the current speed, at the output - the acceleration vector that needs to be given to the ship in order to reach the target.

How I got it: I took unity3d, created a test scene, wrote a script that gives acceleration to an object using this function, it looks something like this (three objects with different initial speeds fly towards the central one).
i.imgur.com/3rf6z0e.jpg
Just in case, the code:
internal class AutopilotController : MonoBehaviour
{
  [SerializeField] private Transform _target;
  [SerializeField] private Vector3 _initialVelocity;
  [SerializeField] private Single _acceleration;

  private void Start()
  {
    GetComponent<Rigidbody>().velocity = _initialVelocity;
  }

  private void Update()
  {
    var thisRigidbody = GetComponent<Rigidbody>();

    Vector2 tdir = _target.position - transform.position;
    Vector2 velocity = thisRigidbody.velocity;

    thisRigidbody.AddForce(CalculateForce(tdir, velocity));
  }

  private Vector2 CalculateForce(Vector2 tdir, Vector2 velocity)
  {
    return (Mathf.Sqrt(_acceleration * tdir.magnitude) * tdir.normalized - velocity)
             .normalized * _acceleration;
  }
}

The result was exactly what I wanted, but in itself is not particularly needed. I need to calculate such a trajectory without using a physical simulator, i.e. get the dependence of coordinates on time . Unfortunately, the knowledge of physics is not enough to understand what to decompose the function / how to integrate, etc., to get this dependence.
Those. I got a variable vector acceleration, depending on the current position (because tdir is a constant - position) and speeds that change from it - I have little idea how to approach this.
11f564bc72fa434b8be41ff52cfcdd65.png
It works quite simply - it simply calculates the direction of the difference between the speed of the ship and the speed that it must have in order to start extinguishing it and stop exactly at the destination. Those. without an initial speed, it will accelerate half the way, and slow down the second.
I solve the problem on a plane, the third coordinate is not needed.
I'd be grateful for any hints/direction in which to dig.
PS rewritten function, if you open the normalization inside
d575ecc6f17340b1b37e6e4f5215797b.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-09-25
@ArXen42

The analytical algorithm is always the same - separately for each of the X and Y coordinates:
acceleration - is the derivative of the speed with respect to time
speed - is the derivative of the location on the axis with respect to time
So we substitute your formula into the acceleration formula, we get a second-order differential equation, we solve it, we substitute the values ​​of location and speed at time t=0 and get the dependence of location you are looking for on time.
If the acceleration formula for one coordinate depends on the location and / or speed along another coordinate, then you will not be able to “untie” the coordinates and instead of two independent differential equations of the second order, we have a system and try to solve it analytically.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question