Skip to content

Commit

Permalink
testing easier dynamic motion with 2d
Browse files Browse the repository at this point in the history
  • Loading branch information
hamaluik committed Nov 14, 2017
1 parent 1dcaa97 commit 685ff01
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 31 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 = shapeB.centre - shapeA.centre;
direction = new Vec2(1, 0);//shapeB.centre - shapeA.centre;
}
case 1: {
// flip the direction
Expand Down
2 changes: 1 addition & 1 deletion src/headbutt/Shape2D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package headbutt;
import glm.Vec2;

interface Shape2D {
public var centre(get, set):Vec2;
public var offset(get, set):Vec2;
public function support(direction:Vec2):Vec2;
}
19 changes: 10 additions & 9 deletions src/headbutt/shapes/Circle.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ using glm.Vec2;
import headbutt.Shape2D;

class Circle implements Shape2D {
public var centre(get, set):Vec2;
private var _centre:Vec2;
public var offset(get, set):Vec2;
private var _offset:Vec2;

public var radius:Float;

public function new(centre:Vec2, radius:Float) {
this.centre = centre;
public function new(radius:Float) {
this.radius = radius;
this.offset = new Vec2(0, 0);
}

private function get_centre():Vec2 {
return _centre;
private function get_offset():Vec2 {
return _offset;
}

private function set_centre(c:Vec2):Vec2 {
return _centre = c;
private function set_offset(c:Vec2):Vec2 {
return _offset = c;
}

public function support(direction:Vec2):Vec2 {
var c:Vec2 = centre.copy(new Vec2());
var c:Vec2 = offset.copy(new Vec2());
var d:Vec2 = direction.normalize(new Vec2());
d.multiplyScalar(radius, d);
c.addVec(d, c);
Expand Down
30 changes: 10 additions & 20 deletions src/headbutt/shapes/Polygon2D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,33 @@ using glm.Vec2;
import headbutt.Shape2D;

class Polygon2D implements Shape2D {
public var centre(get, set):Vec2;
public var offset(get, set):Vec2;
private var _offset:Vec2;
public var vertices:Array<Vec2>;

public function new(?vertices:Array<Vec2>) {
this.vertices = vertices;
}

private function get_centre():Vec2 {
var c:Vec2 = new Vec2();
var count:Float = 0.0;
for(v in vertices) {
c.addVec(v, c);
count += 1.0;
}
c.multiplyScalar(1.0 / count, c);

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

private function set_centre(c:Vec2):Vec2 {
var diff:Vec2 = centre;
c.subtractVec(diff, diff);
for(vert in vertices) {
vert.addVec(diff, vert);
}
return c;
private function set_offset(c:Vec2):Vec2 {
return _offset = c;
}

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

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

Expand Down

0 comments on commit 685ff01

Please sign in to comment.