Fast 2d geometry math: Vector2, Rectangle, Circle, Matrix2x3 (2D transformation), Circle, BoundingBox, Line2, Segment2, Intersections, Distances, Transitions (animation/tween), Noise, Random numbers.
So the objective is "Be fast"
- API completeness
- Testing
- Use falafel/esprima to create an asm.js build
- More Numerical integrators
- AI: Path-finding, Steer, Backtracking
- IK: FABRIK
- Minkowski distance, euclidean, Manhattan
- Beizer math
- Serialization / De-serialization
- did I miss anything useful?
Performance is based on good practices.
- Avoid new
- Use arrays instead of objects, this is huge performance boost!
- Avoid creating unnecessary variables (reuse intermediate variables) only
create
&clone
methods should create new variables. - Cache every function call to a single variable. example:
Vec2.add
=>vec2_add
, evenMath.*
- If access a multi-dimensional array in a loop, cache the array access.
for(i...) carr=arr[i]; carr[X]
- Do not use
forEach
,map
,every
etc. or other looping method that requireapply
/call
usage, both are costly.
See some performance test that prove it.
funlinify It's a library that do function inline expansion for javascript. It's in early stage but it works perfectly for our usage here.
Obviously I ignore myself in some parts of this library. Feel free to issue me :)
npm install -g grunt
npm install -g grunt-cli
Create distribution packages using browserify and documentation.
-
debug: debug/js-2dmath-browser-debug.js
- argumentify Assert on invalid arguments to properly debug your app.
-
dist: dist/js-2dmath-browser.js
- funlinify inline function
-
dist.min: js-2dmath-browser.min.js
Watch every change and rebuild the distribution code.
See some examples.
- Angles
- Beizer
- Circle
- Intersections
- line2
- Matrix23
- Segment2
- Transitions
- Triangle
- Vec2 collisions
- Vec2
The documentation is autogenerated with falafel see dist.js for more fun! :)
- Vec2
- Line2
- Segment2
- Rectangle
- AABB2
- Circle
- Matrix22
- Matrix23
- Polygon
- Beizer
- Triangle
- Intersection
- Distance
- Transitions
- Xorshift
- Noise
- Collision.SAT
- Collision.GJK
- Collision.Resolve
- Collision.Manifold
How do i know a variable type?
You can't, there is no instanceof or anything like that, everything are numbers/arrays.
I choose to keep track of all types using meaningful naming or enclose the variable in an object like
var movable = {
body: Polygon.create(/*...*/), // could be a circle, change the type...
type: "polygon"
}