Search Unity

  1. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Revisiting the Parabola .. again

Discussion in 'Scripting' started by cdalexander, Mar 15, 2012.

  1. cdalexander

    cdalexander

    Joined:
    Nov 9, 2009
    Posts:
    73
    A few years ago, I thought I had the art of Parabola scripting nailed down. After taking a break and loosing some original code, I checked out an old post I made only to find it did not work the way it was intended.

    In class "Tank-Like" fashion, I'm writing a Debug.Line to extend from the barrel to the max-height point, and then from the max-height point to the proposed landing-point. Basically, it shows the path of the projectile prior to the shot.

    Here's what I was working with a few years ago and where I am at:

    Code (csharp):
    1.  
    2. ////////////////////////////////////////////////////////
    3. // PARABOLIC TRAJECTORY
    4. //
    5. // References  Equations:
    6. // http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
    7. //
    8. var velocity : float = 20.0;
    9.  
    10. function Update ()
    11. {
    12.  
    13.   // Gravity - 9.81 meters per second
    14.   var gravity: float = Physics.gravity.y; //-9.8
    15.  
    16.   // Angle - turn angle degree to radians
    17.   var radangle  : float   = Mathf.PI * transform.eulerAngles.x / 180.0;
    18.  
    19.   //var theta     : float   = Mathf.Sin ( radangle * 2 ) * Mathf.Pow(velocity, 2) / gravity;
    20.   var theta     : float   = transform.eulerAngles.x;
    21.  
    22.   // Axis Velocities
    23.   //var velocityY: float = velocity * Mathf.Sin(theta);
    24.   //var velocityZ: float = velocity * Mathf.Cos(theta);
    25.   var velocityY: float = velocity * Mathf.Sin(theta / 180 * Mathf.PI); 
    26.   var velocityZ: float = velocity * Mathf.Cos(theta / 180 * Mathf.PI);
    27.  
    28.  
    29.   // Done getting all the variables we need
    30.   ////////////////////////////////////////////////////////
    31.   // Now let's find out where everything is going to be at
    32.   //
    33.  
    34.   // How long will it take for the projectile to reach the highest/halfway point?
    35.   // general kinematic equation: vf = vo + at | our forumla: t = (0 - vy) / g
    36.   var halftime: float = (0.0 - velocityY) / gravity;
    37.    
    38.   // How long will the projectile be in the air?
    39.   var fulltime: float = halftime*2;
    40.    
    41.   // How high will the projectile get?
    42.   // the 'up' velocity will be at 0 at this point - Halfway (0.5) between begining and end.
    43.   // displacement equation: d=(v*t)+(0.5*g*t^2) | our formula: d=(v*y*t)+(0.5*g*y*t^2)
    44.   var totalHeight: float = ( velocityY * halftime ) + 0.5 * gravity * Mathf.Pow( halftime, 2 );
    45.    
    46.  
    47.   // How far away will the projectile land?
    48.   // assuming no air resistance: (forward velocity) * (time in flight)
    49.   var totalDistance: float = velocityZ * fulltime;
    50.  
    51.   // Where is the max-height/middle of the entire trajectory?
    52.   var midOffset: Vector3 = Vector3 (0, totalHeight, (totalDistance/2) );
    53.   var midPoint = transform.position + transform.TransformDirection(midOffset);
    54.  
    55.   // What vector location the projectile will hit?
    56.   var endOffset: Vector3 = Vector3(0, 0, Mathf.Abs(totalDistance));
    57.   var endPoint = transform.position + transform.TransformDirection(endOffset);
    58.  
    59.   // ### Draw Lines
    60.  
    61.   // draw line from start to point of maximum height
    62.   Debug.DrawLine (transform.position, midPoint, Color.blue);
    63.  
    64.   // draw line from maximum height to end
    65.   Debug.DrawLine (midPoint, endPoint, Color.red);
    66.  
    67. }
    68.  
    69.  
    So, this doesn't work and I've been working on it for the past 3 days off and on. I've seen and used many different parabolic examples given in these forums, but, my lack of trig skills are just killing me, no matter how much I try to learn them.

    So, if you understand what I'm trying to do and by chance see the error in my code, please help me end my suffering!

    :)

    Thanks for any help you can give.
     
  2. jefft

    jefft

    Joined:
    Feb 20, 2012
    Posts:
    23
    I have a few questions that may help clarify things:

    1. How is your proposed trajectory wrong? Can you provide a picture?
    2. Is the ground level? Slightly different assumptions apply if the destination is at a higher or lower y position than the source, e.g. it will affect flight time and final speed and angle.
    3. Output the intermediate values you calculate, so we can see if any look obviously wrong.

    One more observation (which may not apply in such a simple situation); you can't guarantee that a physics engine that uses an iterative solver will come to the exact same answer as the ideal equations which know the exact expected flight time. I would be surprised if the difference was more than a couple of percent though as long as objects in the scene are relatively well centred, which would rule out extreme-range float inaccuracies.
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,615
  4. cdalexander

    cdalexander

    Joined:
    Nov 9, 2009
    Posts:
    73
    I've been to the Wikipedia website. I have found out that I haven't the education or mathematical talents to convert the equations on that page to UnityScript (or any other for that matter).

    As far as the questions are concerned:

    1. I've pasted the code so that anyone can test and see. A picture wouldn't do a good job showing the problem since it requires movement to see. The problem is all about equation anyway - finding the middle-high point and the end point.
    2. Based off of ground level, there is no need to complicate the issue (yet)
    3. What value are you referring too? For code execution - If you set "velocity" to a float value, during execution, the only value that changes as you move is the "theta" (shot angle/range) and the transform heading.

    I've RAR'd up the skeleton project if you wish to download it and view. Hopefully someone will be able to help me connect these loose ends.

    BTW -- I swear that I remember when we could share projects by just passing around the Assets folder. Now there seems to be a bunch of files created on the project root and a few directories that you have to include in order to keep everything intact. I don't know the reason behind this, but it doesn't help support the sharing of project files.
     

    Attached Files:

  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
  6. cdalexander

    cdalexander

    Joined:
    Nov 9, 2009
    Posts:
    73
    That is a great find. I did see this post a while ago, and like a lot of the ones I found, it is calculating the angle needed to hit a defined point. What I am trying to do is different; finding the point by using the current angle.

    Some will suggest that I just need to flip the algorithm around.. and while they are right, I have no idea what to flip around (I'm totally dumb with parabola and other trig measurements)


    Anything or Anyone else that can help?
     
unityunity