Panda3D
 All Classes Functions Variables Enumerations
collisionBox.I
1 // Filename: collisionBox.I
2 // Created by: amith tudur (31Jul09)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: CollisionBox::Constructor
18 // Access: Public
19 // Description: Create the Box by giving a Center and distances of
20 // of each of the sides of box from the Center.
21 ////////////////////////////////////////////////////////////////////
22 INLINE CollisionBox::
23 CollisionBox(const LPoint3 &center, PN_stdfloat x, PN_stdfloat y, PN_stdfloat z) :
24  _center(center), _x(x), _y(y), _z(z)
25 {
26  _min = LPoint3(_center.get_x() - _x, _center.get_y() - _y, _center.get_z() - _z);
27  _max = LPoint3(_center.get_x() + _x, _center.get_y() + _y, _center.get_z() + _z);
28  _radius = sqrt(_x*_x + _y*_y + _z*_z);
29  for(int v = 0; v < 8; v++)
30  _vertex[v] = get_point_aabb(v);
31  for(int p = 0; p < 6; p++)
32  _planes[p] = set_plane(p);
33  setup_box();
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: CollisionBox::Constructor
38 // Access: Public
39 // Description: Create the Box by Specifying the Diagonal Points
40 ////////////////////////////////////////////////////////////////////
41 INLINE CollisionBox::
42 CollisionBox(const LPoint3 &min, const LPoint3 &max) :
43  _min(min), _max(max)
44 {
45  _center = (_min + _max) / 2;
46  _x = _center.get_x() - _min.get_x();
47  _y = _center.get_y() - _min.get_y();
48  _z = _center.get_z() - _min.get_z();
49  _radius = sqrt(_x*_x + _y*_y + _z*_z);
50  for(int v = 0; v < 8; v++)
51  _vertex[v] = get_point_aabb(v);
52  for(int p = 0; p < 6; p++)
53  _planes[p] = set_plane(p);
54  setup_box();
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: CollisionBox::Default constructor
59 // Access: Protected
60 // Description: Creates an invalid Box. Only used when reading
61 // from a bam file.
62 ////////////////////////////////////////////////////////////////////
63 INLINE CollisionBox::
64 CollisionBox() {
65 }
66 
67 ////////////////////////////////////////////////////////////////////
68 // Function: CollisionBox::Copy Constructor
69 // Access: Public
70 // Description:
71 ////////////////////////////////////////////////////////////////////
72 INLINE CollisionBox::
73 CollisionBox(const CollisionBox &copy) :
74  CollisionSolid(copy),
75  _center(copy._center),
76  _min(copy._min),
77  _max(copy._max),
78  _x(copy._x ),
79  _y(copy._y ),
80  _z(copy._z ),
81  _radius(copy._radius )
82 {
83  for(int v = 0; v < 8; v++)
84  _vertex[v] = copy._vertex[v];
85  for(int p = 0; p < 6; p++)
86  _planes[p] = copy._planes[p];
87  setup_box();
88 }
89 
90 ////////////////////////////////////////////////////////////////////
91 // Function: CollisionBox::flush_level
92 // Access: Public, Static
93 // Description: Flushes the PStatCollectors used during traversal.
94 ////////////////////////////////////////////////////////////////////
95 INLINE void CollisionBox::
97  _volume_pcollector.flush_level();
98  _test_pcollector.flush_level();
99 }
100 
101 ////////////////////////////////////////////////////////////////////
102 // Function: CollisionBox::set_center
103 // Access: Published
104 // Description:
105 ////////////////////////////////////////////////////////////////////
106 INLINE void CollisionBox::
107 set_center(const LPoint3 &center) {
108  _center = center;
109  mark_internal_bounds_stale();
110  mark_viz_stale();
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: CollisionBox::set_center
115 // Access: Published
116 // Description:
117 ////////////////////////////////////////////////////////////////////
118 INLINE void CollisionBox::
119 set_center(PN_stdfloat x, PN_stdfloat y, PN_stdfloat z) {
120  set_center(LPoint3(x, y, z));
121 }
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function: CollisionBox::get_center
125 // Access: Published
126 // Description:
127 ////////////////////////////////////////////////////////////////////
128 INLINE const LPoint3 &CollisionBox::
129 get_center() const {
130  return _center;
131 }
132 
133 ////////////////////////////////////////////////////////////////////
134 // Function: CollisionBox::get_radius
135 // Access: Published
136 // Description:
137 ////////////////////////////////////////////////////////////////////
138 INLINE PN_stdfloat CollisionBox::
139 get_radius() const {
140  return _radius;
141 }
142 
143 ////////////////////////////////////////////////////////////////////
144 // Function: CollisionBox::get_num_points
145 // Access: Published
146 // Description: Returns 8: the number of vertices of a rectangular solid.
147 ////////////////////////////////////////////////////////////////////
148 INLINE_MATHUTIL int CollisionBox::
149 get_num_points() const {
150  return 8;
151 }
152 
153 ////////////////////////////////////////////////////////////////////
154 // Function: CollisionBox::get_point
155 // Access: Published
156 // Description: Returns the nth vertex of the OBB.
157 ////////////////////////////////////////////////////////////////////
158 INLINE_MATHUTIL LPoint3 CollisionBox::
159 get_point(int n) const {
160  nassertr(n >= 0 && n < 8, LPoint3::zero());
161  return _vertex[n];
162 }
163 
164 
165 ////////////////////////////////////////////////////////////////////
166 // Function: CollisionBox::get_point_aabb
167 // Access: Published
168 // Description: Returns the nth vertex of the Axis Aligned Bounding Box.
169 ////////////////////////////////////////////////////////////////////
170 INLINE_MATHUTIL LPoint3 CollisionBox::
171 get_point_aabb(int n) const {
172  nassertr(n >= 0 && n < 8, LPoint3::zero());
173 
174  // We do some trickery assuming that _min and _max are consecutive
175  // in memory.
176  const LPoint3 *a = &_min;
177  return LPoint3(a[(n>>2)&1][0], a[(n>>1)&1][1], a[(n)&1][2]);
178 }
179 
180 ////////////////////////////////////////////////////////////////////
181 // Function: CollisionBox::get_num_planes
182 // Access: Published
183 // Description: Returns 6: the number of faces of a rectangular solid.
184 ////////////////////////////////////////////////////////////////////
185 INLINE_MATHUTIL int CollisionBox::
186 get_num_planes() const {
187  return 6;
188 }
189 
190 ////////////////////////////////////////////////////////////////////
191 // Function: CollisionBox::get_plane
192 // Access: Published
193 // Description: Returns the nth face of the rectangular solid.
194 ////////////////////////////////////////////////////////////////////
195 INLINE_MATHUTIL LPlane CollisionBox::
196 get_plane(int n) const {
197  nassertr(n >= 0 && n < 6, LPlane());
198  return _planes[n];
199 }
200 
201 ////////////////////////////////////////////////////////////////////
202 // Function: CollisionBox::set_plane
203 // Access: Published
204 // Description: Creates the nth face of the rectangular solid.
205 ////////////////////////////////////////////////////////////////////
206 INLINE_MATHUTIL LPlane CollisionBox::
207 set_plane(int n) const {
208  nassertr(n >= 0 && n < 6, LPlane());
209  return LPlane(get_point(plane_def[n][0]),
210  get_point(plane_def[n][1]),
211  get_point(plane_def[n][2]));
212 }
213 
214 
215 ////////////////////////////////////////////////////////////////////
216 // Function: CollisionBox::is_right
217 // Access: Private, Static
218 // Description: Returns true if the 2-d v1 is to the right of v2.
219 ////////////////////////////////////////////////////////////////////
220 INLINE bool CollisionBox::
221 is_right(const LVector2 &v1, const LVector2 &v2) {
222  return (v1[0] * v2[1] - v1[1] * v2[0]) > 1.0e-6f;
223 }
224 
225 ////////////////////////////////////////////////////////////////////
226 // Function: CollisionBox::dist_to_line
227 // Access: Private, Static
228 // Description: Returns the linear distance of p to the line defined
229 // by f and f+v, where v is a normalized vector. The
230 // result is negative if p is left of the line, positive
231 // if it is right of the line.
232 ////////////////////////////////////////////////////////////////////
233 INLINE PN_stdfloat CollisionBox::
234 dist_to_line(const LPoint2 &p,
235  const LPoint2 &f, const LVector2 &v) {
236  LVector2 v1 = (p - f);
237  return (v1[0] * v[1] - v1[1] * v[0]);
238 }
239 
240 ////////////////////////////////////////////////////////////////////
241 // Function: CollisionBox::to_2d
242 // Access: Private
243 // Description: Assuming the indicated point in 3-d space lies within
244 // the polygon's plane, returns the corresponding point
245 // in the polygon's 2-d definition space.
246 ////////////////////////////////////////////////////////////////////
247 INLINE LPoint2 CollisionBox::
248 to_2d(const LVecBase3 &point3d, int plane) const {
249  LPoint3 point = LPoint3(point3d) * _to_2d_mat[plane];
250  return LPoint2(point[0], point[2]);
251 }
252 
253 ////////////////////////////////////////////////////////////////////
254 // Function: CollisionBox::calc_to_3d_mat
255 // Access: Private
256 // Description: Fills the indicated matrix with the appropriate
257 // rotation transform to move points from the 2-d plane
258 // into the 3-d (X, 0, Z) plane.
259 ////////////////////////////////////////////////////////////////////
260 INLINE void CollisionBox::
261 calc_to_3d_mat(LMatrix4 &to_3d_mat,int plane) const {
262  // We have to be explicit about the coordinate system--we
263  // specifically mean CS_zup_right, because that points the forward
264  // vector down the Y axis and moves the coords in (X, 0, Z). We
265  // want this effect regardless of the user's coordinate system of
266  // choice.
267 
268  // The up vector, on the other hand, is completely arbitrary.
269 
270  look_at(to_3d_mat, -get_plane(plane).get_normal(),
271  LVector3(0.0f, 0.0f, 1.0f), CS_zup_right);
272  to_3d_mat.set_row(3, get_plane(plane).get_point());
273 }
274 
275 ////////////////////////////////////////////////////////////////////
276 // Function: CollisionBox::rederive_to_3d_mat
277 // Access: Private
278 // Description: Fills the indicated matrix with the appropriate
279 // rotation transform to move points from the 2-d plane
280 // into the 3-d (X, 0, Z) plane.
281 //
282 // This is essentially similar to calc_to_3d_mat, except
283 // that the matrix is rederived from whatever is stored
284 // in _to_2d_mat, guaranteeing that it will match
285 // whatever algorithm produced that one, even if it was
286 // produced on a different machine with different
287 // numerical precision.
288 ////////////////////////////////////////////////////////////////////
289 INLINE void CollisionBox::
290 rederive_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const {
291  to_3d_mat.invert_from(_to_2d_mat[plane]);
292 }
293 
294 ////////////////////////////////////////////////////////////////////
295 // Function: CollisionBox::to_3d
296 // Access: Private, Static
297 // Description: Extrude the indicated point in the polygon's 2-d
298 // definition space back into 3-d coordinates.
299 ////////////////////////////////////////////////////////////////////
300 INLINE LPoint3 CollisionBox::
301 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat) {
302  return LPoint3(point2d[0], 0.0f, point2d[1]) * to_3d_mat;
303 }
304 
305 ////////////////////////////////////////////////////////////////////
306 // Function: CollisionBox::PointDef::Constructor
307 // Access: Public
308 // Description:
309 ////////////////////////////////////////////////////////////////////
310 INLINE CollisionBox::PointDef::
311 PointDef(const LPoint2 &p, const LVector2 &v) : _p(p), _v(v) {
312 }
313 
314 ////////////////////////////////////////////////////////////////////
315 // Function: CollisionBox::PointDef::Constructor
316 // Access: Public
317 // Description:
318 ////////////////////////////////////////////////////////////////////
319 INLINE CollisionBox::PointDef::
320 PointDef(PN_stdfloat x, PN_stdfloat y) : _p(x, y), _v(0.0f, 0.0f) {
321 }
322 
323 ////////////////////////////////////////////////////////////////////
324 // Function: CollisionBox::PointDef::Copy Constructor
325 // Access: Public
326 // Description:
327 ////////////////////////////////////////////////////////////////////
328 INLINE CollisionBox::PointDef::
329 PointDef(const CollisionBox::PointDef &copy) : _p(copy._p), _v(copy._v) {
330 }
331 
332 ////////////////////////////////////////////////////////////////////
333 // Function: CollisionBox::PointDef::Copy Assignment Operator
334 // Access: Public
335 // Description:
336 ////////////////////////////////////////////////////////////////////
337 INLINE void CollisionBox::PointDef::
338 operator = (const CollisionBox::PointDef &copy) {
339  _p = copy._p;
340  _v = copy._v;
341 }
342 
343 ////////////////////////////////////////////////////////////////////
344 // Function: CollisionBox::get_plane_points
345 // Access: Public
346 // Description: returns the points that form the nth plane
347 ////////////////////////////////////////////////////////////////////
350  return _points[n];
351 }
void rederive_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const
Fills the indicated matrix with the appropriate rotation transform to move points from the 2-d plane ...
Definition: collisionBox.I:290
void setup_box()
Compute parameters for each of the box&#39;s sides.
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
LPoint2 to_2d(const LVecBase3 &point3d, int plane) const
Assuming the indicated point in 3-d space lies within the polygon&#39;s plane, returns the corresponding ...
Definition: collisionBox.I:248
LPoint3 get_point_aabb(int n) const
Returns the nth vertex of the Axis Aligned Bounding Box.
Definition: collisionBox.I:171
int get_num_points() const
Returns 8: the number of vertices of a rectangular solid.
Definition: collisionBox.I:149
The abstract base class for all things that can collide with other things in the world, and all the things they can collide with (except geometry).
static const LPoint3f & zero()
Returns a zero-length point.
Definition: lpoint3.h:258
A cuboid collision volume or object.
Definition: collisionBox.h:30
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
static LPoint3 to_3d(const LVecBase2 &point2d, const LMatrix4 &to_3d_mat)
Extrude the indicated point in the polygon&#39;s 2-d definition space back into 3-d coordinates.
Definition: collisionBox.I:301
LPlane get_plane(int n) const
Returns the nth face of the rectangular solid.
Definition: collisionBox.I:196
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
bool invert_from(const LMatrix4f &other)
Computes the inverse of the other matrix, and stores the result in this matrix.
Definition: lmatrix.h:2173
This is the base class for all two-component vectors and points.
Definition: lvecBase2.h:105
int get_num_planes() const
Returns 6: the number of faces of a rectangular solid.
Definition: collisionBox.I:186
static void flush_level()
Flushes the PStatCollectors used during traversal.
Definition: collisionBox.I:96
This is a two-component vector offset.
Definition: lvector2.h:91
void calc_to_3d_mat(LMatrix4 &to_3d_mat, int plane) const
Fills the indicated matrix with the appropriate rotation transform to move points from the 2-d plane ...
Definition: collisionBox.I:261
void set_row(int row, const LVecBase4f &v)
Replaces the indicated row of the matrix.
Definition: lmatrix.h:1187
LPlane set_plane(int n) const
Creates the nth face of the rectangular solid.
Definition: collisionBox.I:207
This is a two-component point in space.
Definition: lpoint2.h:92
LPoint3 get_point(int n) const
Returns the nth vertex of the OBB.
Definition: collisionBox.I:159
Points get_plane_points(int n)
returns the points that form the nth plane
Definition: collisionBox.I:349