Search Unity

Question I need help with some trajectory calculation math

Discussion in 'Scripting' started by dennik, Nov 24, 2020.

  1. dennik

    dennik

    Joined:
    Nov 23, 2011
    Posts:
    101
    I'm working on a simple script where a projectile is given an initial velocity to hit a target.
    I'm plugging in the target and the initial angle for the projectile.
    I used an equation that solves for initial velocity and given angle. Unfortunately it is not working as expected and I'm not sure why. Here is the equation that I used.

    (if image won't show, here is the link to it )

    And here is the code. I broke it down as much as I could for clarity.
    Code (CSharp):
    1.  public float tilt;
    2.     float g;
    3.     Rigidbody rb;
    4.     public GameObject target;
    5.  
    6.     void Start()
    7.     {
    8.         rb = transform.GetComponent<Rigidbody>();
    9.         g = 9.8f;
    10.      
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         if (Input.GetMouseButtonDown(0)) {        
    16.  
    17.             rb.velocity = CalcVelocity(target.transform.position);        
    18.         }
    19.     }
    20.  
    21.  
    22.  
    23.  
    24.     Vector3  CalcVelocity(Vector3 pos1)
    25.     {
    26.          transform.position = Vector3.zero;
    27.      
    28.         float sine, cosine;
    29.         sine = Mathf.Sin(tilt * Mathf.Deg2Rad);
    30.         cosine = Mathf.Cos(tilt * Mathf.Deg2Rad);
    31.         float velocity;
    32.         float numerator, denominator;
    33.         Vector3 force;
    34.         numerator = Mathf.Pow(pos1.z, 2) * 9.8f;    
    35.         denominator = 2 * pos1.z * sine - 2 * pos1.y * Mathf.Pow(cosine,2);
    36.         print(numerator);
    37.         print(denominator);
    38.         velocity = Mathf.Sqrt(f:numerator / denominator);
    39.         force = new Vector3(0.0f,sine,cosine)* velocity;
    40.         return force;
    41.     }
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,001
    Assuming the formula you've linked in the image is correct, you made an error in your implementation. It's not

    2 * pos1.z * Mathf.Sin(tilt * Mathf.Deg2Rad)


    but

    pos1.z * Mathf.Sin(2 * tilt * Mathf.Deg2Rad)


    All this of course assumes that your projectiles starts at the world space origin (0,0,0) and the target object is located at the worldspace position (0,y,z)

    Note by using the double angle identity you could keep your sine and cosine as you originally have calculated them and use this "denominator":

    denominator = (pos1.z*sine - pos1.y*cosine)*2*cosine;


    ps:
    Note that you should avoid using
    Mathf.Pow
    when just calculating the square. _Even though Mathf.Pow internally optimises integer powers, it's generally a slow operation and doesn't really help the readability. Also this:
    Mathf.Pow(pos1.z, 2)
    isn't really shorter than this
    pos1.z * pos1.z
    (it's actually 5 characters longer ^^)
     
    Last edited: Nov 24, 2020
    dennik likes this.
  3. dennik

    dennik

    Joined:
    Nov 23, 2011
    Posts:
    101
    That was it! Goodness, I can waste hours just from overlooking stupid mistakes like that. Thank you for the fresh eyes and the optimization tips. Much appreciated!