diff --git a/differ/math/Matrix.hx b/differ/math/Matrix.hx index 2611a62..d87d18a 100644 --- a/differ/math/Matrix.hx +++ b/differ/math/Matrix.hx @@ -55,19 +55,10 @@ class Matrix { scale( _scale.x, _scale.y ); rotate( _rotation ); - makeTranslation( _position.x, _position.y ); + translate( _position.x, _position.y ); } //compose - public function makeTranslation( _x:Float, _y:Float ) : Matrix { - - tx = _x; - ty = _y; - - return this; - - } //makeTranslation - public function rotate (angle:Float):Void { var cos = Math.cos (angle); diff --git a/differ/math/Vector.hx b/differ/math/Vector.hx index 4e77492..f82255c 100644 --- a/differ/math/Vector.hx +++ b/differ/math/Vector.hx @@ -35,16 +35,22 @@ class Vector { return new Vector(x, y); } //clone + + /** Copy x & y values from a given vector, returns this vector */ + public inline function copy(v:Vector):Vector { + x = v.x; + y = v.y; + + return this; + } /** Transforms Vector based on the given Matrix. Returns this vector, modified. */ public function transform(matrix:Matrix):Vector { + var ox = x; + x = ox*matrix.a + y*matrix.c + matrix.tx; + y = ox*matrix.b + y*matrix.d + matrix.ty; - var v:Vector = clone(); - - v.x = x*matrix.a + y*matrix.c + matrix.tx; - v.y = x*matrix.b + y*matrix.d + matrix.ty; - - return v; + return this; } //transform diff --git a/differ/shapes/Polygon.hx b/differ/shapes/Polygon.hx index 2042478..3aee125 100644 --- a/differ/shapes/Polygon.hx +++ b/differ/shapes/Polygon.hx @@ -15,6 +15,7 @@ class Polygon extends Shape { var _transformedVertices : Array<Vector>; var _vertices : Array<Vector>; + var _transformed : Bool = false; /** Create a new polygon with a given set of vertices at position x,y. */ @@ -24,8 +25,9 @@ class Polygon extends Shape { name = 'polygon(sides:${vertices.length})'; - _transformedVertices = new Array<Vector>(); - _vertices = vertices; + _vertices = vertices; + _transformedVertices = [for (v in _vertices) v.clone()]; + } //new @@ -53,19 +55,13 @@ class Polygon extends Shape { /** Test for a collision with a ray. */ override public function testRay( ray:Ray, ?into:RayCollision ) : RayCollision { - return SAT2D.testRayVsPolygon(ray, this); + return SAT2D.testRayVsPolygon(ray, this, into); } //testRay /** Destroy this polygon and clean up. */ override public function destroy() : Void { - var _count : Int = _vertices.length; - - for(i in 0 ... _count) { - _vertices[i] = null; - } - _transformedVertices = null; _vertices = null; @@ -146,14 +142,14 @@ class Polygon extends Shape { function get_transformedVertices() : Array<Vector> { - if(!_transformed) { - _transformedVertices = new Array<Vector>(); - _transformed = true; + if (_transformed) { + + _transformed = false; var _count : Int = _vertices.length; - for(i in 0..._count) { - _transformedVertices.push( _vertices[i].clone().transform( _transformMatrix ) ); + for (i in 0..._count) { + _transformedVertices[i].copy(_vertices[i]).transform(_transformMatrix); } } @@ -163,5 +159,13 @@ class Polygon extends Shape { function get_vertices() : Array<Vector> { return _vertices; } + + override function refresh_transform() + { + super.refresh_transform(); + + _transformed = true; + } + } //Polygon diff --git a/differ/shapes/Shape.hx b/differ/shapes/Shape.hx index bbd2ab2..19308aa 100644 --- a/differ/shapes/Shape.hx +++ b/differ/shapes/Shape.hx @@ -36,8 +36,7 @@ class Shape { var _scaleX : Float = 1; var _scaleY : Float = 1; - - var _transformed : Bool = false; + var _transformMatrix : Matrix; @@ -57,7 +56,7 @@ class Shape { _scaleY = 1; _transformMatrix = new Matrix(); - _transformMatrix.makeTranslation( _position.x, _position.y ); + _transformMatrix.translate( _position.x, _position.y ); } //new @@ -84,9 +83,8 @@ class Shape { //Getters/Setters function refresh_transform() { - - _transformMatrix.compose( _position, _rotation_radians, _scale ); - _transformed = false; + + _transformMatrix.compose( _position, _rotation_radians, _scale ); }