Answer the question
In order to leave comments, you need to log in
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:
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;
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question