ProjectileHelper  1.02
Library of functions to assist with ballistic projectile calculations.
Static Public Member Functions | List of all members
ProjectileHelper Class Reference

Class of static functions to help with ballistic projectile calculations. These functions assume that gravity acts in the y-axis. More...

Static Public Member Functions

static float ComputeSpeedToReachMaxFlatRange (float range, float gravity_negative, out float timeToLand)
 Given a maximum range (on flat ground) and gravity, what is the maximum speed the projectile can be launched? Use case: The player can only throw grenades with a max range of 30 meters on flat ground. What is the max speed he can throw it? More...
 
static bool CanReachTargetWithSpeed (Vector3 startPosition, Vector3 targetPosition, float gravity_negative, float speed)
 Given a start position, gravity and initial speed, can a projectile reach a target position? Use case: If my cannonballs always fire at 200m/sec, can it reach a target position? More...
 
static bool ComputeDirectionToHitTargetWithSpeed (Vector3 startPosition, Vector3 targetPosition, float gravity_negative, float speed, out Vector3 direction1, out Vector3 direction2)
 Given a start position, gravity and initial speed, what direction should we fire the projectile to hit a target position? More...
 
static bool ComputeTimeToHitGround (Vector3 startPosition, Vector3 velocity, float groundLevel, float gravity_negative, out float timeToHit)
 Given a start position, initial velocity (with direction) and gravity, how much time is needed to reach the ground (at a specified height level). Use case: Artillery has begun firing upon your position. How much time do you have to evacuate / take cover? More...
 
static Vector3 ComputePositionAtTimeAhead (Vector3 currentPosition, Vector3 velocity, float gravity_negative, float timeAhead)
 Determine what position a projectile will be after a certain time, given its current position and velocity. Use case: To display the trajectory of a grenade before you throw it More...
 
static Vector3 ComputeVelocityAtTimeAhead (Vector3 currentPosition, Vector3 currentVelocity, float gravity_negative, float timeAhead)
 Determine the velocity of a projectile after a certain time, given its current position and velocity. More...
 
static Vector3 ComputeVelocityToHitTargetAtTime (Vector3 startPosition, Vector3 targetPosition, float gravity_negative, float timeToTargetPosition)
 Determine the velocity needed to hit a target position in exactly the given amount of time. Use case: you want to fire a catapult projectile to land at a specific position, giving the player exactly 4 seconds of warning time to escape More...
 
static void UpdateProjectile (ref Vector3 currentPosition, ref Vector3 currentVelocity, float gravity_negative, float deltaTime)
 Update a projectile's current position and velocity, given the gravity and delta time More...
 
static float ComputeGravityToReachTargetWithSpeedAndElevation (float distanceOffset, float heightOffset, float elevationRadians, float speed)
 Determine the gravity needed to reach a target distance and height with a given speed and elevation angle More...
 
static bool ComputeSpeedToReachTargetWithElevation (Vector3 startPosition, Vector3 targetPosition, float elevationRadians, float gravity_negative, ref float outSpeed)
 Determine the speed needed to reach a target position given a starting elevation. More...
 
static bool ComputeSpeedToReachFlatRangeWithElevation (float elevationRadians, float range, float gravityNegative, out float speed)
 Determine the speed needed to reach a certain range (on flat ground) given a starting elevation More...
 
static bool ComputeElevationToHitTargetWithSpeed (float heightOffset, float distanceOffset, float gravityNegative, float speed, bool useSmallerAngle, out float angleRadians)
 Given an initial speed, what elevation angle is needed to hit a target at a specified height offset and flat distance away? More...
 

Detailed Description

Class of static functions to help with ballistic projectile calculations. These functions assume that gravity acts in the y-axis.

Member Function Documentation

◆ CanReachTargetWithSpeed()

static bool ProjectileHelper.CanReachTargetWithSpeed ( Vector3  startPosition,
Vector3  targetPosition,
float  gravity_negative,
float  speed 
)
static

Given a start position, gravity and initial speed, can a projectile reach a target position? Use case: If my cannonballs always fire at 200m/sec, can it reach a target position?

Parameters
startPositionStarting Position of the projectile.
targetPositionTarget Position of the projectile.
gravity_negativeAcceleration due to gravity on y-axis. Should be negative, eg -9.8f
speedInitial speed of the projectile.
Returns
Returns true if the target position can be reached with the given parameters.

◆ ComputeDirectionToHitTargetWithSpeed()

static bool ProjectileHelper.ComputeDirectionToHitTargetWithSpeed ( Vector3  startPosition,
Vector3  targetPosition,
float  gravity_negative,
float  speed,
out Vector3  direction1,
out Vector3  direction2 
)
static

Given a start position, gravity and initial speed, what direction should we fire the projectile to hit a target position?

Parameters
startPositionStarting Position of the projectile.
targetPositionTarget Position of the projectile.
gravity_negativeAcceleration due to gravity on y-axis. Should be negative, eg -9.8f
speedInitial speed of the projectile.
direction1First possible direction for the projectile
direction2Second possible direction for the projectile
Returns
Returns true if the target position can be reached with the given parameters.

◆ ComputeElevationToHitTargetWithSpeed()

static bool ProjectileHelper.ComputeElevationToHitTargetWithSpeed ( float  heightOffset,
float  distanceOffset,
float  gravityNegative,
float  speed,
bool  useSmallerAngle,
out float  angleRadians 
)
static

Given an initial speed, what elevation angle is needed to hit a target at a specified height offset and flat distance away?

Parameters
heightOffsetDifference in height of the target from the starting position
distanceOffsetFlat distance away from the starting position.
gravity_negativeAcceleration due to gravity on y-axis. Should be negative, eg -9.8f
speedInitial speed of the projectile.
useSmallerAngleWhether to calculate the smaller or larger elevation angle, since two elevations are possible.
angleRadiansOutput: the elevation angle needed.
Returns
Returns true if the target can be reached.

◆ ComputeGravityToReachTargetWithSpeedAndElevation()

static float ProjectileHelper.ComputeGravityToReachTargetWithSpeedAndElevation ( float  distanceOffset,
float  heightOffset,
float  elevationRadians,
float  speed 
)
static

Determine the gravity needed to reach a target distance and height with a given speed and elevation angle

Parameters
distanceOffsetFlat distance away from the starting position.
heightOffsetHeight difference from the starting position.
elevationRadiansElevation angle the projectile will be fired at.
speedThe initial speed of the projectile.
Returns
Returns the necessary gravity to reach the target.

◆ ComputePositionAtTimeAhead()

static Vector3 ProjectileHelper.ComputePositionAtTimeAhead ( Vector3  currentPosition,
Vector3  velocity,
float  gravity_negative,
float  timeAhead 
)
static

Determine what position a projectile will be after a certain time, given its current position and velocity. Use case: To display the trajectory of a grenade before you throw it

Parameters
currentPositionCurrent position of the projectile.
velocityCurrent velocity of the projectile.
gravity_negativeAcceleration due to gravity on y-axis. Should be negative, eg -9.8f
timeAheadThe amount of time in the future at which you want to know the position
Returns
Returns the position of the projectile after the specified time.

◆ ComputeSpeedToReachFlatRangeWithElevation()

static bool ProjectileHelper.ComputeSpeedToReachFlatRangeWithElevation ( float  elevationRadians,
float  range,
float  gravityNegative,
out float  speed 
)
static

Determine the speed needed to reach a certain range (on flat ground) given a starting elevation

Parameters
elevationRadiansElevation angle the projectile will be fired at.
rangeFlat distance away from the starting position.
gravity_negativeAcceleration due to gravity on y-axis. Should be negative, eg -9.8f
speedOutput: Speed of projectile needed to reach the wanted range.
Returns
Returns true if the range can be reached. This could be false, for eg if the elevation is lower than zero.

◆ ComputeSpeedToReachMaxFlatRange()

static float ProjectileHelper.ComputeSpeedToReachMaxFlatRange ( float  range,
float  gravity_negative,
out float  timeToLand 
)
static

