POLYGON/POLYGON
Our final example in this section checks for the collision of two polygons. Since we really just need to check if any of the sides of one polygon are hitting any of the sides of the other, this is pretty straight-forward! As before, we also test if the one polygon is fully inside the other one.
Here's the full example:
// array of Vectors for each shapevar pentagon = 5;var randomPoly = ; { var canvas = ; canvasparent"sketch"; ; // set position of the pentagon's vertices var angle = TWO_PI / pentagonlength; for var i = 0; i < pentagonlength; i++ var a = angle * i; var x = 300 + * 100; var y = 200 + * 100; pentagoni = ; // and create the random polygon var a = 0; var i = 0; while a < 360 var x = * ; var y = * ; randomPolyi = ; a += ; i += 1; } { ; // update random polygon to mouse position var mouse = ; var diff = p5Vector; for var i = 0; i < randomPolylength; i++ randomPolyi; // check for collision // if hit, change fill color var hit = ; if hit ; else ; // draw the pentagon ; ; for var i = 0; i < pentagonlength; i++ ; ; // draw the random polygon ; ; for var i = 0; i < randomPolylength; i++ ; ;} // POLYGON/POLYGON { // go through each of the vertices, plus the next // vertex in the list var next = 0; for var current = 0; current < p1length; current++ // get next vertex in list // if we've hit the end, wrap around to 0 next = current + 1; if next == p1length next = 0; // get the PVectors at our current position // this makes our if statement a little cleaner var vc = p1current; // c for "current" var vn = p1next; // n for "next" // now we can use these two points (a line) to compare // to the other polygon's vertices using polyLine() var collision = ; if collision return true; // optional: check if the 2nd polygon is INSIDE the first collision = ; if collision return true; return false;} // POLYGON/LINE { // 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 // extract X/Y coordinates from each var x3 = verticescurrentx; var y3 = verticescurrenty; var x4 = verticesnextx; var y4 = verticesnexty; // do a Line/Line comparison // if true, return 'true' immediately and // stop testing (faster) var hit = ; if hit return true; // never got a hit 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// used only to check if the second polygon is INSIDE the first { 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;}