tbrosman / hxmath

A math library for Haxe.
MIT License
92 stars 10 forks source link

HxMath

Build Status

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.

Project Status

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.

Features

Lightweight and unencumbered

Just math, nothing else. Use with your libraries of choice without including ten tons of redundant infrastructure (memory management, etc).

Operator overloads!

Why write this:

a.subtract(b).dot(c.cross(d))

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);

Using Heaps? Adding -D HXMATH_USE_HEAPS_STRUCTURES to your build parameters to use Heaps math types with hxmath instead.

Not using either? hxmath can run without them, falling back on its default inner types or define them manually by overriding hxmath.math.MathTypes.

2D and 3D math

Both affine and linear structures:

Coordinate frames

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 armFrame:

var armPoint = armFrame.transformTo(bodyPoint);

If the bodyFrame is defined in the worldFrame, to create a combined transformation from the armFrame to worldFrame:

// 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);

Conventions

Basic functions/properties

Products

Matrix Indices

The Future