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 */
27 void PhysxUtilLib::
28 set_fpu_exceptions(bool b) {
29
30 _ptr->NxSetFPUExceptions(b);
31 }
32
33 /**
34 * Set FPU precision.
35 */
36 void PhysxUtilLib::
37 set_fpu_precision24() {
38
39 _ptr->NxSetFPUPrecision24();
40 }
41
42 /**
43 * Set FPU precision.
44 */
45 void PhysxUtilLib::
46 set_fpu_precision53() {
47
48 _ptr->NxSetFPUPrecision53();
49 }
50
51 /**
52 * Set FPU precision.
53 */
54 void PhysxUtilLib::
55 set_fpu_precision64() {
56
57 _ptr->NxSetFPUPrecision64();
58 }
59
60 /**
61 * Set FPU precision.
62 */
63 void PhysxUtilLib::
64 set_fpu_rounding_chop() {
65
66 _ptr->NxSetFPURoundingChop();
67 }
68
69 /**
70 * Set FPU rounding mode.
71 */
72 void PhysxUtilLib::
73 set_fpu_rounding_down() {
74
75 _ptr->NxSetFPURoundingDown();
76 }
77
78 /**
79 * Set FPU rounding mode.
80 */
81 void PhysxUtilLib::
82 set_fpu_rounding_near() {
83
84 _ptr->NxSetFPURoundingNear();
85 }
86
87 /**
88 * Set FPU rounding mode.
89 */
90 void PhysxUtilLib::
91 set_fpu_rounding_up() {
92
93 _ptr->NxSetFPURoundingUp();
94 }
95
96 /**
97 * Convert a floating point number to an integer.
98 */
99 int PhysxUtilLib::
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 */
108 int PhysxUtilLib::
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 */
117 int PhysxUtilLib::
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 */
128 bool PhysxUtilLib::
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 */
139 PhysxBox PhysxUtilLib::
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 */
170 PhysxCapsule PhysxUtilLib::
171 compute_capsule_around_box(const PhysxBox &box) {
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 */
184 bool PhysxUtilLib::
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 */
195 PhysxBox PhysxUtilLib::
196 compute_box_around_capsule(const PhysxCapsule &capsule) {
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 */
208 float PhysxUtilLib::
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 */
220 float PhysxUtilLib::
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 */
232 PhysxSphere PhysxUtilLib::
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 */
245 void PhysxUtilLib::
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),
272 PhysxManager::vec3_to_nxVec3(b),
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 */
282 float PhysxUtilLib::
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 */
293 float PhysxUtilLib::
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 */
304 float PhysxUtilLib::
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 */
315 float PhysxUtilLib::
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 */
326 float PhysxUtilLib::
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 */
337 float PhysxUtilLib::
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 */
348 float PhysxUtilLib::
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 */
359 float PhysxUtilLib::
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 */
370 float PhysxUtilLib::
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 */
381 float PhysxUtilLib::
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 */
421 bool PhysxUtilLib::
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(
432 PhysxManager::vec3_to_nxVec3(extents0),
433 PhysxManager::point3_to_nxVec3(center0),
434 PhysxManager::mat3_to_nxMat33(rotation0),
435 PhysxManager::vec3_to_nxVec3(extents1),
436 PhysxManager::point3_to_nxVec3(center1),
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 */
446 bool PhysxUtilLib::
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(
456 PhysxManager::point3_to_nxVec3(vertex0),
457 PhysxManager::point3_to_nxVec3(vertex1),
458 PhysxManager::point3_to_nxVec3(vertex2),
459 PhysxManager::point3_to_nxVec3(center),
460 PhysxManager::point3_to_nxVec3(extents));
461 }
462
463 /**
464 * Ray-plane intersection test.
465 *
466 * \param [in] ray \param [in] plane \param [out] point_on_plane
467 */
468 bool PhysxUtilLib::
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 */
487 bool PhysxUtilLib::
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(
498 PhysxManager::point3_to_nxVec3(origin),
499 PhysxManager::vec3_to_nxVec3(dir),
500 length,
501 PhysxManager::point3_to_nxVec3(center),
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 */
516 bool PhysxUtilLib::
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(
527 PhysxManager::point3_to_nxVec3(p1),
528 PhysxManager::point3_to_nxVec3(p2),
529 PhysxManager::point3_to_nxVec3(bbox_min),
530 PhysxManager::point3_to_nxVec3(bbox_max),
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 */
543 bool PhysxUtilLib::
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(
554 PhysxManager::point3_to_nxVec3(min),
555 PhysxManager::point3_to_nxVec3(max),
556 PhysxManager::point3_to_nxVec3(origin),
557 PhysxManager::vec3_to_nxVec3(dir),
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 */
570 bool PhysxUtilLib::
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(
580 PhysxManager::point3_to_nxVec3(p0),
581 PhysxManager::point3_to_nxVec3(p1),
582 PhysxManager::point3_to_nxVec3(center),
583 PhysxManager::vec3_to_nxVec3(extents),
584 PhysxManager::mat3_to_nxMat33(rot));
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 */
592 bool PhysxUtilLib::
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(
601 PhysxManager::point3_to_nxVec3(p0),
602 PhysxManager::point3_to_nxVec3(p1),
603 PhysxManager::point3_to_nxVec3(min),
604 PhysxManager::point3_to_nxVec3(max));
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 */
612 bool PhysxUtilLib::
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,
621 PhysxManager::point3_to_nxVec3(center),
622 PhysxManager::point3_to_nxVec3(extents),
623 PhysxManager::mat3_to_nxMat33(rot));
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(
641 PhysxManager::point3_to_nxVec3(origin),
642 PhysxManager::vec3_to_nxVec3(dir),
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 */
653 bool PhysxUtilLib::
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 */
674 bool PhysxUtilLib::
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(
686 PhysxManager::point3_to_nxVec3(orig),
687 PhysxManager::vec3_to_nxVec3(dir),
688 PhysxManager::point3_to_nxVec3(vert0),
689 PhysxManager::point3_to_nxVec3(vert1),
690 PhysxManager::point3_to_nxVec3(vert2),
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 */
708 bool PhysxUtilLib::
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,
718 PhysxManager::vec3_to_nxVec3(dir),
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 */
733 bool PhysxUtilLib::
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,
743 PhysxManager::vec3_to_nxVec3(dir),
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 */
758 bool PhysxUtilLib::
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,
769 PhysxManager::vec3_to_nxVec3(dir),
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 */
785 bool PhysxUtilLib::
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,
796 PhysxManager::vec3_to_nxVec3(dir),
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 */
812 bool PhysxUtilLib::
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,
823 PhysxManager::vec3_to_nxVec3(dir),
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 */
839 float PhysxUtilLib::
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(
850 PhysxManager::point3_to_nxVec3(point),
851 PhysxManager::point3_to_nxVec3(center),
852 PhysxManager::vec3_to_nxVec3(extents),
853 PhysxManager::mat3_to_nxMat33(rot),
854 &nParams);
855
856 PhysxManager::update_point3_from_nxVec3(params, nParams);
857 return result;
858 }
PhysxBounds3
Represention of a axis aligned bounding box.
Definition: physxBounds3.h:29
PhysxBox
Represents an oriented bounding box, as a center point, extents(radii) and a rotation.
Definition: physxBox.h:29
PhysxCapsule
Represents a capsule.
Definition: physxCapsule.h:25
PhysxManager::vec3_to_nxVec3
static NxVec3 vec3_to_nxVec3(const LVector3f &v)
Converts from LVector3f to NxVec3.
Definition: physxManager.I:27
PhysxManager::point3_to_nxVec3
static NxVec3 point3_to_nxVec3(const LPoint3f &p)
Converts from LPoint3f to NxVec3.
Definition: physxManager.I:63
PhysxManager::mat4_to_nxMat34
static NxMat34 mat4_to_nxMat34(const LMatrix4f &m)
Converts from LMatrix4f to NxMat34.
Definition: physxManager.I:119
PhysxManager::nxMat33_to_mat3
static LMatrix3f nxMat33_to_mat3(const NxMat33 &m)
Converts from NxMat33 to LMatrix3f.
Definition: physxManager.I:150
PhysxManager::mat3_to_nxMat33
static NxMat33 mat3_to_nxMat33(const LMatrix3f &m)
Converts from LMatrix3f to NxMat33.
Definition: physxManager.I:139
PhysxManager::nxVec3_to_vec3
static LVector3f nxVec3_to_vec3(const NxVec3 &v)
Converts from NxVec3 to LVector3f.
Definition: physxManager.I:36
PhysxPlane
Definition: physxPlane.h:25
PhysxRay
Represents an ray as an origin and direction.
Definition: physxRay.h:26
PhysxSegment
Represents a line segment.
Definition: physxSegment.h:25
PhysxSphere
Represents a sphere defined by its center point and radius.
Definition: physxSphere.h:25
PhysxUtilLib::compute_square_distance
float compute_square_distance(const PhysxSegment &seg, const LPoint3f &point)
Compute the distance squared from a point to a line segment.
Definition: physxUtilLib.cxx:221
PhysxUtilLib::compute_box_world_edge_normal
LVector3f compute_box_world_edge_normal(const PhysxBox &box, unsigned int edge_index)
Compute and edge normals for an oriented box.
Definition: physxUtilLib.cxx:155
PhysxUtilLib::set_fpu_rounding_chop
void set_fpu_rounding_chop()
Set FPU precision.
Definition: physxUtilLib.cxx:64
PhysxUtilLib::sweep_box_sphere
bool sweep_box_sphere(const PhysxBox &box, const PhysxSphere &sphere, const LVector3f &dir, float length, LVector3f &normal)
Box-vs-sphere sweep test.
Definition: physxUtilLib.cxx:734
PhysxUtilLib::compute_box_around_capsule
PhysxBox compute_box_around_capsule(const PhysxCapsule &capsule)
Compute a box which encloses a capsule.
Definition: physxUtilLib.cxx:196
PhysxUtilLib::compute_sphere_density
float compute_sphere_density(float radius, float mass)
Computes density of a homogeneous sphere according to sphere mass.
Definition: physxUtilLib.cxx:294
PhysxUtilLib::set_fpu_rounding_near
void set_fpu_rounding_near()
Set FPU rounding mode.
Definition: physxUtilLib.cxx:82
PhysxUtilLib::ray_aabb_intersect
bool ray_aabb_intersect(const LPoint3f &min, const LPoint3f &max, const LPoint3f &origin, const LVector3f &dir, LPoint3f &coord)
Ray-AABB intersection test.
Definition: physxUtilLib.cxx:544
PhysxUtilLib::sweep_box_capsule
bool sweep_box_capsule(const PhysxBox &box, const PhysxCapsule &lss, const LVector3f &dir, float length, LVector3f &normal)
Box-vs-capsule sweep test.
Definition: physxUtilLib.cxx:709
PhysxUtilLib::compute_cylinder_density
float compute_cylinder_density(float radius, float length, float mass)
Computes density of a homogeneous cylinder according to cylinder mass.
Definition: physxUtilLib.cxx:360
PhysxUtilLib::set_fpu_precision24
void set_fpu_precision24()
Set FPU precision.
Definition: physxUtilLib.cxx:37
PhysxUtilLib::create_box
PhysxBox create_box(const PhysxBounds3 &aabb, const LMatrix4f &mat)
Create an oriented box from an axis aligned box and a transformation.
Definition: physxUtilLib.cxx:140
PhysxUtilLib::int_floor
int int_floor(const float &f)
Convert a floating point number to an integer.
Definition: physxUtilLib.cxx:118
PhysxUtilLib::compute_ellipsoid_mass
float compute_ellipsoid_mass(const LVector3f &extents, float density)
Computes mass of a homogeneous ellipsoid according to ellipsoid density.
Definition: physxUtilLib.cxx:327
PhysxUtilLib::sweep_sphere_capsule
bool sweep_sphere_capsule(const PhysxSphere &sphere, const PhysxCapsule &lss, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal)
Sphere-vs-capsule sweep test.
Definition: physxUtilLib.cxx:786
PhysxUtilLib::compute_cone_mass
float compute_cone_mass(float radius, float length, float density)
Computes mass of a homogeneous cone according to cone density.
Definition: physxUtilLib.cxx:371
PhysxUtilLib::box_box_intersect
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.
Definition: physxUtilLib.cxx:422
PhysxUtilLib::set_fpu_precision53
void set_fpu_precision53()
Set FPU precision.
Definition: physxUtilLib.cxx:46
PhysxUtilLib::compute_sphere_mass
float compute_sphere_mass(float radius, float density)
Computes mass of a homogeneous sphere according to sphere density.
Definition: physxUtilLib.cxx:283
PhysxUtilLib::set_fpu_rounding_up
void set_fpu_rounding_up()
Set FPU rounding mode.
Definition: physxUtilLib.cxx:91
PhysxUtilLib::compute_distance_squared
float compute_distance_squared(const PhysxRay &ray, const LPoint3f &point)
Compute the distance squared from a point to a ray.
Definition: physxUtilLib.cxx:209
PhysxUtilLib::ray_capsule_intersect
unsigned int ray_capsule_intersect(const LPoint3f &origin, const LVector3f &dir, const PhysxCapsule &capsule)
Ray-capsule intersection test.
Definition: physxUtilLib.cxx:633
PhysxUtilLib::is_box_a_inside_box_b
bool is_box_a_inside_box_b(const PhysxBox &a, const PhysxBox &b)
Test if box A is inside another box B.
Definition: physxUtilLib.cxx:185
PhysxUtilLib::segment_aabb_intersect
bool segment_aabb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &min, const LPoint3f &max)
Boolean segment-AABB intersection test.
Definition: physxUtilLib.cxx:593
PhysxUtilLib::merge_spheres
PhysxSphere merge_spheres(const PhysxSphere &sphere0, const PhysxSphere &sphere1)
Compute an overall bounding sphere for a pair of spheres.
Definition: physxUtilLib.cxx:233
PhysxUtilLib::segment_box_intersect
bool segment_box_intersect(const LPoint3f &p1, const LPoint3f &p2, const LPoint3f &bbox_min, const LPoint3f &bbox_max, LPoint3f &intercept)
Segment-AABB intersection test.
Definition: physxUtilLib.cxx:517
PhysxUtilLib::compute_box_inertia_tensor
LVector3f compute_box_inertia_tensor(float mass, float xlength, float ylength, float zlength)
Computes diagonalized inertia tensor for a box.
Definition: physxUtilLib.cxx:394
PhysxUtilLib::box_contains_point
bool box_contains_point(const PhysxBox &box, const LPoint3f &p)
Test if an oriented box contains a point.
Definition: physxUtilLib.cxx:129
PhysxUtilLib::compute_capsule_around_box
PhysxCapsule compute_capsule_around_box(const PhysxBox &box)
Compute a capsule which encloses a box.
Definition: physxUtilLib.cxx:171
PhysxUtilLib::compute_sphere_inertia_tensor
LVector3f compute_sphere_inertia_tensor(float mass, float radius, bool hollow)
Computes diagonalized inertia tensor for a sphere.
Definition: physxUtilLib.cxx:407
PhysxUtilLib::set_fpu_exceptions
void set_fpu_exceptions(bool b)
Set FPU precision.
Definition: physxUtilLib.cxx:28
PhysxUtilLib::ray_tri_intersect
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.
Definition: physxUtilLib.cxx:675
PhysxUtilLib::compute_ellipsoid_density
float compute_ellipsoid_density(const LVector3f &extents, float mass)
Computes density of a homogeneous ellipsoid according to ellipsoid mass.
Definition: physxUtilLib.cxx:338
PhysxUtilLib::int_ceil
int int_ceil(const float &f)
Convert a floating point number to an integer.
Definition: physxUtilLib.cxx:100
PhysxUtilLib::compute_cone_density
float compute_cone_density(float radius, float length, float mass)
Computes density of a homogeneous cone according to cone mass.
Definition: physxUtilLib.cxx:382
PhysxUtilLib::find_rotation_matrix
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).
Definition: physxUtilLib.cxx:268
PhysxUtilLib::point_obb_sqr_dist
float point_obb_sqr_dist(const LPoint3f &point, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot, LPoint3f &params)
Point-vs-OBB distance computation.
Definition: physxUtilLib.cxx:840
PhysxUtilLib::ray_plane_intersect
bool ray_plane_intersect(const PhysxRay &ray, const PhysxPlane &plane, LPoint3f &point_on_plane)
Ray-plane intersection test.
Definition: physxUtilLib.cxx:469
PhysxUtilLib::int_chop
int int_chop(const float &f)
Convert a floating point number to an integer.
Definition: physxUtilLib.cxx:109
PhysxUtilLib::ray_sphere_intersect
bool ray_sphere_intersect(const LPoint3f &origin, const LVector3f &dir, float length, const LPoint3f &center, float radius, LPoint3f &hit_pos)
Ray-sphere intersection test.
Definition: physxUtilLib.cxx:488
PhysxUtilLib::compute_box_density
float compute_box_density(const LVector3f &extents, float mass)
Computes density of a homogeneous box according to box mass.
Definition: physxUtilLib.cxx:316
PhysxUtilLib::compute_cylinder_mass
float compute_cylinder_mass(float radius, float length, float density)
Computes mass of a homogeneous cylinder according to cylinder density.
Definition: physxUtilLib.cxx:349
PhysxUtilLib::swept_spheres_intersect
bool swept_spheres_intersect(const PhysxSphere &sphere0, const LVector3f &velocity0, const PhysxSphere &sphere1, const LVector3f &velocity1)
Sphere-sphere sweep test.
Definition: physxUtilLib.cxx:654
PhysxUtilLib::set_fpu_precision64
void set_fpu_precision64()
Set FPU precision.
Definition: physxUtilLib.cxx:55
PhysxUtilLib::sweep_capsule_capsule
bool sweep_capsule_capsule(const PhysxCapsule &lss0, const PhysxCapsule &lss1, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal)
Capsule-vs-capsule sweep test.
Definition: physxUtilLib.cxx:759
PhysxUtilLib::tri_box_intersect
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.
Definition: physxUtilLib.cxx:447
PhysxUtilLib::compute_box_mass
float compute_box_mass(const LVector3f &extents, float density)
Computes mass of a homogeneous box according to box density.
Definition: physxUtilLib.cxx:305
PhysxUtilLib::ray_obb_intersect
bool ray_obb_intersect(const PhysxRay &ray, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot)
Boolean ray-OBB intersection test.
Definition: physxUtilLib.cxx:613
PhysxUtilLib::set_fpu_rounding_down
void set_fpu_rounding_down()
Set FPU rounding mode.
Definition: physxUtilLib.cxx:73
PhysxUtilLib::sweep_box_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.
Definition: physxUtilLib.cxx:813
PhysxUtilLib::segment_obb_intersect
bool segment_obb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot)
Boolean segment-OBB intersection test.
Definition: physxUtilLib.cxx:571
PhysxUtilLib::normal_to_tangents
void normal_to_tangents(const LVector3f &n, LVector3f &t1, LVector3f &t2)
Get the tangent vectors associated with a normal.
Definition: physxUtilLib.cxx:246
physxBounds3.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxBox.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxCapsule.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxManager.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxPlane.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxRay.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxSegment.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxSphere.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
physxUtilLib.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.