forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollisionBox.I
More file actions
305 lines (272 loc) · 6.95 KB
/
collisionBox.I
File metadata and controls
305 lines (272 loc) · 6.95 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file collisionBox.I
* @author amith tudur
* @date 2009-07-31
*/
/**
* Create the Box by giving a Center and distances of each of the sides of
* box from the Center.
*/
INLINE CollisionBox::
CollisionBox(const LPoint3 ¢er, PN_stdfloat x, PN_stdfloat y, PN_stdfloat z) :
_center(center), _x(x), _y(y), _z(z)
{
_min = LPoint3(_center.get_x() - _x, _center.get_y() - _y, _center.get_z() - _z);
_max = LPoint3(_center.get_x() + _x, _center.get_y() + _y, _center.get_z() + _z);
_radius = sqrt(_x*_x + _y*_y + _z*_z);
for(int v = 0; v < 8; v++)
_vertex[v] = get_point_aabb(v);
for(int p = 0; p < 6; p++)
_planes[p] = set_plane(p);
setup_box();
}
/**
* Create the Box by Specifying the Diagonal Points
*/
INLINE CollisionBox::
CollisionBox(const LPoint3 &min, const LPoint3 &max) :
_min(min), _max(max)
{
_center = (_min + _max) / 2;
_x = _center.get_x() - _min.get_x();
_y = _center.get_y() - _min.get_y();
_z = _center.get_z() - _min.get_z();
_radius = sqrt(_x*_x + _y*_y + _z*_z);
for(int v = 0; v < 8; v++)
_vertex[v] = get_point_aabb(v);
for(int p = 0; p < 6; p++)
_planes[p] = set_plane(p);
setup_box();
}
/**
* Creates an invalid Box. Only used when reading from a bam file.
*/
INLINE CollisionBox::
CollisionBox() {
}
/**
*
*/
INLINE CollisionBox::
CollisionBox(const CollisionBox ©) :
CollisionSolid(copy),
_center(copy._center),
_min(copy._min),
_max(copy._max),
_x(copy._x ),
_y(copy._y ),
_z(copy._z ),
_radius(copy._radius )
{
for(int v = 0; v < 8; v++)
_vertex[v] = copy._vertex[v];
for(int p = 0; p < 6; p++)
_planes[p] = copy._planes[p];
setup_box();
}
/**
* Flushes the PStatCollectors used during traversal.
*/
INLINE void CollisionBox::
flush_level() {
_volume_pcollector.flush_level();
_test_pcollector.flush_level();
}
/**
*
*/
INLINE void CollisionBox::
set_center(const LPoint3 ¢er) {
_center = center;
mark_internal_bounds_stale();
mark_viz_stale();
}
/**
*
*/
INLINE void CollisionBox::
set_center(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z) {
set_center(LPoint3(x, y, z));
}
/**
*
*/
INLINE const LPoint3 &CollisionBox::
get_center() const {
return _center;
}
/**
*
*/
INLINE const LPoint3 &CollisionBox::
get_min() const {
return _min;
}
/**
*
*/
INLINE const LPoint3 &CollisionBox::
get_max() const {
return _max;
}
/**
*
*/
INLINE LVector3 CollisionBox::
get_dimensions() const {
return _max - _min;
}
/**
* Returns 8: the number of vertices of a rectangular solid.
*/
INLINE int CollisionBox::
get_num_points() const {
return 8;
}
/**
* Returns the nth vertex of the OBB.
*/
INLINE LPoint3 CollisionBox::
get_point(int n) const {
nassertr(n >= 0 && n < 8, LPoint3::zero());
return _vertex[n];
}
/**
* Returns the nth vertex of the Axis Aligned Bounding Box.
*/
INLINE LPoint3 CollisionBox::
get_point_aabb(int n) const {
nassertr(n >= 0 && n < 8, LPoint3::zero());
// We do some trickery assuming that _min and _max are consecutive in
// memory.
const LPoint3 *a = &_min;
return LPoint3(a[(n>>2)&1][0], a[(n>>1)&1][1], a[(n)&1][2]);
}
/**
* Returns 6: the number of faces of a rectangular solid.
*/
INLINE int CollisionBox::
get_num_planes() const {
return 6;
}
/**
* Returns the nth face of the rectangular solid.
*/
INLINE LPlane CollisionBox::
get_plane(int n) const {
nassertr(n >= 0 && n < 6, LPlane());
return _planes[n];
}
/**
* Creates the nth face of the rectangular solid.
*/
INLINE LPlane CollisionBox::
set_plane(int n) const {
nassertr(n >= 0 && n < 6, LPlane());
return LPlane(get_point(plane_def[n][0]),
get_point(plane_def[n][1]),
get_point(plane_def[n][2]));
}
/**
* Returns true if the 2-d v1 is to the right of v2.
*/
INLINE bool CollisionBox::
is_right(const LVector2 &v1, const LVector2 &v2) {
return (v1[0] * v2[1] - v1[1] * v2[0]) > 1.0e-6f;
}
/**
* Returns the linear distance of p to the line defined by f and f+v, where v
* is a normalized vector. The result is negative if p is left of the line,
* positive if it is right of the line.
*/
INLINE PN_stdfloat CollisionBox::
dist_to_line(const LPoint2 &p,
const LPoint2 &f, const LVector2 &v) {
LVector2 v1 = (p - f);
return (v1[0] * v[1] - v1[1] * v[0]);
}
/**
* Assuming the indicated point in 3-d space lies within the polygon's plane,
* returns the corresponding point in the polygon's 2-d definition space.
*/
INLINE LPoint2 CollisionBox::
to_2d(const LVecBase3 &point3d, int plane) const {
LPoint3 point = LPoint3(point3d) * _to_2d_mat[plane];
return LPoint2(point[0], point[2]);
}
/**
* Fills the indicated matrix with the appropriate rotation transform to move
* points from the 2-d plane into the 3-d (X, 0, Z) plane.
*/
INLINE void CollisionBox::
calc_to_3d_mat(LMatrix4 &to_3d_mat,int plane) const {
// We have to be explicit about the coordinate system--we specifically mean
// CS_zup_right, because that points the forward vector down the Y axis and
// moves the coords in (X, 0, Z). We want this effect regardless of the
// user's coordinate system of choice.
// The up vector, on the other hand, is completely arbitrary.
look_at(to_3d_mat, -get_plane(plane).get_normal(),
LVector3(0.0f, 0.0f, 1.0f), CS_zup_right);
to_3d_mat.set_row(3, get_plane(plane).get_point());
}
/**
* Fills the indicated matrix with the appropriate rotation transform to move
* points from the 2-d plane into the 3-d (X, 0, Z) plane.
*
* This is essentially similar to calc_to_3d_mat, except that the matrix is
* rederived from whatever is stored in _to_2d_mat, guaranteeing that it will
* match whatever algorithm produced that one, even if it was produced on a
* different machine with different numerical precision.
*/
INLINE void CollisionBox::
rederive_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const {
to_3d_mat.invert_from(_to_2d_mat[plane]);
}
/**
* Extrude the indicated point in the polygon's 2-d definition space back into
* 3-d coordinates.
*/
INLINE LPoint3 CollisionBox::
to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat) {
return LPoint3(point2d[0], 0.0f, point2d[1]) * to_3d_mat;
}
/**
*
*/
INLINE CollisionBox::PointDef::
PointDef(const LPoint2 &p, const LVector2 &v) : _p(p), _v(v) {
}
/**
*
*/
INLINE CollisionBox::PointDef::
PointDef(PN_stdfloat x, PN_stdfloat y) : _p(x, y), _v(0.0f, 0.0f) {
}
/**
*
*/
INLINE CollisionBox::PointDef::
PointDef(const CollisionBox::PointDef ©) : _p(copy._p), _v(copy._v) {
}
/**
*
*/
INLINE void CollisionBox::PointDef::
operator = (const CollisionBox::PointDef ©) {
_p = copy._p;
_v = copy._v;
}
/**
* returns the points that form the nth plane
*/
INLINE CollisionBox::Points CollisionBox::
get_plane_points(int n) {
return _points[n];
}