RECTANGLE
POLYGON/Like the previous example, collision between a polygon and a rectangle really just requires us to extend existing functions. In this case, we can test if any of the edges of the rectangle are hitting any of the edges of the polygon.
To do this, we test Line/Rectangle collision for each side of the polygon. Like our previous exmaples, vc
and vn
are the two PVectors forming a side:
var collision = ;if collision return true;
Also like the last example, we can catch the edge case where the rectangle is inside the polygon by testing if its X/Y position (a point) is inside the polygon. This should be left off unless necessary, since like our previous example it requires going through all the vertices of the polygon again, slowing down your program.
var inside = ;if inside return true;
Here's a full example:
var sx = 0; // a square, controlled by the mousevar sy = 0;var sw = 30; // width and heightvar sh = 30; // array of Vectors, one for each vertex in the polygonvar vertices = ; { var canvas = ; canvasparent"sketch"; ; // set position of the vertices (here a parallelogram) vertices0 = ; vertices1 = ; vertices2 = ; vertices3 = ;} { ; // update circle to mouse coordinates sx = mouseX - sw / 2; sy = mouseY - sh / 2; // check for collision // if hit, change fill color var hit = ; if hit ; else ; // draw the polygon using beginShape() ; ; for var i = 0; i < verticeslength; i++ ; ; // draw the rectangle ; ;} // POLYGON/RECTANGLE { // go through each of the vertices, plus the next // vertex in the list var next = 0; for var current = 0; current < verticeslength; current++ // get next vertex in list // if we've hit the end, wrap around to 0 next = current + 1; if next == verticeslength next = 0; // get the PVectors at our current position // this makes our if statement a little cleaner var vc = verticescurrent; // c for "current" var vn = verticesnext; // n for "next" // check against all four sides of the rectangle var collision = ; if collision return true; // optional: test if the rectangle is INSIDE the polygon // note that this iterates all sides of the polygon // again, so only use this if you need to var inside = ; if inside return true; return false;} // LINE/RECTANGLE { // check if the line has hit any of the rectangle's sides // uses the Line/Line function below var left = ; var right = ; var top = ; var bottom = ; // if ANY of the above are true, the line // has hit the rectangle if left || right || top || bottom return true; return false;} // LINE/LINE { // calculate the distance to intersection point var uA = x4 - x3 * y1 - y3 - y4 - y3 * x1 - x3 / y4 - y3 * x2 - x1 - x4 - x3 * y2 - y1; var uB = x2 - x1 * y1 - y3 - y2 - y1 * x1 - x3 / y4 - y3 * x2 - x1 - x4 - x3 * y2 - y1; // if uA and uB are between 0-1, lines are colliding if uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1 return true; return false;} // POLYGON/POINT { var collision = false; // go through each of the vertices, plus // the next vertex in the list var next = 0; for var current = 0; current < verticeslength; current++ // get next vertex in list // if we've hit the end, wrap around to 0 next = current + 1; if next == verticeslength next = 0; // get the PVectors at our current position // this makes our if statement a little cleaner var vc = verticescurrent; // c for "current" var vn = verticesnext; // n for "next" // compare position, flip 'collision' variable // back and forth if vcy > py && vny < py || vcy < py && vny > py && px < vnx - vcx * py - vcy / vny - vcy + vcx collision = !collision; return collision;}