Ballistics
Like what you are seeing?
Support us as a GitHub Sponsor and get instant access to all our assets, exclusive tools and assets, escalated support and issue tracking and our gratitude. These articles are made possible by our GitHub Sponsors ... become a sponsor today!
Introduction
Found in namespace:
We recommend using aliases to reduce typing and simplify names.
doing this you can fully qualify the name of this class as
This interface uses features derived from Forrest The Woods by Forrest Thomas Smith. https://github.com/forrestthewoods/lib_fts
What can it do?
Used to solve for ballistic (parabolic) trajectory solutions such as throwing a ball, shooting a cannon, etc. The features of this API can be used to find the proper angle to aim at to hit a given target or to find the velocity to launch a projectile at to achieve a desired arc
Max Range
The first step is typically to determine if the target is within range at all. We can find the max range of a ballistic system with
Solution
The main feature of the Ballistics API is the Solution method which comes in several overloads to meet any need.
Each method has an equivalent 2D variant. for example
3D Solution is
2D Solution is
Simple Trajectory
This overload returns an int ranging from 0 to 2 representing the number of valid solutions found. If the value is greater than 1 then there is at least 1 valid solution.
The valid solutions found will be expressed as rotations for a low-angle and high-angle trajectory.
The same as the previous overload only this overload takes a targetVelocity
and can be used to target a moving object.
Variable Trajectory
While the above solutions are simple we often want to tailor the visuals of a parabolic trajectory such that the angle is not to high or flat and that the projectile has a fixed linear speed as if fired strait at the target. This is the more common solution for simulating say a bow shot where the archer will optimize flight time and range for the target.
As with the previous solution there is an overload for targeting a moving target
These overloads return a boolean indicating whether or not a solution was found. If a solution is found then the firingVelocity and gravity output parameters will be populated.
Fixed Time
On occasion its important for gameplay that the projectile reaches its target at a specific time so we can use the following overload to find the launch velocity. Launch velocity indicates the direction and speed of the launch.
This overload outputs a velocity vector. If you need to know the rotation the object should be on you can do
Fixed Angle
Some use cases require that you launch the projectile from a given angle and so you need to know what "speed" to launch the projectile at.
Raycast
The raycast method can be used to trace a ballistic trajectory for a defined length and test for collision along the way. The resulting steps tested will be returned along with the final direction of the projection and any hit data found.
For 2D casts see Raycast2D
This can be used to test for collision, draw trajectory arcs, plan for bounces, etc.
start The point at which the trajectory should start from
velocity The initial velocity of the projectile
gravity The gravity vector, you can read this from Unity via Physics.Gravity or provide your own
resolution The distance for each step of the traversal
maxLength the total length of the traversal before the process is stopped rather or not it reaches a collision point
collisionLayers The layers to check for collision on
hit If a hit occurs this will be populated with the data
pay A tuple defining each step along the path containing the position, velocity and total flight time up to this point
distance the total length of the path
SphereCast / CircleCast
Casts a ray marching a sphere or circle across the path. Works the same as the Raycast option but can account for the radius of the object which will pass along the path. This is the most common means to check the path of a projectile with a significant geometry such as a ball where as a bullet is often small enough that the faster Raycast method works fine.
start The position the trajectory will start from
startCollider This collider will be ignored for the first radius distance of the traverse and helps to avoid self-collision on secondary bounces. This is optional and can be null
velocity The initial velocity of the projectile
gravity The gravity vector to apply you can read this from Physics.Gravity or Physics2D.Gravity or provide your own
radius The radius of the sphere or circle to be cast
resolution The distance between each step of the traverse
collisionLayers The layers to check for collision on
hit If a hit occurs this will be populated with the data
pay A tuple defining each step along the path containing the position, velocity and total flight time up to this point
distance the total length of the path
Flight Time
Estimating flight time can be done with the projectile velocity, the height difference it must travel from start to end and the effect of gravity.
Note that this is an estimate assuming the projectile will travel the full length of a typical trajectory.
Last updated