Skip to content

Commit

Permalink
Merge pull request #5 from hamaluik/normalizing_shape_api
Browse files Browse the repository at this point in the history
Normalize Shape API
  • Loading branch information
hamaluik authored Feb 20, 2019
2 parents ec07226 + 7b7aac4 commit 9020492
Show file tree
Hide file tree
Showing 15 changed files with 313 additions and 220 deletions.
2 changes: 1 addition & 1 deletion src/headbutt/Headbutt2D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Headbutt2D {
private function evolveSimplex():EvolveResult {
switch(vertices.length) {
case 0: {
direction = new Vec2(1, 0);//shapeB.centre - shapeA.centre;
direction = shapeB.origin - shapeA.origin;
}
case 1: {
// flip the direction
Expand Down
2 changes: 1 addition & 1 deletion src/headbutt/Headbutt3D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Headbutt3D {
private function evolveSimplex():EvolveResult {
switch(vertices.length) {
case 0: {
direction = shapeB.centre - shapeA.centre;
direction = shapeB.origin - shapeA.origin;
}
case 1: {
// flip the direction
Expand Down
3 changes: 2 additions & 1 deletion src/headbutt/Shape2D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package headbutt;
import glm.Vec2;

interface Shape2D {
public function support(direction:Vec2):Vec2;
public var origin(get, set): Vec2;
public function support(direction: Vec2): Vec2;
}
2 changes: 1 addition & 1 deletion src/headbutt/Shape3D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package headbutt;
import glm.Vec3;

interface Shape3D {
public var centre(get, set):Vec3;
public var origin(get, set):Vec3;
public function support(direction:Vec3):Vec3;
}
30 changes: 17 additions & 13 deletions src/headbutt/shapes/Circle.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@ using glm.Vec2;
import headbutt.Shape2D;

class Circle implements Shape2D {
public var offset(get, set):Vec2;
private var _offset:Vec2;
private var _origin: Vec2;
public var origin(get, set): Vec2;
public var radius: Float;

public var radius:Float;

public function new(radius:Float) {
/**
Create a new circle
@param origin The centre of the circle in global coordinates
@param radius The radius of the circle
*/
public function new(origin: Vec2, radius: Float) {
this.origin = origin;
this.radius = radius;
this.offset = new Vec2(0, 0);
}

private function get_offset():Vec2 {
return _offset;
function get_origin(): Vec2 {
return _origin;
}

private function set_offset(c:Vec2):Vec2 {
return _offset = c;
function set_origin(origin: Vec2): Vec2 {
return _origin = origin;
}

public function support(direction:Vec2):Vec2 {
var c:Vec2 = offset.copy(new Vec2());
var d:Vec2 = direction.normalize(new Vec2());
public function support(direction: Vec2): Vec2 {
var c: Vec2 = origin.copy(new Vec2());
var d: Vec2 = direction.normalize(new Vec2());
d.multiplyScalar(radius, d);
c.addVec(d, c);
return c;
Expand Down
50 changes: 50 additions & 0 deletions src/headbutt/shapes/Line2D.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package headbutt.shapes;

using glm.Vec2;
import headbutt.Shape2D;

class Line2D implements Shape2D {
/**
Start is in global coordinates
*/
public var start: Vec2;

/**
End is in global coordinates
*/
public var end: Vec2;

private var _origin: Vec2 = new Vec2(0, 0);
public var origin(get, set): Vec2;

public function new(start: Vec2, end: Vec2) {
this.start = start;
this.end = end;
}

function get_origin(): Vec2 {
_origin.x = (start.x + end.x) / 2;
_origin.y = (start.y + end.y) / 2;
return _origin;
}

function set_origin(origin: Vec2): Vec2 {
var dx: Float = origin.x - _origin.x;
var dy: Float = origin.y - _origin.y;
start.x += dx;
end.x += dx;
start.y += dy;
end.y += dy;

return _origin = origin;
}

public function support(direction: Vec2): Vec2 {
if(Vec2.dot(start, direction) > Vec2.dot(end, direction)) {
return start;
}
else {
return end;
}
}
}
52 changes: 52 additions & 0 deletions src/headbutt/shapes/Polygon.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package headbutt.shapes;

using glm.Vec2;
import headbutt.Shape2D;

class Polygon implements Shape2D {
private var _origin: Vec2;
/**
The origin of the polygon in global coordinate space
*/
public var origin(get, set): Vec2;

/**
The vertices in the local coordinate space
*/
public var vertices:Array<Vec2>;

/**
Create a new polygon
@param origin The location of the polygon in global coordinates
@param vertices The locations of the vertices in local coordinates
*/
public function new(origin: Vec2, vertices: Array<Vec2>) {
this.origin = origin;
this.vertices = vertices;
}

function get_origin(): Vec2 {
return _origin;
}

function set_origin(origin: Vec2): Vec2 {
return _origin = origin;
}

public function support(direction: Vec2): Vec2 {
var furthestDistance: Float = Math.NEGATIVE_INFINITY;
var furthestVertex: Vec2 = new Vec2(0, 0);

var vo: Vec2 = new Vec2();
for(v in vertices) {
vo = v.addVec(origin, vo);
var distance: Float = Vec2.dot(vo, direction);
if(distance > furthestDistance) {
furthestDistance = distance;
furthestVertex = vo.copy(furthestVertex);
}
}

return furthestVertex;
}
}
40 changes: 0 additions & 40 deletions src/headbutt/shapes/Polygon2D.hx

This file was deleted.

49 changes: 0 additions & 49 deletions src/headbutt/shapes/Polygon3D.hx

This file was deleted.

52 changes: 52 additions & 0 deletions src/headbutt/shapes/Polyhedron.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package headbutt.shapes;

using glm.Vec3;
import headbutt.Shape3D;

class Polyhedron implements Shape3D {
private var _origin: Vec3;
/**
The origin of the polygon in global coordinate space
*/
public var origin(get, set): Vec3;

/**
The vertices in the local coordinate space
*/
public var vertices:Array<Vec3>;

/**
Create a new polygon
@param origin The location of the polygon in global coordinates
@param vertices The locations of the vertices in local coordinates
*/
public function new(origin: Vec3, vertices: Array<Vec3>) {
this.origin = origin;
this.vertices = vertices;
}

function get_origin(): Vec3 {
return _origin;
}

function set_origin(origin: Vec3): Vec3 {
return _origin = origin;
}

public function support(direction: Vec3): Vec3 {
var furthestDistance: Float = Math.NEGATIVE_INFINITY;
var furthestVertex: Vec3 = new Vec3(0, 0, 0);

var vo: Vec3 = new Vec3();
for(v in vertices) {
vo = v.addVec(origin, vo);
var distance: Float = Vec3.dot(vo, direction);
if(distance > furthestDistance) {
furthestDistance = distance;
furthestVertex = vo.copy(furthestVertex);
}
}

return furthestVertex;
}
}
58 changes: 58 additions & 0 deletions src/headbutt/shapes/Rectangle.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package headbutt.shapes;

using glm.Vec2;
import headbutt.Shape2D;
import haxe.ds.Vector;

class Rectangle implements Shape2D {
private var _origin: Vec2;
public var origin(get, set): Vec2;

var vertices: Vector<Vec2>;

public function new(origin: Vec2, halfsize: Vec2) {
this.origin = origin;
this.vertices = new Vector<Vec2>(4);
this.vertices[0] = new Vec2();
this.vertices[1] = new Vec2();
this.vertices[2] = new Vec2();
this.vertices[3] = new Vec2();
resize(halfsize);
}

function get_origin(): Vec2 {
return _origin;
}

function set_origin(origin: Vec2): Vec2 {
return _origin = origin;
}

public function resize(halfsize: Vec2): Void {
this.vertices[0].x = -1 * halfsize.x;
this.vertices[0].y = -1 * halfsize.y;
this.vertices[1].x = 1 * halfsize.x;
this.vertices[1].y = -1 * halfsize.y;
this.vertices[2].x = 1 * halfsize.x;
this.vertices[2].y = 1 * halfsize.y;
this.vertices[3].x = -1 * halfsize.x;
this.vertices[3].y = 1 * halfsize.y;
}

public function support(direction: Vec2): Vec2 {
var furthestDistance: Float = Math.NEGATIVE_INFINITY;
var furthestVertex: Vec2 = new Vec2();

var vo: Vec2 = new Vec2();
for(v in vertices) {
vo = v.addVec(origin, vo);
var distance: Float = Vec2.dot(vo, direction);
if(distance > furthestDistance) {
furthestDistance = distance;
furthestVertex = vo.copy(furthestVertex);
}
}

return furthestVertex;
}
}
Loading

0 comments on commit 9020492

Please sign in to comment.