Skip to main content Module math_functions Copy item path Source Aabb Axis-aligned bounding box CosSin Cosine and sine pair
This uses a custom implementation designed for cross-platform determinism Mat22 A 2-by-2 Matrix stored as columns Plane separation = dot(normal, point) - offset Rot 2D rotation
This is similar to using a complex number for rotation Transform A 2D rigid transform Vec2 2D vector
This can be used to represent a point or free vector MAT22_ZERO PI https://en.wikipedia.org/wiki/Pi
The C B2_PI literal (3.14159265359f) rounds to exactly this f32 value.POS_ZERO ROT_IDENTITY TRANSFORM_IDENTITY VEC2_ZERO WORLD_TRANSFORM_IDENTITY aabb_center Get the center of the AABB. aabb_contains Does a fully contain b aabb_extents Get the extents of the AABB (half-widths). aabb_overlaps Do a and b overlap aabb_union Union of two AABBs abs Component-wise absolute vector abs_float @return the absolute value of a float abs_int @return the absolute value of an integer add Vector addition atan2 Compute an approximate arctangent in the range [-pi, pi]
This is hand coded for cross-platform determinism. The atan2f
function in the standard library is not cross-platform deterministic.
Accurate to around 0.0023 degrees ceiling_int https://en.wikipedia.org/wiki/Floor_and_ceiling_functions clamp Component-wise clamp vector v into the range [a, b] clamp_float @return a float clamped between a lower and upper bound clamp_int @return an integer clamped between a lower and upper bound compute_angular_velocity Compute the angular velocity necessary to rotate between two rotations over a given time compute_cos_sin Compute the cosine and sine of an angle in radians. Implemented
for cross-platform determinism. compute_rotation_between_unit_vectors Compute the rotation between two unit vectors cross Vector cross product. In 2D this yields a scalar. cross_sv Perform the cross product on a scalar and a vector. In 2D this produces a vector. cross_vs Perform the cross product on a vector and a scalar. In 2D this produces a vector. distance Get the distance between two points distance_squared Get the distance squared between points dot Vector dot product get_inverse_22 Get the inverse of a 2-by-2 matrix get_length_and_normalize Convert a vector into a unit vector if possible, otherwise returns the zero vector. Also
outputs the length. integrate_rotation Integrate rotation from angular velocity inv_mul_rot Transpose multiply two rotations: inv(a) * b
This rotates a vector local in frame b into a vector local in frame a inv_mul_transforms Creates a transform that converts a local point in frame B to a local point in frame A.
v2 = A.q’ * (B.q * v1 + B.p - A.p)
= A.q’ * B.q * v1 + A.q’ * (B.p - A.p) inv_mul_world_transforms Relative transform of frame B in frame A. inv_rotate_vector Inverse rotate a vector inv_transform_point Inverse transform a point (e.g. world space to local space) inv_transform_world_point Transform a world position to a local point. One double subtraction, then float. invert_rot Get the inverse of a rotation is_normalized Determines if the provided vector is normalized (norm(a) == 1). is_normalized_rot Is this rotation normalized? is_valid_aabb Is this a valid bounding box? Not NaN or infinity. Upper bound greater than or equal to lower bound. is_valid_float Is this a valid number? Not NaN or infinity. is_valid_plane Is this a valid plane? Normal is a unit vector. Not NaN or infinity. is_valid_position Is this a valid world position? Not NaN or infinity. is_valid_rotation Is this a valid rotation? Not NaN or infinity. Is normalized. is_valid_transform Is this a valid transform? Not NaN or infinity. Rotation is normalized. is_valid_vec2 Is this a valid vector? Not NaN or infinity. is_valid_world_transform Is this a valid world transform? Not NaN or infinity. Rotation is normalized. left_perp Get a left pointing perpendicular vector. Equivalent to cross_sv(1.0, v) length Get the length of this vector (the norm) length_squared Get the length squared of this vector lerp Vector linear interpolation
https://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/ lerp_position World position interpolation for sweeps and sampling. make_aabb Compute the bounding box of an array of points make_rot Make a rotation using an angle in radians make_rot_from_unit_vector Make a rotation using a unit vector make_world_transform Promote a float transform to a world transform. Lossless. max Component-wise maximum vector max_float @return the maximum of two floats max_int @return the maximum of two integers min Component-wise minimum vector min_float @return the minimum of two floats
Matches the C ternary exactly, including NaN propagation (a < b is false for NaN). min_int @return the minimum of two integers mul Component-wise multiplication mul_add a + s * b mul_mv Multiply a 2-by-2 matrix times a 2D vector mul_rot Multiply two rotations: q * r mul_sub a - s * b mul_sv Multiply a scalar and vector mul_transforms Multiply two transforms. If the result is applied to a point p local to frame B,
the transform would first convert p to a point local to frame A, then into a point
in the world frame.
v2 = A.q.Rot(B.q.Rot(v1) + B.p) + A.p
= (A.q * B.q).Rot(v1) + A.q.Rot(B.p) + A.p neg Vector negation nlerp Normalized linear interpolation
https://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-present-and-future/
https://web.archive.org/web/20170825184056/http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/ normalize Convert a vector into a unit vector if possible, otherwise returns the zero vector. normalize_rot Normalize rotation offset_pos p + d offset_world_transform Convert a local transform B into world space using world transform A. plane_separation Signed separation of a point from a plane relative_angle Relative angle between a and b right_perp Get a right pointing perpendicular vector. Equivalent to cross_vs(v, 1.0) rot_get_angle Get the angle in radians in the range [-pi, pi] rot_get_x_axis Get the x-axis rot_get_y_axis Get the y-axis rotate_vector Rotate a vector round_down_float round_up_float solve_22 Solve A * x = b, where b is a column vector. This is more efficient
than computing the inverse in one-shot cases. spring_damper One-dimensional mass-spring-damper simulation. Returns the new velocity given the position and time step.
You can then compute the new position using:
position += time_step * new_velocity
This drives towards a zero position. By using implicit integration we get a stable solution
that doesn’t require transcendental functions. sub Vector subtraction sub_pos a - b, demoted to float. The primary precision boundary operation. to_pos Convert a vector to a world position. no-op in single precision. to_relative_transform Shift a world transform into the frame of a base position. to_vec2 Lossy conversion of a world position to a float vector. no-op in single precision. transform_point Transform a point (e.g. local space to world space) transform_world_point Transform a local point to a world position. Rotation in float, translation in double. unwind_angle Convert any angle into the range [-pi, pi] Pos WorldTransform