- Home /
2D projectile trajectory prediction
I have problems with predicting trajectory I'm using:
 Vector2 startPos;
 float power = 10.0f;
 float interval = 1/30.0f; 
 GameObject[] ind;
 
 
 void Start (){
         transform.rigidbody2D.isKinematic = true;
         ind = new GameObject[dots];
         for(int i = 0; i<dots; i++){
             GameObject dot = (GameObject)Instantiate(Dot);
             dot.renderer.enabled = false;
             ind[i] = dot;
         }
     }
 
 void Update (){
         if(shot) return;
         if(Input.GetAxis("Fire1") == 1){
             if(!aiming){
                 aiming = true;
                 startPos = Input.mousePosition;
                 ShowPath();
             }
             else{
                 CalculatePath();
             }
             
         }
         else if(aiming && !shot){
             transform.rigidbody2D.isKinematic =  false;    
             transform.rigidbody2D.AddForce(GetForce(Input.mous  ePosition));        
             shot = true;
             aiming = false;
             HidePath();
         }
 }
 
 Vector2 GetForce(Vector3 mouse){
         return (new Vector2(startPos.x, startPos.y)- new Vector2(mouse.x, mouse.y))*power;
         
 }
 
 void CalculatePath(){
         ind[0].transform.position = transform.position; //set frist dot to ball position
         Vector2 vel = GetForce(Input.mousePosition); //get velocity
 
         for(int i = 1; i < dots; i++){            
             ind[i].renderer.enabled = true; //make them visible
             Vector3 point = PathPoint(transform.position, vel, i); //get position of the dot 
             point.z = -1.0f;
             ind[i].transform.position = point;
         } 
     }
 
     Vector2 PathPoint(Vector2 startP, Vector2 startVel, int n){
                //Standard formula for trajectory prediction
         float t = interval;
         Vector2 stepVelocity = t*startVel;
         Vector2 StepGravity = t*t*Physics.gravity;
 
         Vector2 whattoreturn = ((startP + (n * stepVelocity)+(n*n+n)*StepGravity) * 0.5f);
 
         return whattoreturn;
     }
Using this, I get wrong trajectory. 1. It's like gravity doesn't drag trajectory down at all, and yes i know that gravity is weak because:
 t*t*Physics.gravity = 0.03^2 * vector2(0, -9.8) = vector2(0, -0.00882)
But that is the formula :S 2. Since gravity is low, velocity is too strong.
Here is the video: http://tinypic.com/player.php?v=1z50w3m&s=5
Trajectory formula form: http://www.iforce2d.net/b2dtut/projected-trajectory
What should I do?
I found that if I set StepGravity to something stronger like (0, -0.1) and devide startVel by 8 I get nearly right trajectory, but i don't want that, I need true trajectory path.
Answer by Shadowphax · Jun 30, 2014 at 10:58 AM
I have a non-mathematical solution that I have not tested but I believe it can work.
At regular intervals (while the player is aiming) fire a dummy object with a trail renderer with the same velocity and angle as you would do normally. But on this object just remove the collider. The result should be a pulsing trail that shows the predicted trajectory, even with bounces and so forth.
In this way you can let Unity do the math.
Answer by MarkD · Dec 01, 2013 at 09:28 PM
That is a tough one. I think you might even have a better change asking your question on StackOverflow. On that forum is a bigger group of mathematical coders.
Your answer
 
 
             Follow this Question
Related Questions
How do I arc a rigidbody so it lands on a target? 2 Answers
2D Arrow - face move direction? 0 Answers
2D Trajectory/Path Predictor 0 Answers
Tank AI, 2d rigidbody projectile trajectory prediction 0 Answers
simple trajectory for rigidbody 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                