Panda3D
collisionBox.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file collisionBox.cxx
10  * @author amith tudur
11  * @date 2009-07-31
12  */
13 
14 #include "collisionBox.h"
15 #include "collisionLine.h"
16 #include "collisionRay.h"
17 #include "collisionSphere.h"
18 #include "collisionSegment.h"
19 #include "collisionCapsule.h"
20 #include "collisionHandler.h"
21 #include "collisionEntry.h"
22 #include "config_collide.h"
23 #include "boundingSphere.h"
24 #include "datagram.h"
25 #include "datagramIterator.h"
26 #include "bamReader.h"
27 #include "bamWriter.h"
28 #include "nearly_zero.h"
29 #include "cmath.h"
30 #include "mathNumbers.h"
31 #include "geom.h"
32 #include "geomTriangles.h"
33 #include "geomVertexWriter.h"
34 #include "config_mathutil.h"
35 #include "dcast.h"
36 
37 #include <math.h>
38 
39 using std::max;
40 using std::min;
41 
42 PStatCollector CollisionBox::_volume_pcollector("Collision Volumes:CollisionBox");
43 PStatCollector CollisionBox::_test_pcollector("Collision Tests:CollisionBox");
44 TypeHandle CollisionBox::_type_handle;
45 
46 const int CollisionBox::plane_def[6][4] = {
47  {0, 4, 5, 1},
48  {4, 6, 7, 5},
49  {6, 2, 3, 7},
50  {2, 0, 1, 3},
51  {1, 5, 7, 3},
52  {2, 6, 4, 0},
53 };
54 
55 /**
56  *
57  */
58 CollisionSolid *CollisionBox::
59 make_copy() {
60  return new CollisionBox(*this);
61 }
62 
63 /**
64  * Compute parameters for each of the box's sides
65  */
66 void CollisionBox::
68  for(int plane = 0; plane < 6; plane++) {
69  LPoint3 array[4];
70  array[0] = get_point(plane_def[plane][0]);
71  array[1] = get_point(plane_def[plane][1]);
72  array[2] = get_point(plane_def[plane][2]);
73  array[3] = get_point(plane_def[plane][3]);
74  setup_points(array, array+4, plane);
75  }
76 }
77 
78 /**
79  * Computes the plane and 2d projection of points that make up this side.
80  */
81 void CollisionBox::
82 setup_points(const LPoint3 *begin, const LPoint3 *end, int plane) {
83  int num_points = end - begin;
84  nassertv(num_points >= 3);
85 
86  _points[plane].clear();
87 
88  // Construct a matrix that rotates the points from the (X,0,Z) plane into
89  // the 3-d plane.
90  LMatrix4 to_3d_mat;
91  calc_to_3d_mat(to_3d_mat, plane);
92 
93  // And the inverse matrix rotates points from 3-d space into the 2-d plane.
94  _to_2d_mat[plane].invert_from(to_3d_mat);
95 
96  // Now project all of the points onto the 2-d plane.
97 
98  const LPoint3 *pi;
99  for (pi = begin; pi != end; ++pi) {
100  LPoint3 point = (*pi) * _to_2d_mat[plane];
101  _points[plane].push_back(PointDef(point[0], point[2]));
102  }
103 
104  nassertv(_points[plane].size() >= 3);
105 
106 #ifndef NDEBUG
107  /*
108  // Now make sure the points define a convex polygon.
109  if (is_concave()) {
110  collide_cat.error() << "Invalid concave CollisionPolygon defined:\n";
111  const LPoint3 *pi;
112  for (pi = begin; pi != end; ++pi) {
113  collide_cat.error(false) << " " << (*pi) << "\n";
114  }
115  collide_cat.error(false)
116  << " normal " << normal << " with length " << normal.length() << "\n";
117  _points.clear();
118  }
119  */
120 #endif
121 
122  compute_vectors(_points[plane]);
123 }
124 
125 /**
126  * First Dispatch point for box as a FROM object
127  */
128 PT(CollisionEntry) CollisionBox::
129 test_intersection(const CollisionEntry &entry) const {
130  return entry.get_into()->test_intersection_from_box(entry);
131 }
132 
133 /**
134  * Transforms the solid by the indicated matrix.
135  */
136 void CollisionBox::
137 xform(const LMatrix4 &mat) {
138  _min = _min * mat;
139  _max = _max * mat;
140  _center = _center * mat;
141  for(int v = 0; v < 8; v++) {
142  _vertex[v] = _vertex[v] * mat;
143  }
144  for(int p = 0; p < 6 ; p++) {
145  _planes[p] = set_plane(p);
146  }
147  _x = _vertex[0].get_x() - _center.get_x();
148  _y = _vertex[0].get_y() - _center.get_y();
149  _z = _vertex[0].get_z() - _center.get_z();
150  _radius = sqrt(_x * _x + _y * _y + _z * _z);
151  setup_box();
152  mark_viz_stale();
153  mark_internal_bounds_stale();
154 }
155 
156 /**
157  * Returns the point in space deemed to be the "origin" of the solid for
158  * collision purposes. The closest intersection point to this origin point is
159  * considered to be the most significant.
160  */
161 LPoint3 CollisionBox::
163  return _center;
164 }
165 
166 /**
167  * Returns a PStatCollector that is used to count the number of bounding
168  * volume tests made against a solid of this type in a given frame.
169  */
172  return _volume_pcollector;
173 }
174 
175 /**
176  * Returns a PStatCollector that is used to count the number of intersection
177  * tests made against a solid of this type in a given frame.
178  */
181  return _test_pcollector;
182 }
183 
184 /**
185  *
186  */
187 void CollisionBox::
188 output(std::ostream &out) const {
189 }
190 
191 /**
192  * Sphere is chosen as the Bounding Volume type for speed and efficiency
193  */
194 PT(BoundingVolume) CollisionBox::
195 compute_internal_bounds() const {
196  return new BoundingSphere(_center, _radius);
197 }
198 
199 /**
200  * Double dispatch point for sphere as FROM object
201  */
202 PT(CollisionEntry) CollisionBox::
203 test_intersection_from_sphere(const CollisionEntry &entry) const {
204 
205  const CollisionSphere *sphere;
206  DCAST_INTO_R(sphere, entry.get_from(), nullptr);
207 
208  CPT(TransformState) wrt_space = entry.get_wrt_space();
209  CPT(TransformState) wrt_prev_space = entry.get_wrt_prev_space();
210 
211  const LMatrix4 &wrt_mat = wrt_space->get_mat();
212 
213  LPoint3 orig_center = sphere->get_center() * wrt_mat;
214  LPoint3 from_center = orig_center;
215  bool moved_from_center = false;
216  PN_stdfloat t = 1.0f;
217  LPoint3 contact_point(from_center);
218  PN_stdfloat actual_t = 1.0f;
219 
220  LVector3 from_radius_v =
221  LVector3(sphere->get_radius(), 0.0f, 0.0f) * wrt_mat;
222  PN_stdfloat from_radius_2 = from_radius_v.length_squared();
223  PN_stdfloat from_radius = csqrt(from_radius_2);
224 
225  int ip;
226  PN_stdfloat max_dist = 0.0;
227  PN_stdfloat dist = 0.0;
228  bool intersect;
229  LPlane plane;
230  LVector3 normal;
231 
232  for(ip = 0, intersect = false; ip < 6 && !intersect; ip++) {
233  plane = get_plane( ip );
234  if (_points[ip].size() < 3) {
235  continue;
236  }
237  if (wrt_prev_space != wrt_space) {
238  // If we have a delta between the previous position and the current
239  // position, we use that to determine some more properties of the
240  // collision.
241  LPoint3 b = from_center;
242  LPoint3 a = sphere->get_center() * wrt_prev_space->get_mat();
243  LVector3 delta = b - a;
244 
245  // First, there is no collision if the "from" object is definitely
246  // moving in the same direction as the plane's normal.
247  PN_stdfloat dot = delta.dot(plane.get_normal());
248  if (dot > 0.1f) {
249  continue; // no intersection
250  }
251 
252  if (IS_NEARLY_ZERO(dot)) {
253  // If we're moving parallel to the plane, the sphere is tested at its
254  // final point. Leave it as it is.
255 
256  } else {
257 /*
258  * Otherwise, we're moving into the plane; the sphere is tested at the point
259  * along its path that is closest to intersecting the plane. This may be the
260  * actual intersection point, or it may be the starting point or the final
261  * point. dot is equal to the (negative) magnitude of 'delta' along the
262  * direction of the plane normal t = ratio of (distance from start pos to
263  * plane) to (distance from start pos to end pos), along axis of plane normal
264  */
265  PN_stdfloat dist_to_p = plane.dist_to_plane(a);
266  t = (dist_to_p / -dot);
267 
268  // also compute the actual contact point and time of contact for
269  // handlers that need it
270  actual_t = ((dist_to_p - from_radius) / -dot);
271  actual_t = min((PN_stdfloat)1.0, max((PN_stdfloat)0.0, actual_t));
272  contact_point = a + (actual_t * delta);
273 
274  if (t >= 1.0f) {
275  // Leave it where it is.
276 
277  } else if (t < 0.0f) {
278  from_center = a;
279  moved_from_center = true;
280  } else {
281  from_center = a + t * delta;
282  moved_from_center = true;
283  }
284  }
285  }
286 
287  normal = (has_effective_normal() && sphere->get_respect_effective_normal()) ? get_effective_normal() : plane.get_normal();
288 
289 #ifndef NDEBUG
290  /*if (!IS_THRESHOLD_EQUAL(normal.length_squared(), 1.0f, 0.001), NULL) {
291  std::cout
292  << "polygon within " << entry.get_into_node_path()
293  << " has normal " << normal << " of length " << normal.length()
294  << "\n";
295  normal.normalize();
296  }*/
297 #endif
298 
299  // The nearest point within the plane to our center is the intersection of
300  // the line (center, center - normal) with the plane.
301 
302  if (!plane.intersects_line(dist, from_center, -(plane.get_normal()))) {
303  // No intersection with plane? This means the plane's effective normal
304  // was within the plane itself. A useless polygon.
305  continue;
306  }
307 
308  if (dist > from_radius || dist < -from_radius) {
309  // No intersection with the plane.
310  continue;
311  }
312 
313  LPoint2 p = to_2d(from_center - dist * plane.get_normal(), ip);
314  PN_stdfloat edge_dist = 0.0f;
315 
316  const ClipPlaneAttrib *cpa = entry.get_into_clip_planes();
317  if (cpa != nullptr) {
318  // We have a clip plane; apply it.
319  Points new_points;
320  if (apply_clip_plane(new_points, cpa, entry.get_into_node_path().get_net_transform(),ip)) {
321  // All points are behind the clip plane; just do the default test.
322  edge_dist = dist_to_polygon(p, _points[ip]);
323  } else if (new_points.empty()) {
324  // The polygon is completely clipped.
325  continue;
326  } else {
327  // Test against the clipped polygon.
328  edge_dist = dist_to_polygon(p, new_points);
329  }
330  } else {
331  // No clip plane is in effect. Do the default test.
332  edge_dist = dist_to_polygon(p, _points[ip]);
333  }
334 
335  max_dist = from_radius;
336 
337  // Now we have edge_dist, which is the distance from the sphere center to
338  // the nearest edge of the polygon, within the polygon's plane.
339  // edge_dist<0 means the point is within the polygon.
340  if(edge_dist < 0) {
341  intersect = true;
342  continue;
343  }
344 
345  if((edge_dist > 0) &&
346  ((edge_dist * edge_dist + dist * dist) > from_radius_2)) {
347  // No intersection; the circle is outside the polygon.
348  continue;
349  }
350 
351  // The sphere appears to intersect the polygon. If the edge is less than
352  // from_radius away, the sphere may be resting on an edge of the polygon.
353  // Determine how far the center of the sphere must remain from the plane,
354  // based on its distance from the nearest edge.
355 
356  if (edge_dist >= 0.0f) {
357  PN_stdfloat max_dist_2 = max(from_radius_2 - edge_dist * edge_dist, (PN_stdfloat)0.0);
358  max_dist = csqrt(max_dist_2);
359  }
360 
361  if (dist > max_dist) {
362  // There's no intersection: the sphere is hanging off the edge.
363  continue;
364  }
365  intersect = true;
366  }
367  if( !intersect )
368  return nullptr;
369 
370  if (collide_cat.is_debug()) {
371  collide_cat.debug()
372  << "intersection detected from " << entry.get_from_node_path()
373  << " into " << entry.get_into_node_path() << "\n";
374  }
375 
376  PT(CollisionEntry) new_entry = new CollisionEntry(entry);
377 
378  PN_stdfloat into_depth = max_dist - dist;
379  if (moved_from_center) {
380  // We have to base the depth of intersection on the sphere's final resting
381  // point, not the point from which we tested the intersection.
382  PN_stdfloat orig_dist;
383  plane.intersects_line(orig_dist, orig_center, -normal);
384  into_depth = max_dist - orig_dist;
385  }
386 
387  // Clamp the surface point to the box bounds.
388  LPoint3 surface = from_center - normal * dist;
389  surface = surface.fmax(_min);
390  surface = surface.fmin(_max);
391 
392  new_entry->set_surface_normal(normal);
393  new_entry->set_surface_point(surface);
394  new_entry->set_interior_point(surface - normal * into_depth);
395  new_entry->set_contact_pos(contact_point);
396  new_entry->set_contact_normal(plane.get_normal());
397  new_entry->set_t(actual_t);
398 
399  return new_entry;
400 }
401 
402 /**
403  *
404  */
405 PT(CollisionEntry) CollisionBox::
406 test_intersection_from_line(const CollisionEntry &entry) const {
407  const CollisionLine *line;
408  DCAST_INTO_R(line, entry.get_from(), nullptr);
409 
410  const LMatrix4 &wrt_mat = entry.get_wrt_mat();
411 
412  LPoint3 from_origin = line->get_origin() * wrt_mat;
413  LVector3 from_direction = line->get_direction() * wrt_mat;
414 
415  double t1, t2;
416  if (!intersects_line(t1, t2, from_origin, from_direction)) {
417  // No intersection.
418  return nullptr;
419  }
420 
421  if (collide_cat.is_debug()) {
422  collide_cat.debug()
423  << "intersection detected from " << entry.get_from_node_path()
424  << " into " << entry.get_into_node_path() << "\n";
425  }
426  PT(CollisionEntry) new_entry = new CollisionEntry(entry);
427 
428  LPoint3 point = from_origin + t1 * from_direction;
429  new_entry->set_surface_point(point);
430 
432  new_entry->set_surface_normal(get_effective_normal());
433  } else {
434  LVector3 normal(
435  IS_NEARLY_EQUAL(point[0], _max[0]) - IS_NEARLY_EQUAL(point[0], _min[0]),
436  IS_NEARLY_EQUAL(point[1], _max[1]) - IS_NEARLY_EQUAL(point[1], _min[1]),
437  IS_NEARLY_EQUAL(point[2], _max[2]) - IS_NEARLY_EQUAL(point[2], _min[2])
438  );
439  normal.normalize();
440  new_entry->set_surface_normal(normal);
441  }
442 
443  return new_entry;
444 }
445 
446 /**
447  * Double dispatch point for ray as a FROM object
448  */
449 PT(CollisionEntry) CollisionBox::
450 test_intersection_from_ray(const CollisionEntry &entry) const {
451  const CollisionRay *ray;
452  DCAST_INTO_R(ray, entry.get_from(), nullptr);
453  const LMatrix4 &wrt_mat = entry.get_wrt_mat();
454 
455  LPoint3 from_origin = ray->get_origin() * wrt_mat;
456  LVector3 from_direction = ray->get_direction() * wrt_mat;
457 
458  double t1, t2;
459  if (!intersects_line(t1, t2, from_origin, from_direction) || (t1 < 0.0 && t2 < 0.0)) {
460  // No intersection.
461  return nullptr;
462  }
463 
464  if (collide_cat.is_debug()) {
465  collide_cat.debug()
466  << "intersection detected from " << entry.get_from_node_path()
467  << " into " << entry.get_into_node_path() << "\n";
468  }
469  PT(CollisionEntry) new_entry = new CollisionEntry(entry);
470 
471  if (t1 < 0.0) {
472  // The origin is inside the box, so we take the exit as our surface point.
473  new_entry->set_interior_point(from_origin);
474  t1 = t2;
475  }
476 
477  LPoint3 point = from_origin + t1 * from_direction;
478  new_entry->set_surface_point(point);
479 
481  new_entry->set_surface_normal(get_effective_normal());
482  } else {
483  LVector3 normal(
484  IS_NEARLY_EQUAL(point[0], _max[0]) - IS_NEARLY_EQUAL(point[0], _min[0]),
485  IS_NEARLY_EQUAL(point[1], _max[1]) - IS_NEARLY_EQUAL(point[1], _min[1]),
486  IS_NEARLY_EQUAL(point[2], _max[2]) - IS_NEARLY_EQUAL(point[2], _min[2])
487  );
488  normal.normalize();
489  new_entry->set_surface_normal(normal);
490  }
491 
492  return new_entry;
493 }
494 
495 /**
496  * Double dispatch point for segment as a FROM object
497  */
498 PT(CollisionEntry) CollisionBox::
499 test_intersection_from_segment(const CollisionEntry &entry) const {
500  const CollisionSegment *seg;
501  DCAST_INTO_R(seg, entry.get_from(), nullptr);
502  const LMatrix4 &wrt_mat = entry.get_wrt_mat();
503 
504  LPoint3 from_origin = seg->get_point_a() * wrt_mat;
505  LPoint3 from_extent = seg->get_point_b() * wrt_mat;
506  LVector3 from_direction = from_extent - from_origin;
507 
508  double t1, t2;
509  if (!intersects_line(t1, t2, from_origin, from_direction) ||
510  (t1 < 0.0 && t2 < 0.0) || (t1 > 1.0 && t2 > 1.0)) {
511  // No intersection.
512  return nullptr;
513  }
514 
515  if (collide_cat.is_debug()) {
516  collide_cat.debug()
517  << "intersection detected from " << entry.get_from_node_path()
518  << " into " << entry.get_into_node_path() << "\n";
519  }
520  PT(CollisionEntry) new_entry = new CollisionEntry(entry);
521 
522  // In case the segment is entirely inside the cube, we consider the point
523  // closest to the surface as our entry point.
524  if (t1 < (1.0 - t2)) {
525  std::swap(t1, t2);
526  }
527 
528  // Our interior point is the closest point to t2 that is inside the segment.
529  new_entry->set_interior_point(from_origin + std::min(std::max(t2, 0.0), 1.0) * from_direction);
530 
531  LPoint3 point = from_origin + t1 * from_direction;
532  new_entry->set_surface_point(point);
533 
535  new_entry->set_surface_normal(get_effective_normal());
536  } else {
537  LVector3 normal(
538  IS_NEARLY_EQUAL(point[0], _max[0]) - IS_NEARLY_EQUAL(point[0], _min[0]),
539  IS_NEARLY_EQUAL(point[1], _max[1]) - IS_NEARLY_EQUAL(point[1], _min[1]),
540  IS_NEARLY_EQUAL(point[2], _max[2]) - IS_NEARLY_EQUAL(point[2], _min[2])
541  );
542  normal.normalize();
543  new_entry->set_surface_normal(normal);
544  }
545 
546  return new_entry;
547 }
548 
549 /**
550  * Double dispatch point for capsule as a FROM object
551  */
552 PT(CollisionEntry) CollisionBox::
553 test_intersection_from_capsule(const CollisionEntry &entry) const {
554  const CollisionCapsule *capsule;
555  DCAST_INTO_R(capsule, entry.get_from(), nullptr);
556 
557  const LMatrix4 &wrt_mat = entry.get_wrt_mat();
558 
559  LPoint3 from_a = capsule->get_point_a() * wrt_mat;
560  LPoint3 from_b = capsule->get_point_b() * wrt_mat;
561  LVector3 from_direction = from_b - from_a;
562  PN_stdfloat radius_sq = wrt_mat.xform_vec(LVector3(0, 0, capsule->get_radius())).length_squared();
563  PN_stdfloat radius = csqrt(radius_sq);
564 
565  LPoint3 box_min = get_min();
566  LPoint3 box_max = get_max();
567  LVector3 dimensions = box_max - box_min;
568 
569  // The method below is inspired by Christer Ericson's book Real-Time
570  // Collision Detection. Instead of testing a capsule against a box, we test
571  // a segment against an box that is oversized by the capsule radius.
572 
573  // First, we test if the line segment intersects a box with its faces
574  // expanded outwards by the capsule radius. If not, there is no collision.
575  double t1, t2;
576  if (!intersects_line(t1, t2, from_a, from_direction, radius)) {
577  return nullptr;
578  }
579 
580  if (t2 < 0.0 || t1 > 1.0) {
581  return nullptr;
582  }
583 
584  t1 = std::min(1.0, std::max(0.0, (t1 + t2) * 0.5));
585  LPoint3 point = from_a + from_direction * t1;
586 
587  // We now have a point of intersection between the line segment and the
588  // oversized box. Check on how many axes it lies outside the box. If it is
589  // less than two, we know that it does not lie in one of the rounded regions
590  // of the oversized rounded box, and it is a guaranteed hit. Otherwise, we
591  // will need to test against the edge regions.
592  if ((point[0] < box_min[0] || point[0] > box_max[0]) +
593  (point[1] < box_min[1] || point[1] > box_max[1]) +
594  (point[2] < box_min[2] || point[2] > box_max[2]) > 1) {
595  // Test the capsule against each edge of the box.
596  static const struct {
597  LPoint3 point;
598  int axis;
599  } edges[] = {
600  {{0, 0, 0}, 0},
601  {{0, 1, 0}, 0},
602  {{0, 0, 1}, 0},
603  {{0, 1, 1}, 0},
604  {{0, 0, 0}, 1},
605  {{0, 0, 1}, 1},
606  {{1, 0, 0}, 1},
607  {{1, 0, 1}, 1},
608  {{0, 0, 0}, 2},
609  {{0, 1, 0}, 2},
610  {{1, 0, 0}, 2},
611  {{1, 1, 0}, 2},
612  };
613 
614  PN_stdfloat best_dist_sq = FLT_MAX;
615 
616  for (int i = 0; i < 12; ++i) {
617  LPoint3 vertex = edges[i].point;
618  vertex.componentwise_mult(dimensions);
619  vertex += box_min;
620  LVector3 delta(0);
621  delta[edges[i].axis] = dimensions[edges[i].axis];
622  double u1, u2;
623  CollisionCapsule::calc_closest_segment_points(u1, u2, from_a, from_direction, vertex, delta);
624  PN_stdfloat dist_sq = ((from_a + from_direction * u1) - (vertex + delta * u2)).length_squared();
625  if (dist_sq < best_dist_sq) {
626  best_dist_sq = dist_sq;
627  }
628  }
629 
630  if (best_dist_sq > radius_sq) {
631  // It is not actually touching any edge.
632  return nullptr;
633  }
634  }
635 
636  if (collide_cat.is_debug()) {
637  collide_cat.debug()
638  << "intersection detected from " << entry.get_from_node_path()
639  << " into " << entry.get_into_node_path() << "\n";
640  }
641  PT(CollisionEntry) new_entry = new CollisionEntry(entry);
642 
643  // Which is the longest axis?
644  LVector3 diff = point - _center;
645  diff[0] /= dimensions[0];
646  diff[1] /= dimensions[1];
647  diff[2] /= dimensions[2];
648  int axis = 0;
649  if (cabs(diff[0]) > cabs(diff[1])) {
650  if (cabs(diff[0]) > cabs(diff[2])) {
651  axis = 0;
652  } else {
653  axis = 2;
654  }
655  } else {
656  if (cabs(diff[1]) > cabs(diff[2])) {
657  axis = 1;
658  } else {
659  axis = 2;
660  }
661  }
662  LVector3 normal(0);
663  normal[axis] = std::copysign(1, diff[axis]);
664 
665  LPoint3 clamped = point.fmax(box_min).fmin(box_max);
666  LPoint3 surface_point = clamped;
667  surface_point[axis] = (diff[axis] >= 0.0f) ? box_max[axis] : box_min[axis];
668 
669  // Is the point inside the box?
670  LVector3 interior_vec;
671  if (clamped != point) {
672  // No, it is outside. The interior point is in the direction of the
673  // surface point.
674  interior_vec = point - surface_point;
675  if (!interior_vec.normalize()) {
676  interior_vec = normal;
677  }
678  } else {
679  // It is inside. I think any point will work for this.
680  interior_vec = normal;
681  }
682  new_entry->set_interior_point(point - interior_vec * radius);
683  new_entry->set_surface_point(surface_point);
684 
686  new_entry->set_surface_normal(get_effective_normal());
687  } else {
688  new_entry->set_surface_normal(normal);
689  }
690 
691  return new_entry;
692 }
693 
694 /**
695  * Double dispatch point for box as a FROM object
696  */
697 PT(CollisionEntry) CollisionBox::
698 test_intersection_from_box(const CollisionEntry &entry) const {
699  const CollisionBox *box;
700  DCAST_INTO_R(box, entry.get_from(), nullptr);
701 
702  const LMatrix4 &wrt_mat = entry.get_wrt_mat();
703 
704  LPoint3 diff = wrt_mat.xform_point_general(box->get_center()) - _center;
705  LVector3 from_extents = box->get_dimensions() * 0.5f;
706  LVector3 into_extents = get_dimensions() * 0.5f;
707 
708  LVecBase3 box_x = wrt_mat.get_row3(0);
709  LVecBase3 box_y = wrt_mat.get_row3(1);
710  LVecBase3 box_z = wrt_mat.get_row3(2);
711 
712  // To make the math simpler, normalize the box basis vectors, instead
713  // applying the scale to the box dimensions. Note that this doesn't work
714  // for a non-uniform scales applied after a rotation, since that has the
715  // possibility of making the box no longer a box.
716  PN_stdfloat l;
717  l = box_x.length();
718  from_extents[0] *= l;
719  box_x /= l;
720  l = box_y.length();
721  from_extents[1] *= l;
722  box_y /= l;
723  l = box_z.length();
724  from_extents[2] *= l;
725  box_z /= l;
726 
727  PN_stdfloat r1, r2;
728  PN_stdfloat min_pen = 0;
729  PN_stdfloat pen;
730  int axis = 0;
731 
732  // SAT test for the three axes of the into cube.
733  r1 = into_extents[0];
734  r2 = cabs(box_x[0] * from_extents[0]) +
735  cabs(box_y[0] * from_extents[1]) +
736  cabs(box_z[0] * from_extents[2]);
737  pen = r1 + r2 - cabs(diff[0]);
738  if (pen < 0) {
739  return nullptr;
740  }
741  min_pen = pen;
742 
743  r1 = into_extents[1];
744  r2 = cabs(box_x[1] * from_extents[0]) +
745  cabs(box_y[1] * from_extents[1]) +
746  cabs(box_z[1] * from_extents[2]);
747  pen = r1 + r2 - cabs(diff[1]);
748  if (pen < 0) {
749  return nullptr;
750  }
751  if (pen < min_pen) {
752  min_pen = pen;
753  axis = 1;
754  }
755 
756  r1 = into_extents[2];
757  r2 = cabs(box_x[2] * from_extents[0]) +
758  cabs(box_y[2] * from_extents[1]) +
759  cabs(box_z[2] * from_extents[2]);
760  pen = r1 + r2 - cabs(diff[2]);
761  if (pen < 0) {
762  return nullptr;
763  }
764  if (pen < min_pen) {
765  min_pen = pen;
766  axis = 2;
767  }
768 
769  // SAT test for the three axes of the from cube.
770  r1 = cabs(box_x[0] * into_extents[0]) +
771  cabs(box_x[1] * into_extents[1]) +
772  cabs(box_x[2] * into_extents[2]);
773  r2 = from_extents[0];
774  pen = r1 + r2 - cabs(diff.dot(box_x));
775  if (pen < 0) {
776  return nullptr;
777  }
778  if (pen < min_pen) {
779  min_pen = pen;
780  }
781 
782  r1 = cabs(box_y[0] * into_extents[0]) +
783  cabs(box_y[1] * into_extents[1]) +
784  cabs(box_y[2] * into_extents[2]);
785  r2 = from_extents[1];
786  pen = r1 + r2 - cabs(diff.dot(box_y));
787  if (pen < 0) {
788  return nullptr;
789  }
790  if (pen < min_pen) {
791  min_pen = pen;
792  }
793 
794  r1 = cabs(box_z[0] * into_extents[0]) +
795  cabs(box_z[1] * into_extents[1]) +
796  cabs(box_z[2] * into_extents[2]);
797  r2 = from_extents[2];
798  pen = r1 + r2 - cabs(diff.dot(box_z));
799  if (pen < 0) {
800  return nullptr;
801  }
802  if (pen < min_pen) {
803  min_pen = pen;
804  }
805 
806  // SAT test of the nine cross products.
807  r1 = into_extents[1] * cabs(box_x[2]) + into_extents[2] * cabs(box_x[1]);
808  r2 = from_extents[1] * cabs(box_z[0]) + from_extents[2] * cabs(box_y[0]);
809  if (cabs(diff[2] * box_x[1] - diff[1] * box_x[2]) > r1 + r2) {
810  return nullptr;
811  }
812 
813  r1 = into_extents[1] * cabs(box_y[2]) + into_extents[2] * cabs(box_y[1]);
814  r2 = from_extents[0] * cabs(box_z[0]) + from_extents[2] * cabs(box_x[0]);
815  if (cabs(diff[2] * box_y[1] - diff[1] * box_y[2]) > r1 + r2) {
816  return nullptr;
817  }
818 
819  r1 = into_extents[1] * cabs(box_z[2]) + into_extents[2] * cabs(box_z[1]);
820  r2 = from_extents[0] * cabs(box_y[0]) + from_extents[1] * cabs(box_x[0]);
821  if (cabs(diff[2] * box_z[1] - diff[1] * box_z[2]) > r1 + r2) {
822  return nullptr;
823  }
824 
825  r1 = into_extents[0] * cabs(box_x[2]) + into_extents[2] * cabs(box_x[0]);
826  r2 = from_extents[1] * cabs(box_z[1]) + from_extents[2] * cabs(box_y[1]);
827  if (cabs(diff[0] * box_x[2] - diff[2] * box_x[0]) > r1 + r2) {
828  return nullptr;
829  }
830 
831  r1 = into_extents[0] * cabs(box_y[2]) + into_extents[2] * cabs(box_y[0]);
832  r2 = from_extents[0] * cabs(box_z[1]) + from_extents[2] * cabs(box_x[1]);
833  if (cabs(diff[0] * box_y[2] - diff[2] * box_y[0]) > r1 + r2) {
834  return nullptr;
835  }
836 
837  r1 = into_extents[0] * cabs(box_z[2]) + into_extents[2] * cabs(box_z[0]);
838  r2 = from_extents[0] * cabs(box_y[1]) + from_extents[1] * cabs(box_x[1]);
839  if (cabs(diff[0] * box_z[2] - diff[2] * box_z[0]) > r1 + r2) {
840  return nullptr;
841  }
842 
843  r1 = into_extents[0] * cabs(box_x[1]) + into_extents[1] * cabs(box_x[0]);
844  r2 = from_extents[1] * cabs(box_z[2]) + from_extents[2] * cabs(box_y[2]);
845  if (cabs(diff[1] * box_x[0] - diff[0] * box_x[1]) > r1 + r2) {
846  return nullptr;
847  }
848 
849  r1 = into_extents[0] * cabs(box_y[1]) + into_extents[1] * cabs(box_y[0]);
850  r2 = from_extents[0] * cabs(box_z[2]) + from_extents[2] * cabs(box_x[2]);
851  if (cabs(diff[1] * box_y[0] - diff[0] * box_y[1]) > r1 + r2) {
852  return nullptr;
853  }
854 
855  r1 = into_extents[0] * cabs(box_z[1]) + into_extents[1] * cabs(box_z[0]);
856  r2 = from_extents[0] * cabs(box_y[2]) + from_extents[1] * cabs(box_x[2]);
857  if (cabs(diff[1] * box_z[0] - diff[0] * box_z[1]) > r1 + r2) {
858  return nullptr;
859  }
860 
861  if (collide_cat.is_debug()) {
862  collide_cat.debug()
863  << "intersection detected from " << entry.get_from_node_path()
864  << " into " << entry.get_into_node_path() << "\n";
865  }
866  PT(CollisionEntry) new_entry = new CollisionEntry(entry);
867 
868  // This isn't always the correct surface point. However, it seems to be
869  // enough to let the pusher do the right thing.
870  LPoint3 surface(
871  min(max(diff[0], -into_extents[0]), into_extents[0]),
872  min(max(diff[1], -into_extents[1]), into_extents[1]),
873  min(max(diff[2], -into_extents[2]), into_extents[2]));
874 
875  // Create the normal along the axis of least penetration.
876  LVector3 normal(0);
877  PN_stdfloat diff_axis = diff[axis];
878  int sign = (diff_axis >= 0) ? 1 : -1;
879  normal[axis] = sign;
880  surface[axis] = into_extents[axis] * sign;
881 
882  new_entry->set_surface_point(surface + _center);
883 
884  // Does not generate the correct depth. Needs fixing.
885  new_entry->set_interior_point(surface + _center + normal * -min_pen);
886 
888  new_entry->set_surface_normal(get_effective_normal());
889  } else {
890  new_entry->set_surface_normal(normal);
891  }
892 
893  return new_entry;
894 }
895 
896 /**
897  * Fills the _viz_geom GeomNode up with Geoms suitable for rendering this
898  * solid.
899  */
900 void CollisionBox::
901 fill_viz_geom() {
902  if (collide_cat.is_debug()) {
903  collide_cat.debug()
904  << "Recomputing viz for " << *this << "\n";
905  }
906 
907  PT(GeomVertexData) vdata = new GeomVertexData
908  ("collision", GeomVertexFormat::get_v3(),
909  Geom::UH_static);
910 
911  vdata->unclean_set_num_rows(8);
912 
913  {
914  GeomVertexWriter vertex(vdata, InternalName::get_vertex());
915  vertex.set_data3(_min[0], _min[1], _min[2]);
916  vertex.set_data3(_min[0], _max[1], _min[2]);
917  vertex.set_data3(_max[0], _max[1], _min[2]);
918  vertex.set_data3(_max[0], _min[1], _min[2]);
919 
920  vertex.set_data3(_min[0], _min[1], _max[2]);
921  vertex.set_data3(_min[0], _max[1], _max[2]);
922  vertex.set_data3(_max[0], _max[1], _max[2]);
923  vertex.set_data3(_max[0], _min[1], _max[2]);
924  }
925 
926  PT(GeomTriangles) tris = new GeomTriangles(Geom::UH_static);
927 
928  // Bottom
929  tris->add_vertices(0, 1, 2);
930  tris->add_vertices(2, 3, 0);
931 
932  // Top
933  tris->add_vertices(4, 7, 6);
934  tris->add_vertices(6, 5, 4);
935 
936  // Sides
937  tris->add_vertices(0, 4, 1);
938  tris->add_vertices(1, 4, 5);
939 
940  tris->add_vertices(1, 5, 2);
941  tris->add_vertices(2, 5, 6);
942 
943  tris->add_vertices(2, 6, 3);
944  tris->add_vertices(3, 6, 7);
945 
946  tris->add_vertices(3, 7, 0);
947  tris->add_vertices(0, 7, 4);
948 
949  PT(Geom) geom = new Geom(vdata);
950  geom->add_primitive(tris);
951 
952  _viz_geom->add_geom(geom, get_solid_viz_state());
953  _bounds_viz_geom->add_geom(geom, get_solid_bounds_viz_state());
954 }
955 
956 /**
957  * Determine the point(s) of intersection of a parametric line with the box.
958  * The line is infinite in both directions, and passes through "from" and
959  * from+delta. If the line does not intersect the box, the function returns
960  * false, and t1 and t2 are undefined. If it does intersect the box, it
961  * returns true, and t1 and t2 are set to the points along the equation
962  * from+t*delta that correspond to the two points of intersection.
963  */
964 bool CollisionBox::
965 intersects_line(double &t1, double &t2,
966  const LPoint3 &from, const LVector3 &delta,
967  PN_stdfloat inflate_size) const {
968 
969  LPoint3 bmin = _min - LVector3(inflate_size);
970  LPoint3 bmax = _max + LVector3(inflate_size);
971 
972  double tmin = -DBL_MAX;
973  double tmax = DBL_MAX;
974 
975  for (int i = 0; i < 3; ++i) {
976  PN_stdfloat d = delta[i];
977  if (!IS_NEARLY_ZERO(d)) {
978  double tmin2 = (bmin[i] - from[i]) / d;
979  double tmax2 = (bmax[i] - from[i]) / d;
980  if (tmin2 > tmax2) {
981  std::swap(tmin2, tmax2);
982  }
983  tmin = std::max(tmin, tmin2);
984  tmax = std::min(tmax, tmax2);
985 
986  if (tmin > tmax) {
987  return false;
988  }
989 
990  } else if (from[i] < bmin[i] || from[i] > bmax[i]) {
991  // The line is entirely parallel in this dimension.
992  return false;
993  }
994  }
995 
996  t1 = tmin;
997  t2 = tmax;
998  return true;
999 }
1000 
1001 /**
1002  * Clips the polygon by all of the clip planes named in the clip plane
1003  * attribute and fills new_points up with the resulting points.
1004  *
1005  * The return value is true if the set of points is unmodified (all points are
1006  * behind all the clip planes), or false otherwise.
1007  */
1008 bool CollisionBox::
1010  const ClipPlaneAttrib *cpa,
1011  const TransformState *net_transform, int plane_no) const {
1012  bool all_in = true;
1013 
1014  int num_planes = cpa->get_num_on_planes();
1015  bool first_plane = true;
1016 
1017  for (int i = 0; i < num_planes; i++) {
1018  NodePath plane_path = cpa->get_on_plane(i);
1019  PlaneNode *plane_node = DCAST(PlaneNode, plane_path.node());
1020  if ((plane_node->get_clip_effect() & PlaneNode::CE_collision) != 0) {
1021  CPT(TransformState) new_transform =
1022  net_transform->invert_compose(plane_path.get_net_transform());
1023 
1024  LPlane plane = plane_node->get_plane() * new_transform->get_mat();
1025  if (first_plane) {
1026  first_plane = false;
1027  if (!clip_polygon(new_points, _points[plane_no], plane, plane_no)) {
1028  all_in = false;
1029  }
1030  } else {
1031  Points last_points;
1032  last_points.swap(new_points);
1033  if (!clip_polygon(new_points, last_points, plane, plane_no)) {
1034  all_in = false;
1035  }
1036  }
1037  }
1038  }
1039 
1040  if (!all_in) {
1041  compute_vectors(new_points);
1042  }
1043 
1044  return all_in;
1045 }
1046 /**
1047  * Clips the source_points of the polygon by the indicated clipping plane, and
1048  * modifies new_points to reflect the new set of clipped points (but does not
1049  * compute the vectors in new_points).
1050  *
1051  * The return value is true if the set of points is unmodified (all points are
1052  * behind the clip plane), or false otherwise.
1053  */
1054 bool CollisionBox::
1056  const CollisionBox::Points &source_points,
1057  const LPlane &plane, int plane_no) const {
1058  new_points.clear();
1059  if (source_points.empty()) {
1060  return true;
1061  }
1062 
1063  LPoint3 from3d;
1064  LVector3 delta3d;
1065  if (!plane.intersects_plane(from3d, delta3d, get_plane(plane_no))) {
1066  // The clipping plane is parallel to the polygon. The polygon is either
1067  // all in or all out.
1068  if (plane.dist_to_plane(get_plane(plane_no).get_point()) < 0.0) {
1069  // A point within the polygon is behind the clipping plane: the polygon
1070  // is all in.
1071  new_points = source_points;
1072  return true;
1073  }
1074  return false;
1075  }
1076 
1077  // Project the line of intersection into the 2-d plane. Now we have a 2-d
1078  // clipping line.
1079  LPoint2 from2d = to_2d(from3d,plane_no);
1080  LVector2 delta2d = to_2d(delta3d,plane_no);
1081 
1082  PN_stdfloat a = -delta2d[1];
1083  PN_stdfloat b = delta2d[0];
1084  PN_stdfloat c = from2d[0] * delta2d[1] - from2d[1] * delta2d[0];
1085 
1086  // Now walk through the points. Any point on the left of our line gets
1087  // removed, and the line segment clipped at the point of intersection.
1088 
1089  // We might increase the number of vertices by as many as 1, if the plane
1090  // clips off exactly one corner. (We might also decrease the number of
1091  // vertices, or keep them the same number.)
1092  new_points.reserve(source_points.size() + 1);
1093 
1094  LPoint2 last_point = source_points.back()._p;
1095  bool last_is_in = !is_right(last_point - from2d, delta2d);
1096  bool all_in = last_is_in;
1097  Points::const_iterator pi;
1098  for (pi = source_points.begin(); pi != source_points.end(); ++pi) {
1099  const LPoint2 &this_point = (*pi)._p;
1100  bool this_is_in = !is_right(this_point - from2d, delta2d);
1101 
1102  // There appears to be a compiler bug in gcc 4.0: we need to extract this
1103  // comparison outside of the if statement.
1104  bool crossed_over = (this_is_in != last_is_in);
1105  if (crossed_over) {
1106  // We have just crossed over the clipping line. Find the point of
1107  // intersection.
1108  LVector2 d = this_point - last_point;
1109  PN_stdfloat denom = (a * d[0] + b * d[1]);
1110  if (denom != 0.0) {
1111  PN_stdfloat t = -(a * last_point[0] + b * last_point[1] + c) / denom;
1112  LPoint2 p = last_point + t * d;
1113 
1114  new_points.push_back(PointDef(p[0], p[1]));
1115  last_is_in = this_is_in;
1116  }
1117  }
1118 
1119  if (this_is_in) {
1120  // We are behind the clipping line. Keep the point.
1121  new_points.push_back(PointDef(this_point[0], this_point[1]));
1122  } else {
1123  all_in = false;
1124  }
1125 
1126  last_point = this_point;
1127  }
1128 
1129  return all_in;
1130 }
1131 
1132 
1133 /**
1134  * Returns the linear distance from the 2-d point to the nearest part of the
1135  * polygon defined by the points vector. The result is negative if the point
1136  * is within the polygon.
1137  */
1138 PN_stdfloat CollisionBox::
1139 dist_to_polygon(const LPoint2 &p, const CollisionBox::Points &points) const {
1140 
1141  // We know that that the polygon is convex and is defined with the points in
1142  // counterclockwise order. Therefore, we simply compare the signed distance
1143  // to each line segment; we ignore any negative values, and take the minimum
1144  // of all the positive values.
1145 
1146  // If all values are negative, the point is within the polygon; we therefore
1147  // return an arbitrary negative result.
1148 
1149  bool got_dist = false;
1150  PN_stdfloat best_dist = -1.0f;
1151 
1152  size_t num_points = points.size();
1153  for (size_t i = 0; i < num_points - 1; ++i) {
1154  PN_stdfloat d = dist_to_line_segment(p, points[i]._p, points[i + 1]._p,
1155  points[i]._v);
1156  if (d >= 0.0f) {
1157  if (!got_dist || d < best_dist) {
1158  best_dist = d;
1159  got_dist = true;
1160  }
1161  }
1162  }
1163 
1164  PN_stdfloat d = dist_to_line_segment(p, points[num_points - 1]._p, points[0]._p,
1165  points[num_points - 1]._v);
1166  if (d >= 0.0f) {
1167  if (!got_dist || d < best_dist) {
1168  best_dist = d;
1169  got_dist = true;
1170  }
1171  }
1172 
1173  return best_dist;
1174 }
1175 
1176 /**
1177  * Returns the linear distance of p to the line segment defined by f and t,
1178  * where v = (t - f).normalize(). The result is negative if p is left of the
1179  * line, positive if it is right of the line. If the result is positive, it
1180  * is constrained by endpoints of the line segment (i.e. the result might be
1181  * larger than it would be for a straight distance-to-line test). If the
1182  * result is negative, we don't bother.
1183  */
1184 PN_stdfloat CollisionBox::
1185 dist_to_line_segment(const LPoint2 &p,
1186  const LPoint2 &f, const LPoint2 &t,
1187  const LVector2 &v) {
1188  LVector2 v1 = (p - f);
1189  PN_stdfloat d = (v1[0] * v[1] - v1[1] * v[0]);
1190  if (d < 0.0f) {
1191  return d;
1192  }
1193 
1194  // Compute the nearest point on the line.
1195  LPoint2 q = p + LVector2(-v[1], v[0]) * d;
1196 
1197  // Now constrain that point to the line segment.
1198  if (v[0] > 0.0f) {
1199  // X+
1200  if (v[1] > 0.0f) {
1201  // Y+
1202  if (v[0] > v[1]) {
1203  // X-dominant.
1204  if (q[0] < f[0]) {
1205  return (p - f).length();
1206  } if (q[0] > t[0]) {
1207  return (p - t).length();
1208  } else {
1209  return d;
1210  }
1211  } else {
1212  // Y-dominant.
1213  if (q[1] < f[1]) {
1214  return (p - f).length();
1215  } if (q[1] > t[1]) {
1216  return (p - t).length();
1217  } else {
1218  return d;
1219  }
1220  }
1221  } else {
1222  // Y-
1223  if (v[0] > -v[1]) {
1224  // X-dominant.
1225  if (q[0] < f[0]) {
1226  return (p - f).length();
1227  } if (q[0] > t[0]) {
1228  return (p - t).length();
1229  } else {
1230  return d;
1231  }
1232  } else {
1233  // Y-dominant.
1234  if (q[1] > f[1]) {
1235  return (p - f).length();
1236  } if (q[1] < t[1]) {
1237  return (p - t).length();
1238  } else {
1239  return d;
1240  }
1241  }
1242  }
1243  } else {
1244  // X-
1245  if (v[1] > 0.0f) {
1246  // Y+
1247  if (-v[0] > v[1]) {
1248  // X-dominant.
1249  if (q[0] > f[0]) {
1250  return (p - f).length();
1251  } if (q[0] < t[0]) {
1252  return (p - t).length();
1253  } else {
1254  return d;
1255  }
1256  } else {
1257  // Y-dominant.
1258  if (q[1] < f[1]) {
1259  return (p - f).length();
1260  } if (q[1] > t[1]) {
1261  return (p - t).length();
1262  } else {
1263  return d;
1264  }
1265  }
1266  } else {
1267  // Y-
1268  if (-v[0] > -v[1]) {
1269  // X-dominant.
1270  if (q[0] > f[0]) {
1271  return (p - f).length();
1272  } if (q[0] < t[0]) {
1273  return (p - t).length();
1274  } else {
1275  return d;
1276  }
1277  } else {
1278  // Y-dominant.
1279  if (q[1] > f[1]) {
1280  return (p - f).length();
1281  } if (q[1] < t[1]) {
1282  return (p - t).length();
1283  } else {
1284  return d;
1285  }
1286  }
1287  }
1288  }
1289 }
1290 
1291 /**
1292  * Returns true if the indicated point is within the polygon's 2-d space,
1293  * false otherwise.
1294  */
1295 bool CollisionBox::
1296 point_is_inside(const LPoint2 &p, const CollisionBox::Points &points) const {
1297  // We insist that the polygon be convex. This makes things a bit simpler.
1298  // In the case of a convex polygon, defined with points in counterclockwise
1299  // order, a point is interior to the polygon iff the point is not right of
1300  // each of the edges.
1301  for (int i = 0; i < (int)points.size() - 1; i++) {
1302  if (is_right(p - points[i]._p, points[i+1]._p - points[i]._p)) {
1303  return false;
1304  }
1305  }
1306  if (is_right(p - points[points.size() - 1]._p,
1307  points[0]._p - points[points.size() - 1]._p)) {
1308  return false;
1309  }
1310 
1311  return true;
1312 }
1313 
1314 /**
1315  * Now that the _p members of the given points array have been computed, go
1316  * back and compute all of the _v members.
1317  */
1318 void CollisionBox::
1320  size_t num_points = points.size();
1321  for (size_t i = 0; i < num_points; i++) {
1322  points[i]._v = points[(i + 1) % num_points]._p - points[i]._p;
1323  points[i]._v.normalize();
1324  }
1325 }
1326 
1327 /**
1328  * Factory method to generate a CollisionBox object
1329  */
1330 void CollisionBox::
1332  BamReader::get_factory()->register_factory(get_class_type(), make_CollisionBox);
1333 }
1334 
1335 /**
1336  * Function to write the important information in the particular object to a
1337  * Datagram
1338  */
1339 void CollisionBox::
1341  CollisionSolid::write_datagram(manager, me);
1342  _center.write_datagram(me);
1343  _min.write_datagram(me);
1344  _max.write_datagram(me);
1345  for(int i=0; i < 8; i++) {
1346  _vertex[i].write_datagram(me);
1347  }
1348  me.add_stdfloat(_radius);
1349  me.add_stdfloat(_x);
1350  me.add_stdfloat(_y);
1351  me.add_stdfloat(_z);
1352  for(int i=0; i < 6; i++) {
1353  _planes[i].write_datagram(me);
1354  }
1355  for(int i=0; i < 6; i++) {
1356  _to_2d_mat[i].write_datagram(me);
1357  }
1358  for(int i=0; i < 6; i++) {
1359  me.add_uint16(_points[i].size());
1360  for (size_t j = 0; j < _points[i].size(); j++) {
1361  _points[i][j]._p.write_datagram(me);
1362  _points[i][j]._v.write_datagram(me);
1363  }
1364  }
1365 }
1366 
1367 /**
1368  * Factory method to generate a CollisionBox object
1369  */
1370 TypedWritable *CollisionBox::
1371 make_CollisionBox(const FactoryParams &params) {
1372  CollisionBox *me = new CollisionBox;
1373  DatagramIterator scan;
1374  BamReader *manager;
1375 
1376  parse_params(params, scan, manager);
1377  me->fillin(scan, manager);
1378  return me;
1379 }
1380 
1381 /**
1382  * Function that reads out of the datagram (or asks manager to read) all of
1383  * the data that is needed to re-create this object and stores it in the
1384  * appropiate place
1385  */
1386 void CollisionBox::
1387 fillin(DatagramIterator& scan, BamReader* manager) {
1388  CollisionSolid::fillin(scan, manager);
1389  _center.read_datagram(scan);
1390  _min.read_datagram(scan);
1391  _max.read_datagram(scan);
1392  for(int i=0; i < 8; i++) {
1393  _vertex[i].read_datagram(scan);
1394  }
1395  _radius = scan.get_stdfloat();
1396  _x = scan.get_stdfloat();
1397  _y = scan.get_stdfloat();
1398  _z = scan.get_stdfloat();
1399  for(int i=0; i < 6; i++) {
1400  _planes[i].read_datagram(scan);
1401  }
1402  for(int i=0; i < 6; i++) {
1403  _to_2d_mat[i].read_datagram(scan);
1404  }
1405  for(int i=0; i < 6; i++) {
1406  size_t size = scan.get_uint16();
1407  for (size_t j = 0; j < size; j++) {
1408  LPoint2 p;
1409  LVector2 v;
1410  p.read_datagram(scan);
1411  v.read_datagram(scan);
1412  _points[i].push_back(PointDef(p, v));
1413  }
1414  }
1415 }
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:233
const LVector3 & get_effective_normal() const
Returns the normal that was set by set_effective_normal().
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
get_from_node_path
Returns the NodePath that represents the CollisionNode that contains the CollisionSolid that triggere...
void setup_box()
Compute parameters for each of the box's sides.
An infinite ray, with a specific origin and direction.
Definition: collisionRay.h:27
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Indicates a coordinate-system transform on vertices.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PN_stdfloat get_stdfloat()
Extracts either a 32-bit or a 64-bit floating-point number, according to Datagram::set_stdfloat_doubl...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
The abstract base class for all things that can collide with other things in the world,...
bool point_is_inside(const LPoint2 &p, const Points &points) const
Returns true if the indicated point is within the polygon's 2-d space, false otherwise.
This defines a bounding sphere, consisting of a center and a radius.
A cuboid collision volume or object.
Definition: collisionBox.h:27
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This functions similarly to a LightAttrib.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
A spherical collision volume or object.
LPoint2 to_2d(const LVecBase3 &point3d, int plane) const
Assuming the indicated point in 3-d space lies within the polygon's plane, returns the corresponding ...
Definition: collisionBox.I:223
virtual void write_datagram(BamWriter *manager, Datagram &me)
Function to write the important information in the particular object to a Datagram.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
get_into_node_path
Returns the NodePath that represents the specific CollisionNode or GeomNode instance that was collide...
static void compute_vectors(Points &points)
Now that the _p members of the given points array have been computed, go back and compute all of the ...
get_respect_effective_normal
See set_respect_effective_normal().
virtual LPoint3 get_collision_origin() const
Returns the point in space deemed to be the "origin" of the solid for collision purposes.
void add_stdfloat(PN_stdfloat value)
Adds either a 32-bit or a 64-bit floating-point number, according to set_stdfloat_double().
Definition: datagram.I:133
This is an abstract class for any volume in any sense which can be said to define the locality of ref...
A lightweight class that represents a single element that may be timed and/or counted via stats.
void parse_params(const FactoryParams &params, DatagramIterator &scan, BamReader *&manager)
Takes in a FactoryParams, passed from a WritableFactory into any TypedWritable's make function,...
Definition: bamReader.I:275
void add_uint16(uint16_t value)
Adds an unsigned 16-bit integer to the datagram.
Definition: datagram.I:85
A finite line segment, with two specific endpoints but no thickness.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
static void register_with_read_factory()
Factory method to generate a CollisionBox object.
void setup_points(const LPoint3 *begin, const LPoint3 *end, int plane)
Computes the plane and 2d projection of points that make up this side.
const LPlane & get_plane() const
Returns the plane represented by the PlaneNode.
Definition: planeNode.I:54
get_num_on_planes
Returns the number of planes that are enabled by the attribute.
Defines a single collision event.
virtual PStatCollector & get_test_pcollector()
Returns a PStatCollector that is used to count the number of intersection tests made against a solid ...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
This implements a solid consisting of a cylinder with hemispherical endcaps, also known as a capsule ...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A container for geometry primitives.
Definition: geom.h:54
PT(CollisionEntry) CollisionBox
First Dispatch point for box as a FROM object.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void register_factory(TypeHandle handle, CreateFunc *func, void *user_data=nullptr)
Registers a new kind of thing the Factory will be able to create.
Definition: factory.I:73
LPlane set_plane(int n) const
Creates the nth face of the rectangular solid.
Definition: collisionBox.I:190
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
LPlane get_plane(int n) const
Returns the nth face of the rectangular solid.
Definition: collisionBox.I:181
PN_stdfloat dist_to_polygon(const LPoint2 &p, const Points &points) const
Returns the linear distance from the 2-d point to the nearest part of the polygon defined by the poin...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An infinite line, similar to a CollisionRay, except that it extends in both directions.
Definition: collisionLine.h:25
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
uint16_t get_uint16()
Extracts an unsigned 16-bit integer.
static WritableFactory * get_factory()
Returns the global WritableFactory for generating TypedWritable objects.
Definition: bamReader.I:177
PandaNode * node() const
Returns the referenced node of the path.
Definition: nodePath.I:227
get_into
Returns the CollisionSolid pointer for the particular solid was collided into.
get_on_plane
Returns the nth plane enabled by the attribute, sorted in render order.
get_from
Returns the CollisionSolid pointer for the particular solid that triggered this collision.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Defines a series of disconnected triangles.
Definition: geomTriangles.h:23
LPoint3 get_point(int n) const
Returns the nth vertex of the OBB.
Definition: collisionBox.I:150
bool has_effective_normal() const
Returns true if a special normal was set by set_effective_normal(), false otherwise.
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
virtual void write_datagram(BamWriter *manager, Datagram &me)
Function to write the important information in the particular object to a Datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
bool apply_clip_plane(Points &new_points, const ClipPlaneAttrib *cpa, const TransformState *net_transform, int plane_no) const
Clips the polygon by all of the clip planes named in the clip plane attribute and fills new_points up...
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161
static const GeomVertexFormat * get_v3()
Returns a standard vertex format with just a 3-component vertex position.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A node that contains a plane.
Definition: planeNode.h:36
bool clip_polygon(Points &new_points, const Points &source_points, const LPlane &plane, int plane_no) const
Clips the source_points of the polygon by the indicated clipping plane, and modifies new_points to re...
virtual PStatCollector & get_volume_pcollector()
Returns a PStatCollector that is used to count the number of bounding volume tests made against a sol...
int get_clip_effect() const
Returns the clip_effect bits for this clip plane.
Definition: planeNode.I:128