Math
The small maths toolbox the rest of the engine leans on: vectors, shapes, collision tests and interpolation.
The concept
2D games are mostly vectors and rectangles. Keeping them in one dependency-free module, rather than reaching into SDL types, means gameplay, physics, rendering and the editor all speak the same small set of types.
Vectors
A 2D vector is a pair of numbers that can mean a position, a direction or a displacement. Adding vectors moves things, a scalar multiple stretches them, and its length gives distance or speed. Working with vectors instead of separate x and y variables keeps formulas short and hard to get wrong.
Shapes and overlap tests
Two axis-aligned rectangles overlap exactly when they overlap on both axes, which costs a handful of comparisons. Two circles overlap when the distance between their centres is smaller than the sum of the radii. Cheap tests like these are why games approximate complicated art with simple collision shapes.
Penetration
Knowing that two shapes overlap is not enough to react; you also need to know how to separate them. The minimum translation vector is the shortest move that makes them just touch. For rectangles it is the smaller of the overlaps on the two axes; for circles it lies along the line between the centres.
Interpolation and easing
Interpolating means blending between two values by a fraction t from 0 to 1. Feeding t through
an easing function makes motion start slowly, end softly or overshoot, which is what makes UI and animation feel
natural. Splines extend this idea to smooth curves through several points. Floating-point rounding means tests on values
should allow a small tolerance instead of exact equality.
How ASGE does it
Linear algebra
asge::math::Vec2<T> (and Vec3), built on a shared VecN, with the common
aliases Float2, Double2, Int2 and Int642. Arithmetic operators are
provided; positions, sizes and mouse deltas throughout the API are Float2 or Int2.
Geometry and collision
Rect and Circle are the two collider shapes. Collision.hpp has the overlap tests
(AabbOverlap, CircleOverlap) and PenetrationVector overloads for every
rect/circle pairing, returning an std::optional<Float2> that says how to push the first shape out of the
second. Physics is built on these.
Interpolation
EasingandEase(easing, t):Linear,InQuad,OutQuad,InOutQuad,OutCubic; the input is clamped to [0, 1].Tween<T>: moves a float,Float2or similar from one value to another over a duration; callUpdate(dt)each frame and readValue().CatmullRomSpline: a smooth curve through waypoints, with arc-length sampling so motion along it is at constant speed.PathFollowingSystemand the editor's path preview use it.
using asge::math::Easing;
asge::math::Tween<float> slide( 0.0f, 100.0f, /* seconds */ 2.0f, Easing::OutQuad );
// each frame:
slide.Update( dt );
float x = slide.Value(); // eased value; slide.IsFinished() once the duration has elapsed
See also
Source: Core/Math/Math.hpp, Core/Math/Geometry/Collision.hpp, Core/Math/Interpolation/Tween.hpp.
Rotation demo · Path following demo