-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collision.pde
72 lines (58 loc) · 2.2 KB
/
Collision.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
boolean isCollidingCircleRectangle(
float circleX,
float circleY,
float radius,
float rectangleX,
float rectangleY,
float rectangleWidth,
float rectangleHeight)
{
float circleDistanceX = abs(circleX - rectangleX - rectangleWidth/2);
float circleDistanceY = abs(circleY - rectangleY - rectangleHeight/2);
if (circleDistanceX > (rectangleWidth/2 + radius)) { return false; }
if (circleDistanceY > (rectangleHeight/2 + radius)) { return false; }
if (circleDistanceX <= (rectangleWidth/2)) { return true; }
if (circleDistanceY <= (rectangleHeight/2)) { return true; }
float cornerDistance_sq = pow(circleDistanceX - rectangleWidth/2, 2) +
pow(circleDistanceY - rectangleHeight/2, 2);
return (cornerDistance_sq <= pow(radius,2));
}
boolean lineRectangleIntersect(float x1, float y1, float x2, float y2,
float rx, float ry, float rw, float rh) {
float topIntersection;
float bottomIntersection;
float topPoint;
float bottomPoint;
// Calculate m and c for the equation for the line (y = mx+c)
float m = (y2-y1) / (x2-x1);
float c = y1 -(m*x1);
// If the line is going up from right to left then the top intersect point is on the left
if(m > 0) {
topIntersection = (m*rx + c);
bottomIntersection = (m*(rx+rw) + c);
}
// Otherwise it's on the right
else {
topIntersection = (m*(rx+rw) + c);
bottomIntersection = (m*rx + c);
}
// Work out the top and bottom extents for the triangle
if(y1 < y2) {
topPoint = y1;
bottomPoint = y2;
} else {
topPoint = y2;
bottomPoint = y1;
}
float topOverlap;
float botOverlap;
// Calculate the overlap between those two bounds
topOverlap = topIntersection > topPoint ? topIntersection : topPoint;
botOverlap = bottomIntersection < bottomPoint ? bottomIntersection : bottomPoint;
return (topOverlap<botOverlap) && (!((botOverlap<ry) || (topOverlap>ry+rh)));
}
boolean circleCircleCollision(float x1, float y1, float radius1, float x2, float y2, float radius2)
{
float dist = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
return dist < (radius1 + radius2);
}