Given a maximum range (on flat ground) and gravity, what is the maximum speed the projectile can be launched? Use case: The player can only throw grenades with a max range of 30 meters on flat ground. What is the max speed he can throw it?

Parameters
rangeMaximum range of the projectile on flat ground, in meters
gravity_negativeAcceleration due to gravity on y-axis. Should be negative, eg -9.8f
timeToLandOutputs the time taken for the projectile to reach max range on flat ground. In seconds.
Returns
Returns the speed needed to reach the max range.

◆ ComputeSpeedToReachTargetWithElevation()

static bool ProjectileHelper.ComputeSpeedToReachTargetWithElevation ( Vector3  startPosition,
Vector3  targetPosition,
float  elevationRadians,
float  gravity_negative,
ref float  outSpeed 
)
static

Determine the speed needed to reach a target position given a starting elevation.

Parameters
startPositionStarting position of the projectile.
targetPositionTarget position of the projectile.
elevationRadiansElevation angle the projectile will be fired at. This should be less than PiBy2 radians, ie 90degrees.
gravity_negativeAcceleration due to gravity on y-axis. Should be negative, eg -9.8f
outSpeedOutput: The calculated speed needed.
Returns
Returns true if the target can be reached with the given elevation, otherwise false

◆ ComputeTimeToHitGround()

static bool ProjectileHelper.ComputeTimeToHitGround ( Vector3  startPosition,
Vector3  velocity,
float  groundLevel,
float  gravity_negative,
out float  timeToHit 
)
static

Given a start position, initial velocity (with direction) and gravity, how much time is needed to reach the ground (at a specified height level). Use case: Artillery has begun firing upon your position. How much time do you have to evacuate / take cover?

Parameters
startPositionStarting position of the projectile.
velocityInitial velocity of the projectile.
groundLevelThe y-value of the ground level.
gravity_negativeAcceleration due to gravity on y-axis. Should be negative, eg -9.8f
timeToHitOutput: Time to reach the groundlevel specified.
Returns
Returns true if the ground level can be reached with the given parameters.

◆ ComputeVelocityAtTimeAhead()

static Vector3 ProjectileHelper.ComputeVelocityAtTimeAhead ( Vector3  currentPosition,
Vector3  currentVelocity,
float  gravity_negative,
float  timeAhead 
)
static

Determine the velocity of a projectile after a certain time, given its current position and velocity.

Parameters
currentPositionCurrent position of the projectile.
currentVelocityCurrent velocity of the projectile.
gravity_negativeAcceleration due to gravity on y-axis. Should be negative, eg -9.8f
timeAheadThe amount of time in the future at which you want to know the velocity
Returns
Returns the velocity of the projectile after the specified time.

◆ ComputeVelocityToHitTargetAtTime()

static Vector3 ProjectileHelper.ComputeVelocityToHitTargetAtTime ( Vector3  startPosition,
Vector3  targetPosition,
float  gravity_negative,
float  timeToTargetPosition 
)
static

Determine the velocity needed to hit a target position in exactly the given amount of time. Use case: you want to fire a catapult projectile to land at a specific position, giving the player exactly 4 seconds of warning time to escape

Parameters
startPositionStarting position of the projectile.
targetPositionTarget position of the projectile.
gravity_negativeAcceleration due to gravity on y-axis. Should be negative, eg -9.8f
timeToTargetPositionThe amount of time the projectile should take to reach the target
Returns
Returns the necessary initial velocity of the projectile.

◆ UpdateProjectile()

static void ProjectileHelper.UpdateProjectile ( ref Vector3  currentPosition,
ref Vector3  currentVelocity,
float  gravity_negative,
float  deltaTime 
)
static

Update a projectile's current position and velocity, given the gravity and delta time

Parameters
currentPositionCurrent position of the projectile. Will be modified to give the new position.
currentVelocityCurrent velocity of the projectile. Will be modified to give the new velocity.
gravity_negativeAcceleration due to gravity on y-axis. Should be negative, eg -9.8f
deltaTimeThe amount of time elapsed since the last update

The documentation for this class was generated from the following file: