Instantly share code, notes, and snippets.

# FuzzyWuzzie/AABB.hx Last active Jun 16, 2018

Minkowski Difference Collision Detection
 package ; import openfl.display.Sprite; /** * ... * @author Kenton Hamaluik */ class AABB { public var center:Vector = new Vector(); public var extents:Vector = new Vector(); public var min(get, never):Vector; public function get_min() { return new Vector(center.x - extents.x, center.y - extents.y); } public var max(get, never):Vector; public function get_max() { return new Vector(center.x + extents.x, center.y + extents.y); } public var size(get, never):Vector; public function get_size() { return new Vector(extents.x * 2, extents.y * 2); } public function new(center:Vector, extents:Vector) { this.center = center; this.extents = extents; } public function draw(container:Sprite, colour:Int = 0xffffff) { container.graphics.beginFill(colour, 0.5); container.graphics.drawRect(min.x, min.y, size.x, size.y); container.graphics.endFill(); } public function minkowskiDifference(other:AABB):AABB { var topLeft:Vector = min - other.max; var fullSize:Vector = size + other.size; return new AABB(topLeft + (fullSize / 2), fullSize / 2); } public function closestPointOnBoundsToPoint(point:Vector):Vector { // test x first var minDist:Float = Math.abs(point.x - min.x); var boundsPoint:Vector = new Vector(min.x, point.y); if (Math.abs(max.x - point.x) < minDist) { minDist = Math.abs(max.x - point.x); boundsPoint = new Vector(max.x, point.y); } if (Math.abs(max.y - point.y) < minDist) { minDist = Math.abs(max.y - point.y); boundsPoint = new Vector(point.x, max.y); } if (Math.abs(min.y - point.y) < minDist) { minDist = Math.abs(min.y - point.y); boundsPoint = new Vector(point.x, min.y); } return boundsPoint; } }