- Home /
2D Trajectory prediction in open space
I want to draw a line (I have Vectrocity) that shows predicted trajectory of an object based on acting forces (a single force to begin with).
Force is applied by this script:
using UnityEngine;
using System.Collections;
public class AccelerateToPoint : MonoBehaviour
{
Rigidbody2D body;
Transform myTransform;
public float acceleration = 100;
private float startTime;
private bool move = false;
private Vector3 targetPosition;
// Use this for initialization
void Start()
{
body = GetComponent<Rigidbody2D>();
myTransform = transform;
}
// Update is called once per frame
void FixedUpdate()
{
if (Input.GetMouseButtonDown(0)) {
// Acquire destination point
Vector3 pointer = Input.mousePosition;
targetPosition = Camera.main.ScreenToWorldPoint(pointer);
targetPosition.z = 0;
move = true;
}
if (move) {
Vector3 directionToTarget = (targetPosition - myTransform.position).normalized;
// if target was changed mid-flight we have to find our velocity to the new target
Vector3 velocityProjectionToTarget = Vector3.Project(body.velocity, directionToTarget);
float q = velocityProjectionToTarget.normalized == directionToTarget ? 1 : -1;
float S = (targetPosition - myTransform.position).magnitude;
float t = Mathf.Sqrt(S * 2 / acceleration);
// this is how much velocity we can reduce to zero on the way to our destination
float V = acceleration * t;
// this is our velocity to the target in a very near future
float velocityThreshold = velocityProjectionToTarget.magnitude * q + (acceleration * Time.fixedDeltaTime * 2);
bool accelerating;
Vector3 forceDirection;
if (V > velocityThreshold) {
// if we can reduce more velocity than we have, we will accelerate
accelerating = true;
forceDirection = directionToTarget;
} else {
// otherwise we will decelerate
forceDirection = -directionToTarget;
accelerating = false;
}
// condition to stop the movement:
// if we decelerate and we almost stopped - stop completely
if (body.velocity.magnitude < 10 && !accelerating) {
myTransform.position = targetPosition;
body.velocity = Vector3.zero;
move = false;
return;
}
body.AddForce(forceDirection * acceleration);
Debug.Log(
string.Format(
"t = {0} s; S = {1} m; V = {2} m/s; curV = {3}; {4}",
t, S, V, velocityProjectionToTarget.magnitude, accelerating
)
);
}
}
}
If we just click and wait, object travels a straight line and stops at the destination. However, if we change destination mid-flight, object starts orbiting around the destination point (orbit is usually elliptic and this ellipse usually rotates around destination point).
How to predict the motion?
Answer by Maxter · Sep 18, 2015 at 07:07 PM
I found my solution. You'll need a bit of advanced maths.
These links provided all the necessary information to do what I need: http://gafferongames.com/game-physics/integration-basics/ http://gafferongames.com/game-physics/physics-in-3d/
Algorithm described there is not demanding and it provides pretty accurate predictions.
Your answer
Follow this Question
Related Questions
Trajectory prediction 0 Answers
2D Trajectory/Path Predictor 0 Answers
Predicting A Trajectory 1 Answer
Trying to make a trajectory prediction line! 1 Answer
Rendering a 2D trajectory arc 1 Answer