Panda3D
physxUtilLib.cxx
1 // Filename: physxUtilLib.cxx
2 // Created by: enn0x (01Nov09)
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 #include "physxUtilLib.h"
16 #include "physxManager.h"
17 #include "physxBounds3.h"
18 #include "physxBox.h"
19 #include "physxCapsule.h"
20 #include "physxPlane.h"
21 #include "physxRay.h"
22 #include "physxSegment.h"
23 #include "physxSphere.h"
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: PhysxUtilLib::set_fpu_exceptions
27 // Access: Published
28 // Description: Set FPU precision.
29 ////////////////////////////////////////////////////////////////////
30 void PhysxUtilLib::
32 
33  _ptr->NxSetFPUExceptions(b);
34 }
35 
36 ////////////////////////////////////////////////////////////////////
37 // Function: PhysxUtilLib::set_fpu_precision24
38 // Access: Published
39 // Description: Set FPU precision.
40 ////////////////////////////////////////////////////////////////////
41 void PhysxUtilLib::
43 
44  _ptr->NxSetFPUPrecision24();
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: PhysxUtilLib::set_fpu_precision53
49 // Access: Published
50 // Description: Set FPU precision.
51 ////////////////////////////////////////////////////////////////////
52 void PhysxUtilLib::
54 
55  _ptr->NxSetFPUPrecision53();
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: PhysxUtilLib::set_fpu_precision64
60 // Access: Published
61 // Description: Set FPU precision.
62 ////////////////////////////////////////////////////////////////////
63 void PhysxUtilLib::
65 
66  _ptr->NxSetFPUPrecision64();
67 }
68 
69 ////////////////////////////////////////////////////////////////////
70 // Function: PhysxUtilLib::set_fpu_rounding_chop
71 // Access: Published
72 // Description: Set FPU precision.
73 ////////////////////////////////////////////////////////////////////
74 void PhysxUtilLib::
76 
77  _ptr->NxSetFPURoundingChop();
78 }
79 
80 ////////////////////////////////////////////////////////////////////
81 // Function: PhysxUtilLib::set_fpu_rounding_down
82 // Access: Published
83 // Description: Set FPU rounding mode.
84 ////////////////////////////////////////////////////////////////////
85 void PhysxUtilLib::
87 
88  _ptr->NxSetFPURoundingDown();
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: PhysxUtilLib::set_fpu_rounding_near
93 // Access: Published
94 // Description: Set FPU rounding mode.
95 ////////////////////////////////////////////////////////////////////
96 void PhysxUtilLib::
98 
99  _ptr->NxSetFPURoundingNear();
100 }
101 
102 ////////////////////////////////////////////////////////////////////
103 // Function: PhysxUtilLib::set_fpu_rounding_up
104 // Access: Published
105 // Description: Set FPU rounding mode.
106 ////////////////////////////////////////////////////////////////////
107 void PhysxUtilLib::
109 
110  _ptr->NxSetFPURoundingUp();
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: PhysxUtilLib::int_ceil
115 // Access: Published
116 // Description: Convert a floating point number to an integer.
117 ////////////////////////////////////////////////////////////////////
118 int PhysxUtilLib::
119 int_ceil(const float &f) {
120 
121  return _ptr->NxIntCeil(f);
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: PhysxUtilLib::int_chop
126 // Access: Published
127 // Description: Convert a floating point number to an integer.
128 ////////////////////////////////////////////////////////////////////
129 int PhysxUtilLib::
130 int_chop(const float &f) {
131 
132  return _ptr->NxIntChop(f);
133 }
134 
135 ////////////////////////////////////////////////////////////////////
136 // Function: PhysxUtilLib::int_floor
137 // Access: Published
138 // Description: Convert a floating point number to an integer.
139 ////////////////////////////////////////////////////////////////////
140 int PhysxUtilLib::
141 int_floor(const float &f) {
142 
143  return _ptr->NxIntFloor(f);
144 }
145 
146 ////////////////////////////////////////////////////////////////////
147 // Function: PhysxUtilLib::box_contains_point
148 // Access: Published
149 // Description: Test if an oriented box contains a point.
150 //
151 // \param [in] box
152 // \param [in] p
153 ////////////////////////////////////////////////////////////////////
154 bool PhysxUtilLib::
155 box_contains_point(const PhysxBox &box, const LPoint3f &p) {
156 
157  return _ptr->NxBoxContainsPoint(box._box, PhysxManager::point3_to_nxVec3(p));
158 }
159 
160 ////////////////////////////////////////////////////////////////////
161 // Function: PhysxUtilLib::create_box
162 // Access: Published
163 // Description: Create an oriented box from an axis aligned box
164 // and a transformation.
165 //
166 // \param [in] aabb
167 // \param [in] mat
168 ////////////////////////////////////////////////////////////////////
170 create_box(const PhysxBounds3 &aabb, const LMatrix4f &mat) {
171 
172  PhysxBox box;
173  _ptr->NxCreateBox(box._box, aabb._bounds, PhysxManager::mat4_to_nxMat34(mat));
174  return box;
175 }
176 
177 ////////////////////////////////////////////////////////////////////
178 // Function: PhysxUtilLib::compute_box_world_edge_normal
179 // Access: Published
180 // Description: Compute and edge normals for an oriented box. This
181 // is an averaged normal, from the two faces sharing
182 // the edge. The edge index should be from 0 to 11
183 // (i.e. a box has 12 edges).
184 //
185 // \param [in] box
186 // \param [in] edge_index
187 ////////////////////////////////////////////////////////////////////
189 compute_box_world_edge_normal(const PhysxBox &box, unsigned int edge_index) {
190 
191  NxVec3 nNormal;
192 
193  nassertr(edge_index < 12, LVector3f::zero());
194 
195  _ptr->NxComputeBoxWorldEdgeNormal(box._box, edge_index, nNormal);
196  return PhysxManager::nxVec3_to_vec3(nNormal);
197 }
198 
199 ////////////////////////////////////////////////////////////////////
200 // Function: PhysxUtilLib::compute_capsule_around_box
201 // Access: Published
202 // Description: Compute a capsule which encloses a box.
203 //
204 // \param [in] box
205 ////////////////////////////////////////////////////////////////////
208 
209  PhysxCapsule capsule;
210  _ptr->NxComputeCapsuleAroundBox(box._box, capsule._capsule);
211  return capsule;
212 }
213 
214 ////////////////////////////////////////////////////////////////////
215 // Function: PhysxUtilLib::is_box_a_inside_box_b
216 // Access: Published
217 // Description: Test if box A is inside another box B. Returns
218 // TRUE if box A is inside box B.
219 //
220 // \param [in] a
221 // \param [in] b
222 ////////////////////////////////////////////////////////////////////
223 bool PhysxUtilLib::
225 
226  return _ptr->NxIsBoxAInsideBoxB(a._box, b._box);
227 }
228 
229 ////////////////////////////////////////////////////////////////////
230 // Function: PhysxUtilLib::compute_box_around_capsule
231 // Access: Published
232 // Description: Compute a box which encloses a capsule.
233 //
234 // \param [in] capsule
235 ////////////////////////////////////////////////////////////////////
238 
239  PhysxBox box;
240  _ptr->NxComputeBoxAroundCapsule(capsule._capsule, box._box);
241  return box;
242 }
243 
244 ////////////////////////////////////////////////////////////////////
245 // Function: PhysxUtilLib::compute_distance_squared
246 // Access: Published
247 // Description: Compute the distance squared from a point to a
248 // ray.
249 //
250 // \param [in] ray
251 // \param [in] point
252 ////////////////////////////////////////////////////////////////////
253 float PhysxUtilLib::
254 compute_distance_squared(const PhysxRay &ray, const LPoint3f &point) {
255 
256  NxF32 t; // not used
257  return _ptr->NxComputeDistanceSquared(ray._ray, PhysxManager::point3_to_nxVec3(point), &t);
258 }
259 
260 ////////////////////////////////////////////////////////////////////
261 // Function: PhysxUtilLib::compute_square_distance
262 // Access: Published
263 // Description: Compute the distance squared from a point to a
264 // line segment.
265 //
266 // \param [in] seg
267 // \param [in] point
268 ////////////////////////////////////////////////////////////////////
269 float PhysxUtilLib::
270 compute_square_distance(const PhysxSegment &seg, const LPoint3f &point) {
271 
272  NxF32 t; // not used
273  return _ptr->NxComputeSquareDistance(seg._segment, PhysxManager::point3_to_nxVec3(point), &t);
274 }
275 
276 ////////////////////////////////////////////////////////////////////
277 // Function: PhysxUtilLib::merge_spheres
278 // Access: Published
279 // Description: Compute an overall bounding sphere for a pair of
280 // spheres.
281 //
282 // \param [in] sphere0
283 // \param [in] sphere1
284 ////////////////////////////////////////////////////////////////////
286 merge_spheres(const PhysxSphere &sphere0, const PhysxSphere &sphere1) {
287 
288  PhysxSphere merged;
289  _ptr->NxMergeSpheres(merged._sphere, sphere0._sphere, sphere1._sphere);
290  return merged;
291 }
292 
293 ////////////////////////////////////////////////////////////////////
294 // Function: PhysxUtilLib::normal_to_tangents
295 // Access: Published
296 // Description: Get the tangent vectors associated with a normal.
297 //
298 // \param [in] n
299 // \param [out] t1
300 // \param [out] t2
301 ////////////////////////////////////////////////////////////////////
302 void PhysxUtilLib::
304 
305  NxVec3 nt1;
306  NxVec3 nt2;
307 
308  _ptr->NxNormalToTangents(PhysxManager::vec3_to_nxVec3(n), nt1, nt2);
309 
310  t1.set_x(nt1.x);
311  t1.set_y(nt1.y);
312  t1.set_z(nt1.z);
313 
314  t2.set_x(nt2.x);
315  t2.set_y(nt2.y);
316  t2.set_z(nt2.z);
317 }
318 
319 ////////////////////////////////////////////////////////////////////
320 // Function: PhysxUtilLib::find_rotation_matrix
321 // Access: Published
322 // Description: Computes a rotation matrix M so that: M * x = b
323 // (x and b are unit vectors).
324 //
325 // \param [in] x
326 // \param [in] b
327 ////////////////////////////////////////////////////////////////////
330 
331  NxMat33 nmat;
332  _ptr->NxFindRotationMatrix(PhysxManager::vec3_to_nxVec3(x),
334  nmat);
335  return PhysxManager::nxMat33_to_mat3(nmat);
336 }
337 
338 ////////////////////////////////////////////////////////////////////
339 // Function: PhysxUtilLib::compute_sphere_mass
340 // Access: Published
341 // Description: Computes mass of a homogeneous sphere according
342 // to sphere density.
343 //
344 // \param [in] radius
345 // \param [in] density
346 ////////////////////////////////////////////////////////////////////
347 float PhysxUtilLib::
348 compute_sphere_mass(float radius, float density) {
349 
350  return _ptr->NxComputeSphereMass(radius, density);
351 }
352 
353 ////////////////////////////////////////////////////////////////////
354 // Function: PhysxUtilLib::compute_sphere_density
355 // Access: Published
356 // Description: Computes density of a homogeneous sphere according
357 // to sphere mass
358 //
359 // \param [in] radius
360 // \param [in] mass
361 ////////////////////////////////////////////////////////////////////
362 float PhysxUtilLib::
363 compute_sphere_density(float radius, float mass) {
364 
365  return _ptr->NxComputeSphereDensity(radius, mass);
366 }
367 
368 ////////////////////////////////////////////////////////////////////
369 // Function: PhysxUtilLib::compute_box_mass
370 // Access: Published
371 // Description: Computes mass of a homogeneous box according to
372 // box density.
373 //
374 // \param [in] radius
375 // \param [in] density
376 ////////////////////////////////////////////////////////////////////
377 float PhysxUtilLib::
378 compute_box_mass(const LVector3f &extents, float density) {
379 
380  return _ptr->NxComputeBoxMass(PhysxManager::vec3_to_nxVec3(extents), density);
381 }
382 
383 ////////////////////////////////////////////////////////////////////
384 // Function: PhysxUtilLib::compute_box_density
385 // Access: Published
386 // Description: Computes density of a homogeneous box according to
387 // box mass.
388 //
389 // \param [in] radius
390 // \param [in] mass
391 ////////////////////////////////////////////////////////////////////
392 float PhysxUtilLib::
393 compute_box_density(const LVector3f &extents, float mass) {
394 
395  return _ptr->NxComputeBoxDensity(PhysxManager::vec3_to_nxVec3(extents), mass);
396 }
397 
398 ////////////////////////////////////////////////////////////////////
399 // Function: PhysxUtilLib::compute_ellipsoid_mass
400 // Access: Published
401 // Description: Computes mass of a homogeneous ellipsoid according
402 // to ellipsoid density.
403 //
404 // \param [in] radius
405 // \param [in] density
406 ////////////////////////////////////////////////////////////////////
407 float PhysxUtilLib::
408 compute_ellipsoid_mass(const LVector3f &extents, float density ) {
409 
410  return _ptr->NxComputeEllipsoidMass(PhysxManager::vec3_to_nxVec3(extents), density);
411 }
412 
413 ////////////////////////////////////////////////////////////////////
414 // Function: PhysxUtilLib::compute_ellipsoid_density
415 // Access: Published
416 // Description: Computes density of a homogeneous ellipsoid
417 // according to ellipsoid mass.
418 //
419 // \param [in] radius
420 // \param [in] mass
421 ////////////////////////////////////////////////////////////////////
422 float PhysxUtilLib::
423 compute_ellipsoid_density(const LVector3f &extents, float mass) {
424 
425  return _ptr->NxComputeEllipsoidDensity(PhysxManager::vec3_to_nxVec3(extents), mass);
426 }
427 
428 ////////////////////////////////////////////////////////////////////
429 // Function: PhysxUtilLib::compute_cylinder_mass
430 // Access: Published
431 // Description: Computes mass of a homogeneous cylinder according
432 // to cylinder density.
433 //
434 // \param [in] radius
435 // \param [in] density
436 ////////////////////////////////////////////////////////////////////
437 float PhysxUtilLib::
438 compute_cylinder_mass(float radius, float length, float density) {
439 
440  return _ptr->NxComputeCylinderMass(radius, length, density);
441 }
442 
443 ////////////////////////////////////////////////////////////////////
444 // Function: PhysxUtilLib::compute_cylinder_density
445 // Access: Published
446 // Description: Computes density of a homogeneous cylinder
447 // according to cylinder mass.
448 //
449 // \param [in] radius
450 // \param [in] mass
451 ////////////////////////////////////////////////////////////////////
452 float PhysxUtilLib::
453 compute_cylinder_density(float radius, float length, float mass) {
454 
455  return _ptr->NxComputeCylinderDensity(radius, length, mass);
456 }
457 
458 ////////////////////////////////////////////////////////////////////
459 // Function: PhysxUtilLib::compute_cone_mass
460 // Access: Published
461 // Description: Computes mass of a homogeneous cone according to
462 // cone density.
463 //
464 // \param [in] radius
465 // \param [in] density
466 ////////////////////////////////////////////////////////////////////
467 float PhysxUtilLib::
468 compute_cone_mass(float radius, float length, float density) {
469 
470  return _ptr->NxComputeConeMass(radius, length, density);
471 }
472 
473 ////////////////////////////////////////////////////////////////////
474 // Function: PhysxUtilLib::compute_cone_density
475 // Access: Published
476 // Description: Computes density of a homogeneous cone according
477 // to cone mass.
478 //
479 // \param [in] radius
480 // \param [in] mass
481 ////////////////////////////////////////////////////////////////////
482 float PhysxUtilLib::
483 compute_cone_density(float radius, float length, float mass) {
484 
485  return _ptr->NxComputeConeDensity(radius, length, mass);
486 }
487 
488 ////////////////////////////////////////////////////////////////////
489 // Function: PhysxUtilLib::compute_box_inertia_tensor
490 // Access: Published
491 // Description: Computes diagonalized inertia tensor for a box.
492 //
493 // \param [in] mass
494 // \param [in] xlength
495 // \param [in] ylength
496 // \param [in] zlength
497 ////////////////////////////////////////////////////////////////////
499 compute_box_inertia_tensor(float mass, float xlength, float ylength, float zlength) {
500 
501  NxVec3 tensor;
502  _ptr->NxComputeBoxInertiaTensor(tensor, mass, xlength, ylength, zlength);
503  return PhysxManager::nxVec3_to_vec3(tensor);
504 }
505 
506 ////////////////////////////////////////////////////////////////////
507 // Function: PhysxUtilLib::compute_sphere_inertia_tensor
508 // Access: Published
509 // Description: Computes diagonalized inertia tensor for a sphere.
510 //
511 // \param [in] mass
512 // \param [in] radius
513 // \param [in] hollow
514 ////////////////////////////////////////////////////////////////////
516 compute_sphere_inertia_tensor(float mass, float radius, bool hollow) {
517 
518  NxVec3 tensor;
519  _ptr->NxComputeSphereInertiaTensor(tensor, mass, radius, hollow);
520  return PhysxManager::nxVec3_to_vec3(tensor);
521 }
522 
523 ////////////////////////////////////////////////////////////////////
524 // Function: PhysxUtilLib::box_box_intersect
525 // Access: Published
526 // Description: Boolean intersection test between two OBBs. Uses
527 // the separating axis theorem. Disabling 'full_test'
528 // only performs 6 axis tests out of 15.
529 //
530 // \param [in] extents0
531 // \param [in] center0
532 // \param [in] rotation0
533 // \param [in] extents1
534 // \param [in] center1
535 // \param [in] rotation1
536 // \param [in] full_test
537 ////////////////////////////////////////////////////////////////////
538 bool PhysxUtilLib::
539 box_box_intersect(const LVector3f &extents0, const LPoint3f &center0, const LMatrix3f &rotation0, const LVector3f &extents1, const LPoint3f &center1, const LMatrix3f &rotation1, bool full_test) {
540 
541  nassertr_always(!extents0.is_nan(), false);
542  nassertr_always(!center0.is_nan(), false);
543  nassertr_always(!rotation0.is_nan(), false);
544  nassertr_always(!extents1.is_nan(), false);
545  nassertr_always(!center1.is_nan(), false);
546  nassertr_always(!rotation1.is_nan(), false);
547 
548  return _ptr->NxBoxBoxIntersect(
554  PhysxManager::mat3_to_nxMat33(rotation1), full_test);
555 }
556 
557 ////////////////////////////////////////////////////////////////////
558 // Function: PhysxUtilLib::tri_box_intersect
559 // Access: Published
560 // Description: Boolean intersection test between a triangle and
561 // a box.
562 //
563 // \param [in] vertex0
564 // \param [in] vertex1
565 // \param [in] vertex2
566 // \param [in] center
567 // \param [in] extents
568 ////////////////////////////////////////////////////////////////////
569 bool PhysxUtilLib::
570 tri_box_intersect(const LPoint3f &vertex0, const LPoint3f &vertex1, const LPoint3f &vertex2, const LPoint3f &center, const LVector3f &extents) {
571 
572  nassertr_always(!vertex0.is_nan(), false);
573  nassertr_always(!vertex1.is_nan(), false);
574  nassertr_always(!vertex2.is_nan(), false);
575  nassertr_always(!center.is_nan(), false);
576  nassertr_always(!extents.is_nan(), false);
577 
578  return _ptr->NxTriBoxIntersect(
584 }
585 
586 ////////////////////////////////////////////////////////////////////
587 // Function: PhysxUtilLib::ray_plane_intersect
588 // Access: Published
589 // Description: Ray-plane intersection test.
590 //
591 // \param [in] ray
592 // \param [in] plane
593 // \param [out] point_on_plane
594 ////////////////////////////////////////////////////////////////////
595 bool PhysxUtilLib::
596 ray_plane_intersect(const PhysxRay &ray, const PhysxPlane &plane, LPoint3f &point_on_plane) {
597 
598  NxReal dist; // not used
599  NxVec3 nPointOnPlane;
600 
601  bool result = _ptr->NxRayPlaneIntersect(ray._ray, plane._plane, dist, nPointOnPlane);
602 
603  PhysxManager::update_point3_from_nxVec3(point_on_plane, nPointOnPlane);
604  return result;
605 }
606 
607 ////////////////////////////////////////////////////////////////////
608 // Function: PhysxUtilLib::ray_sphere_intersect
609 // Access: Published
610 // Description: Ray-sphere intersection test. Returns true if the
611 // ray intersects the sphere, and the impact point if
612 // needed.
613 //
614 // \param [in] origin
615 // \param [in] dir
616 // \param [in] length
617 // \param [in] center
618 // \param [in] radius
619 // \param [out] hit_pos
620 ////////////////////////////////////////////////////////////////////
621 bool PhysxUtilLib::
622 ray_sphere_intersect(const LPoint3f &origin, const LVector3f &dir, float length, const LPoint3f &center, float radius, LPoint3f &hit_pos) {
623 
624  nassertr_always(!origin.is_nan(), false);
625  nassertr_always(!dir.is_nan(), false);
626  nassertr_always(!center.is_nan(), false);
627 
628  NxReal nHitTime; // not used
629  NxVec3 nPointOnPlane;
630 
631  bool result = _ptr->NxRaySphereIntersect(
634  length,
636  radius,
637  nHitTime,
638  nPointOnPlane);
639 
640  PhysxManager::update_point3_from_nxVec3(hit_pos, nPointOnPlane);
641  return result;
642 }
643 
644 ////////////////////////////////////////////////////////////////////
645 // Function: PhysxUtilLib::segment_box_intersect
646 // Access: Published
647 // Description: Segment-AABB intersection test. Also computes
648 // intersection point.
649 //
650 // \param [in] p1
651 // \param [in] p2
652 // \param [in] bbox_min
653 // \param [in] bbox_max
654 // \param [out] intercept
655 ////////////////////////////////////////////////////////////////////
656 bool PhysxUtilLib::
657 segment_box_intersect(const LPoint3f &p1, const LPoint3f &p2, const LPoint3f &bbox_min, const LPoint3f &bbox_max, LPoint3f &intercept) {
658 
659  nassertr_always(!p1.is_nan(), false);
660  nassertr_always(!p2.is_nan(), false);
661  nassertr_always(!bbox_min.is_nan(), false);
662  nassertr_always(!bbox_max.is_nan(), false);
663 
664  NxVec3 nIntercept;
665 
666  bool result =_ptr->NxSegmentBoxIntersect(
671  nIntercept);
672 
673  PhysxManager::update_point3_from_nxVec3(intercept, nIntercept);
674  return result;
675 }
676 
677 ////////////////////////////////////////////////////////////////////
678 // Function: PhysxUtilLib::ray_aabb_intersect
679 // Access: Published
680 // Description: Ray-AABB intersection test. Also computes
681 // intersection point.
682 //
683 // \param [in] min
684 // \param [in] max
685 // \param [in] origin
686 // \param [in] dir
687 // \param [out] coord
688 ////////////////////////////////////////////////////////////////////
689 bool PhysxUtilLib::
690 ray_aabb_intersect(const LPoint3f &min, const LPoint3f &max, const LPoint3f &origin, const LVector3f &dir, LPoint3f &coord) {
691 
692  nassertr_always(!min.is_nan(), false);
693  nassertr_always(!max.is_nan(), false);
694  nassertr_always(!origin.is_nan(), false);
695  nassertr_always(!dir.is_nan(), false);
696 
697  NxVec3 nCoord;
698 
699  bool result = _ptr->NxRayAABBIntersect(
704  nCoord);
705 
706  PhysxManager::update_point3_from_nxVec3(coord, nCoord);
707  return result;
708 }
709 
710 ////////////////////////////////////////////////////////////////////
711 // Function: PhysxUtilLib::segment_obb_intersect
712 // Access: Published
713 // Description: Boolean segment-OBB intersection test. Based on
714 // separating axis theorem.
715 //
716 // \param [in] p0
717 // \param [in] p1
718 // \param [in] center
719 // \param [in] extents
720 // \param [in] rot
721 ////////////////////////////////////////////////////////////////////
722 bool PhysxUtilLib::
723 segment_obb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot) {
724 
725  nassertr_always(!p0.is_nan(), false);
726  nassertr_always(!p1.is_nan(), false);
727  nassertr_always(!center.is_nan(), false);
728  nassertr_always(!extents.is_nan(), false);
729  nassertr_always(!rot.is_nan(), false);
730 
731  return _ptr->NxSegmentOBBIntersect(
737 }
738 
739 ////////////////////////////////////////////////////////////////////
740 // Function: PhysxUtilLib::segment_aabb_intersect
741 // Access: Published
742 // Description: Boolean segment-AABB intersection test. Based on
743 // separating axis theorem.
744 //
745 // \param [in] p0
746 // \param [in] p1
747 // \param [in] min
748 // \param [in] max
749 ////////////////////////////////////////////////////////////////////
750 bool PhysxUtilLib::
751 segment_aabb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &min, const LPoint3f &max) {
752 
753  nassertr_always(!p0.is_nan(), false);
754  nassertr_always(!p1.is_nan(), false);
755  nassertr_always(!min.is_nan(), false);
756  nassertr_always(!max.is_nan(), false);
757 
758  return _ptr->NxSegmentAABBIntersect(
763 }
764 
765 ////////////////////////////////////////////////////////////////////
766 // Function: PhysxUtilLib::ray_obb_intersect
767 // Access: Published
768 // Description: Boolean ray-OBB intersection test. Based on
769 // separating axis theorem.
770 //
771 // \param [in] ray
772 // \param [in] center
773 // \param [in] extents
774 // \param [in] rot
775 ////////////////////////////////////////////////////////////////////
776 bool PhysxUtilLib::
777 ray_obb_intersect(const PhysxRay &ray, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot) {
778 
779  nassertr_always(!center.is_nan(), false);
780  nassertr_always(!extents.is_nan(), false);
781  nassertr_always(!rot.is_nan(), false);
782 
783  return _ptr->NxRayOBBIntersect(
784  ray._ray,
788 }
789 
790 ////////////////////////////////////////////////////////////////////
791 // Function: PhysxUtilLib::ray_capsule_intersect
792 // Access: Published
793 // Description: Ray-capsule intersection test. Returns number of
794 // intersection points (0,1 or 2) along the ray.
795 //
796 // \param [in] origin
797 // \param [in] dir
798 // \param [in] capsule
799 ////////////////////////////////////////////////////////////////////
800 unsigned int PhysxUtilLib::
801 ray_capsule_intersect(const LPoint3f &origin, const LVector3f &dir, const PhysxCapsule &capsule) {
802 
803  nassertr_always(!origin.is_nan(), -1);
804  nassertr_always(!dir.is_nan(), -1);
805 
806  NxReal t[2] = { 0.0f, 0.0f }; // not used
807 
808  return _ptr->NxRayCapsuleIntersect(
811  capsule._capsule, t);
812 }
813 
814 ////////////////////////////////////////////////////////////////////
815 // Function: PhysxUtilLib::swept_spheres_intersect
816 // Access: Published
817 // Description: Sphere-sphere sweep test. Returns true if spheres
818 // intersect during their linear motion along
819 // provided velocity vectors.
820 //
821 // \param [in] sphere0
822 // \param [in] velocity0
823 // \param [in] sphere1
824 // \param [in] velocity1
825 ////////////////////////////////////////////////////////////////////
826 bool PhysxUtilLib::
827 swept_spheres_intersect(const PhysxSphere &sphere0, const LVector3f &velocity0, const PhysxSphere &sphere1, const LVector3f &velocity1) {
828 
829  nassertr_always(!velocity0.is_nan(), false);
830  nassertr_always(!velocity1.is_nan(), false);
831 
832  return _ptr->NxSweptSpheresIntersect(
833  sphere0._sphere,
834  PhysxManager::vec3_to_nxVec3(velocity0),
835  sphere1._sphere,
836  PhysxManager::vec3_to_nxVec3(velocity1));
837 }
838 
839 ////////////////////////////////////////////////////////////////////
840 // Function: PhysxUtilLib::ray_tri_intersect
841 // Access: Published
842 // Description: Ray-triangle intersection test. Returns impact
843 // distance (t) as well as barycentric coordinates
844 // (u,v) of impact point. The test performs back face
845 // culling or not according to 'cull'.
846 //
847 // \param [in] orig
848 // \param [in] dir
849 // \param [in] vert0
850 // \param [in] vert1
851 // \param [in] vert2
852 // \param [out] hit, with coordinates (t,u,v)
853 // \param [in] cull
854 ////////////////////////////////////////////////////////////////////
855 bool PhysxUtilLib::
856 ray_tri_intersect(const LPoint3f &orig, const LVector3f &dir, const LPoint3f &vert0, const LPoint3f &vert1, const LPoint3f &vert2, LVector3f &hit, bool cull) {
857 
858  nassertr_always(!orig.is_nan(), false);
859  nassertr_always(!dir.is_nan(), false);
860  nassertr_always(!vert0.is_nan(), false);
861  nassertr_always(!vert1.is_nan(), false);
862  nassertr_always(!vert2.is_nan(), false);
863 
864  NxReal t, u, v;
865 
866  bool result = _ptr->NxRayTriIntersect(
872  t, u, v, cull);
873 
874  hit.set_x(t);
875  hit.set_y(u);
876  hit.set_z(v);
877 
878  return result;
879 }
880 
881 ////////////////////////////////////////////////////////////////////
882 // Function: PhysxUtilLib::sweep_box_capsule
883 // Access: Published
884 // Description: Box-vs-capsule sweep test. Sweeps a box against a
885 // capsule, returns true if box hit the capsule. Also
886 // returns contact information.
887 //
888 // \param [in] box Box
889 // \param [in] lss Capsule
890 // \param [in] dir Unit-length sweep direction
891 // \param [in] length Length of sweep
892 // \param [out] normal Normal at impact point
893 ////////////////////////////////////////////////////////////////////
894 bool PhysxUtilLib::
895 sweep_box_capsule(const PhysxBox &box, const PhysxCapsule &lss, const LVector3f &dir, float length, LVector3f &normal) {
896 
897  nassertr_always(!dir.is_nan(), false);
898 
899  NxReal min_dist; // not used
900  NxVec3 nNormal;
901 
902  bool result = _ptr->NxSweepBoxCapsule(
903  box._box, lss._capsule,
905  length, min_dist, nNormal);
906 
907  PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
908  return result;
909 }
910 
911 ////////////////////////////////////////////////////////////////////
912 // Function: PhysxUtilLib::sweep_box_sphere
913 // Access: Published
914 // Description: Box-vs-sphere sweep test. Sweeps a box against a
915 // sphere, returns true if box hit the sphere. Also
916 // returns contact information.
917 //
918 // \param [in] box Box
919 // \param [in] sphere Sphere
920 // \param [in] dir Unit-length sweep direction
921 // \param [in] length Length of sweep
922 // \param [out] normal Normal at impact point
923 ////////////////////////////////////////////////////////////////////
924 bool PhysxUtilLib::
925 sweep_box_sphere(const PhysxBox &box, const PhysxSphere &sphere, const LVector3f &dir, float length, LVector3f &normal) {
926 
927  nassertr_always(!dir.is_nan(), false);
928 
929  NxReal min_dist; // not used
930  NxVec3 nNormal;
931 
932  bool result = _ptr->NxSweepBoxSphere(
933  box._box, sphere._sphere,
935  length, min_dist, nNormal);
936 
937  PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
938  return result;
939 }
940 
941 ////////////////////////////////////////////////////////////////////
942 // Function: PhysxUtilLib::sweep_capsule_capsule
943 // Access: Published
944 // Description: Capsule-vs-capsule sweep test. Sweeps a capsule
945 // against a capsule, returns true if capsule hit the
946 // other capsule. Also returns contact information.
947 //
948 // \param [in] lss0
949 // \param [in] lss1
950 // \param [in] dir Unit-length sweep direction
951 // \param [in] length Length of sweep
952 // \param [out] ip Impact point
953 // \param [out] normal Normal at impact point
954 ////////////////////////////////////////////////////////////////////
955 bool PhysxUtilLib::
956 sweep_capsule_capsule(const PhysxCapsule &lss0, const PhysxCapsule &lss1, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal) {
957 
958  nassertr_always(!dir.is_nan(), false);
959 
960  NxReal min_dist; // not used
961  NxVec3 nIp;
962  NxVec3 nNormal;
963 
964  bool result = _ptr->NxSweepCapsuleCapsule(
965  lss0._capsule, lss1._capsule,
967  length, min_dist, nIp, nNormal);
968 
969  PhysxManager::update_point3_from_nxVec3(ip, nIp);
970  PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
971  return result;
972 }
973 
974 ////////////////////////////////////////////////////////////////////
975 // Function: PhysxUtilLib::sweep_sphere_capsule
976 // Access: Published
977 // Description: Sphere-vs-capsule sweep test. Sweeps a sphere
978 // against a capsule, returns true if sphere hit the
979 // capsule. Also returns contact information.
980 //
981 // \param [in] sphere
982 // \param [in] lss
983 // \param [in] dir Unit-length sweep direction
984 // \param [in] length Length of sweep
985 // \param [out] ip Impact point
986 // \param [out] normal Normal at impact point
987 ////////////////////////////////////////////////////////////////////
988 bool PhysxUtilLib::
989 sweep_sphere_capsule(const PhysxSphere &sphere, const PhysxCapsule &lss, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal) {
990 
991  nassertr_always(!dir.is_nan(), false);
992 
993  NxReal min_dist; // not used
994  NxVec3 nIp;
995  NxVec3 nNormal;
996 
997  bool result = _ptr->NxSweepSphereCapsule(
998  sphere._sphere, lss._capsule,
1000  length, min_dist, nIp, nNormal);
1001 
1002  PhysxManager::update_point3_from_nxVec3(ip, nIp);
1003  PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
1004  return result;
1005 }
1006 
1007 ////////////////////////////////////////////////////////////////////
1008 // Function: PhysxUtilLib::sweep_box_box
1009 // Access: Published
1010 // Description: Box-vs-box sweep test. Sweeps a box against a box,
1011 // returns true if box hit the other box. Also returns
1012 // contact information.
1013 //
1014 // \param [in] box0
1015 // \param [in] box1
1016 // \param [in] dir Unit-length sweep direction
1017 // \param [in] length Length of sweep
1018 // \param [out] ip Impact point
1019 // \param [out] normal Normal at impact point
1020 ////////////////////////////////////////////////////////////////////
1021 bool PhysxUtilLib::
1022 sweep_box_box(const PhysxBox &box0, const PhysxBox &box1, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal) {
1023 
1024  nassertr_always(!dir.is_nan(), false);
1025 
1026  NxReal min_dist; // not used
1027  NxVec3 nIp;
1028  NxVec3 nNormal;
1029 
1030  bool result = _ptr->NxSweepBoxBox(
1031  box0._box, box1._box,
1033  length, nIp, nNormal, min_dist);
1034 
1035  PhysxManager::update_point3_from_nxVec3(ip, nIp);
1036  PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
1037  return result;
1038 }
1039 
1040 ////////////////////////////////////////////////////////////////////
1041 // Function: PhysxUtilLib::point_obb_sqr_dist
1042 // Access: Published
1043 // Description: Point-vs-OBB distance computation. Returns distance
1044 // between a point and an OBB.
1045 //
1046 // \param [in] point The point
1047 // \param [in] center OBB center
1048 // \param [in] extents OBB extents
1049 // \param [in] rot OBB rotation
1050 // \param [out] params Closest point on the box, in box space
1051 ////////////////////////////////////////////////////////////////////
1052 float PhysxUtilLib::
1053 point_obb_sqr_dist(const LPoint3f &point, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot, LPoint3f &params) {
1054 
1055  nassertr_always(!point.is_nan(), 0.0f);
1056  nassertr_always(!center.is_nan(), 0.0f);
1057  nassertr_always(!extents.is_nan(), 0.0f);
1058  nassertr_always(!rot.is_nan(), 0.0f);
1059 
1060  NxVec3 nParams;
1061 
1062  float result = _ptr->NxPointOBBSqrDist(
1067  &nParams);
1068 
1069  PhysxManager::update_point3_from_nxVec3(params, nParams);
1070  return result;
1071 }
1072 
bool ray_sphere_intersect(const LPoint3f &origin, const LVector3f &dir, float length, const LPoint3f &center, float radius, LPoint3f &hit_pos)
Ray-sphere intersection test.
bool box_box_intersect(const LVector3f &extents0, const LPoint3f &center0, const LMatrix3f &rotation0, const LVector3f &extents1, const LPoint3f &center1, const LMatrix3f &rotation1, bool full_test)
Boolean intersection test between two OBBs.
float compute_sphere_density(float radius, float mass)
Computes density of a homogeneous sphere according to sphere mass.
LVector3f compute_box_inertia_tensor(float mass, float xlength, float ylength, float zlength)
Computes diagonalized inertia tensor for a box.
bool sweep_box_box(const PhysxBox &box0, const PhysxBox &box1, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal)
Box-vs-box sweep test.
float compute_cone_density(float radius, float length, float mass)
Computes density of a homogeneous cone according to cone mass.
bool sweep_capsule_capsule(const PhysxCapsule &lss0, const PhysxCapsule &lss1, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal)
Capsule-vs-capsule sweep test.
Represents a sphere defined by its center point and radius.
Definition: physxSphere.h:28
bool segment_obb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot)
Boolean segment-OBB intersection test.
PhysxSphere merge_spheres(const PhysxSphere &sphere0, const PhysxSphere &sphere1)
Compute an overall bounding sphere for a pair of spheres.
bool ray_tri_intersect(const LPoint3f &orig, const LVector3f &dir, const LPoint3f &vert0, const LPoint3f &vert1, const LPoint3f &vert2, LVector3f &hit, bool cull)
Ray-triangle intersection test.
float compute_ellipsoid_density(const LVector3f &extents, float mass)
Computes density of a homogeneous ellipsoid according to ellipsoid mass.
bool tri_box_intersect(const LPoint3f &vertex0, const LPoint3f &vertex1, const LPoint3f &vertex2, const LPoint3f &center, const LVector3f &extents)
Boolean intersection test between a triangle and a box.
float compute_cylinder_density(float radius, float length, float mass)
Computes density of a homogeneous cylinder according to cylinder mass.
Represents an oriented bounding box, as a center point, extents(radii) and a rotation.
Definition: physxBox.h:32
static NxVec3 point3_to_nxVec3(const LPoint3f &p)
Converts from LPoint3f to NxVec3.
Definition: physxManager.I:77
static const LVector3f & zero()
Returns a zero-length vector.
Definition: lvector3.h:270
Represention of a axis aligned bounding box.
Definition: physxBounds3.h:32
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
bool is_nan() const
Returns true if any component of the vector is not-a-number, false otherwise.
Definition: lvecBase3.h:464
bool segment_box_intersect(const LPoint3f &p1, const LPoint3f &p2, const LPoint3f &bbox_min, const LPoint3f &bbox_max, LPoint3f &intercept)
Segment-AABB intersection test.
static NxMat34 mat4_to_nxMat34(const LMatrix4f &m)
Converts from LMatrix4f to NxMat34.
Definition: physxManager.I:145
Represents a line segment.
Definition: physxSegment.h:27
void set_fpu_rounding_down()
Set FPU rounding mode.
float compute_cylinder_mass(float radius, float length, float density)
Computes mass of a homogeneous cylinder according to cylinder density.
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
int int_chop(const float &f)
Convert a floating point number to an integer.
static NxVec3 vec3_to_nxVec3(const LVector3f &v)
Converts from LVector3f to NxVec3.
Definition: physxManager.I:33
bool sweep_sphere_capsule(const PhysxSphere &sphere, const PhysxCapsule &lss, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal)
Sphere-vs-capsule sweep test.
bool ray_aabb_intersect(const LPoint3f &min, const LPoint3f &max, const LPoint3f &origin, const LVector3f &dir, LPoint3f &coord)
Ray-AABB intersection test.
static NxMat33 mat3_to_nxMat33(const LMatrix3f &m)
Converts from LMatrix3f to NxMat33.
Definition: physxManager.I:169
unsigned int ray_capsule_intersect(const LPoint3f &origin, const LVector3f &dir, const PhysxCapsule &capsule)
Ray-capsule intersection test.
bool segment_aabb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &min, const LPoint3f &max)
Boolean segment-AABB intersection test.
static LVector3f nxVec3_to_vec3(const NxVec3 &v)
Converts from NxVec3 to LVector3f.
Definition: physxManager.I:44
LVector3f compute_sphere_inertia_tensor(float mass, float radius, bool hollow)
Computes diagonalized inertia tensor for a sphere.
Represents a capsule.
Definition: physxCapsule.h:27
bool ray_plane_intersect(const PhysxRay &ray, const PhysxPlane &plane, LPoint3f &point_on_plane)
Ray-plane intersection test.
float compute_sphere_mass(float radius, float density)
Computes mass of a homogeneous sphere according to sphere density.
float compute_box_mass(const LVector3f &extents, float density)
Computes mass of a homogeneous box according to box density.
float compute_cone_mass(float radius, float length, float density)
Computes mass of a homogeneous cone according to cone density.
int int_ceil(const float &f)
Convert a floating point number to an integer.
void set_fpu_rounding_up()
Set FPU rounding mode.
void set_fpu_precision64()
Set FPU precision.
bool is_nan() const
Returns true if any component of the matrix is not-a-number, false otherwise.
Definition: lmatrix.h:3147
PhysxCapsule compute_capsule_around_box(const PhysxBox &box)
Compute a capsule which encloses a box.
float compute_distance_squared(const PhysxRay &ray, const LPoint3f &point)
Compute the distance squared from a point to a ray.
LMatrix3f find_rotation_matrix(const LVector3f &x, const LVector3f &b)
Computes a rotation matrix M so that: M * x = b (x and b are unit vectors).
void set_fpu_rounding_chop()
Set FPU precision.
void set_fpu_rounding_near()
Set FPU rounding mode.
bool is_box_a_inside_box_b(const PhysxBox &a, const PhysxBox &b)
Test if box A is inside another box B.
void normal_to_tangents(const LVector3f &n, LVector3f &t1, LVector3f &t2)
Get the tangent vectors associated with a normal.
bool box_contains_point(const PhysxBox &box, const LPoint3f &p)
Test if an oriented box contains a point.
float compute_ellipsoid_mass(const LVector3f &extents, float density)
Computes mass of a homogeneous ellipsoid according to ellipsoid density.
bool ray_obb_intersect(const PhysxRay &ray, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot)
Boolean ray-OBB intersection test.
void set_fpu_exceptions(bool b)
Set FPU precision.
void set_fpu_precision24()
Set FPU precision.
This is a 3-by-3 transform matrix.
Definition: lmatrix.h:110
PhysxBox create_box(const PhysxBounds3 &aabb, const LMatrix4f &mat)
Create an oriented box from an axis aligned box and a transformation.
static LMatrix3f nxMat33_to_mat3(const NxMat33 &m)
Converts from NxMat33 to LMatrix3f.
Definition: physxManager.I:182
int int_floor(const float &f)
Convert a floating point number to an integer.
float compute_box_density(const LVector3f &extents, float mass)
Computes density of a homogeneous box according to box mass.
float point_obb_sqr_dist(const LPoint3f &point, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot, LPoint3f &params)
Point-vs-OBB distance computation.
bool sweep_box_capsule(const PhysxBox &box, const PhysxCapsule &lss, const LVector3f &dir, float length, LVector3f &normal)
Box-vs-capsule sweep test.
float compute_square_distance(const PhysxSegment &seg, const LPoint3f &point)
Compute the distance squared from a point to a line segment.
bool sweep_box_sphere(const PhysxBox &box, const PhysxSphere &sphere, const LVector3f &dir, float length, LVector3f &normal)
Box-vs-sphere sweep test.
Represents an ray as an origin and direction.
Definition: physxRay.h:28
PhysxBox compute_box_around_capsule(const PhysxCapsule &capsule)
Compute a box which encloses a capsule.
bool swept_spheres_intersect(const PhysxSphere &sphere0, const LVector3f &velocity0, const PhysxSphere &sphere1, const LVector3f &velocity1)
Sphere-sphere sweep test.
void set_fpu_precision53()
Set FPU precision.
LVector3f compute_box_world_edge_normal(const PhysxBox &box, unsigned int edge_index)
Compute and edge normals for an oriented box.