Unity3D - Ballistic system
- Get link
- X
- Other Apps
Today I'll show you a ballistic system I've made on Unity3D. It's a static class so you can use it easily in any project.
This ballistic system can be used in 2 ways:
- You know the velocity for projectile shot, the system give you the angle needed to reach your target at the given velocity.
- You know the angle for projectile, the system give you the velocity needed to reach your target at the given angle.
This ballistic system can be used in 2 ways:
- You know the velocity for projectile shot, the system give you the angle needed to reach your target at the given velocity.
- You know the angle for projectile, the system give you the velocity needed to reach your target at the given angle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | using UnityEngine; using System.Collections; using System.Collections.Generic; public static class Balistic { #region Members // Gravity (g) private static Vector3 m_Gravity = new Vector3(0, -9.81f, 0); #endregion #region Public Manipulators /// <summary> /// Get angle needed for trajectory /// </summary> /// <param name="startPoint">Start point</param> /// <param name="targetPoint">Target point</param> /// <param name="velocity">Object speed</param> /// <returns>Angle</returns> public static float[] GetAngle(Vector3 startPoint, Vector3 targetPoint, float velocity) { /* * Polynome 2nd degree : Ax² + Bx + C * * Angle = g * Px² * tan² + 2 * V.magnitude² * tan + g * Px² - 2 * V.magnitude² * Py */ // Distance on X and Z (based on 2D axis) float Px = (new Vector3(targetPoint.x, 0, targetPoint.z) - new Vector3(startPoint.x, 0, startPoint.z)).magnitude; // Distance on Y float Py = targetPoint.y - startPoint.y; // Get possible angles float[] angles = PolynomeDegree2(m_Gravity.y * Px * Px, 2 * velocity * velocity * Px, m_Gravity.y * Px * Px - (2 * velocity * velocity * Py)); if (angles != null) { angles[0] = Mathf.Atan(angles[0]) * Mathf.Rad2Deg; angles[1] = Mathf.Atan(angles[1]) * Mathf.Rad2Deg; } return angles; } /// <summary> /// Get squared velocity needed for trajectory /// </summary> /// <param name="startPoint">Start point</param> /// <param name="targetPoint">End point</param> /// <param name="angle">Angle X of shot</param> /// <returns>Squared velocity</returns> public static float GetSquaredVelocity(Vector3 startPoint, Vector3 targetPoint, float angle) { /* * g x Px² x tan² + g x Px² * V0² = - ________________________ * 2 x Px x tan - 2 x Py */ // Angle tan float tan = Mathf.Tan(angle * Mathf.Deg2Rad); // Distance on X and Z (based on 2D axis) float Px = (new Vector3(targetPoint.x, 0, targetPoint.z) - new Vector3(startPoint.x, 0, startPoint.z)).magnitude; // Distance on Y float Py = targetPoint.y - startPoint.y; // Calculate velocity float squaredVelocity = -( m_Gravity.y * Px * Px * tan * tan + m_Gravity.y * Px * Px); squaredVelocity /= 2 * Px * tan - 2 * Py; return squaredVelocity; } /// <summary> /// Get velocity needed for trajectory /// </summary> /// <param name="startPoint">Start point</param> /// <param name="targetPoint">End point</param> /// <param name="angle">Angle X of shot</param> /// <returns>Velocity</returns> public static float GetVelocity(Vector3 startPoint, Vector3 targetPoint, float angle) { return Mathf.Sqrt(GetSquaredVelocity(startPoint, targetPoint, angle)); } #endregion #region Private Manipulators /// <summary> /// Return a solution of a 2nd degree polynome /// </summary> /// <param name="a">Multiplicative constant A</param> /// <param name="b">Multiplicative constant B</param> /// <param name="c">Multiplicative constant C</param> /// <returns>1 of the 2 solutions, -1 if no solutions</returns> private static float[] PolynomeDegree2(float a, float b, float c) { // Delta : B² - 4AC float delta = b * b - (4 * a * c); // If delta is positive if (delta >= 0) { // Return a solution float[] solutions = new float[2]; solutions[0] = (-b - Mathf.Sqrt(delta)) / (2 * a); solutions[1] = (-b + Mathf.Sqrt(delta)) / (2 * a); return solutions; } else { return null; } } #endregion } |
Comments
Post a Comment