Arcade Jump
Simple crate with utilities for creating jump trajectories for video games.
Introduction
In real-life, a projectile thrown in the air follows a parabolic trajectory. In video games, we want to reproduce this behavior but with some variation. We want to be able to control the jump's height by holding or releasing the jump button. And we want to fall faster than we ascend.
This crate intends to provide a simple and efficient way to create jump trajectories for video games while taking into account all the little edge cases a game developper could think of. (e.g. double jump, triple jump, wall jump, bouncing off an enemy head, bouncing on a bumper, etc..)
This is possible by composing a trajectory based on the four parameters below:
- The initial vertical velocity (Impulse)
- The gravity applied to the object (Gravity)
- The height of peak of the jump (Height)
- The time to reach the peak of the jump (Time)
Each of those four parameters can be computed by picking only two of the others.
Features
This crate is no-std compatible. It supports the num-traits crate.
It provides three different ways to compute the four parameters:
- Using the raw functions
- Using the
Trajectorytype - Using the
compute!procedural macro
Usage
Raw functions
The raw functions for computing each of the four parameters can be found at the root of this crate.
All of those functions return a Result<N, Error> type.
Because most of the computations involve divisions, providing a parameter with a value of zero will result in an error. Some of the functions have a nofailure variants available in the nofailure module:
height_from_time_and_impulseheight_from_time_and_gravityimpulse_from_height_and_gravityimpulse_from_time_and_gravity
Trajectory type
The crate provide a simple trajectory::Trajectory<N> type which contains the four parameters as fields. It is more intended as an example on how to use this crate but still can be used as is if you don't care about computing and storing extra parameters that you may not use.
Compute macro
Finally, the crate provide a compute! procedural macro for writing parameters computation in a less convoluted way.
The macro expect you to write the two parameters you want as input and the one or two parameters you expect as output.
The macro will take care of identifying the type of each of the parameters and forwarding computation errors.
use ;