Created
July 8, 2014 16:17
-
-
Save takashicompany/e41506fd202cd231b45e to your computer and use it in GitHub Desktop.
Calculate Trajectory Sample.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Calculates the trajectory. | |
/// </summary> | |
/// <returns>The trajectory.</returns> | |
/// <param name="start">Start.</param> | |
/// <param name="power">Power.</param> | |
/// <param name="mass">Mass.</param> | |
/// <param name="gravity">Gravity.</param> | |
/// <param name="gravityScale">Gravity scale.</param> | |
/// <param name="time">Time.</param> | |
public static Vector3 CalcTrajectory( | |
Vector3 start, | |
Vector3 power, | |
float mass, | |
Vector3 gravity, | |
float gravityScale, | |
float time | |
) | |
{ | |
var speedX = power.x / mass * Time.fixedDeltaTime; | |
var speedY = power.y / mass * Time.fixedDeltaTime; | |
var speedZ = power.z / mass * Time.fixedDeltaTime; | |
var halfGravityX = gravity.x * 0.5f * gravityScale; | |
var halfGravityY = gravity.y * 0.5f * gravityScale; | |
var halfGravityZ = gravity.z * 0.5f * gravityScale; | |
var positionX = speedX * time + halfGravityX * Mathf.Pow(time, 2); | |
var positionY = speedY * time + halfGravityY * Mathf.Pow(time, 2); | |
var positionZ = speedZ * time + halfGravityZ * Mathf.Pow(time, 2); | |
return start + new Vector3(positionX, positionY, positionZ); | |
} | |
/// <summary> | |
/// Calculates the trajectory. | |
/// </summary> | |
/// <returns>The trajectory.</returns> | |
/// <param name="start">Start.</param> | |
/// <param name="velocity">Velocity.</param> | |
/// <param name="gravity">Gravity.</param> | |
/// <param name="gravityScale">Gravity scale.</param> | |
/// <param name="time">Time.</param> | |
public static Vector3 CalcTrajectory( | |
Vector3 start, | |
Vector3 velocity, | |
Vector3 gravity, | |
float gravityScale, | |
float time | |
) | |
{ | |
var halfGravityX = gravity.x * 0.5f * gravityScale; | |
var halfGravityY = gravity.y * 0.5f * gravityScale; | |
var halfGravityZ = gravity.z * 0.5f * gravityScale; | |
var positionX = velocity.x * time + halfGravityX * Mathf.Pow(time, 2); | |
var positionY = velocity.y * time + halfGravityY * Mathf.Pow(time, 2); | |
var positionZ = velocity.z * time + halfGravityZ * Mathf.Pow(time, 2); | |
return start + new Vector3(positionX, positionY, positionZ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment