Guest User

Untitled

a guest
Feb 1st, 2018
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. /// <summary>
  2. /// Calculates the necessary initial velocity to loft an object from the start position (with the given inclination angle) to end position
  3. /// </summary>
  4. /// <param name="startPos">the start of the loft</param>
  5. /// <param name="inclinationAngle">the angle (up from flat) to loft at</param>
  6. /// <param name="targetPos">the target point to hit with the loft</param>
  7. /// <param name="minThrowDistance">the minimum throw distance to allow (fixes issues with throwing infinitely hard straight up)</param>
  8. /// <returns>the velocity to assign to an object to make it loft up and land perfectly at the target position</returns>
  9. static public Vector3 ThrowVelocityToPoint(Vector3 startPos, float inclinationAngle, Vector3 targetPos, float minThrowDistance)
  10. {
  11. // Get the flat-direction from thrower to target position (and flat-distance to target while we're at it)
  12. Vector3 flatDir = targetPos - startPos;
  13. float vertDis = flatDir.y;
  14. flatDir.y = 0.0f;
  15. float horizDis = flatDir.magnitude;
  16. flatDir.Normalize();
  17.  
  18. if (horizDis < minThrowDistance) { horizDis = minThrowDistance; }
  19.  
  20. // Now calculate the throw dir based on throw angle and flat-dir
  21. Vector3 rightDir = Vector3.Cross(Vector3.up, flatDir);
  22. Vector3 throwDir = Quaternion.AngleAxis(-inclinationAngle, rightDir) * flatDir;
  23.  
  24. // Equation: v^2 = (g * (horiz_distance)^2) / ( 2.0 * ( (horiz_distance * tan(angle)) - vert_distance ) * cos^2(angle) )
  25. float gravity = Mathf.Abs(Physics.gravity.y);
  26. float initialSpeed = Mathf.Sqrt((gravity * horizDis * horizDis) / (2.0f * ((horizDis * Mathf.Tan(Mathf.Deg2Rad * inclinationAngle)) - vertDis) * Mathf.Cos(Mathf.Deg2Rad * inclinationAngle) * Mathf.Cos(Mathf.Deg2Rad * inclinationAngle)));
  27.  
  28. return throwDir * initialSpeed;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment