├── CollisionLogo.ai ├── CollisionLogo.png ├── Examples ├── ballBall_Example │ └── ballBall_Example.pde ├── ballLine_Example │ └── ballLine_Example.pde ├── lineLine_Example │ └── lineLine_Example.pde ├── pointBall_Example │ └── pointBall_Example.pde ├── pointLine_Example │ └── pointLine_Example.pde ├── pointPoint_Example │ └── pointPoint_Example.pde ├── pointPolygon_Example │ └── pointPolygon_Example.pde ├── pointTriangle_Example │ └── pointTriangle_Example.pde ├── rectBall_Example │ └── rectBall_Example.pde └── rectRect_Example │ └── rectRect_Example.pde ├── PointRect └── PointRect.pde ├── README.md ├── ballBall └── ballBall.pde ├── ballLine └── ballLine.pde ├── lineLine └── lineLine.pde ├── linePlane └── linePlane.pde ├── pointBall └── pointBall.pde ├── pointLine └── pointLine.pde ├── pointPoint └── pointPoint.pde ├── pointPolygon └── pointPolygon.pde ├── pointTriangle └── pointTriangle.pde ├── rectBall └── rectBall.pde └── rectRect └── rectRect.pde /CollisionLogo.ai: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffThompson/CollisionDetectionFunctionsForProcessing/b06af14e5f35afe29ad7d355dc157ae4d3686747/CollisionLogo.ai -------------------------------------------------------------------------------- /CollisionLogo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeffThompson/CollisionDetectionFunctionsForProcessing/b06af14e5f35afe29ad7d355dc157ae4d3686747/CollisionLogo.png -------------------------------------------------------------------------------- /Examples/ballBall_Example/ballBall_Example.pde: -------------------------------------------------------------------------------- 1 | /* 2 | BALL/BALL COLLISION DETECTION 3 | Jeff Thompson 4 | Fall 2011 5 | 6 | www.jeffreythompson.org 7 | */ 8 | 9 | int youX, youY; // x,y position of "you" 10 | int youSize = 30; // assumes a circle - elliptical collision is VERY complicated 11 | int speed = 2; // speed to move you around 12 | 13 | int ballX, ballY; // x,y position of the ball - will be randomly placed in the setup 14 | int ballSize = 100; // assumes a circle - elliptical collision is VERY complicated 15 | 16 | 17 | void setup() { 18 | 19 | // BASIC SETUP STUFF 20 | size(400, 400); 21 | smooth(); 22 | noStroke(); 23 | 24 | // PLACE "YOU" IN THE CENTER 25 | youX = width/2; 26 | youY = height/2; 27 | 28 | // STATIC RECTANGLE IN RANDOM POSITION ON SCREEN 29 | ballX = int(random(ballSize, width-ballSize)); 30 | ballY = int(random(ballSize, height-ballSize)); 31 | } 32 | 33 | void draw() { 34 | 35 | // REDRAW EACH FRAME 36 | background(255); 37 | 38 | // TEST FOR COLLISION 39 | // returns true if hit, false if not 40 | if (ballBall(youX, youY, youSize, ballX, ballY, ballSize) == true) { 41 | fill(0); 42 | } 43 | else { 44 | fill(200); 45 | } 46 | 47 | // DRAW STATIC BALL 48 | ellipse(ballX, ballY, ballSize, ballSize); 49 | 50 | // DRAW "YOU" 51 | fill(255, 0, 0); 52 | ellipse(youX, youY, youSize,youSize); 53 | 54 | // IF ARROW KEYS ARE PRESSED, UPDATE "YOU" POSITION 55 | if (keyPressed) { 56 | if (key == CODED) { 57 | if (keyCode == LEFT) { 58 | youX -= speed; 59 | } 60 | if (keyCode == RIGHT) { 61 | youX += speed; 62 | } 63 | if (keyCode == UP) { 64 | youY -= speed; 65 | } 66 | if (keyCode == DOWN) { 67 | youY += speed; 68 | } 69 | } 70 | } 71 | } 72 | 73 | /* 74 | BALL/BALL COLLISION FUNCTION 75 | Takes 6 arguments: 76 | + x,y position of the first ball - in this case "you" 77 | + diameter of first ball - elliptical collision is VERY difficult 78 | + x,y position of the second ball 79 | + diameter of second ball 80 | 81 | */ 82 | boolean ballBall(int x1, int y1, int d1, int x2, int y2, int d2) { 83 | 84 | // find distance between the two objects 85 | float xDist = x1-x2; // distance horiz 86 | float yDist = y1-y2; // distance vert 87 | float distance = sqrt((xDist*xDist) + (yDist*yDist)); // diagonal distance 88 | 89 | // test for collision 90 | if (d1/2 + d2/2 > distance) { 91 | return true; // if a hit, return true 92 | } 93 | else { // if not, return false 94 | return false; 95 | } 96 | } 97 | 98 | -------------------------------------------------------------------------------- /Examples/ballLine_Example/ballLine_Example.pde: -------------------------------------------------------------------------------- 1 | /* 2 | BALL/LINE COLLISION DETECTION 3 | Jeff Thompson 4 | Fall 2011 5 | 6 | Based on a post by Philip Nicoletti (thanks!) 7 | http://www.codeguru.com/forum/showthread.php?threadid=194400 8 | 9 | www.jeffreythompson.org 10 | */ 11 | 12 | // requires all position values be floats - will NOT work with ints 13 | float youX, youY; // x,y position of "you" 14 | int youSize = 30; // diameter of ball 15 | int speed = 2; // speed to move you around 16 | 17 | float lineX1, lineY1, lineX2, lineY2; 18 | 19 | void setup() { 20 | 21 | // BASIC SETUP STUFF 22 | size(400, 400); 23 | smooth(); 24 | strokeWeight(3); // makes seeing things a little easier 25 | 26 | // PLACE "YOU" IN THE UPPER-LEFT QUADRANT 27 | youX = width/4; 28 | youY = height/4; 29 | 30 | // POSITION LINE ACROSS THE SCREEN 31 | lineX1 = 0; 32 | lineY1 = height; 33 | lineX2 = width; 34 | lineY2 = 0; 35 | } 36 | 37 | void draw() { 38 | 39 | // REDRAW EACH FRAME 40 | background(255); 41 | 42 | // DRAW "YOU" 43 | noStroke(); 44 | fill(255, 0, 0); 45 | ellipse(youX, youY, youSize, youSize); 46 | 47 | // TEST FOR COLLISION 48 | // returns true if hit, false if not 49 | if (ballLine(youX, youY, youSize, lineX1, lineY1, lineX2, lineY2) == true) { 50 | stroke(0, 100); 51 | } 52 | else { 53 | stroke(200); 54 | } 55 | 56 | // DRAW STATIC LINE 57 | line(lineX1, lineY1, lineX2, lineY2); 58 | 59 | // IF ARROW KEYS ARE PRESSED, UPDATE "YOU" POSITION 60 | if (keyPressed) { 61 | if (key == CODED) { 62 | if (keyCode == LEFT) { 63 | youX -= speed; 64 | } 65 | if (keyCode == RIGHT) { 66 | youX += speed; 67 | } 68 | if (keyCode == UP) { 69 | youY -= speed; 70 | } 71 | if (keyCode == DOWN) { 72 | youY += speed; 73 | } 74 | } 75 | } 76 | } 77 | 78 | /* 79 | BALL/LINE COLLISION FUNCTION 80 | Based on the example by Philip Nicoletti 81 | http://www.codeguru.com/forum/showthread.php?threadid=194400 82 | 83 | Takes 7 arguments: 84 | + x,y position of the point 85 | + diameter of ball (assumes a circle - ellipse collision is REALLY hard) 86 | + start x,y and end x,y of the line 87 | 88 | Note: all values must be floats, otherwise rounding from ints will cause 89 | errors on one side of the line 90 | */ 91 | 92 | boolean ballLine(float bx, float by, int d, float lx1, float ly1, float lx2, float ly2) { 93 | 94 | // first get the length of the line using the Pythagorean theorem 95 | float distX = lx1-lx2; 96 | float distY = ly1-ly2; 97 | float lineLength = sqrt((distX*distX) + (distY*distY)); 98 | 99 | // then solve for r 100 | float r = (((bx-lx1)*(lx2-lx1))+((by-ly1)*(ly2-ly1)))/pow(lineLength, 2); 101 | 102 | // get x,y points of the closest point 103 | float closestX = lx1 + r*(lx2-lx1); 104 | float closestY = ly1 + r*(ly2-ly1); 105 | 106 | // to get the length of the line, use the Pythagorean theorem again 107 | float distToPointX = closestX - bx; 108 | float distToPointY = closestY - by; 109 | float distToPoint = sqrt(pow(distToPointX, 2) + pow(distToPointY, 2)); 110 | 111 | // for explanation purposes, draw a line to the ball from the closest point 112 | strokeWeight(1); 113 | stroke(255,0,0); 114 | line(closestX, closestY, bx, by); 115 | strokeWeight(3); 116 | 117 | // if that distance is less than the radius of the ball: collision 118 | if (distToPoint <= d/2) { 119 | return true; 120 | } 121 | else { 122 | return false; 123 | } 124 | } 125 | 126 | -------------------------------------------------------------------------------- /Examples/lineLine_Example/lineLine_Example.pde: -------------------------------------------------------------------------------- 1 | /* 2 | LINE/LINE COLLISION DETECTION 3 | Jeff Thompson 4 | Fall 2011 5 | 6 | www.jeffreythompson.org 7 | */ 8 | 9 | // requires all position values be floats - will NOT work with ints 10 | float youStartX, youStartY; // x,y position of "you" - fixed starting point 11 | float youX, youY; // x,y position - this moves based on the mouse 12 | 13 | float lineX1, lineY1, lineX2, lineY2; 14 | 15 | void setup() { 16 | 17 | // BASIC SETUP STUFF 18 | size(400, 400); 19 | smooth(); 20 | noCursor(); 21 | strokeWeight(3); // makes seeing things a little easier 22 | 23 | // PLACE FIXED "YOU" POINT IN UPPER-LEFT CORNER 24 | youStartX = 0; 25 | youStartY = 0; 26 | 27 | // POSITION FIXED LINE ACROSS THE SCREEN 28 | lineX1 = 0; 29 | lineY1 = height; 30 | lineX2 = width; 31 | lineY2 = 0; 32 | } 33 | 34 | void draw() { 35 | 36 | // REDRAW EACH FRAME 37 | background(255); 38 | 39 | // DRAW "YOU" 40 | youX = mouseX; 41 | youY = mouseY; 42 | stroke(255, 0, 0); 43 | line(youStartX, youStartY, youX, youY); 44 | 45 | // TEST FOR COLLISION 46 | // returns true if hit, false if not 47 | if (lineLine(youX, youY, youStartX, youStartY, lineX1, lineY1, lineX2, lineY2) == true) { 48 | stroke(0, 200); 49 | } 50 | else { 51 | stroke(200); 52 | } 53 | 54 | // DRAW STATIC LINE 55 | line(lineX1, lineY1, lineX2, lineY2); 56 | } 57 | 58 | /* 59 | LINE/LINE COLLISION FUNCTION 60 | Based on the tutorial by Paul Bourke (thanks!): 61 | http://paulbourke.net/geometry/lineline2d 62 | ... and Ibackstrom (thanks!) 63 | http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=geometry2 64 | 65 | Takes 8 arguments: 66 | + x,y positions of start and end of one line (in this case, the moving one) 67 | + x,y positions of start and end of the other line (in this case, the fixed one) 68 | 69 | Note: all values must be floats, otherwise rounding from ints will cause 70 | errors on one side of the line 71 | */ 72 | 73 | boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { 74 | 75 | // find uA and uB 76 | float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); 77 | float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); 78 | 79 | // note: if the below equations is true, the lines are parallel 80 | // ... this is the denominator of the above equations 81 | // (y4-y3)*(x2-x1) - (x4-x3)*(y2-y1) 82 | 83 | if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) { 84 | 85 | // find intersection point, if desired 86 | float intersectionX = x1 + (uA * (x2-x1)); 87 | float intersectionY = y1 + (uA * (y2-y1)); 88 | noStroke(); 89 | fill(0); 90 | ellipse(intersectionX, intersectionY, 10,10); 91 | 92 | return true; 93 | } 94 | else { 95 | return false; 96 | } 97 | } 98 | 99 | -------------------------------------------------------------------------------- /Examples/pointBall_Example/pointBall_Example.pde: -------------------------------------------------------------------------------- 1 | /* 2 | POINT/BALL COLLISION DETECTION 3 | Jeff Thompson 4 | Fall 2011 5 | 6 | A bit more difficult than point/rectangle, since we need to use 7 | the Pythagorean theorem to find the distance between the two objects. 8 | 9 | www.jeffreythompson.org 10 | */ 11 | 12 | int youX, youY; // x,y position of "you" 13 | int speed = 2; // speed to move you around 14 | // note there is no size, since a point is assumed to be infinitely small 15 | 16 | int ballX, ballY; // x,y position of the ball - will be randomly placed in the setup 17 | int ballSize = 100; // assumes a circle - elliptical collision is VERY complicated 18 | 19 | 20 | void setup() { 21 | 22 | // BASIC SETUP STUFF 23 | size(400, 400); 24 | smooth(); 25 | strokeWeight(3); // we'll make the point a bit bigger so it is easy to see 26 | 27 | // PLACE "YOU" IN THE CENTER 28 | youX = width/2; 29 | youY = height/2; 30 | 31 | // STATIC RECTANGLE IN RANDOM POSITION ON SCREEN 32 | ballX = int(random(ballSize, width-ballSize)); 33 | ballY = int(random(ballSize, height-ballSize)); 34 | } 35 | 36 | void draw() { 37 | 38 | // REDRAW EACH FRAME 39 | background(255); 40 | 41 | // TEST FOR COLLISION 42 | // returns true if hit, false if not 43 | if (pointBall(youX, youY, ballX, ballY, ballSize) == true) { 44 | fill(0); 45 | } 46 | else { 47 | fill(200); 48 | } 49 | 50 | // DRAW STATIC BALL 51 | ellipse(ballX, ballY, ballSize,ballSize); 52 | 53 | // DRAW "YOU" 54 | stroke(255, 0, 0); 55 | point(youX, youY); 56 | noStroke(); 57 | 58 | // IF ARROW KEYS ARE PRESSED, UPDATE "YOU" POSITION 59 | if (keyPressed) { 60 | if (key == CODED) { 61 | if (keyCode == LEFT) { 62 | youX -= speed; 63 | } 64 | if (keyCode == RIGHT) { 65 | youX += speed; 66 | } 67 | if (keyCode == UP) { 68 | youY -= speed; 69 | } 70 | if (keyCode == DOWN) { 71 | youY += speed; 72 | } 73 | } 74 | } 75 | } 76 | 77 | /* 78 | POINT/BALL COLLISION FUNCTION 79 | Takes 5 arguments: 80 | + x,y position of the point - in this case "you" 81 | + x,y position of the ball 82 | + diameter of ball - elliptical collision is VERY difficult 83 | */ 84 | boolean pointBall(int px, int py, int bx, int by, int bSize) { 85 | 86 | // find distance between the two objects 87 | float xDist = px-bx; // distance horiz 88 | float yDist = py-by; // distance vert 89 | float distance = sqrt((xDist*xDist) + (yDist*yDist)); // diagonal distance 90 | 91 | // test for collision 92 | if (bSize/2 > distance) { 93 | return true; // if a hit, return true 94 | } 95 | else { // if not, return false 96 | return false; 97 | } 98 | } 99 | 100 | -------------------------------------------------------------------------------- /Examples/pointLine_Example/pointLine_Example.pde: -------------------------------------------------------------------------------- 1 | /* 2 | POINT/LINE COLLISION DETECTION 3 | Jeff Thompson 4 | Fall 2011 5 | 6 | www.jeffreythompson.org 7 | */ 8 | 9 | // requires all position values be floats - will NOT work with ints 10 | float youX, youY; // x,y position of "you" 11 | int speed = 2; // speed to move you around 12 | // note no size since a point has no width or height 13 | 14 | float lineX1, lineY1, lineX2, lineY2; 15 | 16 | void setup() { 17 | 18 | // BASIC SETUP STUFF 19 | size(400, 400); 20 | smooth(); 21 | strokeWeight(3); // makes seeing things a little easier 22 | 23 | // PLACE "YOU" IN THE UPPER-LEFT QUADRANT 24 | youX = width/4; 25 | youY = height/4; 26 | 27 | // POSITION LINE ACROSS THE SCREEN 28 | lineX1 = 0; 29 | lineY1 = height; 30 | lineX2 = width; 31 | lineY2 = 0; 32 | } 33 | 34 | void draw() { 35 | 36 | // REDRAW EACH FRAME 37 | background(255); 38 | 39 | // TEST FOR COLLISION 40 | // returns true if hit, false if not 41 | if (pointLine(youX,youY, lineX1,lineY1, lineX2,lineY2) == true) { 42 | stroke(0); 43 | } 44 | else { 45 | stroke(200); 46 | } 47 | 48 | // DRAW STATIC BALL 49 | line(lineX1,lineY1, lineX2,lineY2); 50 | 51 | // DRAW "YOU" 52 | stroke(255, 0, 0); 53 | point(youX, youY); 54 | 55 | // IF ARROW KEYS ARE PRESSED, UPDATE "YOU" POSITION 56 | if (keyPressed) { 57 | if (key == CODED) { 58 | if (keyCode == LEFT) { 59 | youX -= speed; 60 | } 61 | if (keyCode == RIGHT) { 62 | youX += speed; 63 | } 64 | if (keyCode == UP) { 65 | youY -= speed; 66 | } 67 | if (keyCode == DOWN) { 68 | youY += speed; 69 | } 70 | } 71 | } 72 | } 73 | 74 | /* 75 | POINT/LINE COLLISION FUNCTION 76 | 77 | Takes 6 arguments: 78 | + x,y position of the point 79 | + start x,y and end x,y of the line 80 | 81 | Note: all values must be floats, otherwise rounding from ints will cause 82 | errors on one side of the line 83 | */ 84 | 85 | boolean pointLine(float px, float py, float lx1, float ly1, float lx2, float ly2) { 86 | 87 | // get the slope of the entire line 88 | float lineSlope = (ly2-ly1)/(lx2-lx1); 89 | 90 | // get slope from one end of the line to the point 91 | float pointSlope = (ly2-py)/(lx2-px); 92 | 93 | // if the slopes are the same, then the point is on the line! 94 | if (lineSlope == pointSlope) { 95 | return true; 96 | } 97 | else { 98 | return false; 99 | } 100 | 101 | } 102 | 103 | -------------------------------------------------------------------------------- /Examples/pointPoint_Example/pointPoint_Example.pde: -------------------------------------------------------------------------------- 1 | /* 2 | POINT/POINT COLLISION DETECTION 3 | Jeff Thompson 4 | Fall 2011 5 | 6 | Note: it's VERY hard to actually hit two points together, since to do so they 7 | would need to share a single pixel. 8 | 9 | www.jeffreythompson.org 10 | */ 11 | 12 | int youX, youY; // x,y position of "you" 13 | int speed = 1; // speed to move you around 14 | int pointX, pointY; // x,y position of other point - will be randomly placed in the setup 15 | // note there are no sizes, since a point is assumed to be infinitely small 16 | 17 | 18 | void setup() { 19 | 20 | // BASIC SETUP STUFF 21 | size(200, 200); 22 | smooth(); 23 | strokeWeight(3); // we'll make it a bit bigger so it is easy to see 24 | 25 | // PLACE "YOU" IN THE CENTER 26 | youX = width/2; 27 | youY = height/2; 28 | 29 | // STATIC POINT IN RANDOM POSITION ON SCREEN 30 | pointX = int(random(2, width-2)); 31 | pointY = int(random(2, height-2)); 32 | } 33 | 34 | void draw() { 35 | 36 | // REDRAW EACH FRAME 37 | background(255); 38 | 39 | // TEST FOR COLLISION 40 | // returns true if hit, false if not 41 | if (pointPoint(youX, youY, pointX, pointY) == true) { 42 | println("It's a hit!"); // print to let us know, since the objects are too small to see color changes 43 | } 44 | 45 | // DRAW STATIC POINT 46 | stroke(0); 47 | point(pointX, pointY); 48 | 49 | // DRAW "YOU" 50 | stroke(255, 0, 0); 51 | point(youX, youY); 52 | 53 | // IF ARROW KEYS ARE PRESSED, UPDATE "YOU" POSITION 54 | if (keyPressed) { 55 | if (key == CODED) { 56 | if (keyCode == LEFT) { 57 | youX -= speed; 58 | } 59 | if (keyCode == RIGHT) { 60 | youX += speed; 61 | } 62 | if (keyCode == UP) { 63 | youY -= speed; 64 | } 65 | if (keyCode == DOWN) { 66 | youY += speed; 67 | } 68 | } 69 | } 70 | } 71 | 72 | /* 73 | POINT/POINT COLLISION FUNCTION 74 | Takes 4 arguments: 75 | + x,y position of point 1 - in this case "you" 76 | + x,y position of point 2 - in this case the static point 77 | */ 78 | boolean pointPoint(int x1, int y1, int x2, int y2) { 79 | 80 | // test for collision 81 | if (x1 == x2 && y1 == y2) { 82 | return true; // if a hit, return true 83 | } 84 | else { // if not, return false 85 | return false; 86 | } 87 | } 88 | 89 | -------------------------------------------------------------------------------- /Examples/pointPolygon_Example/pointPolygon_Example.pde: -------------------------------------------------------------------------------- 1 | /* 2 | POINT/POLYGON COLLISION DETECTION 3 | Jeff Thompson | 2013 | www.jeffreythompson.org 4 | 5 | Finds collisions betwen a point and a N-sided polygon - very 6 | flexible! 7 | 8 | Via: http://stackoverflow.com/a/2922778/1167783 9 | */ 10 | 11 | int numVertices = 4; // number of sides in polygon 12 | float[] vertX = new float[numVertices]; // array of x/y coordinates for polygon 13 | float[] vertY = new float[numVertices]; 14 | 15 | void setup() { 16 | size(500, 500); 17 | smooth(); 18 | noStroke(); 19 | 20 | // create vertices for polygon (here a trapezoid) 21 | vertX[0] = 100; 22 | vertY[0] = 100; 23 | vertX[1] = width-100; 24 | vertY[1] = 100; 25 | vertX[2] = width-200; 26 | vertY[2] = height-100; 27 | vertX[3] = 200; 28 | vertY[3] = height-100; 29 | } 30 | 31 | void draw() { 32 | background(150, 50, 0); 33 | 34 | // if hit, change the fill color for the polygon 35 | if (pointPolygon(numVertices, vertX, vertY, mouseX, mouseY)) { 36 | fill(255); 37 | } 38 | else { 39 | fill(255, 0, 0); 40 | } 41 | 42 | // draw polygon 43 | beginShape(); 44 | for (int i=0; ipy) != (vertY[j]>py)) && (px < (vertX[j]-vertX[i]) * (py-vertY[i]) / (vertY[j]-vertY[i]) + vertX[i]) ) { 65 | collision = !collision; 66 | } 67 | } 68 | return collision; 69 | } 70 | 71 | -------------------------------------------------------------------------------- /Examples/pointTriangle_Example/pointTriangle_Example.pde: -------------------------------------------------------------------------------- 1 | /* 2 | POINT/TRIANGLE COLLISION DETECTION 3 | Jeff Thompson | 2013 | www.jeffreythompson.org 4 | 5 | Check if a point is inside a triangle. Built using a modified version 6 | of this post: 7 | http://gmc.yoyogames.com/index.php?showtopic=106307 8 | */ 9 | 10 | int x1, y1, x2, y2, x3, y3; // variables for the triangle 11 | boolean collision = false; 12 | 13 | void setup() { 14 | size(500,500); 15 | smooth(); 16 | noStroke(); 17 | 18 | // setup triangle 19 | x1 = 50; 20 | y1 = 50; 21 | x2 = width-50; 22 | y2 = 50; 23 | x3 = width/2; 24 | y3 = height-50; 25 | } 26 | 27 | void draw() { 28 | 29 | // draw background and triangle 30 | background(0,150,75); 31 | fill(255,150,0); 32 | triangle(x1, y1, x2, y2, x3, y3); 33 | 34 | // if point is in triangle, change color of the cursor! 35 | if (pointTriangle(x1, y1, x2, y2, x3, y3, mouseX, mouseY)) { 36 | fill(255); 37 | } 38 | else { 39 | fill(0); 40 | } 41 | ellipse(mouseX, mouseY, 20,20); 42 | } 43 | 44 | /* 45 | POINT/TRIANGLE COLLISION FUNCTION 46 | Takes 2 sets of arguments: 47 | + x/y coordinates for triangle 48 | + x/y coordinates for the point 49 | */ 50 | boolean pointTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int px, int py) { 51 | int a0 = abs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)); 52 | int a1 = abs((x1-px)*(y2-py)-(x2-px)*(y1-py)); 53 | int a2 = abs((x2-px)*(y3-py)-(x3-px)*(y2-py)); 54 | int a3 = abs((x3-px)*(y1-py)-(x1-px)*(y3-py)); 55 | 56 | return (abs(a1+a2+a3 - a0) <= 1/256); 57 | } 58 | -------------------------------------------------------------------------------- /Examples/rectBall_Example/rectBall_Example.pde: -------------------------------------------------------------------------------- 1 | /* 2 | RECT/BALL COLLISION DETECTION 3 | Jeff Thompson 4 | Fall 2011 5 | 6 | Built from this fantastic example by Matt Worden: 7 | http://vband3d.tripod.com/visualbasic/tut_mixedcollisions.htm 8 | 9 | www.jeffreythompson.org 10 | */ 11 | 12 | int youX, youY; // x,y position of "you" 13 | int youW = 50; // width 14 | int youH = 50; // height 15 | int speed = 2; // speed to move you around 16 | 17 | int ballX, ballY; // x,y position of the ball - will be randomly placed in the setup 18 | int ballSize = 30; // assumes a circle - elliptical collision is VERY complicated 19 | 20 | 21 | void setup() { 22 | 23 | // BASIC SETUP STUFF 24 | size(400, 400); 25 | smooth(); 26 | noStroke(); 27 | rectMode(CENTER); // draw from center 28 | 29 | // PLACE "YOU" IN THE CENTER 30 | youX = width/2; 31 | youY = height/2; 32 | 33 | // STATIC BALL IN RANDOM POSITION ON SCREEN 34 | ballX = int(random(ballSize, width-ballSize)); 35 | ballY = int(random(ballSize, height-ballSize)); 36 | } 37 | 38 | void draw() { 39 | 40 | // REDRAW EACH FRAME 41 | background(255); 42 | 43 | // TEST FOR COLLISION 44 | // returns true if hit, false if not 45 | if (rectBall(youX, youY, youW, youH, ballX, ballY, ballSize) == true) { 46 | fill(0); 47 | } 48 | else { 49 | fill(200); 50 | } 51 | 52 | // DRAW STATIC BALL 53 | ellipse(ballX, ballY, ballSize, ballSize); 54 | 55 | // DRAW "YOU" 56 | fill(255, 0, 0); 57 | rect(youX, youY, youW, youH); 58 | 59 | // IF ARROW KEYS ARE PRESSED, UPDATE "YOU" POSITION 60 | if (keyPressed) { 61 | if (key == CODED) { 62 | if (keyCode == LEFT) { 63 | youX -= speed; 64 | } 65 | if (keyCode == RIGHT) { 66 | youX += speed; 67 | } 68 | if (keyCode == UP) { 69 | youY -= speed; 70 | } 71 | if (keyCode == DOWN) { 72 | youY += speed; 73 | } 74 | } 75 | } 76 | } 77 | 78 | /* 79 | RECT/BALL COLLISION FUNCTION 80 | Actually quite a bit harder than it looks! 81 | Built from an example by Matt Worden (http://vband3d.tripod.com/visualbasic/tut_mixedcollisions.htm) 82 | 83 | Takes 7 arguments: 84 | + x,y position of the first ball - in this case "you" 85 | + width and height of rect 86 | + x,y position of the second ball 87 | + diameter of second ball 88 | 89 | */ 90 | 91 | boolean rectBall(int rx, int ry, int rw, int rh, int bx, int by, int d) { 92 | 93 | // first test the edges (this is necessary if the rectangle is larger 94 | // than the ball) - do this with the Pythagorean theorem 95 | 96 | // if ball entire width position is between rect L/R sides 97 | if (bx+d/2 >= rx-rw/2 && bx-d/2 <= rx+rw/2 && abs(ry-by) <= d/2) { 98 | return true; 99 | } 100 | // if not, check if ball's entire height is between top/bottom of the rect 101 | else if (by+d/2 >= ry-rh/2 && by-d/2 <= ry+rh/2 && abs(rx-bx) <= d/2) { 102 | return true; 103 | } 104 | 105 | // if that doesn't return a hit, find closest corner 106 | // this is really just a point, so we can test if we've hit it 107 | // upper-left 108 | float xDist = (rx-rw/2) - bx; // same as ball/ball, but first value defines point, not center 109 | float yDist = (ry-rh/2) - by; 110 | float shortestDist = sqrt((xDist*xDist) + (yDist * yDist)); 111 | 112 | // upper-right 113 | xDist = (rx+rw/2) - bx; 114 | yDist = (ry-rh/2) - by; 115 | float distanceUR = sqrt((xDist*xDist) + (yDist * yDist)); 116 | if (distanceUR < shortestDist) { // if this new distance is shorter... 117 | shortestDist = distanceUR; // ... update 118 | } 119 | 120 | // lower-right 121 | xDist = (rx+rw/2) - bx; 122 | yDist = (ry+rh/2) - by; 123 | float distanceLR = sqrt((xDist*xDist) + (yDist * yDist)); 124 | if (distanceLR < shortestDist) { 125 | shortestDist = distanceLR; 126 | } 127 | 128 | // lower-left 129 | xDist = (rx-rw/2) - bx; 130 | yDist = (ry+rh/2) - by; 131 | float distanceLL = sqrt((xDist*xDist) + (yDist * yDist)); 132 | if (distanceLL < shortestDist) { 133 | shortestDist = distanceLL; 134 | } 135 | 136 | // test for collision 137 | if (shortestDist < d/2) { // if less than radius 138 | return true; // return true 139 | } 140 | else { // otherwise, return false 141 | return false; 142 | } 143 | } 144 | 145 | -------------------------------------------------------------------------------- /Examples/rectRect_Example/rectRect_Example.pde: -------------------------------------------------------------------------------- 1 | /* 2 | RECT/RECT COLLISION DETECTION 3 | Jeff Thompson 4 | Fall 2011 5 | 6 | www.jeffreythompson.org 7 | */ 8 | 9 | int youX, youY; // x,y position of "you" 10 | int youSize = 10; // size - assumes square, though that isn't necessary in the code 11 | int speed = 2; // speed to move you around 12 | 13 | int rectX, rectY; // x,y position of rectangle - will be randomly placed in the setup 14 | int rectW = 50; // width of rectangle to test 15 | int rectH = 50; // height of rectangle to test 16 | 17 | 18 | void setup() { 19 | 20 | // BASIC SETUP STUFF 21 | size(400, 400); 22 | noStroke(); 23 | smooth(); 24 | rectMode(CENTER); // draw rectangles from the center out 25 | 26 | // PLACE "YOU" IN THE CENTER 27 | youX = width/2; 28 | youY = height/2; 29 | 30 | // STATIC RECTANGLE IN RANDOM POSITION ON SCREEN 31 | rectX = int(random(rectW, width-rectW)); 32 | rectY = int(random(rectH, height-rectH)); 33 | } 34 | 35 | void draw() { 36 | 37 | // REDRAW EACH FRAME 38 | background(255); 39 | 40 | // TEST FOR COLLISION 41 | // returns true if hit, false if not 42 | if (rectRect(youX, youY, youSize, youSize, rectX, rectY, rectW, rectH) == true) { 43 | fill(0); // if hit, fill black 44 | } 45 | else { // otherwise, fill light gray 46 | fill(200); 47 | } 48 | 49 | // DRAW STATIC RECTANGLE 50 | rect(rectX, rectY, rectW, rectH); 51 | 52 | // DRAW "YOU" 53 | fill(255, 0, 0); 54 | rect(youX, youY, youSize, youSize); 55 | 56 | // IF ARROW KEYS ARE PRESSED, UPDATE "YOU" POSITION 57 | if (keyPressed) { 58 | if (key == CODED) { 59 | if (keyCode == LEFT) { 60 | youX -= speed; 61 | } 62 | if (keyCode == RIGHT) { 63 | youX += speed; 64 | } 65 | if (keyCode == UP) { 66 | youY -= speed; 67 | } 68 | if (keyCode == DOWN) { 69 | youY += speed; 70 | } 71 | } 72 | } 73 | } 74 | 75 | /* 76 | RECT/RECT COLLISION FUNCTION 77 | Takes 8 arguments: 78 | + x,y position of object 1 - in this case "you" 79 | + width and height of object 1 - also "you" 80 | + x,y position of object 2 - in this case the static rectangle 81 | + width and height of object 2 82 | 83 | */ 84 | boolean rectRect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) { 85 | 86 | // test for collision 87 | if (x1+w1/2 >= x2-w2/2 && x1-w1/2 <= x2+w2/2 && y1+h1/2 >= y2-h2/2 && y1-h1/2 <= y2+h2/2) { 88 | return true; // if a hit, return true 89 | } 90 | else { // if not, return false 91 | return false; 92 | } 93 | } 94 | 95 | -------------------------------------------------------------------------------- /PointRect/PointRect.pde: -------------------------------------------------------------------------------- 1 | /* 2 | POINT/RECT COLLISION DETECTION 3 | Jeff Thompson 4 | Fall 2011 5 | 6 | www.jeffreythompson.org 7 | */ 8 | 9 | int youX, youY; // x,y position of "you" 10 | int speed = 1; // speed to move you around 11 | // note there is no size, since a point is assumed to be infinitely small 12 | 13 | int rectX, rectY; // x,y position of the rectangle - will be randomly placed in the setup 14 | int rectW = 100; 15 | int rectH = 100; 16 | 17 | 18 | void setup() { 19 | 20 | // BASIC SETUP STUFF 21 | size(400, 400); 22 | smooth(); 23 | rectMode(CENTER); // draw rectangle from center out 24 | strokeWeight(3); // we'll make the point a bit bigger so it is easy to see 25 | 26 | // PLACE "YOU" IN THE CENTER 27 | youX = width/2; 28 | youY = height/2; 29 | 30 | // STATIC RECTANGLE IN RANDOM POSITION ON SCREEN 31 | rectX = int(random(rectW, width-rectW)); 32 | rectY = int(random(rectH, height-rectH)); 33 | } 34 | 35 | void draw() { 36 | 37 | // REDRAW EACH FRAME 38 | background(255); 39 | 40 | // TEST FOR COLLISION 41 | // returns true if hit, false if not 42 | if (pointRect(youX, youY, rectX, rectY, rectW, rectH) == true) { 43 | fill(0); 44 | } 45 | else { 46 | fill(200); 47 | } 48 | 49 | // DRAW STATIC RECT 50 | rect(rectX, rectY, rectW,rectH); 51 | 52 | // DRAW "YOU" 53 | stroke(255, 0, 0); 54 | point(youX, youY); 55 | noStroke(); 56 | 57 | // IF ARROW KEYS ARE PRESSED, UPDATE "YOU" POSITION 58 | if (keyPressed) { 59 | if (key == CODED) { 60 | if (keyCode == LEFT) { 61 | youX -= speed; 62 | } 63 | if (keyCode == RIGHT) { 64 | youX += speed; 65 | } 66 | if (keyCode == UP) { 67 | youY -= speed; 68 | } 69 | if (keyCode == DOWN) { 70 | youY += speed; 71 | } 72 | } 73 | } 74 | } 75 | 76 | /* 77 | POINT/RECT COLLISION FUNCTION 78 | Takes 6 arguments: 79 | + x,y position of point 1 - in this case "you" 80 | + x,y position of point 2 - in this case the static rectangle 81 | + width and height of rectangle 82 | */ 83 | boolean pointRect(int px, int py, int rx, int ry, int rw, int rh) { 84 | 85 | // test for collision 86 | if (px >= rx-rw/2 && px <= rx+rw/2 && py >= ry-rh/2 && py <= ry+rh/2) { 87 | return true; // if a hit, return true 88 | } 89 | else { // if not, return false 90 | return false; 91 | } 92 | } 93 | 94 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![screenshot](https://raw.github.com/jeffThompson/CollisionDetectionFunctionsForProcessing/master/CollisionLogo.png) 2 | 3 | COLLISION DETECTION FUNCTIONS 4 | ======================= 5 | ####*Please note: this project has moved to an [updated repository](https://github.com/jeffThompson/CollisionDetection) with full explanations of the code in book form.* 6 | See: [https://github.com/jeffThompson/CollisionDetection](https://github.com/jeffThompson/CollisionDetection) 7 | 8 | \- \- \- 9 | 10 | A set of functions to do basic 2d collision detection in Processing (www.processing.org). 11 | 12 | While some tools already exist, like the excellent Box2D, the source code is not easy to understand and the implementation is a more than a bit complex. Other examples (equally great), like the line-line collision in the toxiclibs collection, use vector math which isn't easy for us "creative programmers" (ie: those who got bad grades in high school math). 13 | 14 | Instead, these tools can be used as simple, one-line commands to determine whether two objects have collided. Designed for simple games and interactive systems, they are intended to be building blocks for larger projects. 15 | 16 | Enjoy! 17 | 18 | \- \- \- 19 | 20 | ####HOW TO EXPERIMENT AND SEE WHAT THE FUNCTIONS DO 21 | The folder `examples` has sketches to demonstrate each function "live". These are meant to quickly try the functions, hack them, and otherwise experiment. 22 | 23 | ####HOW TO USE IN YOUR OWN CODE 24 | The example sketches are more fully-featured uses of the collision functions. To use the functions themselves, follow these steps: 25 | 26 | 1. Open the function you want to use (ex: "pointPoint.pde) - NOT the example file 27 | 2. Copy the code 28 | 3. Paste the code into your sketch after the end of the draw function 29 | 30 | Note: you can also paste the code into a new tab, which will be a bit cleaner to use 31 | 32 | \- \- \- 33 | 34 | ####CURRENT FUNCTIONS: 35 | All functions return a simple true/false value whether there is collision or not. In many cases, code is included to also return the point of collision. 36 | 37 | + Point/Point 38 | + Point/Rect 39 | + Point/Ball* 40 | 41 | + Rect/Rect 42 | + Ball/Ball* 43 | + Ball/Rect* 44 | 45 | + Point/Line 46 | + Line/Line 47 | + Ball/Line* 48 | 49 | \* A "ball" is defined as a circle - ellipses are really hard! 50 | 51 | \- \- \- 52 | 53 | ####REQUESTS and BUG REPORTING 54 | A few things that would be great, but were just a little (or a lot) complex to include at this point: 55 | 56 | + Ellipses 57 | + Triangles 58 | + Polygons 59 | + Curved lines (definitely not happening any time soon!) 60 | 61 | Finding that something is missing? Wish list? Major error? Little one? Please post here! 62 | 63 | \- \- \- 64 | 65 | ####THANKS TO 66 | Several online tutorials and examples were used for the more confusing examples. A special thanks goes to: 67 | 68 | + Paul Bourke 69 | http://paulbourke.net/geometry/lineline2d 70 | 71 | + Ibackstrom 72 | http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=geometry2 73 | 74 | + Philip Nicoletti 75 | http://www.codeguru.com/forum/showthread.php?threadid=194400 76 | 77 | + Matt Worden 78 | http://vband3d.tripod.com/visualbasic/tut_mixedcollisions.htm 79 | 80 | \- \- \- 81 | 82 | \[ all code available under [Creative Commons BY-NC-SA license](http://creativecommons.org/licenses/by-nc-sa/3.0/) - feel free to use but [please let me know](http://www.jeffreythompson.org) \] 83 | -------------------------------------------------------------------------------- /ballBall/ballBall.pde: -------------------------------------------------------------------------------- 1 | /* 2 | BALL/BALL COLLISION FUNCTION 3 | Jeff Thompson // v0.9 // November 2011 // www.jeffreythompson.org 4 | 5 | Takes 6 arguments: 6 | + x,y position of the first ball - in this case "you" 7 | + diameter of first ball - elliptical collision is VERY difficult 8 | + x,y position of the second ball 9 | + diameter of second ball 10 | 11 | */ 12 | boolean ballBall(int x1, int y1, int d1, int x2, int y2, int d2) { 13 | 14 | // find distance between the two objects 15 | float xDist = x1-x2; // distance horiz 16 | float yDist = y1-y2; // distance vert 17 | float distance = sqrt((xDist*xDist) + (yDist*yDist)); // diagonal distance 18 | 19 | // test for collision 20 | if (d1/2 + d2/2 > distance) { 21 | return true; // if a hit, return true 22 | } 23 | else { // if not, return false 24 | return false; 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /ballLine/ballLine.pde: -------------------------------------------------------------------------------- 1 | /* 2 | BALL/LINE COLLISION FUNCTION 3 | Jeff Thompson // v0.9 // November 2011 // www.jeffreythompson.org 4 | 5 | Based on the example by Philip Nicoletti 6 | http://www.codeguru.com/forum/showthread.php?threadid=194400 7 | 8 | Takes 7 arguments: 9 | + x,y position of the point 10 | + diameter of ball (assumes a circle - ellipse collision is REALLY hard) 11 | + start x,y and end x,y of the line 12 | 13 | Note: all values must be floats, otherwise rounding from ints will cause 14 | errors on one side of the line 15 | */ 16 | 17 | boolean ballLine(float bx, float by, int d, float lx1, float ly1, float lx2, float ly2) { 18 | 19 | // first get the length of the line using the Pythagorean theorem 20 | float distX = lx1-lx2; 21 | float distY = ly1-ly2; 22 | float lineLength = sqrt((distX*distX) + (distY*distY)); 23 | 24 | // then solve for r 25 | float r = (((bx-lx1)*(lx2-lx1))+((by-ly1)*(ly2-ly1)))/pow(lineLength, 2); 26 | 27 | // get x,y points of the closest point 28 | float closestX = lx1 + r*(lx2-lx1); 29 | float closestY = ly1 + r*(ly2-ly1); 30 | 31 | // to get the length of the line, use the Pythagorean theorem again 32 | float distToPointX = closestX - bx; 33 | float distToPointY = closestY - by; 34 | float distToPoint = sqrt(pow(distToPointX, 2) + pow(distToPointY, 2)); 35 | 36 | // for explanation purposes, draw a line to the ball from the closest point 37 | strokeWeight(1); 38 | stroke(255,0,0); 39 | line(closestX, closestY, bx, by); 40 | strokeWeight(3); 41 | 42 | // if that distance is less than the radius of the ball: collision 43 | if (distToPoint <= d/2) { 44 | return true; 45 | } 46 | else { 47 | return false; 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /lineLine/lineLine.pde: -------------------------------------------------------------------------------- 1 | /* 2 | LINE/LINE COLLISION FUNCTION 3 | Jeff Thompson // v0.9 // November 2011 // www.jeffreythompson.org 4 | 5 | Based on the tutorial by Paul Bourke (thanks!): 6 | http://paulbourke.net/geometry/lineline2d 7 | ... and Ibackstrom (thanks!) 8 | http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=geometry2 9 | 10 | Takes 8 arguments: 11 | + x,y positions of start and end of one line (in this case, the moving one) 12 | + x,y positions of start and end of the other line (in this case, the fixed one) 13 | 14 | Note: all values must be floats, otherwise rounding from ints will cause 15 | errors on one side of the line 16 | */ 17 | 18 | boolean lineLine(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { 19 | 20 | // find uA and uB 21 | float uA = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); 22 | float uB = ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)); 23 | 24 | // note: if the below equations is true, the lines are parallel 25 | // ... this is the denominator of the above equations 26 | // (y4-y3)*(x2-x1) - (x4-x3)*(y2-y1) 27 | 28 | if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) { 29 | 30 | // find intersection point, if desired 31 | float intersectionX = x1 + (uA * (x2-x1)); 32 | float intersectionY = y1 + (uA * (y2-y1)); 33 | noStroke(); 34 | fill(0); 35 | ellipse(intersectionX, intersectionY, 10,10); 36 | 37 | return true; 38 | } 39 | else { 40 | return false; 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /linePlane/linePlane.pde: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | LINE/PLANE COLLISION - 3d 4 | Jeff Thompson 5 | 6 | Line to plane collision in 3d. Use mouse to rotate view, arrow 7 | keys to move the line and watch the results! 8 | 9 | Based on examples from: 10 | http://gmc.yoyogames.com/index.php?showtopic=501626 11 | 12 | www.jeffreythompson.org 13 | */ 14 | 15 | // end of line (changed with arrow keys) 16 | float lx, ly, lz; 17 | 18 | // intersection points - start at 0 19 | float sx = 0; 20 | float sy = 0; 21 | float sz = 0; 22 | 23 | // rotation variables (grab with mouse to move) 24 | float rotX = -5.0; 25 | float rotY = -12.0; 26 | 27 | 28 | void setup() { 29 | size(400, 400, P3D); 30 | smooth(); 31 | 32 | lx = width; 33 | ly = height; 34 | lz = width; 35 | 36 | // three points on the plane, followed by ends of the line 37 | intersect(0, height/2, 0, width, height/2, 0, 0, height/2, width, 0, 0, 0, lx, ly, lz); 38 | 39 | // print result 40 | println("Intersection: " + sx + ", " + sy + ", " + sz); 41 | } 42 | 43 | void draw() { 44 | background(32); 45 | lights(); 46 | 47 | rotateX(rotY); 48 | rotateY(rotX); 49 | 50 | // draw the plane 51 | fill(255, 150, 0); 52 | noStroke(); 53 | beginShape(); 54 | vertex(0, height/2, 0); 55 | vertex(0, height/2, width); 56 | vertex(width, height/2, width); 57 | vertex(width, height/2, 0); 58 | endShape(CLOSE); 59 | 60 | // draw the line 61 | stroke(255); 62 | strokeWeight(2); 63 | line(0,0,0, lx,ly,lz); 64 | strokeWeight(1); 65 | 66 | // draw a dot at the intersection point! 67 | fill(255, 0, 0); 68 | noStroke(); 69 | pushMatrix(); 70 | translate(sx, sy, sz); 71 | sphere(5); 72 | popMatrix(); 73 | } 74 | 75 | // arrow keys move the end of the line 76 | void keyPressed() { 77 | if (key == CODED) { 78 | if (keyCode == RIGHT) { 79 | lx += 10; 80 | } 81 | else if (keyCode == LEFT) { 82 | lx -= 10; 83 | } 84 | else if (keyCode == UP) { 85 | lz -= 10; 86 | } 87 | else if (keyCode == DOWN) { 88 | lz += 10; 89 | } 90 | } 91 | } 92 | 93 | // on release, check intersection again and print the result! 94 | void keyReleased() { 95 | if (key == CODED) { 96 | intersect(0, height/2, 0, width, height/2, 0, 0, height/2, width, 0, 0, 0, lx, ly, lz); 97 | println("Intersection: " + sx + ", " + sy + ", " + sz); 98 | } 99 | } 100 | 101 | // mouse moves your view 102 | void mouseDragged() { 103 | rotX += (mouseX - pmouseX) * 0.01; 104 | rotY -= (mouseY - pmouseY) * 0.01; 105 | } 106 | 107 | // three points on the plane, followed by ends of the line (x,y,z) 108 | void intersect(float ax, float ay, float az, float bx, float by, float bz, float cx, float cy, float cz, float px, float py, float pz, float qx, float qy, float qz) { 109 | 110 | // vector from A to C 111 | float ex = cx-ax; 112 | float ey = cy-ay; 113 | float ez = cz-az; 114 | 115 | // vector from A to B 116 | float fx = bx-ax; 117 | float fy = by-ay; 118 | float fz = bz-az; 119 | 120 | // cross product of E and F 121 | float nx = fy*ez-fz*ey; 122 | float ny = fz*ex-fx*ez; 123 | float nz = fx*ey-fy*ex; 124 | 125 | // normalize 126 | float m = sqrt(nx*nx+ny*ny+nz*nz); 127 | nx /= m; 128 | ny /= m; 129 | nz /= m; 130 | 131 | //get ray direction vector 132 | float dx = qx-px; 133 | float dy = qy-py; 134 | float dz = qz-pz; 135 | 136 | m = sqrt(dx*dx+dy*dy+dz*dz); 137 | dx /= m; 138 | dy /= m; 139 | dz /= m; 140 | 141 | // dot product of ray and normal 142 | // this tells us if the ray is pointing towards the plane 143 | m = dx*nx + dy*ny + dz*nz; 144 | 145 | // less than 0 means the vectors are opposed 146 | // more than 0 means the vectors point the same way 147 | // equal to 0 means they are perpendicular 148 | if (m < 0) { 149 | 150 | // get the vector from the ray to point A on the plane 151 | float gx = ax-px; 152 | float gy = ay-py; 153 | float gz = az-pz; 154 | 155 | // dot product of G and plane normal 156 | float t = gx*nx+gy*ny+gz*nz; 157 | 158 | // if we are on non-culled side of the plane 159 | if (t < 0) { 160 | // get distance to plane by dividing the two dot products 161 | float k = t/m; 162 | 163 | // find the point of intersection on the plane, return result 164 | sx = px+dx*k; 165 | sy = py+dy*k; 166 | sz = pz+dz*k; 167 | } 168 | } 169 | } 170 | -------------------------------------------------------------------------------- /pointBall/pointBall.pde: -------------------------------------------------------------------------------- 1 | /* 2 | POINT/BALL COLLISION FUNCTION 3 | Jeff Thompson // v0.9 // November 2011 // www.jeffreythompson.org 4 | 5 | Takes 5 arguments: 6 | + x,y position of the point - in this case "you" 7 | + x,y position of the ball 8 | + diameter of ball - elliptical collision is VERY difficult 9 | */ 10 | boolean pointBall(int px, int py, int bx, int by, int bSize) { 11 | 12 | // find distance between the two objects 13 | float xDist = px-bx; // distance horiz 14 | float yDist = py-by; // distance vert 15 | float distance = sqrt((xDist*xDist) + (yDist*yDist)); // diagonal distance 16 | 17 | // test for collision 18 | if (bSize/2 > distance) { 19 | return true; // if a hit, return true 20 | } 21 | else { // if not, return false 22 | return false; 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /pointLine/pointLine.pde: -------------------------------------------------------------------------------- 1 | /* 2 | POINT/LINE COLLISION FUNCTION 3 | Jeff Thompson // v0.9 // November 2011 // www.jeffreythompson.org 4 | 5 | Takes 6 arguments: 6 | + x,y position of the point 7 | + start x,y and end x,y of the line 8 | 9 | Note: all values must be floats, otherwise rounding from ints will cause 10 | errors on one side of the line 11 | */ 12 | 13 | boolean pointLine(float px, float py, float lx1, float ly1, float lx2, float ly2) { 14 | 15 | // get the slope of the entire line 16 | float lineSlope = (ly2-ly1)/(lx2-lx1); 17 | 18 | // get slope from one end of the line to the point 19 | float pointSlope = (ly2-py)/(lx2-px); 20 | 21 | // if the slopes are the same, then the point is on the line! 22 | if (lineSlope == pointSlope) { 23 | return true; 24 | } 25 | else { 26 | return false; 27 | } 28 | 29 | } 30 | 31 | -------------------------------------------------------------------------------- /pointPoint/pointPoint.pde: -------------------------------------------------------------------------------- 1 | /* 2 | POINT/POINT COLLISION FUNCTION 3 | Jeff Thompson // v0.9 // November 2011 // www.jeffreythompson.org 4 | 5 | Takes 4 arguments: 6 | + x,y position of point 1 - in this case "you" 7 | + x,y position of point 2 - in this case the static point 8 | */ 9 | boolean pointPoint(int x1, int y1, int x2, int y2) { 10 | 11 | // test for collision 12 | if (x1 == x2 && y1 == y2) { 13 | return true; // if a hit, return true 14 | } 15 | else { // if not, return false 16 | return false; 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /pointPolygon/pointPolygon.pde: -------------------------------------------------------------------------------- 1 | /* 2 | POINT/POLYGON COLLISION DETECTION 3 | Jeff Thompson | 2013 | www.jeffreythompson.org 4 | 5 | Finds collisions betwen a point and a N-sided polygon - very 6 | flexible! 7 | 8 | Via: http://stackoverflow.com/a/2922778/1167783 9 | 10 | Takes 5 arguments: 11 | + # of vertices 12 | + float array x and y coordinates for vertices 13 | + x/y coordinates for point 14 | */ 15 | 16 | boolean pointPolygon (int numVertices, float[] vertX, float[] vertY, float px, float py) { 17 | boolean collision = false; 18 | for (int i=0, j=numVertices-1; i < numVertices; j = i++) { 19 | if ( ((vertY[i]>py) != (vertY[j]>py)) && (px < (vertX[j]-vertX[i]) * (py-vertY[i]) / (vertY[j]-vertY[i]) + vertX[i]) ) { 20 | collision = !collision; 21 | } 22 | } 23 | return collision; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /pointTriangle/pointTriangle.pde: -------------------------------------------------------------------------------- 1 | /* 2 | POINT/TRIANGLE COLLISION DETECTION 3 | Jeff Thompson | 2013 | www.jeffreythompson.org 4 | 5 | Takes 2 sets of arguments: 6 | + x/y coordinates for triangle 7 | + x/y coordinates for the point 8 | 9 | Built using a modified version of this post: 10 | http://gmc.yoyogames.com/index.php?showtopic=106307 11 | */ 12 | 13 | boolean pointTriangle(int x1, int y1, int x2, int y2, int x3, int y3, int px, int py) { 14 | int a0 = abs((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1)); 15 | int a1 = abs((x1-px)*(y2-py)-(x2-px)*(y1-py)); 16 | int a2 = abs((x2-px)*(y3-py)-(x3-px)*(y2-py)); 17 | int a3 = abs((x3-px)*(y1-py)-(x1-px)*(y3-py)); 18 | 19 | return (abs(a1+a2+a3 - a0) <= 1/256); 20 | } 21 | -------------------------------------------------------------------------------- /rectBall/rectBall.pde: -------------------------------------------------------------------------------- 1 | /* 2 | RECT/BALL COLLISION FUNCTION 3 | Jeff Thompson // v0.9 // November 2011 // www.jeffreythompson.org 4 | 5 | Actually quite a bit harder than it looks! 6 | Built from an example by Matt Worden (http://vband3d.tripod.com/visualbasic/tut_mixedcollisions.htm) 7 | 8 | Takes 7 arguments: 9 | + x,y position of the first ball - in this case "you" 10 | + width and height of rect 11 | + x,y position of the second ball 12 | + diameter of second ball 13 | 14 | */ 15 | 16 | boolean rectBall(int rx, int ry, int rw, int rh, int bx, int by, int d) { 17 | 18 | // first test the edges (this is necessary if the rectangle is larger 19 | // than the ball) - do this with the Pythagorean theorem 20 | 21 | // if ball entire width position is between rect L/R sides 22 | if (bx+d/2 >= rx-rw/2 && bx-d/2 <= rx+rw/2 && abs(ry-by) <= d/2) { 23 | return true; 24 | } 25 | // if not, check if ball's entire height is between top/bottom of the rect 26 | else if (by+d/2 >= ry-rh/2 && by-d/2 <= ry+rh/2 && abs(rx-bx) <= d/2) { 27 | return true; 28 | } 29 | 30 | // if that doesn't return a hit, find closest corner 31 | // this is really just a point, so we can test if we've hit it 32 | // upper-left 33 | float xDist = (rx-rw/2) - bx; // same as ball/ball, but first value defines point, not center 34 | float yDist = (ry-rh/2) - by; 35 | float shortestDist = sqrt((xDist*xDist) + (yDist * yDist)); 36 | 37 | // upper-right 38 | xDist = (rx+rw/2) - bx; 39 | yDist = (ry-rh/2) - by; 40 | float distanceUR = sqrt((xDist*xDist) + (yDist * yDist)); 41 | if (distanceUR < shortestDist) { // if this new distance is shorter... 42 | shortestDist = distanceUR; // ... update 43 | } 44 | 45 | // lower-right 46 | xDist = (rx+rw/2) - bx; 47 | yDist = (ry+rh/2) - by; 48 | float distanceLR = sqrt((xDist*xDist) + (yDist * yDist)); 49 | if (distanceLR < shortestDist) { 50 | shortestDist = distanceLR; 51 | } 52 | 53 | // lower-left 54 | xDist = (rx-rw/2) - bx; 55 | yDist = (ry+rh/2) - by; 56 | float distanceLL = sqrt((xDist*xDist) + (yDist * yDist)); 57 | if (distanceLL < shortestDist) { 58 | shortestDist = distanceLL; 59 | } 60 | 61 | // test for collision 62 | if (shortestDist < d/2) { // if less than radius 63 | return true; // return true 64 | } 65 | else { // otherwise, return false 66 | return false; 67 | } 68 | } 69 | 70 | -------------------------------------------------------------------------------- /rectRect/rectRect.pde: -------------------------------------------------------------------------------- 1 | /* 2 | RECT/RECT COLLISION FUNCTION 3 | Jeff Thompson // v0.9 // November 2011 // www.jeffreythompson.org 4 | 5 | Takes 8 arguments: 6 | + x,y position of object 1 - in this case "you" 7 | + width and height of object 1 - also "you" 8 | + x,y position of object 2 - in this case the static rectangle 9 | + width and height of object 2 10 | 11 | */ 12 | boolean rectRect(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) { 13 | 14 | // test for collision 15 | if (x1+w1/2 >= x2-w2/2 && x1-w1/2 <= x2+w2/2 && y1+h1/2 >= y2-h2/2 && y1-h1/2 <= y2+h2/2) { 16 | return true; // if a hit, return true 17 | } 18 | else { // if not, return false 19 | return false; 20 | } 21 | } 22 | 23 | --------------------------------------------------------------------------------