Skip to content

Instantly share code, notes, and snippets.

@ryzed
Created April 3, 2014 12:45
Show Gist options
  • Save ryzed/9953612 to your computer and use it in GitHub Desktop.
Save ryzed/9953612 to your computer and use it in GitHub Desktop.
simple sat
inline static function collidePolys(a:Array<V3>, b:Array<V3>):Bool
{
var MPOS = Math.POSITIVE_INFINITY;
var MNEG = Math.NEGATIVE_INFINITY;
var result = true;
var aLen = a.length;
var bLen = b.length;
// current poly
var cPoly = a;
var cLen = aLen;
var testPoly = 0;
while ((testPoly < 2) && result)
{
// axis from current poly
// start from last point
var p0 = cPoly[cLen - 1];
for (n in 0...cLen)
{
var p1 = cPoly[n];
// axis (-y, x)
var ax = -(p1.y - p0.y);
var ay = p1.x - p0.x;
// project both polys
var minA = MPOS;
var maxA = MNEG;
for (i in 0...aLen)
{
var p = a[i];
var r = p.x * ax + p.y * ay;
if (r < minA) minA = r;
if (r > maxA) maxA = r;
}
var minB = MPOS;
var maxB = MNEG;
for (i in 0...bLen)
{
var p = b[i];
var r = p.x * ax + p.y * ay;
if (r < minB) minB = r;
if (r > maxB) maxB = r;
}
// check overlap
if ((maxA < minB) || (maxB < minA))
{
result = false;
break;
}
// prev point == curr point
p0 = p1;
}
// next poly
cPoly = b;
cLen = bLen;
testPoly++;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment