We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have another post with collisions between circles, lines, and rectangles here: https://forum.processing.org/two/discussion/14407/collision-detection
but this post makes all of the functions for triangles. If you have questions, the other post may be able to answer those, other wise ask on the website!
//line x 1, line y 1, line x 2, line y 2, triangle x 1, triangle y 1, tri x 2, tri x 2, tri x 3, tri y 3
boolean lineTriColl(float x1L, float y1L, float x2L, float y2L, float x1t, float y1t, float x2t, float y2t, float x3t, float y3t) {
if (lineLineColl(x1L, y1L, x2L, y2L, x1t, y1t, x2t, y2t) == true) {
return true;
}
if (lineLineColl(x1L, y1L, x2L, y2L, x1t, y1t, x3t, y3t) == true) {
return true;
}
if (lineLineColl(x1L, y1L, x2L, y2L, x2t, y2t, x3t, y3t) == true) {
return true;
}
return false;
}
//triangle x 1, triangle y 1, tri x 2, tri x 2, tri x 3, tri y 3, circ x, circ y, circ radius
boolean circTriColl(float x1t, float y1t, float x2t, float y2t, float x3t, float y3t, float cx, float cy, float cr) {
float smallXt;
float smallYt;
float sizeXt;
float sizeYt;
if (x1t < x2t) {
smallXt = x1t;
} else {
smallXt = x2t;
}
if (x3t < smallXt) {
smallXt = x3t;
}
if (y1t < y2t) {
smallYt = y1t;
} else {
smallYt = y2t;
}
if (y3t < smallYt) {
smallYt = y3t;
}
if (x1t > x2t) {
sizeXt = x1t - smallXt;
} else {
sizeXt = x2t - smallXt;
}
if (x3t - smallXt > sizeXt) {
sizeXt = x3t - smallXt;
}
if (y1t > y2t) {
sizeYt = y1t - smallYt;
} else {
sizeYt = y2t - smallYt;
}
if (y3t - smallYt > sizeYt) {
sizeYt = y3t - smallYt;
}
if (lineCircColl(x1t, y1t, x2t, y2t, cx, cy, cr*2) == true) {
return true;
}
if (lineCircColl(x2t, y2t, x3t, y3t, cx, cy, cr*2) == true) {
return true;
}
if (lineCircColl(x1t, y1t, x3t, y3t, cx, cy, cr*2) == true) {
return true;
}
if (cx >= smallXt && cx <= smallXt+sizeXt && cy >= smallYt && cy <= smallYt+sizeYt) {
String dir = "UP";
float m;
float b;
if (x1t == smallXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t == smallXt + sizeXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y1t-y2t)/(x1t-x2t);
b = y1t - (m*x1t);
if (dir == "UP") {
if (cy > cx*m + b) {
return false;
}
} else {
if (cy < cx*m + b) {
return false;
}
}
if (x1t == smallXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t == smallXt + sizeXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y1t-y2t)/(x1t-x2t);
b = y1t - (m*x1t);
if (dir == "UP") {
if (cy > cx*m + b) {
return false;
}
} else {
if (cy < cx*m + b) {
return false;
}
}
if (x1t == smallXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t == smallXt + sizeXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y1t-y3t)/(x1t-x3t);
b = y3t - (m*x3t);
if (dir == "UP") {
if (cy > cx*m + b) {
return false;
}
} else {
if (cy < cx*m + b) {
return false;
}
}
if (x3t == smallXt) {
if (y3t == smallYt) {
dir = "UP";
} else {
if (y3t == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t == smallXt + sizeXt) {
if (y3t == smallYt) {
dir = "UP";
} else {
if (y3t == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y2t-y3t)/(x2t-x3t);
b = y2t - (m*x2t);
if (dir == "UP") {
if (cy > cx*m + b) {
return false;
}
} else {
if (cy < cx*m + b) {
return false;
}
}
m = (y2t-y3t)/(x2t-x3t);
b = y3t - (m*x3t);
if (dir == "UP") {
if (cy > cx*m + b) {
return false;
}
} else {
if (cy < cx*m + b) {
return false;
}
}
if (x3t == smallXt) {
if (y3t == smallYt) {
dir = "UP";
} else {
if (y3t == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x2t == smallXt + sizeXt) {
if (y3t == smallYt) {
dir = "UP";
} else {
if (y3t == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y2t-y3t)/(x2t-x3t);
b = y2t - (m*x2t);
if (dir == "UP") {
if (cy > cx*m + b) {
return false;
}
} else {
if (cy < cx*m + b) {
return false;
}
}
}
return false;
}
//triangle x 1, triangle y 1, tri x 2, tri x 2, tri x 3, tri y 3, rect x, rect y, rect x size, rect y size
boolean rectTriColl(float x1t, float y1t, float x2t, float y2t, float x3t, float y3t, float xr, float yr, float xs, float ys) {
if (lineLineColl(xr, yr+ys, xr+xs, yr+ys, x1t, y1t, x2t, y2t) == true) {
return true;
}
if (lineLineColl(xr+xs, yr+ys, xr+xs, yr, x1t, y1t, x2t, y2t) == true) {
return true;
}
if (lineLineColl(xr, yr, xr, yr+ys, x1t, y1t, x2t, y2t) == true) {
return true;
}
if (lineLineColl(xr+xs, yr, xr, yr, x1t, y1t, x2t, y2t) == true) {
return true;
}
if (lineLineColl(xr, yr+ys, xr+xs, yr+ys, x1t, y1t, x3t, y3t) == true) {
return true;
}
if (lineLineColl(xr+xs, yr+ys, xr+xs, yr, x1t, y1t, x3t, y3t) == true) {
return true;
}
if (lineLineColl(xr, yr, xr, yr+ys, x1t, y1t, x3t, y3t) == true) {
return true;
}
if (lineLineColl(xr+xs, yr, xr, yr, x1t, y1t, x3t, y3t) == true) {
return true;
}
if (lineLineColl(xr, yr+ys, xr+xs, yr+ys, x2t, y2t, x3t, y3t) == true) {
return true;
}
if (lineLineColl(xr+xs, yr+ys, xr+xs, yr, x2t, y2t, x3t, y3t) == true) {
return true;
}
if (lineLineColl(xr, yr, xr, yr+ys, x2t, y2t, x3t, y3t) == true) {
return true;
}
if (lineLineColl(xr+xs, yr, xr, yr, x2t, y2t, x3t, y3t) == true) {
return true;
}
float smallXt;
float smallYt;
float sizeXt;
float sizeYt;
if (x1t < x2t) {
smallXt = x1t;
} else {
smallXt = x2t;
}
if (x3t < smallXt) {
smallXt = x3t;
}
if (y1t < y2t) {
smallYt = y1t;
} else {
smallYt = y2t;
}
if (y3t < smallYt) {
smallYt = y3t;
}
if (x1t > x2t) {
sizeXt = x1t - smallXt;
} else {
sizeXt = x2t - smallXt;
}
if (x3t - smallXt > sizeXt) {
sizeXt = x3t - smallXt;
}
if (y1t > y2t) {
sizeYt = y1t - smallYt;
} else {
sizeYt = y2t - smallYt;
}
if (y3t - smallYt > sizeYt) {
sizeYt = y3t - smallYt;
}
if (xr >= smallXt && xr <= smallXt+sizeXt && yr >= smallYt && yr <= smallYt+sizeYt) {
String dir = "UP";
float m;
float b;
if (x1t == smallXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t == smallXt + sizeXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y1t-y2t)/(x1t-x2t);
b = y1t - (m*x1t);
if (dir == "UP") {
if (yr > xr*m + b) {
return false;
}
} else {
if (yr < xr*m + b) {
return false;
}
}
if (x1t == smallXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t == smallXt + sizeXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y1t-y2t)/(x1t-x2t);
b = y1t - (m*x1t);
if (dir == "UP") {
if (yr > xr*m + b) {
return false;
}
} else {
if (yr < xr*m + b) {
return false;
}
}
if (x1t == smallXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t == smallXt + sizeXt) {
if (y2t == smallYt) {
dir = "UP";
} else {
if (y2t == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y1t-y3t)/(x1t-x3t);
b = y3t - (m*x3t);
if (dir == "UP") {
if (yr > xr*m + b) {
return false;
}
} else {
if (yr < xr*m + b) {
return false;
}
}
if (x3t == smallXt) {
if (y3t == smallYt) {
dir = "UP";
} else {
if (y3t == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t == smallXt + sizeXt) {
if (y3t == smallYt) {
dir = "UP";
} else {
if (y3t == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y2t-y3t)/(x2t-x3t);
b = y2t - (m*x2t);
if (dir == "UP") {
if (yr > xr*m + b) {
return false;
}
} else {
if (yr < xr*m + b) {
return false;
}
}
m = (y2t-y3t)/(x2t-x3t);
b = y3t - (m*x3t);
if (dir == "UP") {
if (yr > xr*m + b) {
return false;
}
} else {
if (yr < xr*m + b) {
return false;
}
}
if (x3t == smallXt) {
if (y3t == smallYt) {
dir = "UP";
} else {
if (y3t == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x2t == smallXt + sizeXt) {
if (y3t == smallYt) {
dir = "UP";
} else {
if (y3t == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y2t-y3t)/(x2t-x3t);
b = y2t - (m*x2t);
if (dir == "UP") {
if (yr > xr*m + b) {
return false;
}
} else {
if (yr < xr*m + b) {
return false;
}
}
}
return false;
}
//triangle 1 x 1, triangle 1 y 1, tri 1 x 2, tri 1 x 2, tri 1 x 3, tri 1 y 3, tri 2 x 1, tri 2 y 1, tri 2 x 2, tri 2 y 2, tri 2 x 3, tri 2 y 3
boolean triTriColl(float x1t1, float y1t1, float x2t1, float y2t1, float x3t1, float y3t1, float x1t2, float y1t2, float x2t2, float y2t2, float x3t2, float y3t2) {
if (lineLineColl(x1t1, y1t1, x2t1, x2t1, x1t2, y1t2, x2t2, y2t2) == true) {
return true;
}
if (lineLineColl(x1t1, y1t1, x2t1, x2t1, x1t2, y1t2, x3t2, y3t2) == true) {
return true;
}
if (lineLineColl(x1t1, y1t1, x2t1, x2t1, x2t2, y2t2, x3t2, y3t2) == true) {
return true;
}
if (lineLineColl(x1t1, y1t1, x3t1, x3t1, x1t2, y1t2, x2t2, y2t2) == true) {
return true;
}
if (lineLineColl(x1t1, y1t1, x3t1, x3t1, x1t2, y1t2, x3t2, y3t2) == true) {
return true;
}
if (lineLineColl(x1t1, y1t1, x3t1, x3t1, x2t2, y2t2, x3t2, y3t2) == true) {
return true;
}
if (lineLineColl(x2t1, y2t1, x3t1, x3t1, x1t2, y1t2, x2t2, y2t2) == true) {
return true;
}
if (lineLineColl(x2t1, y2t1, x3t1, x3t1, x1t2, y1t2, x3t2, y3t2) == true) {
return true;
}
if (lineLineColl(x2t1, y2t1, x3t1, x3t1, x2t2, y2t2, x3t2, y3t2) == true) {
return true;
}
float smallXt;
float smallYt;
float sizeXt;
float sizeYt;
if (x1t1 < x2t1) {
smallXt = x1t1;
} else {
smallXt = x2t1;
}
if (x3t1 < smallXt) {
smallXt = x3t1;
}
if (y1t1 < y2t1) {
smallYt = y1t1;
} else {
smallYt = y2t1;
}
if (y3t1 < smallYt) {
smallYt = y3t1;
}
if (x1t1 > x2t1) {
sizeXt = x1t1 - smallXt;
} else {
sizeXt = x2t1 - smallXt;
}
if (x3t1 - smallXt > sizeXt) {
sizeXt = x3t1 - smallXt;
}
if (y1t1 > y2t1) {
sizeYt = y1t1 - smallYt;
} else {
sizeYt = y2t1 - smallYt;
}
if (y3t1 - smallYt > sizeYt) {
sizeYt = y3t1 - smallYt;
}
if (x1t2 >= smallXt && x1t2 <= smallXt+sizeXt && y1t2 >= smallYt && y1t2 <= smallYt+sizeYt) {
String dir = "UP";
float m;
float b;
if (x1t1 == smallXt) {
if (y2t1 == smallYt) {
dir = "UP";
} else {
if (y2t1 == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t1 == smallXt + sizeXt) {
if (y2t1 == smallYt) {
dir = "UP";
} else {
if (y2t1 == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y1t1-y2t1)/(x1t1-x2t1);
b = y1t1 - (m*x1t1);
if (dir == "UP") {
if (y1t2 > x1t2*m + b) {
return false;
}
} else {
if (y1t2 < x1t2*m + b) {
return false;
}
}
if (x1t1 == smallXt) {
if (y2t1 == smallYt) {
dir = "UP";
} else {
if (y2t1 == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t1 == smallXt + sizeXt) {
if (y2t1 == smallYt) {
dir = "UP";
} else {
if (y2t1 == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y1t1-y2t1)/(x1t1-x2t1);
b = y1t1 - (m*x1t1);
if (dir == "UP") {
if (y1t2 > x1t2*m + b) {
return false;
}
} else {
if (y1t2 < x1t2*m + b) {
return false;
}
}
if (x1t1 == smallXt) {
if (y2t1 == smallYt) {
dir = "UP";
} else {
if (y2t1 == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t1 == smallXt + sizeXt) {
if (y2t1 == smallYt) {
dir = "UP";
} else {
if (y2t1 == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y1t1-y3t1)/(x1t1-x3t1);
b = y3t1 - (m*x3t1);
if (dir == "UP") {
if (y1t2 > x1t2*m + b) {
return false;
}
} else {
if (y1t2 < x1t2*m + b) {
return false;
}
}
if (x3t1 == smallXt) {
if (y3t1 == smallYt) {
dir = "UP";
} else {
if (y3t1 == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x1t1 == smallXt + sizeXt) {
if (y3t1 == smallYt) {
dir = "UP";
} else {
if (y3t1 == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y2t1-y3t1)/(x2t1-x3t1);
b = y2t1 - (m*x2t1);
if (dir == "UP") {
if (y1t2 > x1t2*m + b) {
return false;
}
} else {
if (y1t2 < x1t2*m + b) {
return false;
}
}
m = (y2t1-y3t1)/(x2t1-x3t1);
b = y3t1 - (m*x3t1);
if (dir == "UP") {
if (y1t2 > x1t2*m + b) {
return false;
}
} else {
if (y1t2 < x1t2*m + b) {
return false;
}
}
if (x3t1 == smallXt) {
if (y3t1 == smallYt) {
dir = "UP";
} else {
if (y3t1 == smallYt + sizeYt) {
dir = "DOWN";
}
}
} else {
if (x2t1 == smallXt + sizeXt) {
if (y3t1 == smallYt) {
dir = "UP";
} else {
if (y3t1 == smallYt + sizeYt) {
dir = "DOWN";
}
}
}
}
m = (y2t1-y3t1)/(x2t1-x3t1);
b = y2t1 - (m*x2t1);
if (dir == "UP") {
if (y1t2 > x1t2*m + b) {
return false;
}
} else {
if (y1t2 < x1t2*m + b) {
return false;
}
}
}
return false;
}
Comments
Very thanks
Mille grazie
:)
You're welcome!