Latest stable release: http://lib.haxe.org/p/hxmath
API documentation: http://tbrosman.github.io/hxmath
What it is
A game-oriented math library for the Haxe language using abstracts instead of classes to allow for more expressive code while still using OpenFL's math types internally. Specifically, the 2D math abstracts use OpenFL types like flash.geom.Point and flash.geom.Matrix when
HXMATH_USE_OPENFL_STRUCTURES is defined at compile time.
2D math is stable/reasonably fast on Flash and C++. All structures and most operations have test coverage. There are additional features planned, but most of these are beyond the domain of the core math (hxmath.math) structures.
Lightweight and unencumbered
Just math, nothing else. Use with your libraries of choice without including ten tons of redundant infrastructure (memory management, etc).
Why write this:
when you can write this:
(a - b) * (c % d)
('%' chosen due to operator precedence)
Consistency across platforms
Abstracts allow consistency regardless of which implementation type is used.
Using OpenFL? Add
-D HXMATH_USE_OPENFL_STRUCTURES to your build parameters and you can use OpenFL math types seamlessly with hxmath.
For example, since openfl.geom.Point will be the inner type, you can cast to a Vector2 without copying:
var pointA = new flixel.util.FlxPoint(3.0, 2.0); var pointACast:Vector2 = new Vector2(pointA.x, pointA.y); var pointBCast:Vector2 = new flash.geom.Point(2.0, 1.0); trace(pointACast * pointBCast);
Not using OpenFL? hxmath can run without it, falling back on its default inner types.
2D and 3D math
Both affine and linear structures: Vector2, Vector3, Vector4 Matrix2x2, Matrix3x2, Matrix3x3, Matrix4x4 * Quaternion
More expressive than matrices with intuitive to/from notation. Example: say your character has an
armFrame and a
bodyFrame, with the
armFrame oriented at a 90 degree angle to the
bodyFrame and offset by 10 units up, 4 units right:
var armFrame = new Frame2(new Vector2(4.0, 10.0), 90);
To get a point defined in the
armFrame into the `bodyFrame you would write:
var bodyPoint = armFrame.transformFrom(armPoint);
Similarly, to get a point from the
bodyFrame to the
var armPoint = armFrame.transformTo(bodyPoint);
bodyFrame is defined in the
worldFrame, to create a combined transformation from the
// In the from direction: apply armFrame.from followed by bodyFrame.from // (bodyFrame.matrix * armFrame.matrix) // In the to direction: apply bodyFrame.to followed by armFrame.to // (armFrame.inverse().matrix * bodyFrame.inverse().matrix) == (bodyFrame * armFrame).inverse().matrix var armInWorldFrame = bodyFrame.concat(armFrame);
- Operator overloads: All (linear) structures have the following operators:
-, and unary
.subtractWithare available as functions for direct modification of the object. This is due to the fact you cannot overwrite
-=, etc directly and the generated implementations create new objects. For the
*withoperations, no new object is created and the additional structure of the underlying object is preserved.
copyTois like clone, but without re-allocating.
copyFromShapeallow you to copy to/from shape-compatible types without writing custom conversion functions.
Array access (read/write) for linear structures
- On linear structures and other objects as appropriate (e.g. you can interpolate between
*operator is overloaded for multiple right-hand types:
matrix * matrixwill multiply two matrices, whereas
matrix * vectorwill transform a vector. For vectors, the dot product is defined as
vector * vector.
The cross product between two Vector3 structures is defined using
Vector3.xAxis % Vector3.yAxis == Vector3.zAxis.
All matrix indices are column-major and start at 0. For example, m10 is the element in the 2nd column of the 1st row.
All matrix functions are row-major (left-to-right, top-to-bottom) so that when called the syntax mirrors the layout of the matrix.
- More int-math types
- Useful for tilemaps, voxel intersection, etc.
- Polygon intersection (no collision processing, just the intersection portion), volume calculations, etc