-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRay.pde
155 lines (132 loc) · 4.41 KB
/
Ray.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
https://www.youtube.com/watch?v=TOEi6T2mtHo
*/
public class Ray {
PVector pos, dir;
float extent;
boolean isColliding;
public RayCollisionData data;
public Ray() {}
public Ray(PVector p, PVector d, float e) {
lookAt(p, d);
extent = e;
}
public Ray(PVector p, PVector d) {
lookAt(p, d);
extent = 1;
}
public void lookAt(PVector p, PVector d) {
pos = p;
lookAt(d);
}
public void lookAt(PVector d) {
dir = d;
dir.normalize();
}
public boolean isCollidingWall(Wall wall) {
RayCollisionData tdata = null;
float closestDist = Float.MAX_VALUE;
for (Boundary b : wall.shape) {
RayCollisionData ttdata = checkIntersection(b);
if (ttdata != null) {
if (ttdata.distance < closestDist) {
closestDist = ttdata.distance;
tdata = ttdata;
}
}
}
if (tdata == null) {
isColliding = false;
} else {
data = tdata;
isColliding = true;
}
return isColliding;
}
public boolean isCollidingWall(Wall wall, float ex) {
RayCollisionData tdata = null;
float closestDist = Float.MAX_VALUE;
for (Boundary b : wall.shape) {
RayCollisionData ttdata = checkIntersection(b, ex);
if (ttdata != null) {
if (ttdata.distance < closestDist) {
closestDist = ttdata.distance;
tdata = ttdata;
}
}
}
if (tdata == null) {
isColliding = false;
} else {
data = tdata;
isColliding = true;
}
return isColliding;
}
public RayCollisionData checkIntersection(Boundary b) {
float x1 = pos.x, y1 = pos.y, x2 = pos.x + dir.x * extent, y2 = pos.y + dir.y * extent, x3 = b.start.x, y3 = b.start.y, x4 = b.end.x, y4 = b.end.y;
float divisor = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); // denominator
if (divisor == 0) {
// ray and line are parallel
return null;
};
float t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / divisor;
float u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / divisor;
boolean isIntersecting = t >= 0 && t <= 1 && u >= 0 && u <= 1;
if (!isIntersecting) {
return null;
}
PVector coll = new PVector(
x1 + t * (x2 - x1),
y1 + t * (y2 - y1)
);
data = new RayCollisionData(b, t, coll);
return data;
}
public RayCollisionData checkIntersection(Boundary b, float ex) {
float x1 = pos.x, y1 = pos.y, x2 = pos.x + dir.x * ex, y2 = pos.y + dir.y * ex, x3 = b.start.x, y3 = b.start.y, x4 = b.end.x, y4 = b.end.y;
float divisor = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4); // denominator
if (divisor == 0) {
// ray and line are parallel
return null;
};
float t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / divisor;
float u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / divisor;
boolean isIntersecting = t >= 0 && t <= 1 && u >= 0 && u <= 1;
if (!isIntersecting) {
return null;
}
PVector coll = new PVector(
x1 + t * (x2 - x1),
y1 + t * (y2 - y1)
);
data = new RayCollisionData(b, t, coll);
return data;
}
public void update(PVector np) {
pos = np;
}
public void show() {
line(pos.x, pos.y, pos.x + dir.x * extent, pos.y + dir.y * extent);
}
public void show(color c) {
pushMatrix();
stroke(c);
line(pos.x, pos.y, pos.x + dir.x * extent, pos.y + dir.y * extent);
popMatrix();
}
public void showColliding() {
if (isColliding) line(pos.x, pos.y, data.collidingPoint.x, data.collidingPoint.y);
}
}
public class RayCollisionData {
Boundary boundary;
float distance;
PVector collidingPoint;
RayCollisionData(Boundary boundary, float distance, PVector collidingPoint) {
this.boundary = boundary;
this.distance = distance;
this.collidingPoint = collidingPoint;
}
RayCollisionData() {}
}