Panda3D
physxUtilLib.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 physxUtilLib.cxx
10  * @author enn0x
11  * @date 2009-11-01
12  */
13 
14 #include "physxUtilLib.h"
15 #include "physxManager.h"
16 #include "physxBounds3.h"
17 #include "physxBox.h"
18 #include "physxCapsule.h"
19 #include "physxPlane.h"
20 #include "physxRay.h"
21 #include "physxSegment.h"
22 #include "physxSphere.h"
23 
24 /**
25  * Set FPU precision.
26  */
28 set_fpu_exceptions(bool b) {
29 
30  _ptr->NxSetFPUExceptions(b);
31 }
32 
33 /**
34  * Set FPU precision.
35  */
38 
39  _ptr->NxSetFPUPrecision24();
40 }
41 
42 /**
43  * Set FPU precision.
44  */
47 
48  _ptr->NxSetFPUPrecision53();
49 }
50 
51 /**
52  * Set FPU precision.
53  */
56 
57  _ptr->NxSetFPUPrecision64();
58 }
59 
60 /**
61  * Set FPU precision.
62  */
65 
66  _ptr->NxSetFPURoundingChop();
67 }
68 
69 /**
70  * Set FPU rounding mode.
71  */
74 
75  _ptr->NxSetFPURoundingDown();
76 }
77 
78 /**
79  * Set FPU rounding mode.
80  */
83 
84  _ptr->NxSetFPURoundingNear();
85 }
86 
87 /**
88  * Set FPU rounding mode.
89  */
92 
93  _ptr->NxSetFPURoundingUp();
94 }
95 
96 /**
97  * Convert a floating point number to an integer.
98  */
100 int_ceil(const float &f) {
101 
102  return _ptr->NxIntCeil(f);
103 }
104 
105 /**
106  * Convert a floating point number to an integer.
107  */
109 int_chop(const float &f) {
110 
111  return _ptr->NxIntChop(f);
112 }
113 
114 /**
115  * Convert a floating point number to an integer.
116  */
118 int_floor(const float &f) {
119 
120  return _ptr->NxIntFloor(f);
121 }
122 
123 /**
124  * Test if an oriented box contains a point.
125  *
126  * \param [in] box \param [in] p
127  */
129 box_contains_point(const PhysxBox &box, const LPoint3f &p) {
130 
131  return _ptr->NxBoxContainsPoint(box._box, PhysxManager::point3_to_nxVec3(p));
132 }
133 
134 /**
135  * Create an oriented box from an axis aligned box and a transformation.
136  *
137  * \param [in] aabb \param [in] mat
138  */
140 create_box(const PhysxBounds3 &aabb, const LMatrix4f &mat) {
141 
142  PhysxBox box;
143  _ptr->NxCreateBox(box._box, aabb._bounds, PhysxManager::mat4_to_nxMat34(mat));
144  return box;
145 }
146 
147 /**
148  * Compute and edge normals for an oriented box. This is an averaged normal,
149  * from the two faces sharing the edge. The edge index should be from 0 to 11
150  * (i.e. a box has 12 edges).
151  *
152  * \param [in] box \param [in] edge_index
153  */
154 LVector3f PhysxUtilLib::
155 compute_box_world_edge_normal(const PhysxBox &box, unsigned int edge_index) {
156 
157  NxVec3 nNormal;
158 
159  nassertr(edge_index < 12, LVector3f::zero());
160 
161  _ptr->NxComputeBoxWorldEdgeNormal(box._box, edge_index, nNormal);
162  return PhysxManager::nxVec3_to_vec3(nNormal);
163 }
164 
165 /**
166  * Compute a capsule which encloses a box.
167  *
168  * \param [in] box
169  */
172 
173  PhysxCapsule capsule;
174  _ptr->NxComputeCapsuleAroundBox(box._box, capsule._capsule);
175  return capsule;
176 }
177 
178 /**
179  * Test if box A is inside another box B. Returns TRUE if box A is inside box
180  * B.
181  *
182  * \param [in] a \param [in] b
183  */
185 is_box_a_inside_box_b(const PhysxBox &a, const PhysxBox &b) {
186 
187  return _ptr->NxIsBoxAInsideBoxB(a._box, b._box);
188 }
189 
190 /**
191  * Compute a box which encloses a capsule.
192  *
193  * \param [in] capsule
194  */
197 
198  PhysxBox box;
199  _ptr->NxComputeBoxAroundCapsule(capsule._capsule, box._box);
200  return box;
201 }
202 
203 /**
204  * Compute the distance squared from a point to a ray.
205  *
206  * \param [in] ray \param [in] point
207  */
209 compute_distance_squared(const PhysxRay &ray, const LPoint3f &point) {
210 
211  NxF32 t; // not used
212  return _ptr->NxComputeDistanceSquared(ray._ray, PhysxManager::point3_to_nxVec3(point), &t);
213 }
214 
215 /**
216  * Compute the distance squared from a point to a line segment.
217  *
218  * \param [in] seg \param [in] point
219  */
221 compute_square_distance(const PhysxSegment &seg, const LPoint3f &point) {
222 
223  NxF32 t; // not used
224  return _ptr->NxComputeSquareDistance(seg._segment, PhysxManager::point3_to_nxVec3(point), &t);
225 }
226 
227 /**
228  * Compute an overall bounding sphere for a pair of spheres.
229  *
230  * \param [in] sphere0 \param [in] sphere1
231  */
233 merge_spheres(const PhysxSphere &sphere0, const PhysxSphere &sphere1) {
234 
235  PhysxSphere merged;
236  _ptr->NxMergeSpheres(merged._sphere, sphere0._sphere, sphere1._sphere);
237  return merged;
238 }
239 
240 /**
241  * Get the tangent vectors associated with a normal.
242  *
243  * \param [in] n \param [out] t1 \param [out] t2
244  */
246 normal_to_tangents(const LVector3f &n, LVector3f &t1, LVector3f &t2) {
247 
248  NxVec3 nt1;
249  NxVec3 nt2;
250 
251  _ptr->NxNormalToTangents(PhysxManager::vec3_to_nxVec3(n), nt1, nt2);
252 
253  t1.set_x(nt1.x);
254  t1.set_y(nt1.y);
255  t1.set_z(nt1.z);
256 
257  t2.set_x(nt2.x);
258  t2.set_y(nt2.y);
259  t2.set_z(nt2.z);
260 }
261 
262 /**
263  * Computes a rotation matrix M so that: M * x = b (x and b are unit vectors).
264  *
265  * \param [in] x \param [in] b
266  */
267 LMatrix3f PhysxUtilLib::
268 find_rotation_matrix(const LVector3f &x, const LVector3f &b) {
269 
270  NxMat33 nmat;
271  _ptr->NxFindRotationMatrix(PhysxManager::vec3_to_nxVec3(x),
273  nmat);
274  return PhysxManager::nxMat33_to_mat3(nmat);
275 }
276 
277 /**
278  * Computes mass of a homogeneous sphere according to sphere density.
279  *
280  * \param [in] radius \param [in] density
281  */
283 compute_sphere_mass(float radius, float density) {
284 
285  return _ptr->NxComputeSphereMass(radius, density);
286 }
287 
288 /**
289  * Computes density of a homogeneous sphere according to sphere mass
290  *
291  * \param [in] radius \param [in] mass
292  */
294 compute_sphere_density(float radius, float mass) {
295 
296  return _ptr->NxComputeSphereDensity(radius, mass);
297 }
298 
299 /**
300  * Computes mass of a homogeneous box according to box density.
301  *
302  * \param [in] radius \param [in] density
303  */
305 compute_box_mass(const LVector3f &extents, float density) {
306 
307  return _ptr->NxComputeBoxMass(PhysxManager::vec3_to_nxVec3(extents), density);
308 }
309 
310 /**
311  * Computes density of a homogeneous box according to box mass.
312  *
313  * \param [in] radius \param [in] mass
314  */
316 compute_box_density(const LVector3f &extents, float mass) {
317 
318  return _ptr->NxComputeBoxDensity(PhysxManager::vec3_to_nxVec3(extents), mass);
319 }
320 
321 /**
322  * Computes mass of a homogeneous ellipsoid according to ellipsoid density.
323  *
324  * \param [in] radius \param [in] density
325  */
327 compute_ellipsoid_mass(const LVector3f &extents, float density ) {
328 
329  return _ptr->NxComputeEllipsoidMass(PhysxManager::vec3_to_nxVec3(extents), density);
330 }
331 
332 /**
333  * Computes density of a homogeneous ellipsoid according to ellipsoid mass.
334  *
335  * \param [in] radius \param [in] mass
336  */
338 compute_ellipsoid_density(const LVector3f &extents, float mass) {
339 
340  return _ptr->NxComputeEllipsoidDensity(PhysxManager::vec3_to_nxVec3(extents), mass);
341 }
342 
343 /**
344  * Computes mass of a homogeneous cylinder according to cylinder density.
345  *
346  * \param [in] radius \param [in] density
347  */
349 compute_cylinder_mass(float radius, float length, float density) {
350 
351  return _ptr->NxComputeCylinderMass(radius, length, density);
352 }
353 
354 /**
355  * Computes density of a homogeneous cylinder according to cylinder mass.
356  *
357  * \param [in] radius \param [in] mass
358  */
360 compute_cylinder_density(float radius, float length, float mass) {
361 
362  return _ptr->NxComputeCylinderDensity(radius, length, mass);
363 }
364 
365 /**
366  * Computes mass of a homogeneous cone according to cone density.
367  *
368  * \param [in] radius \param [in] density
369  */
371 compute_cone_mass(float radius, float length, float density) {
372 
373  return _ptr->NxComputeConeMass(radius, length, density);
374 }
375 
376 /**
377  * Computes density of a homogeneous cone according to cone mass.
378  *
379  * \param [in] radius \param [in] mass
380  */
382 compute_cone_density(float radius, float length, float mass) {
383 
384  return _ptr->NxComputeConeDensity(radius, length, mass);
385 }
386 
387 /**
388  * Computes diagonalized inertia tensor for a box.
389  *
390  * \param [in] mass \param [in] xlength \param [in] ylength \param [in]
391  * zlength
392  */
393 LVector3f PhysxUtilLib::
394 compute_box_inertia_tensor(float mass, float xlength, float ylength, float zlength) {
395 
396  NxVec3 tensor;
397  _ptr->NxComputeBoxInertiaTensor(tensor, mass, xlength, ylength, zlength);
398  return PhysxManager::nxVec3_to_vec3(tensor);
399 }
400 
401 /**
402  * Computes diagonalized inertia tensor for a sphere.
403  *
404  * \param [in] mass \param [in] radius \param [in] hollow
405  */
406 LVector3f PhysxUtilLib::
407 compute_sphere_inertia_tensor(float mass, float radius, bool hollow) {
408 
409  NxVec3 tensor;
410  _ptr->NxComputeSphereInertiaTensor(tensor, mass, radius, hollow);
411  return PhysxManager::nxVec3_to_vec3(tensor);
412 }
413 
414 /**
415  * Boolean intersection test between two OBBs. Uses the separating axis
416  * theorem. Disabling 'full_test' only performs 6 axis tests out of 15.
417  *
418  * \param [in] extents0 \param [in] center0 \param [in] rotation0 \param [in]
419  * extents1 \param [in] center1 \param [in] rotation1 \param [in] full_test
420  */
422 box_box_intersect(const LVector3f &extents0, const LPoint3f &center0, const LMatrix3f &rotation0, const LVector3f &extents1, const LPoint3f &center1, const LMatrix3f &rotation1, bool full_test) {
423 
424  nassertr_always(!extents0.is_nan(), false);
425  nassertr_always(!center0.is_nan(), false);
426  nassertr_always(!rotation0.is_nan(), false);
427  nassertr_always(!extents1.is_nan(), false);
428  nassertr_always(!center1.is_nan(), false);
429  nassertr_always(!rotation1.is_nan(), false);
430 
431  return _ptr->NxBoxBoxIntersect(
437  PhysxManager::mat3_to_nxMat33(rotation1), full_test);
438 }
439 
440 /**
441  * Boolean intersection test between a triangle and a box.
442  *
443  * \param [in] vertex0 \param [in] vertex1 \param [in] vertex2 \param [in]
444  * center \param [in] extents
445  */
447 tri_box_intersect(const LPoint3f &vertex0, const LPoint3f &vertex1, const LPoint3f &vertex2, const LPoint3f &center, const LVector3f &extents) {
448 
449  nassertr_always(!vertex0.is_nan(), false);
450  nassertr_always(!vertex1.is_nan(), false);
451  nassertr_always(!vertex2.is_nan(), false);
452  nassertr_always(!center.is_nan(), false);
453  nassertr_always(!extents.is_nan(), false);
454 
455  return _ptr->NxTriBoxIntersect(
461 }
462 
463 /**
464  * Ray-plane intersection test.
465  *
466  * \param [in] ray \param [in] plane \param [out] point_on_plane
467  */
469 ray_plane_intersect(const PhysxRay &ray, const PhysxPlane &plane, LPoint3f &point_on_plane) {
470 
471  NxReal dist; // not used
472  NxVec3 nPointOnPlane;
473 
474  bool result = _ptr->NxRayPlaneIntersect(ray._ray, plane._plane, dist, nPointOnPlane);
475 
476  PhysxManager::update_point3_from_nxVec3(point_on_plane, nPointOnPlane);
477  return result;
478 }
479 
480 /**
481  * Ray-sphere intersection test. Returns true if the ray intersects the
482  * sphere, and the impact point if needed.
483  *
484  * \param [in] origin \param [in] dir \param [in] length \param [in] center
485  * \param [in] radius \param [out] hit_pos
486  */
488 ray_sphere_intersect(const LPoint3f &origin, const LVector3f &dir, float length, const LPoint3f &center, float radius, LPoint3f &hit_pos) {
489 
490  nassertr_always(!origin.is_nan(), false);
491  nassertr_always(!dir.is_nan(), false);
492  nassertr_always(!center.is_nan(), false);
493 
494  NxReal nHitTime; // not used
495  NxVec3 nPointOnPlane;
496 
497  bool result = _ptr->NxRaySphereIntersect(
500  length,
502  radius,
503  nHitTime,
504  nPointOnPlane);
505 
506  PhysxManager::update_point3_from_nxVec3(hit_pos, nPointOnPlane);
507  return result;
508 }
509 
510 /**
511  * Segment-AABB intersection test. Also computes intersection point.
512  *
513  * \param [in] p1 \param [in] p2 \param [in] bbox_min \param [in] bbox_max
514  * \param [out] intercept
515  */
517 segment_box_intersect(const LPoint3f &p1, const LPoint3f &p2, const LPoint3f &bbox_min, const LPoint3f &bbox_max, LPoint3f &intercept) {
518 
519  nassertr_always(!p1.is_nan(), false);
520  nassertr_always(!p2.is_nan(), false);
521  nassertr_always(!bbox_min.is_nan(), false);
522  nassertr_always(!bbox_max.is_nan(), false);
523 
524  NxVec3 nIntercept;
525 
526  bool result =_ptr->NxSegmentBoxIntersect(
531  nIntercept);
532 
533  PhysxManager::update_point3_from_nxVec3(intercept, nIntercept);
534  return result;
535 }
536 
537 /**
538  * Ray-AABB intersection test. Also computes intersection point.
539  *
540  * \param [in] min \param [in] max \param [in] origin \param [in] dir \param
541  * [out] coord
542  */
544 ray_aabb_intersect(const LPoint3f &min, const LPoint3f &max, const LPoint3f &origin, const LVector3f &dir, LPoint3f &coord) {
545 
546  nassertr_always(!min.is_nan(), false);
547  nassertr_always(!max.is_nan(), false);
548  nassertr_always(!origin.is_nan(), false);
549  nassertr_always(!dir.is_nan(), false);
550 
551  NxVec3 nCoord;
552 
553  bool result = _ptr->NxRayAABBIntersect(
558  nCoord);
559 
560  PhysxManager::update_point3_from_nxVec3(coord, nCoord);
561  return result;
562 }
563 
564 /**
565  * Boolean segment-OBB intersection test. Based on separating axis theorem.
566  *
567  * \param [in] p0 \param [in] p1 \param [in] center \param [in] extents \param
568  * [in] rot
569  */
571 segment_obb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot) {
572 
573  nassertr_always(!p0.is_nan(), false);
574  nassertr_always(!p1.is_nan(), false);
575  nassertr_always(!center.is_nan(), false);
576  nassertr_always(!extents.is_nan(), false);
577  nassertr_always(!rot.is_nan(), false);
578 
579  return _ptr->NxSegmentOBBIntersect(
585 }
586 
587 /**
588  * Boolean segment-AABB intersection test. Based on separating axis theorem.
589  *
590  * \param [in] p0 \param [in] p1 \param [in] min \param [in] max
591  */
593 segment_aabb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &min, const LPoint3f &max) {
594 
595  nassertr_always(!p0.is_nan(), false);
596  nassertr_always(!p1.is_nan(), false);
597  nassertr_always(!min.is_nan(), false);
598  nassertr_always(!max.is_nan(), false);
599 
600  return _ptr->NxSegmentAABBIntersect(
605 }
606 
607 /**
608  * Boolean ray-OBB intersection test. Based on separating axis theorem.
609  *
610  * \param [in] ray \param [in] center \param [in] extents \param [in] rot
611  */
613 ray_obb_intersect(const PhysxRay &ray, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot) {
614 
615  nassertr_always(!center.is_nan(), false);
616  nassertr_always(!extents.is_nan(), false);
617  nassertr_always(!rot.is_nan(), false);
618 
619  return _ptr->NxRayOBBIntersect(
620  ray._ray,
624 }
625 
626 /**
627  * Ray-capsule intersection test. Returns number of intersection points (0,1
628  * or 2) along the ray.
629  *
630  * \param [in] origin \param [in] dir \param [in] capsule
631  */
632 unsigned int PhysxUtilLib::
633 ray_capsule_intersect(const LPoint3f &origin, const LVector3f &dir, const PhysxCapsule &capsule) {
634 
635  nassertr_always(!origin.is_nan(), -1);
636  nassertr_always(!dir.is_nan(), -1);
637 
638  NxReal t[2] = { 0.0f, 0.0f }; // not used
639 
640  return _ptr->NxRayCapsuleIntersect(
643  capsule._capsule, t);
644 }
645 
646 /**
647  * Sphere-sphere sweep test. Returns true if spheres intersect during their
648  * linear motion along provided velocity vectors.
649  *
650  * \param [in] sphere0 \param [in] velocity0 \param [in] sphere1 \param [in]
651  * velocity1
652  */
654 swept_spheres_intersect(const PhysxSphere &sphere0, const LVector3f &velocity0, const PhysxSphere &sphere1, const LVector3f &velocity1) {
655 
656  nassertr_always(!velocity0.is_nan(), false);
657  nassertr_always(!velocity1.is_nan(), false);
658 
659  return _ptr->NxSweptSpheresIntersect(
660  sphere0._sphere,
661  PhysxManager::vec3_to_nxVec3(velocity0),
662  sphere1._sphere,
663  PhysxManager::vec3_to_nxVec3(velocity1));
664 }
665 
666 /**
667  * Ray-triangle intersection test. Returns impact distance (t) as well as
668  * barycentric coordinates (u,v) of impact point. The test performs back face
669  * culling or not according to 'cull'.
670  *
671  * \param [in] orig \param [in] dir \param [in] vert0 \param [in] vert1 \param
672  * [in] vert2 \param [out] hit, with coordinates (t,u,v) \param [in] cull
673  */
675 ray_tri_intersect(const LPoint3f &orig, const LVector3f &dir, const LPoint3f &vert0, const LPoint3f &vert1, const LPoint3f &vert2, LVector3f &hit, bool cull) {
676 
677  nassertr_always(!orig.is_nan(), false);
678  nassertr_always(!dir.is_nan(), false);
679  nassertr_always(!vert0.is_nan(), false);
680  nassertr_always(!vert1.is_nan(), false);
681  nassertr_always(!vert2.is_nan(), false);
682 
683  NxReal t, u, v;
684 
685  bool result = _ptr->NxRayTriIntersect(
691  t, u, v, cull);
692 
693  hit.set_x(t);
694  hit.set_y(u);
695  hit.set_z(v);
696 
697  return result;
698 }
699 
700 /**
701  * Box-vs-capsule sweep test. Sweeps a box against a capsule, returns true if
702  * box hit the capsule. Also returns contact information.
703  *
704  * \param [in] box Box \param [in] lss Capsule \param [in] dir Unit-length
705  * sweep direction \param [in] length Length of sweep \param [out] normal
706  * Normal at impact point
707  */
709 sweep_box_capsule(const PhysxBox &box, const PhysxCapsule &lss, const LVector3f &dir, float length, LVector3f &normal) {
710 
711  nassertr_always(!dir.is_nan(), false);
712 
713  NxReal min_dist; // not used
714  NxVec3 nNormal;
715 
716  bool result = _ptr->NxSweepBoxCapsule(
717  box._box, lss._capsule,
719  length, min_dist, nNormal);
720 
721  PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
722  return result;
723 }
724 
725 /**
726  * Box-vs-sphere sweep test. Sweeps a box against a sphere, returns true if
727  * box hit the sphere. Also returns contact information.
728  *
729  * \param [in] box Box \param [in] sphere Sphere \param [in] dir Unit-length
730  * sweep direction \param [in] length Length of sweep \param [out] normal
731  * Normal at impact point
732  */
734 sweep_box_sphere(const PhysxBox &box, const PhysxSphere &sphere, const LVector3f &dir, float length, LVector3f &normal) {
735 
736  nassertr_always(!dir.is_nan(), false);
737 
738  NxReal min_dist; // not used
739  NxVec3 nNormal;
740 
741  bool result = _ptr->NxSweepBoxSphere(
742  box._box, sphere._sphere,
744  length, min_dist, nNormal);
745 
746  PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
747  return result;
748 }
749 
750 /**
751  * Capsule-vs-capsule sweep test. Sweeps a capsule against a capsule, returns
752  * true if capsule hit the other capsule. Also returns contact information.
753  *
754  * \param [in] lss0 \param [in] lss1 \param [in] dir Unit-length sweep
755  * direction \param [in] length Length of sweep \param [out] ip Impact point
756  * \param [out] normal Normal at impact point
757  */
759 sweep_capsule_capsule(const PhysxCapsule &lss0, const PhysxCapsule &lss1, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal) {
760 
761  nassertr_always(!dir.is_nan(), false);
762 
763  NxReal min_dist; // not used
764  NxVec3 nIp;
765  NxVec3 nNormal;
766 
767  bool result = _ptr->NxSweepCapsuleCapsule(
768  lss0._capsule, lss1._capsule,
770  length, min_dist, nIp, nNormal);
771 
772  PhysxManager::update_point3_from_nxVec3(ip, nIp);
773  PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
774  return result;
775 }
776 
777 /**
778  * Sphere-vs-capsule sweep test. Sweeps a sphere against a capsule, returns
779  * true if sphere hit the capsule. Also returns contact information.
780  *
781  * \param [in] sphere \param [in] lss \param [in] dir Unit-length sweep
782  * direction \param [in] length Length of sweep \param [out] ip Impact point
783  * \param [out] normal Normal at impact point
784  */
786 sweep_sphere_capsule(const PhysxSphere &sphere, const PhysxCapsule &lss, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal) {
787 
788  nassertr_always(!dir.is_nan(), false);
789 
790  NxReal min_dist; // not used
791  NxVec3 nIp;
792  NxVec3 nNormal;
793 
794  bool result = _ptr->NxSweepSphereCapsule(
795  sphere._sphere, lss._capsule,
797  length, min_dist, nIp, nNormal);
798 
799  PhysxManager::update_point3_from_nxVec3(ip, nIp);
800  PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
801  return result;
802 }
803 
804 /**
805  * Box-vs-box sweep test. Sweeps a box against a box, returns true if box hit
806  * the other box. Also returns contact information.
807  *
808  * \param [in] box0 \param [in] box1 \param [in] dir Unit-length sweep
809  * direction \param [in] length Length of sweep \param [out] ip Impact point
810  * \param [out] normal Normal at impact point
811  */
813 sweep_box_box(const PhysxBox &box0, const PhysxBox &box1, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal) {
814 
815  nassertr_always(!dir.is_nan(), false);
816 
817  NxReal min_dist; // not used
818  NxVec3 nIp;
819  NxVec3 nNormal;
820 
821  bool result = _ptr->NxSweepBoxBox(
822  box0._box, box1._box,
824  length, nIp, nNormal, min_dist);
825 
826  PhysxManager::update_point3_from_nxVec3(ip, nIp);
827  PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
828  return result;
829 }
830 
831 /**
832  * Point-vs-OBB distance computation. Returns distance between a point and an
833  * OBB.
834  *
835  * \param [in] point The point \param [in] center OBB center \param [in]
836  * extents OBB extents \param [in] rot OBB rotation \param [out] params
837  * Closest point on the box, in box space
838  */
840 point_obb_sqr_dist(const LPoint3f &point, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot, LPoint3f &params) {
841 
842  nassertr_always(!point.is_nan(), 0.0f);
843  nassertr_always(!center.is_nan(), 0.0f);
844  nassertr_always(!extents.is_nan(), 0.0f);
845  nassertr_always(!rot.is_nan(), 0.0f);
846 
847  NxVec3 nParams;
848 
849  float result = _ptr->NxPointOBBSqrDist(
854  &nParams);
855 
856  PhysxManager::update_point3_from_nxVec3(params, nParams);
857  return result;
858 }
Represention of a axis aligned bounding box.
Definition: physxBounds3.h:29
Represents an oriented bounding box, as a center point, extents(radii) and a rotation.
Definition: physxBox.h:29
Represents a capsule.
Definition: physxCapsule.h:25
static NxVec3 vec3_to_nxVec3(const LVector3f &v)
Converts from LVector3f to NxVec3.
Definition: physxManager.I:27
static NxVec3 point3_to_nxVec3(const LPoint3f &p)
Converts from LPoint3f to NxVec3.
Definition: physxManager.I:63
static NxMat34 mat4_to_nxMat34(const LMatrix4f &m)
Converts from LMatrix4f to NxMat34.
Definition: physxManager.I:119
static LMatrix3f nxMat33_to_mat3(const NxMat33 &m)
Converts from NxMat33 to LMatrix3f.
Definition: physxManager.I:150
static NxMat33 mat3_to_nxMat33(const LMatrix3f &m)
Converts from LMatrix3f to NxMat33.
Definition: physxManager.I:139
static LVector3f nxVec3_to_vec3(const NxVec3 &v)
Converts from NxVec3 to LVector3f.
Definition: physxManager.I:36
Represents an ray as an origin and direction.
Definition: physxRay.h:26
Represents a line segment.
Definition: physxSegment.h:25
Represents a sphere defined by its center point and radius.
Definition: physxSphere.h:25
float compute_square_distance(const PhysxSegment &seg, const LPoint3f &point)
Compute the distance squared from a point to a line segment.
LVector3f compute_box_world_edge_normal(const PhysxBox &box, unsigned int edge_index)
Compute and edge normals for an oriented box.
void set_fpu_rounding_chop()
Set FPU precision.
bool sweep_box_sphere(const PhysxBox &box, const PhysxSphere &sphere, const LVector3f &dir, float length, LVector3f &normal)
Box-vs-sphere sweep test.
PhysxBox compute_box_around_capsule(const PhysxCapsule &capsule)
Compute a box which encloses a capsule.
float compute_sphere_density(float radius, float mass)
Computes density of a homogeneous sphere according to sphere mass.
void set_fpu_rounding_near()
Set FPU rounding mode.
bool ray_aabb_intersect(const LPoint3f &min, const LPoint3f &max, const LPoint3f &origin, const LVector3f &dir, LPoint3f &coord)
Ray-AABB intersection test.
bool sweep_box_capsule(const PhysxBox &box, const PhysxCapsule &lss, const LVector3f &dir, float length, LVector3f &normal)
Box-vs-capsule sweep test.
float compute_cylinder_density(float radius, float length, float mass)
Computes density of a homogeneous cylinder according to cylinder mass.
void set_fpu_precision24()
Set FPU precision.
PhysxBox create_box(const PhysxBounds3 &aabb, const LMatrix4f &mat)
Create an oriented box from an axis aligned box and a transformation.
int int_floor(const float &f)
Convert a floating point number to an integer.
float compute_ellipsoid_mass(const LVector3f &extents, float density)
Computes mass of a homogeneous ellipsoid according to ellipsoid density.
bool sweep_sphere_capsule(const PhysxSphere &sphere, const PhysxCapsule &lss, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal)
Sphere-vs-capsule sweep test.
float compute_cone_mass(float radius, float length, float density)
Computes mass of a homogeneous cone according to cone density.
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.
void set_fpu_precision53()
Set FPU precision.
float compute_sphere_mass(float radius, float density)
Computes mass of a homogeneous sphere according to sphere density.
void set_fpu_rounding_up()
Set FPU rounding mode.
float compute_distance_squared(const PhysxRay &ray, const LPoint3f &point)
Compute the distance squared from a point to a ray.
unsigned int ray_capsule_intersect(const LPoint3f &origin, const LVector3f &dir, const PhysxCapsule &capsule)
Ray-capsule intersection test.
bool is_box_a_inside_box_b(const PhysxBox &a, const PhysxBox &b)
Test if box A is inside another box B.
bool segment_aabb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &min, const LPoint3f &max)
Boolean segment-AABB intersection test.
PhysxSphere merge_spheres(const PhysxSphere &sphere0, const PhysxSphere &sphere1)
Compute an overall bounding sphere for a pair of spheres.
bool segment_box_intersect(const LPoint3f &p1, const LPoint3f &p2, const LPoint3f &bbox_min, const LPoint3f &bbox_max, LPoint3f &intercept)
Segment-AABB intersection test.
LVector3f compute_box_inertia_tensor(float mass, float xlength, float ylength, float zlength)
Computes diagonalized inertia tensor for a box.
bool box_contains_point(const PhysxBox &box, const LPoint3f &p)
Test if an oriented box contains a point.
PhysxCapsule compute_capsule_around_box(const PhysxBox &box)
Compute a capsule which encloses a box.
LVector3f compute_sphere_inertia_tensor(float mass, float radius, bool hollow)
Computes diagonalized inertia tensor for a sphere.
void set_fpu_exceptions(bool b)
Set FPU precision.
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.
int int_ceil(const float &f)
Convert a floating point number to an integer.
float compute_cone_density(float radius, float length, float mass)
Computes density of a homogeneous cone according to cone mass.
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).
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 ray_plane_intersect(const PhysxRay &ray, const PhysxPlane &plane, LPoint3f &point_on_plane)
Ray-plane intersection test.
int int_chop(const float &f)
Convert a floating point number to an integer.
bool ray_sphere_intersect(const LPoint3f &origin, const LVector3f &dir, float length, const LPoint3f &center, float radius, LPoint3f &hit_pos)
Ray-sphere intersection test.
float compute_box_density(const LVector3f &extents, float mass)
Computes density of a homogeneous box according to box mass.
float compute_cylinder_mass(float radius, float length, float density)
Computes mass of a homogeneous cylinder according to cylinder density.
bool swept_spheres_intersect(const PhysxSphere &sphere0, const LVector3f &velocity0, const PhysxSphere &sphere1, const LVector3f &velocity1)
Sphere-sphere sweep test.
void set_fpu_precision64()
Set FPU precision.
bool sweep_capsule_capsule(const PhysxCapsule &lss0, const PhysxCapsule &lss1, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal)
Capsule-vs-capsule sweep test.
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_box_mass(const LVector3f &extents, float density)
Computes mass of a homogeneous box according to box 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_rounding_down()
Set FPU rounding mode.
bool sweep_box_box(const PhysxBox &box0, const PhysxBox &box1, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal)
Box-vs-box sweep test.
bool segment_obb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot)
Boolean segment-OBB intersection test.
void normal_to_tangents(const LVector3f &n, LVector3f &t1, LVector3f &t2)
Get the tangent vectors associated with a normal.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.