From 24bd792b40dc8182e40ffdae084926a16cd824aa Mon Sep 17 00:00:00 2001 From: Ignatiev Mikhail Date: Sun, 2 Apr 2017 14:59:18 +0300 Subject: [PATCH 1/5] Polygon testRay is not taking ?into parameter seriously --- differ/shapes/Polygon.hx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/differ/shapes/Polygon.hx b/differ/shapes/Polygon.hx index 2042478..df9ad0e 100644 --- a/differ/shapes/Polygon.hx +++ b/differ/shapes/Polygon.hx @@ -53,7 +53,7 @@ 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 From 51bf5f2e7516eda1705f90b1512f5032d53843dc Mon Sep 17 00:00:00 2001 From: Ignatiev Mikhail Date: Sun, 2 Apr 2017 17:53:07 +0300 Subject: [PATCH 2/5] removed makeTranslation since it is similar to translate method --- differ/math/Matrix.hx | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) 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); From d43a3f44c291d01064b0843802e2e9ca072b070d Mon Sep 17 00:00:00 2001 From: Ignatiev Mikhail Date: Sun, 2 Apr 2017 18:03:05 +0300 Subject: [PATCH 3/5] +Vector.copy to copy x&y valus from other vector, Vector.transform is now NOT creating a temp vector --- differ/math/Vector.hx | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) 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 From bb2159a02515e51c60af1ff42048788c589663e0 Mon Sep 17 00:00:00 2001 From: Ignatiev Mikhail Date: Sun, 2 Apr 2017 18:07:39 +0300 Subject: [PATCH 4/5] Polygon.transformedVertices is now more memory efficient, moved _transformed from Shape to Polygon since only Polygon is tracking changes of _transformMatrix --- differ/shapes/Polygon.hx | 27 +++++++++++++++------------ differ/shapes/Shape.hx | 10 ++++------ 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/differ/shapes/Polygon.hx b/differ/shapes/Polygon.hx index df9ad0e..0b39a79 100644 --- a/differ/shapes/Polygon.hx +++ b/differ/shapes/Polygon.hx @@ -15,6 +15,7 @@ class Polygon extends Shape { var _transformedVertices : Array; var _vertices : Array; + var _transformed : Bool = false; /** Create a new polygon with a given set of vertices at position x,y. */ @@ -24,7 +25,7 @@ class Polygon extends Shape { name = 'polygon(sides:${vertices.length})'; - _transformedVertices = new Array(); + _transformedVertices = [for (v in _vertices) v.clone()]; _vertices = vertices; } //new @@ -60,12 +61,6 @@ class Polygon extends Shape { /** 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 +141,14 @@ class Polygon extends Shape { function get_transformedVertices() : Array { - if(!_transformed) { - _transformedVertices = new Array(); - _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 +158,13 @@ class Polygon extends Shape { function get_vertices() : Array { 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 ); } From 0a39047e762d48698dbb12a9883dd69bc42d677c Mon Sep 17 00:00:00 2001 From: Ignatiev Mikhail Date: Sun, 2 Apr 2017 18:48:28 +0300 Subject: [PATCH 5/5] oops, order of operations --- differ/shapes/Polygon.hx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/differ/shapes/Polygon.hx b/differ/shapes/Polygon.hx index 0b39a79..3aee125 100644 --- a/differ/shapes/Polygon.hx +++ b/differ/shapes/Polygon.hx @@ -25,8 +25,9 @@ class Polygon extends Shape { name = 'polygon(sides:${vertices.length})'; + _vertices = vertices; _transformedVertices = [for (v in _vertices) v.clone()]; - _vertices = vertices; + } //new