Skip to content

Commit

Permalink
added line3d and box
Browse files Browse the repository at this point in the history
  • Loading branch information
hamaluik committed Feb 21, 2019
1 parent 8a1cc7d commit 38def40
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/headbutt/threed/shapes/Box.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package headbutt.threed.shapes;

using glm.Vec3;
import headbutt.threed.Shape;
import haxe.ds.Vector;

class Box implements Shape {
private var _origin: Vec3;
public var origin(get, set): Vec3;

var vertices: Vector<Vec3>;

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

function get_origin(): Vec3 {
return _origin;
}

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

public function resize(halfsize: Vec3): Void {
this.vertices[0].x = -1 * halfsize.x;
this.vertices[0].y = -1 * halfsize.y;
this.vertices[0].z = -1 * halfsize.z;
this.vertices[1].x = 1 * halfsize.x;
this.vertices[1].y = -1 * halfsize.y;
this.vertices[1].z = -1 * halfsize.z;
this.vertices[2].x = 1 * halfsize.x;
this.vertices[2].y = 1 * halfsize.y;
this.vertices[2].z = -1 * halfsize.z;
this.vertices[3].x = -1 * halfsize.x;
this.vertices[3].y = 1 * halfsize.y;
this.vertices[3].z = -1 * halfsize.z;
this.vertices[4].x = -1 * halfsize.x;
this.vertices[4].y = -1 * halfsize.y;
this.vertices[4].z = 1 * halfsize.z;
this.vertices[5].x = 1 * halfsize.x;
this.vertices[5].y = -1 * halfsize.y;
this.vertices[5].z = 1 * halfsize.z;
this.vertices[6].x = 1 * halfsize.x;
this.vertices[6].y = 1 * halfsize.y;
this.vertices[6].z = 1 * halfsize.z;
this.vertices[7].x = -1 * halfsize.x;
this.vertices[7].y = 1 * halfsize.y;
this.vertices[7].z = 1 * halfsize.z;
}

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

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;
}
}
50 changes: 50 additions & 0 deletions src/headbutt/threed/shapes/Line.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package headbutt.threed.shapes;

using glm.Vec3;
import headbutt.threed.Shape;

class Line implements Shape {
/**
Start is in global coordinates
*/
public var start: Vec3;

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

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

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

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

function set_origin(origin: Vec3): Vec3 {
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: Vec3): Vec3 {
if(Vec3.dot(start, direction) > Vec3.dot(end, direction)) {
return start;
}
else {
return end;
}
}
}
16 changes: 16 additions & 0 deletions test/TestHeadbutt3D.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import headbutt.threed.Headbutt;
import headbutt.threed.shapes.Sphere;
import headbutt.threed.shapes.Polyhedron;
import headbutt.threed.shapes.Line;
import headbutt.threed.shapes.Box;
import buddy.*;
using buddy.Should;
import glm.Vec3;
Expand Down Expand Up @@ -84,6 +86,20 @@ class TestHeadbutt3D extends BuddySuite {
result.should.be(true);
});

it('should detect collisions between a line and a sphere', {
var line: Line = new Line(new Vec3(), new Vec3(5, 5, 5));
var sphere: Sphere = new Sphere(new Vec3(3, 3, 3), 1);
var result: Bool = hb.test(line, sphere);
result.should.be(true);
});

it('should detect collisions between two boxes', {
var boxA: Box = new Box(new Vec3(0, 0, 0), new Vec3(1, 1, 1));
var boxB: Box = new Box(new Vec3(0, 0.5, 0), new Vec3(1, 1, 1));
var result: Bool = hb.test(boxA, boxB);
result.should.be(true);
});

it('should calculate the intersection of two spheres');
it('should calculate the intersection of two polyhedrons');
it('should calculate the intersection of a polygon and a sphere');
Expand Down

0 comments on commit 38def40

Please sign in to comment.