Panda3D
 All Classes Functions Variables Enumerations
physxUtilLib.cxx
00001 // Filename: physxUtilLib.cxx
00002 // Created by:  enn0x (01Nov09)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "physxUtilLib.h"
00016 #include "physxManager.h"
00017 #include "physxBounds3.h"
00018 #include "physxBox.h"
00019 #include "physxCapsule.h"
00020 #include "physxPlane.h"
00021 #include "physxRay.h"
00022 #include "physxSegment.h"
00023 #include "physxSphere.h"
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //     Function: PhysxUtilLib::set_fpu_exceptions
00027 //       Access: Published
00028 //  Description: Set FPU precision.
00029 ////////////////////////////////////////////////////////////////////
00030 void PhysxUtilLib::
00031 set_fpu_exceptions(bool b) {
00032 
00033   _ptr->NxSetFPUExceptions(b);
00034 }
00035 
00036 ////////////////////////////////////////////////////////////////////
00037 //     Function: PhysxUtilLib::set_fpu_precision24
00038 //       Access: Published
00039 //  Description: Set FPU precision.
00040 ////////////////////////////////////////////////////////////////////
00041 void PhysxUtilLib::
00042 set_fpu_precision24() {
00043 
00044   _ptr->NxSetFPUPrecision24();
00045 }
00046 
00047 ////////////////////////////////////////////////////////////////////
00048 //     Function: PhysxUtilLib::set_fpu_precision53
00049 //       Access: Published
00050 //  Description: Set FPU precision.
00051 ////////////////////////////////////////////////////////////////////
00052 void PhysxUtilLib::
00053 set_fpu_precision53() {
00054 
00055   _ptr->NxSetFPUPrecision53();
00056 }
00057 
00058 ////////////////////////////////////////////////////////////////////
00059 //     Function: PhysxUtilLib::set_fpu_precision64
00060 //       Access: Published
00061 //  Description: Set FPU precision.
00062 ////////////////////////////////////////////////////////////////////
00063 void PhysxUtilLib::
00064 set_fpu_precision64() {
00065 
00066   _ptr->NxSetFPUPrecision64();
00067 }
00068 
00069 ////////////////////////////////////////////////////////////////////
00070 //     Function: PhysxUtilLib::set_fpu_rounding_chop
00071 //       Access: Published
00072 //  Description: Set FPU precision.
00073 ////////////////////////////////////////////////////////////////////
00074 void PhysxUtilLib::
00075 set_fpu_rounding_chop() {
00076 
00077   _ptr->NxSetFPURoundingChop();
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //     Function: PhysxUtilLib::set_fpu_rounding_down
00082 //       Access: Published
00083 //  Description: Set FPU rounding mode.
00084 ////////////////////////////////////////////////////////////////////
00085 void PhysxUtilLib::
00086 set_fpu_rounding_down() {
00087 
00088   _ptr->NxSetFPURoundingDown();
00089 }
00090 
00091 ////////////////////////////////////////////////////////////////////
00092 //     Function: PhysxUtilLib::set_fpu_rounding_near
00093 //       Access: Published
00094 //  Description: Set FPU rounding mode.
00095 ////////////////////////////////////////////////////////////////////
00096 void PhysxUtilLib::
00097 set_fpu_rounding_near() {
00098 
00099   _ptr->NxSetFPURoundingNear();
00100 }
00101 
00102 ////////////////////////////////////////////////////////////////////
00103 //     Function: PhysxUtilLib::set_fpu_rounding_up
00104 //       Access: Published
00105 //  Description: Set FPU rounding mode.
00106 ////////////////////////////////////////////////////////////////////
00107 void PhysxUtilLib::
00108 set_fpu_rounding_up() {
00109 
00110   _ptr->NxSetFPURoundingUp();
00111 }
00112 
00113 ////////////////////////////////////////////////////////////////////
00114 //     Function: PhysxUtilLib::int_ceil
00115 //       Access: Published
00116 //  Description: Convert a floating point number to an integer.
00117 ////////////////////////////////////////////////////////////////////
00118 int PhysxUtilLib::
00119 int_ceil(const float &f) {
00120 
00121   return _ptr->NxIntCeil(f);
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //     Function: PhysxUtilLib::int_chop
00126 //       Access: Published
00127 //  Description: Convert a floating point number to an integer.
00128 ////////////////////////////////////////////////////////////////////
00129 int PhysxUtilLib::
00130 int_chop(const float &f) {
00131 
00132   return _ptr->NxIntChop(f);
00133 }
00134 
00135 ////////////////////////////////////////////////////////////////////
00136 //     Function: PhysxUtilLib::int_floor
00137 //       Access: Published
00138 //  Description: Convert a floating point number to an integer.
00139 ////////////////////////////////////////////////////////////////////
00140 int PhysxUtilLib::
00141 int_floor(const float &f) {
00142 
00143   return _ptr->NxIntFloor(f);
00144 }
00145 
00146 ////////////////////////////////////////////////////////////////////
00147 //     Function: PhysxUtilLib::box_contains_point
00148 //       Access: Published
00149 //  Description: Test if an oriented box contains a point.
00150 //
00151 //               \param [in] box
00152 //               \param [in] p
00153 ////////////////////////////////////////////////////////////////////
00154 bool PhysxUtilLib::
00155 box_contains_point(const PhysxBox &box, const LPoint3f &p) {
00156 
00157   return _ptr->NxBoxContainsPoint(box._box, PhysxManager::point3_to_nxVec3(p));
00158 }
00159 
00160 ////////////////////////////////////////////////////////////////////
00161 //     Function: PhysxUtilLib::create_box
00162 //       Access: Published
00163 //  Description: Create an oriented box from an axis aligned box
00164 //               and a transformation. 
00165 //
00166 //               \param [in] aabb
00167 //               \param [in] mat
00168 ////////////////////////////////////////////////////////////////////
00169 PhysxBox PhysxUtilLib::
00170 create_box(const PhysxBounds3 &aabb, const LMatrix4f &mat) {
00171 
00172   PhysxBox box;
00173   _ptr->NxCreateBox(box._box, aabb._bounds, PhysxManager::mat4_to_nxMat34(mat));
00174   return box;
00175 }
00176 
00177 ////////////////////////////////////////////////////////////////////
00178 //     Function: PhysxUtilLib::compute_box_world_edge_normal
00179 //       Access: Published
00180 //  Description: Compute and edge normals for an oriented box. This
00181 //               is an averaged normal, from the two faces sharing
00182 //               the edge. The edge index should be from 0 to 11 
00183 //               (i.e. a box has 12 edges).
00184 //
00185 //               \param [in] box
00186 //               \param [in] edge_index
00187 ////////////////////////////////////////////////////////////////////
00188 LVector3f PhysxUtilLib::
00189 compute_box_world_edge_normal(const PhysxBox &box, unsigned int edge_index) {
00190 
00191   NxVec3 nNormal;
00192 
00193   nassertr(edge_index < 12, LVector3f::zero());
00194 
00195   _ptr->NxComputeBoxWorldEdgeNormal(box._box, edge_index, nNormal);
00196   return PhysxManager::nxVec3_to_vec3(nNormal);
00197 }
00198 
00199 ////////////////////////////////////////////////////////////////////
00200 //     Function: PhysxUtilLib::compute_capsule_around_box
00201 //       Access: Published
00202 //  Description: Compute a capsule which encloses a box.
00203 //
00204 //               \param [in] box
00205 ////////////////////////////////////////////////////////////////////
00206 PhysxCapsule PhysxUtilLib::
00207 compute_capsule_around_box(const PhysxBox &box) {
00208 
00209   PhysxCapsule capsule;
00210   _ptr->NxComputeCapsuleAroundBox(box._box, capsule._capsule);
00211   return capsule;
00212 }
00213 
00214 ////////////////////////////////////////////////////////////////////
00215 //     Function: PhysxUtilLib::is_box_a_inside_box_b
00216 //       Access: Published
00217 //  Description: Test if box A is inside another box B. Returns
00218 //               TRUE if box A is inside box B.
00219 //
00220 //               \param [in] a
00221 //               \param [in] b
00222 ////////////////////////////////////////////////////////////////////
00223 bool PhysxUtilLib::
00224 is_box_a_inside_box_b(const PhysxBox &a, const PhysxBox &b) {
00225 
00226   return _ptr->NxIsBoxAInsideBoxB(a._box, b._box);
00227 }
00228 
00229 ////////////////////////////////////////////////////////////////////
00230 //     Function: PhysxUtilLib::compute_box_around_capsule
00231 //       Access: Published
00232 //  Description: Compute a box which encloses a capsule.
00233 //
00234 //               \param [in] capsule
00235 ////////////////////////////////////////////////////////////////////
00236 PhysxBox PhysxUtilLib::
00237 compute_box_around_capsule(const PhysxCapsule &capsule) {
00238 
00239   PhysxBox box;
00240   _ptr->NxComputeBoxAroundCapsule(capsule._capsule, box._box);
00241   return box;
00242 }
00243 
00244 ////////////////////////////////////////////////////////////////////
00245 //     Function: PhysxUtilLib::compute_distance_squared
00246 //       Access: Published
00247 //  Description: Compute the distance squared from a point to a
00248 //               ray.
00249 //
00250 //               \param [in] ray
00251 //               \param [in] point
00252 ////////////////////////////////////////////////////////////////////
00253 float PhysxUtilLib::
00254 compute_distance_squared(const PhysxRay &ray, const LPoint3f &point) {
00255 
00256   NxF32 t; // not used
00257   return _ptr->NxComputeDistanceSquared(ray._ray, PhysxManager::point3_to_nxVec3(point), &t);
00258 }
00259 
00260 ////////////////////////////////////////////////////////////////////
00261 //     Function: PhysxUtilLib::compute_square_distance
00262 //       Access: Published
00263 //  Description: Compute the distance squared from a point to a
00264 //               line segment.
00265 //
00266 //               \param [in] seg
00267 //               \param [in] point
00268 ////////////////////////////////////////////////////////////////////
00269 float PhysxUtilLib::
00270 compute_square_distance(const PhysxSegment &seg, const LPoint3f &point) {
00271 
00272   NxF32 t; // not used
00273   return _ptr->NxComputeSquareDistance(seg._segment, PhysxManager::point3_to_nxVec3(point), &t);
00274 }
00275 
00276 ////////////////////////////////////////////////////////////////////
00277 //     Function: PhysxUtilLib::merge_spheres
00278 //       Access: Published
00279 //  Description: Compute an overall bounding sphere for a pair of
00280 //               spheres.
00281 //
00282 //               \param [in] sphere0
00283 //               \param [in] sphere1
00284 ////////////////////////////////////////////////////////////////////
00285 PhysxSphere PhysxUtilLib::
00286 merge_spheres(const PhysxSphere &sphere0, const PhysxSphere &sphere1) {
00287 
00288   PhysxSphere merged;
00289   _ptr->NxMergeSpheres(merged._sphere, sphere0._sphere, sphere1._sphere);
00290   return merged;
00291 }
00292 
00293 ////////////////////////////////////////////////////////////////////
00294 //     Function: PhysxUtilLib::normal_to_tangents
00295 //       Access: Published
00296 //  Description: Get the tangent vectors associated with a normal.
00297 //
00298 //               \param [in] n
00299 //               \param [out] t1
00300 //               \param [out] t2
00301 ////////////////////////////////////////////////////////////////////
00302 void PhysxUtilLib::
00303 normal_to_tangents(const LVector3f &n, LVector3f &t1, LVector3f &t2) {
00304 
00305   NxVec3 nt1;
00306   NxVec3 nt2;
00307 
00308   _ptr->NxNormalToTangents(PhysxManager::vec3_to_nxVec3(n), nt1, nt2);
00309 
00310   t1.set_x(nt1.x);
00311   t1.set_y(nt1.y);
00312   t1.set_z(nt1.z);
00313 
00314   t2.set_x(nt2.x);
00315   t2.set_y(nt2.y);
00316   t2.set_z(nt2.z);
00317 }
00318 
00319 ////////////////////////////////////////////////////////////////////
00320 //     Function: PhysxUtilLib::find_rotation_matrix
00321 //       Access: Published
00322 //  Description: Computes a rotation matrix M so that: M * x = b
00323 //               (x and b are unit vectors).
00324 //
00325 //               \param [in] x
00326 //               \param [in] b
00327 ////////////////////////////////////////////////////////////////////
00328 LMatrix3f PhysxUtilLib::
00329 find_rotation_matrix(const LVector3f &x, const LVector3f &b) {
00330 
00331   NxMat33 nmat;
00332   _ptr->NxFindRotationMatrix(PhysxManager::vec3_to_nxVec3(x),
00333                              PhysxManager::vec3_to_nxVec3(b),
00334                              nmat);
00335   return PhysxManager::nxMat33_to_mat3(nmat);
00336 }
00337 
00338 ////////////////////////////////////////////////////////////////////
00339 //     Function: PhysxUtilLib::compute_sphere_mass
00340 //       Access: Published
00341 //  Description: Computes mass of a homogeneous sphere according
00342 //               to sphere density. 
00343 //
00344 //               \param [in] radius
00345 //               \param [in] density
00346 ////////////////////////////////////////////////////////////////////
00347 float PhysxUtilLib::
00348 compute_sphere_mass(float radius, float density) {
00349 
00350   return _ptr->NxComputeSphereMass(radius, density);
00351 }
00352 
00353 ////////////////////////////////////////////////////////////////////
00354 //     Function: PhysxUtilLib::compute_sphere_density
00355 //       Access: Published
00356 //  Description: Computes density of a homogeneous sphere according
00357 //               to sphere mass
00358 //
00359 //               \param [in] radius
00360 //               \param [in] mass
00361 ////////////////////////////////////////////////////////////////////
00362 float PhysxUtilLib::
00363 compute_sphere_density(float radius, float mass) {
00364 
00365   return _ptr->NxComputeSphereDensity(radius, mass);
00366 }
00367 
00368 ////////////////////////////////////////////////////////////////////
00369 //     Function: PhysxUtilLib::compute_box_mass
00370 //       Access: Published
00371 //  Description: Computes mass of a homogeneous box according to
00372 //               box density.
00373 //
00374 //               \param [in] radius
00375 //               \param [in] density
00376 ////////////////////////////////////////////////////////////////////
00377 float PhysxUtilLib::
00378 compute_box_mass(const LVector3f &extents, float density) {
00379 
00380   return _ptr->NxComputeBoxMass(PhysxManager::vec3_to_nxVec3(extents), density);
00381 }
00382 
00383 ////////////////////////////////////////////////////////////////////
00384 //     Function: PhysxUtilLib::compute_box_density
00385 //       Access: Published
00386 //  Description: Computes density of a homogeneous box according to
00387 //               box mass.
00388 //
00389 //               \param [in] radius
00390 //               \param [in] mass
00391 ////////////////////////////////////////////////////////////////////
00392 float PhysxUtilLib::
00393 compute_box_density(const LVector3f &extents, float mass) {
00394 
00395   return _ptr->NxComputeBoxDensity(PhysxManager::vec3_to_nxVec3(extents), mass);
00396 }
00397 
00398 ////////////////////////////////////////////////////////////////////
00399 //     Function: PhysxUtilLib::compute_ellipsoid_mass
00400 //       Access: Published
00401 //  Description: Computes mass of a homogeneous ellipsoid according
00402 //               to ellipsoid density.
00403 //
00404 //               \param [in] radius
00405 //               \param [in] density
00406 ////////////////////////////////////////////////////////////////////
00407 float PhysxUtilLib::
00408 compute_ellipsoid_mass(const LVector3f &extents, float density ) {
00409 
00410   return _ptr->NxComputeEllipsoidMass(PhysxManager::vec3_to_nxVec3(extents), density);
00411 }
00412 
00413 ////////////////////////////////////////////////////////////////////
00414 //     Function: PhysxUtilLib::compute_ellipsoid_density
00415 //       Access: Published
00416 //  Description: Computes density of a homogeneous ellipsoid
00417 //               according to ellipsoid mass.
00418 //
00419 //               \param [in] radius
00420 //               \param [in] mass
00421 ////////////////////////////////////////////////////////////////////
00422 float PhysxUtilLib::
00423 compute_ellipsoid_density(const LVector3f &extents, float mass) {
00424 
00425   return _ptr->NxComputeEllipsoidDensity(PhysxManager::vec3_to_nxVec3(extents), mass);
00426 }
00427 
00428 ////////////////////////////////////////////////////////////////////
00429 //     Function: PhysxUtilLib::compute_cylinder_mass
00430 //       Access: Published
00431 //  Description: Computes mass of a homogeneous cylinder according
00432 //               to cylinder density.
00433 //
00434 //               \param [in] radius
00435 //               \param [in] density
00436 ////////////////////////////////////////////////////////////////////
00437 float PhysxUtilLib::
00438 compute_cylinder_mass(float radius, float length, float density) {
00439 
00440   return _ptr->NxComputeCylinderMass(radius, length, density);
00441 }
00442 
00443 ////////////////////////////////////////////////////////////////////
00444 //     Function: PhysxUtilLib::compute_cylinder_density
00445 //       Access: Published
00446 //  Description: Computes density of a homogeneous cylinder
00447 //               according to cylinder mass.
00448 //
00449 //               \param [in] radius
00450 //               \param [in] mass
00451 ////////////////////////////////////////////////////////////////////
00452 float PhysxUtilLib::
00453 compute_cylinder_density(float radius, float length, float mass) {
00454 
00455   return _ptr->NxComputeCylinderDensity(radius, length, mass);
00456 }
00457 
00458 ////////////////////////////////////////////////////////////////////
00459 //     Function: PhysxUtilLib::compute_cone_mass
00460 //       Access: Published
00461 //  Description: Computes mass of a homogeneous cone according to
00462 //               cone density.
00463 //
00464 //               \param [in] radius
00465 //               \param [in] density
00466 ////////////////////////////////////////////////////////////////////
00467 float PhysxUtilLib::
00468 compute_cone_mass(float radius, float length, float density) {
00469 
00470   return _ptr->NxComputeConeMass(radius, length, density);
00471 }
00472 
00473 ////////////////////////////////////////////////////////////////////
00474 //     Function: PhysxUtilLib::compute_cone_density
00475 //       Access: Published
00476 //  Description: Computes density of a homogeneous cone according
00477 //               to cone mass.
00478 //
00479 //               \param [in] radius
00480 //               \param [in] mass
00481 ////////////////////////////////////////////////////////////////////
00482 float PhysxUtilLib::
00483 compute_cone_density(float radius, float length, float mass) {
00484 
00485   return _ptr->NxComputeConeDensity(radius, length, mass);
00486 }
00487 
00488 ////////////////////////////////////////////////////////////////////
00489 //     Function: PhysxUtilLib::compute_box_inertia_tensor
00490 //       Access: Published
00491 //  Description: Computes diagonalized inertia tensor for a box.
00492 //
00493 //               \param [in] mass
00494 //               \param [in] xlength
00495 //               \param [in] ylength
00496 //               \param [in] zlength
00497 ////////////////////////////////////////////////////////////////////
00498 LVector3f PhysxUtilLib::
00499 compute_box_inertia_tensor(float mass, float xlength, float ylength, float zlength) {
00500 
00501   NxVec3 tensor;
00502   _ptr->NxComputeBoxInertiaTensor(tensor, mass, xlength, ylength, zlength);
00503   return PhysxManager::nxVec3_to_vec3(tensor);
00504 }
00505 
00506 ////////////////////////////////////////////////////////////////////
00507 //     Function: PhysxUtilLib::compute_sphere_inertia_tensor
00508 //       Access: Published
00509 //  Description: Computes diagonalized inertia tensor for a sphere.
00510 //
00511 //               \param [in] mass
00512 //               \param [in] radius
00513 //               \param [in] hollow
00514 ////////////////////////////////////////////////////////////////////
00515 LVector3f PhysxUtilLib::
00516 compute_sphere_inertia_tensor(float mass, float radius, bool hollow) {
00517 
00518   NxVec3 tensor;
00519   _ptr->NxComputeSphereInertiaTensor(tensor, mass, radius, hollow);
00520   return PhysxManager::nxVec3_to_vec3(tensor);
00521 }
00522 
00523 ////////////////////////////////////////////////////////////////////
00524 //     Function: PhysxUtilLib::box_box_intersect
00525 //       Access: Published
00526 //  Description: Boolean intersection test between two OBBs. Uses
00527 //               the separating axis theorem. Disabling 'full_test'
00528 //               only performs 6 axis tests out of 15.
00529 //
00530 //               \param [in] extents0
00531 //               \param [in] center0
00532 //               \param [in] rotation0
00533 //               \param [in] extents1
00534 //               \param [in] center1
00535 //               \param [in] rotation1
00536 //               \param [in] full_test
00537 ////////////////////////////////////////////////////////////////////
00538 bool PhysxUtilLib::
00539 box_box_intersect(const LVector3f &extents0, const LPoint3f &center0, const LMatrix3f &rotation0, const LVector3f &extents1, const LPoint3f &center1, const LMatrix3f &rotation1, bool full_test) {
00540 
00541   nassertr_always(!extents0.is_nan(), false);
00542   nassertr_always(!center0.is_nan(), false);
00543   nassertr_always(!rotation0.is_nan(), false);
00544   nassertr_always(!extents1.is_nan(), false);
00545   nassertr_always(!center1.is_nan(), false);
00546   nassertr_always(!rotation1.is_nan(), false);
00547 
00548   return _ptr->NxBoxBoxIntersect(
00549     PhysxManager::vec3_to_nxVec3(extents0),
00550     PhysxManager::point3_to_nxVec3(center0),
00551     PhysxManager::mat3_to_nxMat33(rotation0),
00552     PhysxManager::vec3_to_nxVec3(extents1),
00553     PhysxManager::point3_to_nxVec3(center1),
00554     PhysxManager::mat3_to_nxMat33(rotation1), full_test);
00555 }
00556 
00557 ////////////////////////////////////////////////////////////////////
00558 //     Function: PhysxUtilLib::tri_box_intersect
00559 //       Access: Published
00560 //  Description: Boolean intersection test between a triangle and
00561 //               a box.
00562 //
00563 //               \param [in] vertex0
00564 //               \param [in] vertex1
00565 //               \param [in] vertex2
00566 //               \param [in] center
00567 //               \param [in] extents
00568 ////////////////////////////////////////////////////////////////////
00569 bool PhysxUtilLib::
00570 tri_box_intersect(const LPoint3f &vertex0, const LPoint3f &vertex1, const LPoint3f &vertex2, const LPoint3f &center, const LVector3f &extents) {
00571 
00572   nassertr_always(!vertex0.is_nan(), false);
00573   nassertr_always(!vertex1.is_nan(), false);
00574   nassertr_always(!vertex2.is_nan(), false);
00575   nassertr_always(!center.is_nan(), false);
00576   nassertr_always(!extents.is_nan(), false);
00577 
00578   return _ptr->NxTriBoxIntersect(
00579     PhysxManager::point3_to_nxVec3(vertex0),
00580     PhysxManager::point3_to_nxVec3(vertex1),
00581     PhysxManager::point3_to_nxVec3(vertex2),
00582     PhysxManager::point3_to_nxVec3(center),
00583     PhysxManager::point3_to_nxVec3(extents));
00584 }
00585 
00586 ////////////////////////////////////////////////////////////////////
00587 //     Function: PhysxUtilLib::ray_plane_intersect
00588 //       Access: Published
00589 //  Description: Ray-plane intersection test. 
00590 //
00591 //               \param [in] ray
00592 //               \param [in] plane
00593 //               \param [out] point_on_plane
00594 ////////////////////////////////////////////////////////////////////
00595 bool PhysxUtilLib::
00596 ray_plane_intersect(const PhysxRay &ray, const PhysxPlane &plane, LPoint3f &point_on_plane) {
00597 
00598   NxReal dist; // not used
00599   NxVec3 nPointOnPlane;
00600 
00601   bool result = _ptr->NxRayPlaneIntersect(ray._ray, plane._plane, dist, nPointOnPlane);
00602 
00603   PhysxManager::update_point3_from_nxVec3(point_on_plane, nPointOnPlane);
00604   return result;
00605 }
00606 
00607 ////////////////////////////////////////////////////////////////////
00608 //     Function: PhysxUtilLib::ray_sphere_intersect
00609 //       Access: Published
00610 //  Description: Ray-sphere intersection test. Returns true if the
00611 //               ray intersects the sphere, and the impact point if
00612 //               needed.
00613 //
00614 //               \param [in] origin
00615 //               \param [in] dir
00616 //               \param [in] length
00617 //               \param [in] center
00618 //               \param [in] radius
00619 //               \param [out] hit_pos
00620 ////////////////////////////////////////////////////////////////////
00621 bool PhysxUtilLib::
00622 ray_sphere_intersect(const LPoint3f &origin, const LVector3f &dir, float length, const LPoint3f &center, float radius, LPoint3f &hit_pos) {
00623 
00624   nassertr_always(!origin.is_nan(), false);
00625   nassertr_always(!dir.is_nan(), false);
00626   nassertr_always(!center.is_nan(), false);
00627 
00628   NxReal nHitTime; // not used
00629   NxVec3 nPointOnPlane;
00630 
00631   bool result = _ptr->NxRaySphereIntersect(
00632     PhysxManager::point3_to_nxVec3(origin),
00633     PhysxManager::vec3_to_nxVec3(dir),
00634     length,
00635     PhysxManager::point3_to_nxVec3(center),
00636     radius,
00637     nHitTime,
00638     nPointOnPlane);
00639 
00640   PhysxManager::update_point3_from_nxVec3(hit_pos, nPointOnPlane);
00641   return result;
00642 }
00643 
00644 ////////////////////////////////////////////////////////////////////
00645 //     Function: PhysxUtilLib::segment_box_intersect
00646 //       Access: Published
00647 //  Description: Segment-AABB intersection test. Also computes
00648 //               intersection point.
00649 //
00650 //               \param [in] p1
00651 //               \param [in] p2
00652 //               \param [in] bbox_min
00653 //               \param [in] bbox_max
00654 //               \param [out] intercept
00655 ////////////////////////////////////////////////////////////////////
00656 bool PhysxUtilLib::
00657 segment_box_intersect(const LPoint3f &p1, const LPoint3f &p2, const LPoint3f &bbox_min, const LPoint3f &bbox_max, LPoint3f &intercept) {
00658 
00659   nassertr_always(!p1.is_nan(), false);
00660   nassertr_always(!p2.is_nan(), false);
00661   nassertr_always(!bbox_min.is_nan(), false);
00662   nassertr_always(!bbox_max.is_nan(), false);
00663 
00664   NxVec3 nIntercept;
00665 
00666   bool result =_ptr->NxSegmentBoxIntersect(
00667     PhysxManager::point3_to_nxVec3(p1),
00668     PhysxManager::point3_to_nxVec3(p2),
00669     PhysxManager::point3_to_nxVec3(bbox_min),
00670     PhysxManager::point3_to_nxVec3(bbox_max),
00671     nIntercept);
00672 
00673   PhysxManager::update_point3_from_nxVec3(intercept, nIntercept);
00674   return result;
00675 }
00676 
00677 ////////////////////////////////////////////////////////////////////
00678 //     Function: PhysxUtilLib::ray_aabb_intersect
00679 //       Access: Published
00680 //  Description: Ray-AABB intersection test. Also computes
00681 //               intersection point.
00682 //
00683 //               \param [in] min
00684 //               \param [in] max
00685 //               \param [in] origin
00686 //               \param [in] dir
00687 //               \param [out] coord
00688 ////////////////////////////////////////////////////////////////////
00689 bool PhysxUtilLib::
00690 ray_aabb_intersect(const LPoint3f &min, const LPoint3f &max, const LPoint3f &origin, const LVector3f &dir, LPoint3f &coord) {
00691 
00692   nassertr_always(!min.is_nan(), false);
00693   nassertr_always(!max.is_nan(), false);
00694   nassertr_always(!origin.is_nan(), false);
00695   nassertr_always(!dir.is_nan(), false);
00696 
00697   NxVec3 nCoord;
00698 
00699   bool result = _ptr->NxRayAABBIntersect(
00700     PhysxManager::point3_to_nxVec3(min),
00701     PhysxManager::point3_to_nxVec3(max),
00702     PhysxManager::point3_to_nxVec3(origin),
00703     PhysxManager::vec3_to_nxVec3(dir),
00704     nCoord);
00705 
00706   PhysxManager::update_point3_from_nxVec3(coord, nCoord);
00707   return result;
00708 }
00709 
00710 ////////////////////////////////////////////////////////////////////
00711 //     Function: PhysxUtilLib::segment_obb_intersect
00712 //       Access: Published
00713 //  Description: Boolean segment-OBB intersection test. Based on
00714 //               separating axis theorem.
00715 //
00716 //               \param [in] p0
00717 //               \param [in] p1
00718 //               \param [in] center
00719 //               \param [in] extents
00720 //               \param [in] rot
00721 ////////////////////////////////////////////////////////////////////
00722 bool PhysxUtilLib::
00723 segment_obb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot) {
00724 
00725   nassertr_always(!p0.is_nan(), false);
00726   nassertr_always(!p1.is_nan(), false);
00727   nassertr_always(!center.is_nan(), false);
00728   nassertr_always(!extents.is_nan(), false);
00729   nassertr_always(!rot.is_nan(), false);
00730 
00731   return _ptr->NxSegmentOBBIntersect(
00732     PhysxManager::point3_to_nxVec3(p0),
00733     PhysxManager::point3_to_nxVec3(p1),
00734     PhysxManager::point3_to_nxVec3(center),
00735     PhysxManager::vec3_to_nxVec3(extents),
00736     PhysxManager::mat3_to_nxMat33(rot));
00737 }
00738 
00739 ////////////////////////////////////////////////////////////////////
00740 //     Function: PhysxUtilLib::segment_aabb_intersect
00741 //       Access: Published
00742 //  Description: Boolean segment-AABB intersection test. Based on
00743 //               separating axis theorem.
00744 //
00745 //               \param [in] p0
00746 //               \param [in] p1
00747 //               \param [in] min
00748 //               \param [in] max
00749 ////////////////////////////////////////////////////////////////////
00750 bool PhysxUtilLib::
00751 segment_aabb_intersect(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &min, const LPoint3f &max) {
00752 
00753   nassertr_always(!p0.is_nan(), false);
00754   nassertr_always(!p1.is_nan(), false);
00755   nassertr_always(!min.is_nan(), false);
00756   nassertr_always(!max.is_nan(), false);
00757 
00758   return _ptr->NxSegmentAABBIntersect(
00759     PhysxManager::point3_to_nxVec3(p0),
00760     PhysxManager::point3_to_nxVec3(p1),
00761     PhysxManager::point3_to_nxVec3(min),
00762     PhysxManager::point3_to_nxVec3(max));
00763 }
00764 
00765 ////////////////////////////////////////////////////////////////////
00766 //     Function: PhysxUtilLib::ray_obb_intersect
00767 //       Access: Published
00768 //  Description: Boolean ray-OBB intersection test. Based on
00769 //               separating axis theorem.
00770 //
00771 //               \param [in] ray
00772 //               \param [in] center
00773 //               \param [in] extents
00774 //               \param [in] rot
00775 ////////////////////////////////////////////////////////////////////
00776 bool PhysxUtilLib::
00777 ray_obb_intersect(const PhysxRay &ray, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot) {
00778 
00779   nassertr_always(!center.is_nan(), false);
00780   nassertr_always(!extents.is_nan(), false);
00781   nassertr_always(!rot.is_nan(), false);
00782 
00783   return _ptr->NxRayOBBIntersect(
00784     ray._ray,
00785     PhysxManager::point3_to_nxVec3(center),
00786     PhysxManager::point3_to_nxVec3(extents),
00787     PhysxManager::mat3_to_nxMat33(rot));
00788 }
00789 
00790 ////////////////////////////////////////////////////////////////////
00791 //     Function: PhysxUtilLib::ray_capsule_intersect
00792 //       Access: Published
00793 //  Description: Ray-capsule intersection test. Returns number of
00794 //               intersection points (0,1 or 2) along the ray.
00795 //
00796 //               \param [in] origin
00797 //               \param [in] dir
00798 //               \param [in] capsule
00799 ////////////////////////////////////////////////////////////////////
00800 unsigned int PhysxUtilLib::
00801 ray_capsule_intersect(const LPoint3f &origin, const LVector3f &dir, const PhysxCapsule &capsule) {
00802 
00803   nassertr_always(!origin.is_nan(), -1);
00804   nassertr_always(!dir.is_nan(), -1);
00805 
00806   NxReal t[2] = { 0.0f, 0.0f }; // not used
00807 
00808   return _ptr->NxRayCapsuleIntersect(
00809     PhysxManager::point3_to_nxVec3(origin),
00810     PhysxManager::vec3_to_nxVec3(dir),
00811     capsule._capsule, t);
00812 }
00813 
00814 ////////////////////////////////////////////////////////////////////
00815 //     Function: PhysxUtilLib::swept_spheres_intersect
00816 //       Access: Published
00817 //  Description: Sphere-sphere sweep test. Returns true if spheres
00818 //               intersect during their linear motion along
00819 //               provided velocity vectors.
00820 //
00821 //               \param [in] sphere0
00822 //               \param [in] velocity0
00823 //               \param [in] sphere1
00824 //               \param [in] velocity1
00825 ////////////////////////////////////////////////////////////////////
00826 bool PhysxUtilLib::
00827 swept_spheres_intersect(const PhysxSphere &sphere0, const LVector3f &velocity0, const PhysxSphere &sphere1, const LVector3f &velocity1) {
00828 
00829   nassertr_always(!velocity0.is_nan(), false);
00830   nassertr_always(!velocity1.is_nan(), false);
00831 
00832   return _ptr->NxSweptSpheresIntersect(
00833     sphere0._sphere,
00834     PhysxManager::vec3_to_nxVec3(velocity0),
00835     sphere1._sphere,
00836     PhysxManager::vec3_to_nxVec3(velocity1));
00837 }
00838 
00839 ////////////////////////////////////////////////////////////////////
00840 //     Function: PhysxUtilLib::ray_tri_intersect
00841 //       Access: Published
00842 //  Description: Ray-triangle intersection test. Returns impact 
00843 //               distance (t) as well as barycentric coordinates
00844 //               (u,v) of impact point. The test performs back face
00845 //               culling or not according to 'cull'.
00846 //
00847 //               \param [in] orig
00848 //               \param [in] dir
00849 //               \param [in] vert0
00850 //               \param [in] vert1
00851 //               \param [in] vert2
00852 //               \param [out] hit, with coordinates (t,u,v)
00853 //               \param [in] cull
00854 ////////////////////////////////////////////////////////////////////
00855 bool PhysxUtilLib::
00856 ray_tri_intersect(const LPoint3f &orig, const LVector3f &dir, const LPoint3f &vert0, const LPoint3f &vert1, const LPoint3f &vert2, LVector3f &hit, bool cull) {
00857 
00858   nassertr_always(!orig.is_nan(), false);
00859   nassertr_always(!dir.is_nan(), false);
00860   nassertr_always(!vert0.is_nan(), false);
00861   nassertr_always(!vert1.is_nan(), false);
00862   nassertr_always(!vert2.is_nan(), false);
00863 
00864   NxReal t, u, v;
00865 
00866   bool result = _ptr->NxRayTriIntersect(
00867     PhysxManager::point3_to_nxVec3(orig),
00868     PhysxManager::vec3_to_nxVec3(dir),
00869     PhysxManager::point3_to_nxVec3(vert0),
00870     PhysxManager::point3_to_nxVec3(vert1),
00871     PhysxManager::point3_to_nxVec3(vert2),
00872     t, u, v, cull);
00873 
00874   hit.set_x(t);
00875   hit.set_y(u);
00876   hit.set_z(v);
00877 
00878   return result;
00879 }
00880 
00881 ////////////////////////////////////////////////////////////////////
00882 //     Function: PhysxUtilLib::sweep_box_capsule
00883 //       Access: Published
00884 //  Description: Box-vs-capsule sweep test. Sweeps a box against a
00885 //               capsule, returns true if box hit the capsule. Also
00886 //               returns contact information.
00887 //
00888 //               \param [in] box Box
00889 //               \param [in] lss Capsule
00890 //               \param [in] dir Unit-length sweep direction
00891 //               \param [in] length Length of sweep
00892 //               \param [out] normal Normal at impact point
00893 ////////////////////////////////////////////////////////////////////
00894 bool PhysxUtilLib::
00895 sweep_box_capsule(const PhysxBox &box, const PhysxCapsule &lss, const LVector3f &dir, float length, LVector3f &normal) {
00896 
00897   nassertr_always(!dir.is_nan(), false);
00898 
00899   NxReal min_dist; // not used
00900   NxVec3 nNormal;
00901 
00902   bool result = _ptr->NxSweepBoxCapsule(
00903     box._box, lss._capsule,
00904     PhysxManager::vec3_to_nxVec3(dir),
00905     length, min_dist, nNormal);
00906 
00907   PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
00908   return result;
00909 }
00910 
00911 ////////////////////////////////////////////////////////////////////
00912 //     Function: PhysxUtilLib::sweep_box_sphere
00913 //       Access: Published
00914 //  Description: Box-vs-sphere sweep test. Sweeps a box against a
00915 //               sphere, returns true if box hit the sphere. Also
00916 //               returns contact information.
00917 //
00918 //               \param [in] box Box
00919 //               \param [in] sphere Sphere
00920 //               \param [in] dir Unit-length sweep direction
00921 //               \param [in] length Length of sweep
00922 //               \param [out] normal Normal at impact point
00923 ////////////////////////////////////////////////////////////////////
00924 bool PhysxUtilLib::
00925 sweep_box_sphere(const PhysxBox &box, const PhysxSphere &sphere, const LVector3f &dir, float length, LVector3f &normal) {
00926 
00927   nassertr_always(!dir.is_nan(), false);
00928 
00929   NxReal min_dist; // not used
00930   NxVec3 nNormal;
00931 
00932   bool result = _ptr->NxSweepBoxSphere(
00933     box._box, sphere._sphere,
00934     PhysxManager::vec3_to_nxVec3(dir),
00935     length, min_dist, nNormal);
00936 
00937   PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
00938   return result;
00939 }
00940 
00941 ////////////////////////////////////////////////////////////////////
00942 //     Function: PhysxUtilLib::sweep_capsule_capsule
00943 //       Access: Published
00944 //  Description: Capsule-vs-capsule sweep test. Sweeps a capsule
00945 //               against a capsule, returns true if capsule hit the
00946 //               other capsule. Also returns contact information.
00947 //
00948 //               \param [in] lss0
00949 //               \param [in] lss1
00950 //               \param [in] dir Unit-length sweep direction
00951 //               \param [in] length Length of sweep
00952 //               \param [out] ip Impact point
00953 //               \param [out] normal Normal at impact point
00954 ////////////////////////////////////////////////////////////////////
00955 bool PhysxUtilLib::
00956 sweep_capsule_capsule(const PhysxCapsule &lss0, const PhysxCapsule &lss1, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal) {
00957 
00958   nassertr_always(!dir.is_nan(), false);
00959 
00960   NxReal min_dist; // not used
00961   NxVec3 nIp;
00962   NxVec3 nNormal;
00963 
00964   bool result = _ptr->NxSweepCapsuleCapsule(
00965     lss0._capsule, lss1._capsule,
00966     PhysxManager::vec3_to_nxVec3(dir),
00967     length, min_dist, nIp, nNormal);
00968 
00969   PhysxManager::update_point3_from_nxVec3(ip, nIp);
00970   PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
00971   return result;
00972 }
00973 
00974 ////////////////////////////////////////////////////////////////////
00975 //     Function: PhysxUtilLib::sweep_sphere_capsule
00976 //       Access: Published
00977 //  Description: Sphere-vs-capsule sweep test. Sweeps a sphere
00978 //               against a capsule, returns true if sphere hit the
00979 //               capsule. Also returns contact information.
00980 //
00981 //               \param [in] sphere
00982 //               \param [in] lss
00983 //               \param [in] dir Unit-length sweep direction
00984 //               \param [in] length Length of sweep
00985 //               \param [out] ip Impact point
00986 //               \param [out] normal Normal at impact point
00987 ////////////////////////////////////////////////////////////////////
00988 bool PhysxUtilLib::
00989 sweep_sphere_capsule(const PhysxSphere &sphere, const PhysxCapsule &lss, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal) {
00990 
00991   nassertr_always(!dir.is_nan(), false);
00992 
00993   NxReal min_dist; // not used
00994   NxVec3 nIp;
00995   NxVec3 nNormal;
00996 
00997   bool result = _ptr->NxSweepSphereCapsule(
00998     sphere._sphere, lss._capsule,
00999     PhysxManager::vec3_to_nxVec3(dir),
01000     length, min_dist, nIp, nNormal);
01001 
01002   PhysxManager::update_point3_from_nxVec3(ip, nIp);
01003   PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
01004   return result;
01005 }
01006 
01007 ////////////////////////////////////////////////////////////////////
01008 //     Function: PhysxUtilLib::sweep_box_box
01009 //       Access: Published
01010 //  Description: Box-vs-box sweep test. Sweeps a box against a box,
01011 //               returns true if box hit the other box. Also returns
01012 //               contact information.
01013 //
01014 //               \param [in] box0
01015 //               \param [in] box1
01016 //               \param [in] dir Unit-length sweep direction
01017 //               \param [in] length Length of sweep
01018 //               \param [out] ip Impact point
01019 //               \param [out] normal Normal at impact point
01020 ////////////////////////////////////////////////////////////////////
01021 bool PhysxUtilLib::
01022 sweep_box_box(const PhysxBox &box0, const PhysxBox &box1, const LVector3f &dir, float length, LPoint3f &ip, LVector3f &normal) {
01023 
01024   nassertr_always(!dir.is_nan(), false);
01025 
01026   NxReal min_dist; // not used
01027   NxVec3 nIp;
01028   NxVec3 nNormal;
01029 
01030   bool result = _ptr->NxSweepBoxBox(
01031     box0._box, box1._box,
01032     PhysxManager::vec3_to_nxVec3(dir),
01033     length, nIp, nNormal, min_dist);
01034 
01035   PhysxManager::update_point3_from_nxVec3(ip, nIp);
01036   PhysxManager::update_vec3_from_nxVec3(normal, nNormal);
01037   return result;
01038 }
01039 
01040 ////////////////////////////////////////////////////////////////////
01041 //     Function: PhysxUtilLib::point_obb_sqr_dist
01042 //       Access: Published
01043 //  Description: Point-vs-OBB distance computation. Returns distance
01044 //               between a point and an OBB.
01045 //
01046 //               \param [in] point The point
01047 //               \param [in] center OBB center
01048 //               \param [in] extents OBB extents
01049 //               \param [in] rot OBB rotation
01050 //               \param [out] params Closest point on the box, in box space
01051 ////////////////////////////////////////////////////////////////////
01052 float PhysxUtilLib::
01053 point_obb_sqr_dist(const LPoint3f &point, const LPoint3f &center, const LVector3f &extents, const LMatrix3f &rot, LPoint3f &params) {
01054 
01055   nassertr_always(!point.is_nan(), 0.0f);
01056   nassertr_always(!center.is_nan(), 0.0f);
01057   nassertr_always(!extents.is_nan(), 0.0f);
01058   nassertr_always(!rot.is_nan(), 0.0f);
01059 
01060   NxVec3 nParams;
01061 
01062   float result = _ptr->NxPointOBBSqrDist(
01063     PhysxManager::point3_to_nxVec3(point),
01064     PhysxManager::point3_to_nxVec3(center),
01065     PhysxManager::vec3_to_nxVec3(extents),
01066     PhysxManager::mat3_to_nxMat33(rot),
01067     &nParams);
01068 
01069   PhysxManager::update_point3_from_nxVec3(params, nParams);
01070   return result;
01071 }
01072 
 All Classes Functions Variables Enumerations