Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory efficiency, Api incosistency and other small but usefull fixes :) #23

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions differ/math/Matrix.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 12 additions & 6 deletions differ/math/Vector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 18 additions & 14 deletions differ/shapes/Polygon.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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
10 changes: 4 additions & 6 deletions differ/shapes/Shape.hx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class Shape {

var _scaleX : Float = 1;
var _scaleY : Float = 1;

var _transformed : Bool = false;

var _transformMatrix : Matrix;


Expand All @@ -57,7 +56,7 @@ class Shape {
_scaleY = 1;

_transformMatrix = new Matrix();
_transformMatrix.makeTranslation( _position.x, _position.y );
_transformMatrix.translate( _position.x, _position.y );

} //new

Expand All @@ -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 );

}

Expand Down