From 10a64db343419275a9bb09220d6e16fa17239c72 Mon Sep 17 00:00:00 2001 From: enn0x Date: Sat, 14 May 2011 07:00:02 +0000 Subject: [PATCH] Adding bullet physics. --- direct/src/ffi/panda3d.py | 1 + direct/src/p3d/panda3d.pdef | 11 + dtool/src/parser-inc/btBulletDynamicsCommon.h | 118 ++ makepanda/makepanda.py | 51 +- panda/metalibs/pandabullet/Sources.pp | 20 + panda/metalibs/pandabullet/pandabullet.cxx | 27 + panda/metalibs/pandabullet/pandabullet.h | 13 + panda/src/bullet/Sources.pp | 222 ++++ panda/src/bullet/bulletAllHitsRayResult.I | 42 + panda/src/bullet/bulletAllHitsRayResult.cxx | 166 +++ panda/src/bullet/bulletAllHitsRayResult.h | 82 ++ panda/src/bullet/bulletBodyNode.I | 205 ++++ panda/src/bullet/bulletBodyNode.cxx | 640 ++++++++++ panda/src/bullet/bulletBodyNode.h | 152 +++ panda/src/bullet/bulletBoxShape.I | 26 + panda/src/bullet/bulletBoxShape.cxx | 84 ++ panda/src/bullet/bulletBoxShape.h | 72 ++ panda/src/bullet/bulletCapsuleShape.I | 47 + panda/src/bullet/bulletCapsuleShape.cxx | 55 + panda/src/bullet/bulletCapsuleShape.h | 67 ++ .../bullet/bulletCharacterControllerNode.I | 47 + .../bullet/bulletCharacterControllerNode.cxx | 434 +++++++ .../bullet/bulletCharacterControllerNode.h | 119 ++ panda/src/bullet/bulletClosestHitRayResult.I | 30 + .../src/bullet/bulletClosestHitRayResult.cxx | 125 ++ panda/src/bullet/bulletClosestHitRayResult.h | 59 + .../src/bullet/bulletClosestHitSweepResult.I | 30 + .../bullet/bulletClosestHitSweepResult.cxx | 125 ++ .../src/bullet/bulletClosestHitSweepResult.h | 59 + panda/src/bullet/bulletConeShape.I | 47 + panda/src/bullet/bulletConeShape.cxx | 55 + panda/src/bullet/bulletConeShape.h | 67 ++ panda/src/bullet/bulletConeTwistConstraint.I | 25 + .../src/bullet/bulletConeTwistConstraint.cxx | 95 ++ panda/src/bullet/bulletConeTwistConstraint.h | 76 ++ panda/src/bullet/bulletConstraint.I | 24 + panda/src/bullet/bulletConstraint.cxx | 85 ++ panda/src/bullet/bulletConstraint.h | 71 ++ panda/src/bullet/bulletContactCallbacks.h | 128 ++ panda/src/bullet/bulletContactResult.I | 115 ++ panda/src/bullet/bulletContactResult.cxx | 54 + panda/src/bullet/bulletContactResult.h | 81 ++ panda/src/bullet/bulletConvexHullShape.I | 25 + panda/src/bullet/bulletConvexHullShape.cxx | 102 ++ panda/src/bullet/bulletConvexHullShape.h | 71 ++ .../src/bullet/bulletConvexPointCloudShape.I | 36 + .../bullet/bulletConvexPointCloudShape.cxx | 91 ++ .../src/bullet/bulletConvexPointCloudShape.h | 69 ++ panda/src/bullet/bulletCylinderShape.I | 58 + panda/src/bullet/bulletCylinderShape.cxx | 83 ++ panda/src/bullet/bulletCylinderShape.h | 69 ++ panda/src/bullet/bulletDebugNode.I | 47 + panda/src/bullet/bulletDebugNode.cxx | 392 +++++++ panda/src/bullet/bulletDebugNode.h | 135 +++ panda/src/bullet/bulletDistanceConstraint.I | 25 + panda/src/bullet/bulletDistanceConstraint.cxx | 111 ++ panda/src/bullet/bulletDistanceConstraint.h | 79 ++ panda/src/bullet/bulletGenericConstraint.I | 25 + panda/src/bullet/bulletGenericConstraint.cxx | 115 ++ panda/src/bullet/bulletGenericConstraint.h | 82 ++ panda/src/bullet/bulletGhostNode.I | 51 + panda/src/bullet/bulletGhostNode.cxx | 113 ++ panda/src/bullet/bulletGhostNode.h | 83 ++ panda/src/bullet/bulletHeightfieldShape.I | 26 + panda/src/bullet/bulletHeightfieldShape.cxx | 68 ++ panda/src/bullet/bulletHeightfieldShape.h | 69 ++ panda/src/bullet/bulletHelper.I | 42 + panda/src/bullet/bulletHelper.cxx | 307 +++++ panda/src/bullet/bulletHelper.h | 70 ++ panda/src/bullet/bulletHingeConstraint.I | 25 + panda/src/bullet/bulletHingeConstraint.cxx | 158 +++ panda/src/bullet/bulletHingeConstraint.h | 88 ++ panda/src/bullet/bulletManifoldPoint.I | 24 + panda/src/bullet/bulletManifoldPoint.cxx | 103 ++ panda/src/bullet/bulletManifoldPoint.h | 51 + panda/src/bullet/bulletPersistentManifold.I | 24 + panda/src/bullet/bulletPersistentManifold.cxx | 110 ++ panda/src/bullet/bulletPersistentManifold.h | 56 + panda/src/bullet/bulletPlaneShape.I | 47 + panda/src/bullet/bulletPlaneShape.cxx | 57 + panda/src/bullet/bulletPlaneShape.h | 72 ++ panda/src/bullet/bulletRigidBodyNode.I | 71 ++ panda/src/bullet/bulletRigidBodyNode.cxx | 418 +++++++ panda/src/bullet/bulletRigidBodyNode.h | 127 ++ panda/src/bullet/bulletShape.I | 101 ++ panda/src/bullet/bulletShape.cxx | 75 ++ panda/src/bullet/bulletShape.h | 76 ++ panda/src/bullet/bulletSliderConstraint.I | 25 + panda/src/bullet/bulletSliderConstraint.cxx | 157 +++ panda/src/bullet/bulletSliderConstraint.h | 85 ++ panda/src/bullet/bulletSoftBodyConfig.I | 556 +++++++++ panda/src/bullet/bulletSoftBodyConfig.cxx | 86 ++ panda/src/bullet/bulletSoftBodyConfig.h | 115 ++ panda/src/bullet/bulletSoftBodyMaterial.I | 115 ++ panda/src/bullet/bulletSoftBodyMaterial.cxx | 26 + panda/src/bullet/bulletSoftBodyMaterial.h | 51 + panda/src/bullet/bulletSoftBodyNode.I | 116 ++ panda/src/bullet/bulletSoftBodyNode.cxx | 1039 +++++++++++++++++ panda/src/bullet/bulletSoftBodyNode.h | 232 ++++ panda/src/bullet/bulletSoftBodyShape.I | 28 + panda/src/bullet/bulletSoftBodyShape.cxx | 59 + panda/src/bullet/bulletSoftBodyShape.h | 68 ++ panda/src/bullet/bulletSoftBodyWorldInfo.I | 35 + panda/src/bullet/bulletSoftBodyWorldInfo.cxx | 149 +++ panda/src/bullet/bulletSoftBodyWorldInfo.h | 59 + panda/src/bullet/bulletSphereShape.I | 36 + panda/src/bullet/bulletSphereShape.cxx | 52 + panda/src/bullet/bulletSphereShape.h | 70 ++ panda/src/bullet/bulletTriangleMesh.I | 48 + panda/src/bullet/bulletTriangleMesh.cxx | 220 ++++ panda/src/bullet/bulletTriangleMesh.h | 91 ++ panda/src/bullet/bulletTriangleMeshShape.I | 53 + panda/src/bullet/bulletTriangleMeshShape.cxx | 91 ++ panda/src/bullet/bulletTriangleMeshShape.h | 75 ++ panda/src/bullet/bulletVehicle.I | 191 +++ panda/src/bullet/bulletVehicle.cxx | 245 ++++ panda/src/bullet/bulletVehicle.h | 128 ++ panda/src/bullet/bulletWheel.I | 39 + panda/src/bullet/bulletWheel.cxx | 547 +++++++++ panda/src/bullet/bulletWheel.h | 97 ++ panda/src/bullet/bulletWorld.I | 242 ++++ panda/src/bullet/bulletWorld.cxx | 736 ++++++++++++ panda/src/bullet/bulletWorld.h | 221 ++++ panda/src/bullet/bullet_composite.cxx | 40 + panda/src/bullet/bullet_includes.h | 38 + panda/src/bullet/bullet_utils.I | 15 + panda/src/bullet/bullet_utils.cxx | 182 +++ panda/src/bullet/bullet_utils.h | 61 + panda/src/bullet/config_bullet.cxx | 185 +++ panda/src/bullet/config_bullet.h | 45 + panda/src/pandabase/pandasymbols.h | 12 +- 131 files changed, 14937 insertions(+), 3 deletions(-) create mode 100644 dtool/src/parser-inc/btBulletDynamicsCommon.h create mode 100644 panda/metalibs/pandabullet/Sources.pp create mode 100644 panda/metalibs/pandabullet/pandabullet.cxx create mode 100644 panda/metalibs/pandabullet/pandabullet.h create mode 100644 panda/src/bullet/Sources.pp create mode 100644 panda/src/bullet/bulletAllHitsRayResult.I create mode 100644 panda/src/bullet/bulletAllHitsRayResult.cxx create mode 100644 panda/src/bullet/bulletAllHitsRayResult.h create mode 100644 panda/src/bullet/bulletBodyNode.I create mode 100644 panda/src/bullet/bulletBodyNode.cxx create mode 100644 panda/src/bullet/bulletBodyNode.h create mode 100644 panda/src/bullet/bulletBoxShape.I create mode 100644 panda/src/bullet/bulletBoxShape.cxx create mode 100644 panda/src/bullet/bulletBoxShape.h create mode 100644 panda/src/bullet/bulletCapsuleShape.I create mode 100644 panda/src/bullet/bulletCapsuleShape.cxx create mode 100644 panda/src/bullet/bulletCapsuleShape.h create mode 100644 panda/src/bullet/bulletCharacterControllerNode.I create mode 100644 panda/src/bullet/bulletCharacterControllerNode.cxx create mode 100644 panda/src/bullet/bulletCharacterControllerNode.h create mode 100644 panda/src/bullet/bulletClosestHitRayResult.I create mode 100644 panda/src/bullet/bulletClosestHitRayResult.cxx create mode 100644 panda/src/bullet/bulletClosestHitRayResult.h create mode 100644 panda/src/bullet/bulletClosestHitSweepResult.I create mode 100644 panda/src/bullet/bulletClosestHitSweepResult.cxx create mode 100644 panda/src/bullet/bulletClosestHitSweepResult.h create mode 100644 panda/src/bullet/bulletConeShape.I create mode 100644 panda/src/bullet/bulletConeShape.cxx create mode 100644 panda/src/bullet/bulletConeShape.h create mode 100644 panda/src/bullet/bulletConeTwistConstraint.I create mode 100644 panda/src/bullet/bulletConeTwistConstraint.cxx create mode 100644 panda/src/bullet/bulletConeTwistConstraint.h create mode 100644 panda/src/bullet/bulletConstraint.I create mode 100644 panda/src/bullet/bulletConstraint.cxx create mode 100644 panda/src/bullet/bulletConstraint.h create mode 100644 panda/src/bullet/bulletContactCallbacks.h create mode 100644 panda/src/bullet/bulletContactResult.I create mode 100644 panda/src/bullet/bulletContactResult.cxx create mode 100644 panda/src/bullet/bulletContactResult.h create mode 100644 panda/src/bullet/bulletConvexHullShape.I create mode 100644 panda/src/bullet/bulletConvexHullShape.cxx create mode 100644 panda/src/bullet/bulletConvexHullShape.h create mode 100644 panda/src/bullet/bulletConvexPointCloudShape.I create mode 100644 panda/src/bullet/bulletConvexPointCloudShape.cxx create mode 100644 panda/src/bullet/bulletConvexPointCloudShape.h create mode 100644 panda/src/bullet/bulletCylinderShape.I create mode 100644 panda/src/bullet/bulletCylinderShape.cxx create mode 100644 panda/src/bullet/bulletCylinderShape.h create mode 100644 panda/src/bullet/bulletDebugNode.I create mode 100644 panda/src/bullet/bulletDebugNode.cxx create mode 100644 panda/src/bullet/bulletDebugNode.h create mode 100644 panda/src/bullet/bulletDistanceConstraint.I create mode 100644 panda/src/bullet/bulletDistanceConstraint.cxx create mode 100644 panda/src/bullet/bulletDistanceConstraint.h create mode 100644 panda/src/bullet/bulletGenericConstraint.I create mode 100644 panda/src/bullet/bulletGenericConstraint.cxx create mode 100644 panda/src/bullet/bulletGenericConstraint.h create mode 100644 panda/src/bullet/bulletGhostNode.I create mode 100644 panda/src/bullet/bulletGhostNode.cxx create mode 100644 panda/src/bullet/bulletGhostNode.h create mode 100644 panda/src/bullet/bulletHeightfieldShape.I create mode 100644 panda/src/bullet/bulletHeightfieldShape.cxx create mode 100644 panda/src/bullet/bulletHeightfieldShape.h create mode 100644 panda/src/bullet/bulletHelper.I create mode 100644 panda/src/bullet/bulletHelper.cxx create mode 100644 panda/src/bullet/bulletHelper.h create mode 100644 panda/src/bullet/bulletHingeConstraint.I create mode 100644 panda/src/bullet/bulletHingeConstraint.cxx create mode 100644 panda/src/bullet/bulletHingeConstraint.h create mode 100644 panda/src/bullet/bulletManifoldPoint.I create mode 100644 panda/src/bullet/bulletManifoldPoint.cxx create mode 100644 panda/src/bullet/bulletManifoldPoint.h create mode 100644 panda/src/bullet/bulletPersistentManifold.I create mode 100644 panda/src/bullet/bulletPersistentManifold.cxx create mode 100644 panda/src/bullet/bulletPersistentManifold.h create mode 100644 panda/src/bullet/bulletPlaneShape.I create mode 100644 panda/src/bullet/bulletPlaneShape.cxx create mode 100644 panda/src/bullet/bulletPlaneShape.h create mode 100644 panda/src/bullet/bulletRigidBodyNode.I create mode 100644 panda/src/bullet/bulletRigidBodyNode.cxx create mode 100644 panda/src/bullet/bulletRigidBodyNode.h create mode 100644 panda/src/bullet/bulletShape.I create mode 100644 panda/src/bullet/bulletShape.cxx create mode 100644 panda/src/bullet/bulletShape.h create mode 100644 panda/src/bullet/bulletSliderConstraint.I create mode 100644 panda/src/bullet/bulletSliderConstraint.cxx create mode 100644 panda/src/bullet/bulletSliderConstraint.h create mode 100644 panda/src/bullet/bulletSoftBodyConfig.I create mode 100644 panda/src/bullet/bulletSoftBodyConfig.cxx create mode 100644 panda/src/bullet/bulletSoftBodyConfig.h create mode 100644 panda/src/bullet/bulletSoftBodyMaterial.I create mode 100644 panda/src/bullet/bulletSoftBodyMaterial.cxx create mode 100644 panda/src/bullet/bulletSoftBodyMaterial.h create mode 100644 panda/src/bullet/bulletSoftBodyNode.I create mode 100644 panda/src/bullet/bulletSoftBodyNode.cxx create mode 100644 panda/src/bullet/bulletSoftBodyNode.h create mode 100644 panda/src/bullet/bulletSoftBodyShape.I create mode 100644 panda/src/bullet/bulletSoftBodyShape.cxx create mode 100644 panda/src/bullet/bulletSoftBodyShape.h create mode 100644 panda/src/bullet/bulletSoftBodyWorldInfo.I create mode 100644 panda/src/bullet/bulletSoftBodyWorldInfo.cxx create mode 100644 panda/src/bullet/bulletSoftBodyWorldInfo.h create mode 100644 panda/src/bullet/bulletSphereShape.I create mode 100644 panda/src/bullet/bulletSphereShape.cxx create mode 100644 panda/src/bullet/bulletSphereShape.h create mode 100644 panda/src/bullet/bulletTriangleMesh.I create mode 100644 panda/src/bullet/bulletTriangleMesh.cxx create mode 100644 panda/src/bullet/bulletTriangleMesh.h create mode 100644 panda/src/bullet/bulletTriangleMeshShape.I create mode 100644 panda/src/bullet/bulletTriangleMeshShape.cxx create mode 100644 panda/src/bullet/bulletTriangleMeshShape.h create mode 100644 panda/src/bullet/bulletVehicle.I create mode 100644 panda/src/bullet/bulletVehicle.cxx create mode 100644 panda/src/bullet/bulletVehicle.h create mode 100644 panda/src/bullet/bulletWheel.I create mode 100644 panda/src/bullet/bulletWheel.cxx create mode 100644 panda/src/bullet/bulletWheel.h create mode 100644 panda/src/bullet/bulletWorld.I create mode 100644 panda/src/bullet/bulletWorld.cxx create mode 100644 panda/src/bullet/bulletWorld.h create mode 100644 panda/src/bullet/bullet_composite.cxx create mode 100644 panda/src/bullet/bullet_includes.h create mode 100644 panda/src/bullet/bullet_utils.I create mode 100644 panda/src/bullet/bullet_utils.cxx create mode 100644 panda/src/bullet/bullet_utils.h create mode 100644 panda/src/bullet/config_bullet.cxx create mode 100644 panda/src/bullet/config_bullet.h diff --git a/direct/src/ffi/panda3d.py b/direct/src/ffi/panda3d.py index 44f7b28ab0..6c9a003263 100644 --- a/direct/src/ffi/panda3d.py +++ b/direct/src/ffi/panda3d.py @@ -9,6 +9,7 @@ panda3d_modules = { "direct" : "libp3direct", "egg" : "libpandaegg", "ode" : "libpandaode", + "bullet" : "libpandabullet", "vision" : "libp3vision", "physx" : "libpandaphysx", "ai" : "libpandaai", diff --git a/direct/src/p3d/panda3d.pdef b/direct/src/p3d/panda3d.pdef index f0131a1a55..431706f079 100755 --- a/direct/src/p3d/panda3d.pdef +++ b/direct/src/p3d/panda3d.pdef @@ -69,6 +69,7 @@ class panda3d(package): # except for skel - this is useless in a shipped game anyways. excludeModule('libpandaegg') excludeModule('libpandaode') + excludeModule('libpandabullet') excludeModule('libp3vision') excludeModule('libpandaskel') excludeModule('libpandaphysx') @@ -264,6 +265,16 @@ class ode(package): file('libpandaode.dll', required = True) +class bullet(package): + # This package contains the code for the Bullet integration. + # As not every application uses the Bullet layers, and to cut down + # the download size, it is provided as separate package. + + config(display_name = "Panda3D Bullet integration") + require('panda3d') + + file('libpandabullet.dll', required = True) + class physx(package): # This package contains the code for the NVIDIA PhysX integration. # As not every application uses the NVIDIA PhysX layers, and to cut down diff --git a/dtool/src/parser-inc/btBulletDynamicsCommon.h b/dtool/src/parser-inc/btBulletDynamicsCommon.h new file mode 100644 index 0000000000..9628556047 --- /dev/null +++ b/dtool/src/parser-inc/btBulletDynamicsCommon.h @@ -0,0 +1,118 @@ + +class bt32BitAxisSweep3; +class btActionInterface; +class btAxisSweep3; +class btBoxShape; +class btBroadphaseInterface; +class btBulletWorldImporter; +class btBvhTriangleMeshShape; +class btCapsuleShape; +class btCapsuleShapeX; +class btCapsuleShapeZ; +class btCharacterControllerInterface; +class btCollisionConfiguration; +class btCollisionDispatcher; +class btCollisionObject; +class btCollisionShape; +class btCompoundShape; +class btConcaveShape; +class btConeShape; +class btConeShapeX; +class btConeShapeZ; +class btConeTwistConstraint; +class btConstraintSolver; +class btContactConstraint; +class btContinuousDynamicsWorld; +class btConvexShape; +class btConvexHullShape; +class btConvexInternalShape; +class btConvexPointCloudShape; +class btCylinderShape; +class btCylinderShapeX; +class btCylinderShapeZ; +class btDefaultCollisionConfiguration; +class btDefaultMotionState; +class btDiscreteDynamicsWorld; +class btDispatcher; +class btDynamicsWorld; +class btEmptyShape; +class btGeneric6DofConstraint; +class btGeneric6DofSpringConstraint; +class btGhostPairCallback; +class btGhostObject; +class btGImpactMeshShape; +class btHeightfieldTerrainShape; +class btHingeConstraint; +class btHinge2Constraint; +class btIDebugDraw; +class btKinematicCharacterController; +class btManifoldPoint; +class btMatrix3x3; +class btMotionState; +class btPairCachingGhostObject; +class btParalleSequentialImpulseSolver; +class btPersistentManifold; +class btPlaneShape; +class btPoint2PointConstraint; +class btPolyhedralConvexShape; +class btQuaternion; +class btSequentialImpulseConstraintSolver; +class btScalar; +class btSliderConstraint; +class btSoftBodyHelpers; +class btSoftBodyRigidBodyCollisionConfiguration; +class btSoftBodyCollisionShape; +class btSoftBodyWorldInfo; +class btSoftRigidDynamicsWorld; +class btSphereShape; +class btStaticPlaneShape; +class btStridingMeshInterface; +class btTransform; +class btTriangleMesh; +class btTypedConstraint; +class btTypedObject; +class btVector3; +class btVehicleRaycaster; +class btWheelInfo; + +class btCollisionWorld { +public: + struct LocalRayResult; + struct ClosestRayResultCallback; + struct RayResultCallback; + struct ContactResultCallback; + struct AllHitsRayResultCallback; + struct ClosestConvexResultCallback; +}; + +class btRaycastVehicle { +public: + class btVehicleTuning; +}; + +class btRigidBody { +public: + class btRigidBodyConstructionInfo; +}; + +class btSoftBody { +public: + struct Config; + struct Joint; + struct Link; + struct Material; + struct Node; +}; + +// BulletCollision/CollisionDispatch/btCollisionObject.h +#define ACTIVE_TAG 1 +#define ISLAND_SLEEPING 2 +#define WANTS_DEACTIVATION 3 +#define DISABLE_DEACTIVATION 4 +#define DISABLE_SIMULATION 5 + +// BulletCollision/CollisionDispatch/btManifoldResult.h +typedef bool (*ContactAddedCallback); +typedef bool (*ContactProcessedCallback); +typedef bool (*ContactDestroyedCallback); + diff --git a/makepanda/makepanda.py b/makepanda/makepanda.py index 065f1bf92a..b2c76df117 100755 --- a/makepanda/makepanda.py +++ b/makepanda/makepanda.py @@ -60,7 +60,7 @@ PkgListSet(["PYTHON", "DIRECT", # Python support "GL", "GLES", "GLES2"] + DXVERSIONS + ["TINYDISPLAY", "NVIDIACG", # 3D graphics "EGL", # OpenGL (ES) integration "OPENAL", "FMODEX", "FFMPEG", # Multimedia - "ODE", "PHYSX", # Physics + "ODE", "PHYSX", "BULLET", # Physics "SPEEDTREE", # SpeedTree "ZLIB", "PNG", "JPEG", "TIFF", "SQUISH", "FREETYPE", # 2D Formats support ] + MAYAVERSIONS + MAXVERSIONS + [ "FCOLLADA", # 3D Formats support @@ -506,6 +506,12 @@ if (COMPILER=="MSVC"): if (SDK["SPEEDTREEAPI"] == "OpenGL"): LibName("SPEEDTREE", "%sglew32.lib" % (libdir)) IncDirectory("SPEEDTREE", SDK["SPEEDTREE"] + "/Include") + if (PkgSkip("BULLET")==0): + LibName("BULLET", GetThirdpartyDir() + "bullet/lib/LinearMath.lib") + LibName("BULLET", GetThirdpartyDir() + "bullet/lib/BulletCollision.lib") + LibName("BULLET", GetThirdpartyDir() + "bullet/lib/BulletDynamics.lib") + LibName("BULLET", GetThirdpartyDir() + "bullet/lib/BulletSoftBody.lib") + LibName("BULLET", GetThirdpartyDir() + "bullet/lib/BulletMultiThreaded.lib") if (COMPILER=="LINUX"): PkgDisable("AWESOMIUM") @@ -670,6 +676,9 @@ if (COMPILER=="LINUX"): LibName("PHYSX", "-lPhysXLoader") LibName("PHYSX", "-lNxCharacter") + #if (PkgSkip("BULLET")==0): + # TODO + DefSymbol("ALWAYS", "MAKEPANDA", "") DefSymbol("WITHINPANDA", "WITHIN_PANDA", "1") IncDirectory("ALWAYS", GetOutputDir()+"/tmp") @@ -744,8 +753,10 @@ def CompileCxx(obj,src,opts): cmd += "/wd4996 /wd4275 /wd4267 /wd4101 /wd4273 " # Enables Windows 7 mode if SDK is detected. + # But only if it is Windows 7 (0x601) and not e. g. Vista (0x600) platsdk = GetRegistryKey("SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\v7.0", "InstallationFolder") - if platsdk and os.path.isdir(platsdk): + winver = sys.getwindowsversion() + if platsdk and os.path.isdir(platsdk) and winver[0] >= 6 and winver[1] >= 1: cmd += "/DPANDA_WIN7 /DWINVER=0x601 " cmd += "/Fo" + obj + " /nologo /c" @@ -1472,6 +1483,7 @@ DTOOL_CONFIG=[ ("_SECURE_SCL", '1', 'UNDEF'), ("_SECURE_SCL_THROWS", '0', 'UNDEF'), ("HAVE_P3D_PLUGIN", 'UNDEF', 'UNDEF'), + ("HAVE_BULLET", 'UNDEF', 'UNDEF'), ] PRC_PARAMETERS=[ @@ -2057,6 +2069,10 @@ if (PkgSkip("PHYSX")==0): CopyAllHeaders('panda/src/physx') CopyAllHeaders('panda/metalibs/pandaphysx') +if (PkgSkip("BULLET")==0): + CopyAllHeaders('panda/src/bullet') + CopyAllHeaders('panda/metalibs/pandabullet') + if (PkgSkip("SPEEDTREE")==0): CopyAllHeaders('panda/src/speedtree') @@ -3450,6 +3466,35 @@ if (PkgSkip("ODE")==0 and not RUNTIME): TargetAdd('libpandaode.dll', input=COMMON_PANDA_LIBS) TargetAdd('libpandaode.dll', opts=['WINUSER', 'ODE']) +# +# DIRECTORY: panda/src/bullet/ +# +if (PkgSkip("BULLET")==0 and not RUNTIME): + OPTS=['DIR:panda/src/bullet', 'BUILDING:PANDABULLET', 'BULLET'] + TargetAdd('bullet_composite.obj', opts=OPTS, input='bullet_composite.cxx') + IGATEFILES=GetDirectoryContents('panda/src/bullet', ["*.h", "*_composite.cxx"]) + TargetAdd('libpandabullet.in', opts=OPTS, input=IGATEFILES) + TargetAdd('libpandabullet.in', opts=['IMOD:pandabullet', 'ILIB:libpandabullet', 'SRCDIR:panda/src/bullet']) + TargetAdd('libpandabullet_igate.obj', input='libpandabullet.in', opts=["DEPENDENCYONLY"]) + +# +# DIRECTORY: panda/metalibs/pandabullet/ +# +if (PkgSkip("BULLET")==0 and not RUNTIME): + OPTS=['DIR:panda/metalibs/pandabullet', 'BUILDING:PANDABULLET', 'BULLET'] + TargetAdd('pandabullet_pandabullet.obj', opts=OPTS, input='pandabullet.cxx') + + TargetAdd('libpandabullet_module.obj', input='libpandabullet.in') + TargetAdd('libpandabullet_module.obj', opts=OPTS) + TargetAdd('libpandabullet_module.obj', opts=['IMOD:pandabullet', 'ILIB:libpandabullet']) + + TargetAdd('libpandabullet.dll', input='pandabullet_pandabullet.obj') + TargetAdd('libpandabullet.dll', input='libpandabullet_module.obj') + TargetAdd('libpandabullet.dll', input='bullet_composite.obj') + TargetAdd('libpandabullet.dll', input='libpandabullet_igate.obj') + TargetAdd('libpandabullet.dll', input=COMMON_PANDA_LIBS) + TargetAdd('libpandabullet.dll', opts=['WINUSER', 'BULLET']) + # # DIRECTORY: panda/src/physx/ # @@ -4869,6 +4914,8 @@ if (PkgSkip("PYTHON")==0 and not RUNTIME): TargetAdd('PandaModules.py', input='libp3awesomium.dll') if (PkgSkip("ODE")==0): TargetAdd('PandaModules.py', input='libpandaode.dll') + if (PkgSkip("BULLET")==0): + TargetAdd('PandaModules.py', input='libpandabullet.dll') # # Generate the models directory and samples directory diff --git a/panda/metalibs/pandabullet/Sources.pp b/panda/metalibs/pandabullet/Sources.pp new file mode 100644 index 0000000000..32d26cb5a8 --- /dev/null +++ b/panda/metalibs/pandabullet/Sources.pp @@ -0,0 +1,20 @@ +// DIR_TYPE "metalib" indicates we are building a shared library that +// consists mostly of references to other shared libraries. Under +// Windows, this directly produces a DLL (as opposed to the regular +// src libraries, which don't produce anything but a pile of OBJ files +// under Windows). + +#define DIR_TYPE metalib +#define BUILD_DIRECTORY $[HAVE_BULLET] +#define BUILDING_DLL BUILDING_PANDABULLET + +#define COMPONENT_LIBS bullet +#define LOCAL_LIBS linmath putil express +#define OTHER_LIBS interrogatedb:c dconfig:c dtoolconfig:m \ + dtoolutil:c dtoolbase:c dtool:m prc:c + +#begin metalib_target + #define TARGET pandabullet + #define SOURCES pandabullet.cxx pandabullet.h + #define INSTALL_HEADERS pandabullet.h +#end metalib_target diff --git a/panda/metalibs/pandabullet/pandabullet.cxx b/panda/metalibs/pandabullet/pandabullet.cxx new file mode 100644 index 0000000000..388df4cb36 --- /dev/null +++ b/panda/metalibs/pandabullet/pandabullet.cxx @@ -0,0 +1,27 @@ +// Filename: pandabullet.cxx +// Created by: enn0x (10May2011) +// +//////////////////////////////////////////////////////////////////// + +#include "pandabullet.h" +#include "config_bullet.h" + +// By including checkPandaVersion.h, we guarantee that runtime +// attempts to load libpandabullet.so/.dll will fail if they +// inadvertently link with the wrong version of libdtool.so/.dll. + +#include "checkPandaVersion.h" + +//////////////////////////////////////////////////////////////////// +// Function: init_libpandabullet +// Description: Initializes the library. This must be called at +// least once before any of the functions or classes in +// this library can be used. Normally it will be +// called by the static initializers and need not be +// called explicitly, but special cases exist. +//////////////////////////////////////////////////////////////////// +void +init_libpandabullet() { + init_libbullet(); +} + diff --git a/panda/metalibs/pandabullet/pandabullet.h b/panda/metalibs/pandabullet/pandabullet.h new file mode 100644 index 0000000000..842db1f646 --- /dev/null +++ b/panda/metalibs/pandabullet/pandabullet.h @@ -0,0 +1,13 @@ +// Filename: pandabullet.h +// Created by: enn0x (10May2011) +// +//////////////////////////////////////////////////////////////////// + +#ifndef PANDABULLET_H +#define PANDABULLET_H + +#include "pandabase.h" + +EXPCL_PANDABULLET void init_libpandabullet(); + +#endif diff --git a/panda/src/bullet/Sources.pp b/panda/src/bullet/Sources.pp new file mode 100644 index 0000000000..ba19a25074 --- /dev/null +++ b/panda/src/bullet/Sources.pp @@ -0,0 +1,222 @@ +#define BUILD_DIRECTORY $[HAVE_BULLET] +#define BUILDING_DLL BUILDING_PANDABULLET + +#define OTHER_LIBS interrogatedb:c dconfig:c dtoolconfig:m dtoolutil:c dtoolbase:c dtool:m prc:c + +#begin lib_target + #define TARGET bullet + #define LOCAL_LIBS pgraph linmath grutil + #define USE_PACKAGES bullet + #define COMBINED_SOURCES $[TARGET]_composite.cxx + + #define SOURCES \ + bulletAllHitsRayResult.I \ + bulletAllHitsRayResult.h \ + bulletBodyNode.I \ + bulletBodyNode.h \ + bulletBoxShape.I \ + bulletBoxShape.h \ + bulletCapsuleShape.I \ + bulletCapsuleShape.h \ + bulletCharacterControllerNode.I \ + bulletCharacterControllerNode.h \ + bulletClosestHitRayResult.I \ + bulletClosestHitRayResult.h \ + bulletClosestHitSweepResult.I \ + bulletClosestHitSweepResult.h \ + bulletConeShape.I \ + bulletConeShape.h \ + bulletConeTwistConstraint.I \ + bulletConeTwistConstraint.h \ + bulletConstraint.I \ + bulletConstraint.h \ + bulletContactCallbacks.h \ + bulletContactResult.I \ + bulletContactResult.h \ + bulletConvexHullShape.I \ + bulletConvexHullShape.h \ + bulletConvexPointCloudShape.I \ + bulletConvexPointCloudShape.h \ + bulletCylinderShape.I \ + bulletCylinderShape.h \ + bulletDebugNode.I \ + bulletDebugNode.h \ + bulletDistanceConstraint.I \ + bulletDistanceConstraint.h \ + bulletGenericConstraint.I \ + bulletGenericConstraint.h \ + bulletGhostNode.I \ + bulletGhostNode.h \ + bulletHeightfieldShape.I \ + bulletHeightfieldShape.h \ + bulletHelper.I \ + bulletHelper.h \ + bulletHingeConstraint.I \ + bulletHingeConstraint.h \ + bulletManifoldPoint.I \ + bulletManifoldPoint.h \ + bulletPersistentManifold.I \ + bulletPersistentManifold.h \ + bulletPlaneShape.I \ + bulletPlaneShape.h \ + bulletRigidBodyNode.I \ + bulletRigidBodyNode.h \ + bulletShape.I \ + bulletShape.h \ + bulletSliderConstraint.I \ + bulletSliderConstraint.h \ + bulletSoftBodyConfig.I \ + bulletSoftBodyConfig.h \ + bulletSoftBodyMaterial.I \ + bulletSoftBodyMaterial.h \ + bulletSoftBodyNode.I \ + bulletSoftBodyNode.h \ + bulletSoftBodyShape.I \ + bulletSoftBodyShape.h \ + bulletSoftBodyWorldInfo.I \ + bulletSoftBodyWorldInfo.h \ + bulletSphereShape.I \ + bulletSphereShape.h \ + bulletTriangleMesh.I \ + bulletTriangleMesh.h \ + bulletTriangleMeshShape.I \ + bulletTriangleMeshShape.h \ + bulletVehicle.I \ + bulletVehicle.h \ + bulletWheel.I \ + bulletWheel.h \ + bulletWorld.I \ + bulletWorld.h \ + bullet_includes.h \ + bullet_utils.I \ + bullet_utils.h \ + config_bullet.h \ + + #define INCLUDED_SOURCES \ + bulletAllHitsRayResult.cxx \ + bulletBodyNode.cxx \ + bulletBoxShape.cxx \ + bulletCapsuleShape.cxx \ + bulletCharacterControllerNode.cxx \ + bulletClosestHitRayResult.cxx \ + bulletClosestHitSweepResult.cxx \ + bulletConeShape.cxx \ + bulletConeTwistConstraint.cxx \ + bulletConstraint.cxx \ + bulletContactResult.cxx \ + bulletConvexHullShape.cxx \ + bulletConvexPointCloudShape.cxx \ + bulletCylinderShape.cxx \ + bulletDebugNode.cxx \ + bulletDistanceConstraint.cxx \ + bulletGenericConstraint.cxx \ + bulletGhostNode.cxx \ + bulletHeightfieldShape.cxx \ + bulletHelper.cxx \ + bulletHingeConstraint.cxx \ + bulletManifoldPoint.cxx \ + bulletPersistentManifold.cxx \ + bulletPlaneShape.cxx \ + bulletRigidBodyNode.cxx \ + bulletShape.cxx \ + bulletSliderConstraint.cxx \ + bulletSoftBodyConfig.cxx \ + bulletSoftBodyMaterial.cxx \ + bulletSoftBodyNode.cxx \ + bulletSoftBodyShape.cxx \ + bulletSoftBodyWorldInfo.cxx \ + bulletSphereShape.cxx \ + bulletTriangleMesh.cxx \ + bulletTriangleMeshShape.cxx \ + bulletVehicle.cxx \ + bulletWheel.cxx \ + bulletWorld.cxx \ + bullet_composite.cxx \ + bullet_utils.cxx \ + config_bullet.cxx \ + + #define INSTALL_HEADERS \ + bulletAllHitsRayResult.I \ + bulletAllHitsRayResult.h \ + bulletBodyNode.I \ + bulletBodyNode.h \ + bulletBoxShape.I \ + bulletBoxShape.h \ + bulletCapsuleShape.I \ + bulletCapsuleShape.h \ + bulletCharacterControllerNode.I \ + bulletCharacterControllerNode.h \ + bulletClosestHitRayResult.I \ + bulletClosestHitRayResult.h \ + bulletClosestHitSweepResult.I \ + bulletClosestHitSweepResult.h \ + bulletConeShape.I \ + bulletConeShape.h \ + bulletConeTwistConstraint.I \ + bulletConeTwistConstraint.h \ + bulletConstraint.I \ + bulletConstraint.h \ + bulletContactCallbacks.h \ + bulletContactResult.I \ + bulletContactResult.h \ + bulletConvexHullShape.I \ + bulletConvexHullShape.h \ + bulletConvexPointCloudShape.I \ + bulletConvexPointCloudShape.h \ + bulletCylinderShape.I \ + bulletCylinderShape.h \ + bulletDebugNode.I \ + bulletDebugNode.h \ + bulletDistanceConstraint.I \ + bulletDistanceConstraint.h \ + bulletGenericConstraint.I \ + bulletGenericConstraint.h \ + bulletGhostNode.I \ + bulletGhostNode.h \ + bulletHeightfieldShape.I \ + bulletHeightfieldShape.h \ + bulletHelper.I \ + bulletHelper.h \ + bulletHingeConstraint.I \ + bulletHingeConstraint.h \ + bulletManifoldPoint.I \ + bulletManifoldPoint.h \ + bulletPersistentManifold.I \ + bulletPersistentManifold.h \ + bulletPlaneShape.I \ + bulletPlaneShape.h \ + bulletRigidBodyNode.I \ + bulletRigidBodyNode.h \ + bulletShape.I \ + bulletShape.h \ + bulletSliderConstraint.I \ + bulletSliderConstraint.h \ + bulletSoftBodyConfig.I \ + bulletSoftBodyConfig.h \ + bulletSoftBodyMaterial.I \ + bulletSoftBodyMaterial.h \ + bulletSoftBodyNode.I \ + bulletSoftBodyNode.h \ + bulletSoftBodyShape.I \ + bulletSoftBodyShape.h \ + bulletSoftBodyWorldInfo.I \ + bulletSoftBodyWorldInfo.h \ + bulletSphereShape.I \ + bulletSphereShape.h \ + bulletTriangleMesh.I \ + bulletTriangleMesh.h \ + bulletTriangleMeshShape.I \ + bulletTriangleMeshShape.h \ + bulletVehicle.I \ + bulletVehicle.h \ + bulletWheel.I \ + bulletWheel.h \ + bulletWorld.I \ + bulletWorld.h \ + bullet_includes.h \ + bullet_utils.I \ + bullet_utils.h \ + + #define IGATESCAN all + +#end lib_target \ No newline at end of file diff --git a/panda/src/bullet/bulletAllHitsRayResult.I b/panda/src/bullet/bulletAllHitsRayResult.I new file mode 100644 index 0000000000..96907e9284 --- /dev/null +++ b/panda/src/bullet/bulletAllHitsRayResult.I @@ -0,0 +1,42 @@ +// Filename: bulletAllHitsRayResult.I +// Created by: enn0x (21Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletAllHitsRayResult::empty +// Access: Published +// Description: Named constructor intended to be used for asserts +// with have to return a concrete value. +//////////////////////////////////////////////////////////////////// +INLINE BulletAllHitsRayResult BulletAllHitsRayResult:: +empty() { + + btVector3 from; + btVector3 to; + + return BulletAllHitsRayResult(from, to, CollideMask::all_on()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRayHit::empty +// Access: Published +// Description: Named constructor intended to be used for asserts +// with have to return a concrete value. +//////////////////////////////////////////////////////////////////// +INLINE BulletRayHit BulletRayHit:: +empty() { + + return BulletRayHit(); +} + diff --git a/panda/src/bullet/bulletAllHitsRayResult.cxx b/panda/src/bullet/bulletAllHitsRayResult.cxx new file mode 100644 index 0000000000..8a099a21b0 --- /dev/null +++ b/panda/src/bullet/bulletAllHitsRayResult.cxx @@ -0,0 +1,166 @@ +// Filename: bulletAllHitsRayResult.cxx +// Created by: enn0x (21Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletAllHitsRayResult.h" + +//////////////////////////////////////////////////////////////////// +// Function: BulletAllHitsRayResult::Constructor +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +BulletAllHitsRayResult:: +BulletAllHitsRayResult(const btVector3 &from_pos, const btVector3 &to_pos, const CollideMask &mask) + : btCollisionWorld::AllHitsRayResultCallback(from_pos, to_pos), _mask(mask) { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletAllHitsRayResult::needsCollision +// Access: Protected +// Description: Override default implementation. +//////////////////////////////////////////////////////////////////// +bool BulletAllHitsRayResult:: +needsCollision(btBroadphaseProxy* proxy0) const { + + // Original implementation: + //bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0; + //collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask); + //return collides; + + btCollisionObject *obj0 = (btCollisionObject *) proxy0->m_clientObject; + PandaNode *node0 = (PandaNode *) obj0->getUserPointer(); + CollideMask mask0 = node0->get_into_collide_mask(); + + return (_mask & mask0) != 0; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletAllHitsRayResult::get_from_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletAllHitsRayResult:: +get_from_pos() const { + + return btVector3_to_LPoint3f(m_rayFromWorld); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletAllHitsRayResult::get_to_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletAllHitsRayResult:: +get_to_pos() const { + + return btVector3_to_LPoint3f(m_rayToWorld); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletAllHitsRayResult::has_hits +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletAllHitsRayResult:: +has_hits() const { + + return hasHit(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletAllHitsRayResult::get_closest_hit_fraction +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletAllHitsRayResult:: +get_closest_hit_fraction() const { + + return m_closestHitFraction; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletAllHitsRayResult::get_num_hits +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +int BulletAllHitsRayResult:: +get_num_hits() const { + + return m_collisionObjects.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletAllHitsRayResult::get_hit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +const BulletRayHit BulletAllHitsRayResult:: +get_hit(int idx) const { + + nassertr(idx >= 0 && idx < get_num_hits(), BulletRayHit::empty()); + + BulletRayHit hit; + + hit._object = m_collisionObjects[idx]; + hit._normal = m_hitNormalWorld[idx]; + hit._pos = m_hitPointWorld[idx]; + hit._fraction = m_hitFractions[idx]; + + return hit; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRayHit::get_hit_fraction +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletRayHit:: +get_hit_fraction() const { + + return _fraction; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRayHit::get_node +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PandaNode *BulletRayHit:: +get_node() const { + + return (_object) ? (PandaNode *)_object->getUserPointer() : NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRayHit::get_hit_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletRayHit:: +get_hit_pos() const { + + return btVector3_to_LPoint3f(_pos); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRayHit::get_hit_normal +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f BulletRayHit:: +get_hit_normal() const { + + return btVector3_to_LVector3f(_normal); +} + diff --git a/panda/src/bullet/bulletAllHitsRayResult.h b/panda/src/bullet/bulletAllHitsRayResult.h new file mode 100644 index 0000000000..f6122d89f5 --- /dev/null +++ b/panda/src/bullet/bulletAllHitsRayResult.h @@ -0,0 +1,82 @@ +// Filename: bulletAllHitsRayResult.h +// Created by: enn0x (21Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_ALL_HITS_RAY_RESULT_H__ +#define __BULLET_ALL_HITS_RAY_RESULT_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" + +#include "luse.h" +#include "pandaNode.h" +#include "collideMask.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletRayHit +// Description : +//////////////////////////////////////////////////////////////////// +struct BulletRayHit { + +PUBLISHED: + INLINE static BulletRayHit empty(); + + PandaNode *get_node() const; + LPoint3f get_hit_pos() const; + LVector3f get_hit_normal() const; + float get_hit_fraction() const; + +private: + btCollisionObject *_object; + btVector3 _normal; + btVector3 _pos; + btScalar _fraction; + + friend struct BulletAllHitsRayResult; +}; + +//////////////////////////////////////////////////////////////////// +// Class : BulletAllHitsRayResult +// Description : +//////////////////////////////////////////////////////////////////// +struct EXPCL_PANDABULLET BulletAllHitsRayResult : public btCollisionWorld::AllHitsRayResultCallback { + +PUBLISHED: + INLINE static BulletAllHitsRayResult empty(); + + LPoint3f get_from_pos() const; + LPoint3f get_to_pos() const; + + bool has_hits() const; + float get_closest_hit_fraction() const; + + int get_num_hits() const; + const BulletRayHit get_hit(int idx) const; + MAKE_SEQ(get_hits, get_num_hits, get_hit); + +public: + virtual bool needsCollision(btBroadphaseProxy* proxy0) const; + +private: + BulletAllHitsRayResult(const btVector3 &from_pos, const btVector3 &to_pos, const CollideMask &mask); + + CollideMask _mask; + + friend class BulletWorld; +}; + +#include "bulletAllHitsRayResult.I" + +#endif // __BULLET_ALL_HITS_RAY_RESULT_H__ diff --git a/panda/src/bullet/bulletBodyNode.I b/panda/src/bullet/bulletBodyNode.I new file mode 100644 index 0000000000..fa22a7d61f --- /dev/null +++ b/panda/src/bullet/bulletBodyNode.I @@ -0,0 +1,205 @@ +// Filename: bulletBodyNode.I +// Created by: enn0x (19Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletBodyNode:: +~BulletBodyNode() { + + if (_shape && ( + _shape->getShapeType() == COMPOUND_SHAPE_PROXYTYPE || + _shape->getShapeType() == EMPTY_SHAPE_PROXYTYPE)) { + delete _shape; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::notify_collisions +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void BulletBodyNode:: +notify_collisions(bool value) { + + set_collision_flag(btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK, value); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::notifies_collisions +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletBodyNode:: +notifies_collisions() const { + + return get_collision_flag(btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_collision_flag +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void BulletBodyNode:: +set_collision_flag(int flag, bool value) { + + int flags = get_object()->getCollisionFlags(); + + if (value == true) { + flags |= flag; + } + else { + flags &= ~(flag); + } + + get_object()->setCollisionFlags(flags); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_collision_flag +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletBodyNode:: +get_collision_flag(int flag) const { + + return (get_object()->getCollisionFlags() & flag) ? true : false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::is_static +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletBodyNode:: +is_static() const { + + return get_object()->isStaticObject(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::is_kinematic +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletBodyNode:: +is_kinematic() const { + + return get_object()->isKinematicObject(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_static +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void BulletBodyNode:: +set_static(bool value) { + + set_collision_flag(btCollisionObject::CF_STATIC_OBJECT, value); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_kinematic +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void BulletBodyNode:: +set_kinematic(bool value) { + + set_collision_flag(btCollisionObject::CF_KINEMATIC_OBJECT, value); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_restitution +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletBodyNode:: +get_restitution() const { + + return get_object()->getRestitution(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_restitution +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void BulletBodyNode:: +set_restitution(float restitution) { + + return get_object()->setRestitution(restitution); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_friction +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletBodyNode:: +get_friction() const { + + return get_object()->getFriction(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_friction +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void BulletBodyNode:: +set_friction(float friction) { + + return get_object()->setFriction(friction); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::has_anisotropic_friction +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletBodyNode:: +has_anisotropic_friction() const { + + return get_object()->hasAnisotropicFriction(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_num_shapes +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletBodyNode:: +get_num_shapes() const { + + return _shapes.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_shape +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletShape *BulletBodyNode:: +get_shape(int idx) const { + + nassertr(idx >= 0 && idx < (int)_shapes.size(), NULL); + return _shapes[idx]; +} + diff --git a/panda/src/bullet/bulletBodyNode.cxx b/panda/src/bullet/bulletBodyNode.cxx new file mode 100644 index 0000000000..16749733f5 --- /dev/null +++ b/panda/src/bullet/bulletBodyNode.cxx @@ -0,0 +1,640 @@ +// Filename: bulletBodyNode.cxx +// Created by: enn0x (19Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletBodyNode.h" +#include "bulletShape.h" +#include "bulletWorld.h" +#include "bulletTriangleMesh.h" + +#include "collisionBox.h" +#include "collisionPlane.h" +#include "collisionSphere.h" +#include "collisionPolygon.h" + +TypeHandle BulletBodyNode::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletBodyNode:: +BulletBodyNode(const char *name) : PandaNode(name) { + + // Shape + _shape = new btEmptyShape(); + + // Transform changed callback + _disable_transform_changed = false; + + // Default collide mask + set_into_collide_mask(CollideMask::all_on()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_legal_collide_mask +// Access: Public, virtual +// Description: Returns the subset of CollideMask bits that may be +// set for this particular type of PandaNode. For +// BodyNodes this returns all bits on. +//////////////////////////////////////////////////////////////////// +CollideMask BulletBodyNode:: +get_legal_collide_mask() const { + + return CollideMask::all_on(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::safe_to_flatten +// Access: Public, Virtual +// Description: Returns true if it is generally safe to flatten out +// this particular kind of Node by duplicating +// instances, false otherwise (for instance, a Camera +// cannot be safely flattened, because the Camera +// pointer itself is meaningful). +//////////////////////////////////////////////////////////////////// +bool BulletBodyNode:: +safe_to_flatten() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::safe_to_transform +// Access: Public, Virtual +// Description: Returns true if it is generally safe to transform +// this particular kind of Node by calling the xform() +// method, false otherwise. For instance, it's usually +// a bad idea to attempt to xform a Character. +//////////////////////////////////////////////////////////////////// +bool BulletBodyNode:: +safe_to_transform() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::safe_to_modify_transform +// Access: Public, Virtual +// Description: Returns true if it is safe to automatically adjust +// the transform on this kind of node. Usually, this is +// only a bad idea if the user expects to find a +// particular transform on the node. +// +// ModelNodes with the preserve_transform flag set are +// presently the only kinds of nodes that should not +// have their transform even adjusted. +//////////////////////////////////////////////////////////////////// +bool BulletBodyNode:: +safe_to_modify_transform() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::safe_to_combine +// Access: Public, Virtual +// Description: Returns true if it is generally safe to combine this +// particular kind of PandaNode with other kinds of +// PandaNodes of compatible type, adding children or +// whatever. For instance, an LODNode should not be +// combined with any other PandaNode, because its set of +// children is meaningful. +//////////////////////////////////////////////////////////////////// +bool BulletBodyNode:: +safe_to_combine() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::safe_to_combine_children +// Access: Public, Virtual +// Description: Returns true if it is generally safe to combine the +// children of this PandaNode with each other. For +// instance, an LODNode's children should not be +// combined with each other, because the set of children +// is meaningful. +//////////////////////////////////////////////////////////////////// +bool BulletBodyNode:: +safe_to_combine_children() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::safe_to_flatten_below +// Access: Public, Virtual +// Description: Returns true if a flatten operation may safely +// continue past this node, or false if nodes below this +// node may not be molested. +//////////////////////////////////////////////////////////////////// +bool BulletBodyNode:: +safe_to_flatten_below() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::transform_changed +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +transform_changed() { + + // Apply scale to collision shape + CPT(TransformState) xform = this->get_transform(); + if (xform->has_scale()) { + LVecBase3f scale = xform->get_scale(); + _shape->setLocalScaling(LVecBase3f_to_btVector3(scale)); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::add_shape +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +add_shape(BulletShape *shape, CPT(TransformState) xform) { + + nassertv(get_object()); + + // Transform + btTransform trans; + if (xform) { + trans = LMatrix4f_to_btTrans(xform->get_mat()); + } + else { + trans.setIdentity(); + } + + // Root shape + btCollisionShape *previous = get_object()->getCollisionShape(); + btCollisionShape *next; + + if (_shapes.size() == 0) { + nassertv(previous->getShapeType() == EMPTY_SHAPE_PROXYTYPE); + + if (xform) { + // One shape, with transform + next = new btCompoundShape(); + ((btCompoundShape *)next)->addChildShape(trans, shape->ptr()); + } + else { + // One shape, w/o transform + next = shape->ptr(); + } + + get_object()->setCollisionShape(next); + _shape = next; + + delete previous; + } + else if (_shapes.size() == 1) { + // Two shapes. Previous shape might already be a compound. + if (previous->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) { + next = previous; + } + else { + next = new btCompoundShape(); + + btTransform previous_trans = btTransform::getIdentity(); + ((btCompoundShape *)next)->addChildShape(previous_trans, previous); + } + + ((btCompoundShape *)next)->addChildShape(trans, shape->ptr()); + + get_object()->setCollisionShape(next); + _shape = next; + } + else { + // Three or more shapes. Previous shape is always a compound + nassertv(previous->getShapeType() == COMPOUND_SHAPE_PROXYTYPE); + + ((btCompoundShape *)previous)->addChildShape(trans, shape->ptr()); + } + + _shapes.push_back(shape); + + shape_changed(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::remove_shape +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +remove_shape(BulletShape *shape) { + + nassertv(get_object()); + + BulletShapes::iterator found; + PT(BulletShape) ptshape = shape; + found = find(_shapes.begin(), _shapes.end(), ptshape); + + if (found == _shapes.end()) { + bullet_cat.warning() << "shape not attached" << endl; + } + else { + _shapes.erase(found); + + // Determine the new root shape + btCollisionShape *previous = get_object()->getCollisionShape(); + btCollisionShape *next; + + if (_shapes.size() == 0) { + // No more shapes remaining + next = new btEmptyShape(); + + get_object()->setCollisionShape(next); + _shape = next; + + // The previous shape might be a compound. Then delete it. + if (previous->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) { + delete previous; + } + } + else if (_shapes.size() == 1) { + // Only one shape remaining + nassertv(previous->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) + + btCompoundShape *compound = (btCompoundShape *)previous; + compound->removeChildShape(shape->ptr()); + + nassertv(compound->getNumChildShapes() == 1); + + // The compound is no longer required if the remaining shape + // has no transform + btTransform trans = compound->getChildTransform(0); + if (is_identity(trans)) { + next = compound->getChildShape(0); + + get_object()->setCollisionShape(next); + _shape = next; + + delete compound; + } + } + else { + // More than one shape are remaining + nassertv(previous->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) + + btCompoundShape *compound = (btCompoundShape *)previous; + compound->removeChildShape(shape->ptr()); + } + + shape_changed(); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::is_identity +// Access: Private +// Description: Returns TRUE if the transform is an identity +// transform, otherwise FALSE. +//////////////////////////////////////////////////////////////////// +bool BulletBodyNode:: +is_identity(btTransform &trans) { + + btVector3 null(0, 0, 0); + + return (trans.getOrigin() == null + && trans.getRotation().getAxis() == null); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_shape_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletBodyNode:: +get_shape_pos(int idx) const { + + nassertr(idx >= 0 && idx < (int)_shapes.size(), LPoint3f::zero()); + return get_shape_mat(idx).get_row3(3); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_shape_mat +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LMatrix4f BulletBodyNode:: +get_shape_mat(int idx) const { + + nassertr(idx >= 0 && idx < (int)_shapes.size(), LMatrix4f::ident_mat()); + + btCollisionShape *root = get_object()->getCollisionShape(); + if (root->getShapeType() == COMPOUND_SHAPE_PROXYTYPE) { + btCompoundShape *compound = (btCompoundShape *)root; + + btTransform xform = compound->getChildTransform(idx); + return btTrans_to_LMatrix4f(xform); + + // The above code assumes that shape's index in _shapes member + // is the same as the shapes index within the compound. If it + // turns out that this is not always true we could use the + // following code: + /* + btCollisionShape *shape = get_shape(idx)->ptr(); + for (int i=0; igetNumChildShapes(); i++) { + if (compound->getChildShape(i) == shape) { + btTransform xform = compound->getChildTransform(idx); + return btTrans_to_LMatrix4f(xform); + } + } + */ + } + + return LMatrix4f::ident_mat(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::shape_changed +// Access: Published +// Description: Hook which will be called whenever the total shape +// of a body changed. Used for example to update +// the mass properties (inertia) of a rigid body. +// The default implementation does nothing. +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +shape_changed() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_deactivation_time +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +set_deactivation_time(float dt) { + + get_object()->setDeactivationTime(dt); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_deactivation_time +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletBodyNode:: +get_deactivation_time() const { + + return get_object()->getDeactivationTime(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::is_active +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletBodyNode:: +is_active() const { + + return get_object()->isActive(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_active +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +set_active(bool active, bool force) { + + if (active) { + get_object()->activate(force); + } + else { + if (force) { + get_object()->forceActivationState(ISLAND_SLEEPING); + } + else { + get_object()->setActivationState(ISLAND_SLEEPING); + } + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_disable_deactivation +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +set_disable_deactivation(bool disable, bool force) { + + int state = (disable) ? DISABLE_DEACTIVATION : WANTS_DEACTIVATION; + + if (force) { + get_object()->forceActivationState(state); + } + else { + get_object()->setActivationState(state); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_disable_deactivation +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletBodyNode:: +get_disable_deactivation() const { + + return (get_object()->getActivationState() & DISABLE_DEACTIVATION) != 0; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::check_collision_with +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletBodyNode:: +check_collision_with(PandaNode *node) { + + btCollisionObject *obj = BulletWorld::get_collision_object(node); + + if (obj) { + return get_object()->checkCollideWith(obj); + } + else { + return false; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_anisotropic_friction +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVecBase3f BulletBodyNode:: +get_anisotropic_friction() const { + + return btVector3_to_LVecBase3f(get_object()->getAnisotropicFriction()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_anisotropic_friction +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +set_anisotropic_friction(const LVecBase3f &friction) { + + nassertv(!friction.is_nan()); + get_object()->setAnisotropicFriction(LVecBase3f_to_btVector3(friction)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::has_contact_response +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletBodyNode:: +has_contact_response() const { + + return get_object()->hasContactResponse(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_contact_processing_threshold +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletBodyNode:: +get_contact_processing_threshold() const { + + return get_object()->getContactProcessingThreshold(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_contact_processing_threshold +// Access: Published +// Description: The constraint solver can discard solving +// contacts, if the distance is above this threshold. +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +set_contact_processing_threshold(float threshold) { + + get_object()->setContactProcessingThreshold(threshold); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_ccd_swept_sphere_radius +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletBodyNode:: +get_ccd_swept_sphere_radius() const { + + return get_object()->getCcdSweptSphereRadius(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_ccd_swept_sphere_radius +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +set_ccd_swept_sphere_radius(float radius) { + + return get_object()->setCcdSweptSphereRadius(radius); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::get_ccd_motion_threshold +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletBodyNode:: +get_ccd_motion_threshold() const { + + return get_object()->getCcdMotionThreshold(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::set_ccd_motion_threshold +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +set_ccd_motion_threshold(float threshold) { + + return get_object()->setCcdMotionThreshold(threshold); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBodyNode::add_shapes_from_collision_solids +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletBodyNode:: +add_shapes_from_collision_solids(CollisionNode *cnode) { + + PT(BulletTriangleMesh) mesh = NULL; + + for (int j=0; jget_num_solids(); j++) { + CPT(CollisionSolid) solid = cnode->get_solid(j); + TypeHandle type = solid->get_type(); + + // CollisionSphere + if (CollisionSphere::get_class_type() == type) { + CPT(CollisionSphere) sphere = DCAST(CollisionSphere, solid); + CPT(TransformState) ts = TransformState::make_pos(sphere->get_center()); + + add_shape(BulletSphereShape::make_from_solid(sphere), ts); + } + + // CollisionBox + else if (CollisionBox::get_class_type() == type) { + CPT(CollisionBox) box = DCAST(CollisionBox, solid); + CPT(TransformState) ts = TransformState::make_pos(box->get_approx_center()); + + add_shape(BulletBoxShape::make_from_solid(box), ts); + } + + // CollisionPlane + else if (CollisionPlane::get_class_type() == type) { + CPT(CollisionPlane) plane = DCAST(CollisionPlane, solid); + + add_shape(BulletPlaneShape::make_from_solid(plane)); + } + + // CollisionGeom + else if (CollisionPolygon::get_class_type() == type) { + CPT(CollisionPolygon) polygon = DCAST(CollisionPolygon, solid); + + if (!mesh) { + mesh = new BulletTriangleMesh(); + } + + for (int i=2; i < polygon->get_num_points(); i++ ) { + LPoint3f p1 = polygon->get_point(0); + LPoint3f p2 = polygon->get_point(i-1); + LPoint3f p3 = polygon->get_point(i); + + mesh->add_triangle(p1, p2, p3, true); + } + } + } + + if (mesh && mesh->get_num_triangles() > 0) { + add_shape(new BulletTriangleMeshShape(mesh, true)); + } +} + diff --git a/panda/src/bullet/bulletBodyNode.h b/panda/src/bullet/bulletBodyNode.h new file mode 100644 index 0000000000..1c4afa1201 --- /dev/null +++ b/panda/src/bullet/bulletBodyNode.h @@ -0,0 +1,152 @@ +// Filename: bulletBodyNode.h +// Created by: enn0x (19Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_BODY_NODE_H__ +#define __BULLET_BODY_NODE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" + +#include "pandaNode.h" +#include "collideMask.h" +#include "collisionNode.h" +#include "transformState.h" + +class BulletShape; + +//////////////////////////////////////////////////////////////////// +// Class : BulletBodyNode +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletBodyNode : public PandaNode { + +PUBLISHED: + BulletBodyNode(const char *name); + INLINE ~BulletBodyNode(); + + // Shapes + void add_shape(BulletShape *shape, CPT(TransformState) xform=NULL); + void remove_shape(BulletShape *shape); + + INLINE int get_num_shapes() const; + INLINE BulletShape *get_shape(int idx) const; + MAKE_SEQ(get_shapes, get_num_shapes, get_shape); + + LPoint3f get_shape_pos(int idx) const; + LMatrix4f get_shape_mat(int idx) const; + + void add_shapes_from_collision_solids(CollisionNode *cnode); + + // Static and kinematic + INLINE bool is_static() const; + INLINE bool is_kinematic() const; + + INLINE void set_static(bool value); + INLINE void set_kinematic(bool value); + + // Contacts + INLINE void notify_collisions(bool value); + INLINE bool notifies_collisions() const; + + bool check_collision_with(PandaNode *node); + + bool has_contact_response() const; + + float get_contact_processing_threshold() const; + void set_contact_processing_threshold(float threshold); + + // Deactivation + bool is_active() const; + void set_active(bool active, bool force=false); + + void set_deactivation_time(float dt); + float get_deactivation_time() const; + + void set_disable_deactivation(bool disable, bool force=false); + bool get_disable_deactivation() const; + + // Friction and Restitution + INLINE float get_restitution() const; + INLINE void set_restitution(float restitution); + + INLINE float get_friction() const; + INLINE void set_friction(float friction); + + INLINE bool has_anisotropic_friction() const; + void set_anisotropic_friction(const LVecBase3f &friction); + LVecBase3f get_anisotropic_friction() const; + + // CCD + float get_ccd_swept_sphere_radius() const; + float get_ccd_motion_threshold() const; + void set_ccd_swept_sphere_radius(float radius); + void set_ccd_motion_threshold(float threshold); + +public: + virtual btCollisionObject *get_object() const = 0; + + virtual CollideMask get_legal_collide_mask() const; + + virtual bool safe_to_flatten() const; + virtual bool safe_to_transform() const; + virtual bool safe_to_modify_transform() const; + virtual bool safe_to_combine() const; + virtual bool safe_to_combine_children() const; + virtual bool safe_to_flatten_below() const; + +protected: + INLINE void set_collision_flag(int flag, bool value); + INLINE bool get_collision_flag(int flag) const; + + btCollisionShape *_shape; + + typedef PTA(PT(BulletShape)) BulletShapes; + BulletShapes _shapes; + + bool _disable_transform_changed; + virtual void transform_changed(); + +private: + virtual void shape_changed(); + + static bool is_identity(btTransform &trans); + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + PandaNode::init_type(); + register_type(_type_handle, "BulletBodyNode", + PandaNode::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletBodyNode.I" + +#endif // __BULLET_BODY_NODE_H__ + diff --git a/panda/src/bullet/bulletBoxShape.I b/panda/src/bullet/bulletBoxShape.I new file mode 100644 index 0000000000..ee3b3b524e --- /dev/null +++ b/panda/src/bullet/bulletBoxShape.I @@ -0,0 +1,26 @@ +// Filename: bulletBoxShape.I +// Created by: enn0x (24Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletBoxShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletBoxShape:: +~BulletBoxShape() { + + delete _shape; +} + diff --git a/panda/src/bullet/bulletBoxShape.cxx b/panda/src/bullet/bulletBoxShape.cxx new file mode 100644 index 0000000000..967552857b --- /dev/null +++ b/panda/src/bullet/bulletBoxShape.cxx @@ -0,0 +1,84 @@ +// Filename: bulletBoxShape.cxx +// Created by: enn0x (24Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletBoxShape.h" +#include "bullet_utils.h" + +TypeHandle BulletBoxShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletBoxShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletBoxShape:: +BulletBoxShape(const LVecBase3f &halfExtents) { + + btVector3 btHalfExtents = LVecBase3f_to_btVector3(halfExtents); + + _shape = new btBoxShape(btHalfExtents); + _shape->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBoxShape::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionShape *BulletBoxShape:: +ptr() const { + + return _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBoxShape::get_half_extents_without_marging +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVecBase3f BulletBoxShape:: +get_half_extents_without_marging() const { + + return btVector3_to_LVecBase3f(_shape->getHalfExtentsWithoutMargin()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBoxShape::get_half_extents_with_marging +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVecBase3f BulletBoxShape:: +get_half_extents_with_marging() const { + + return btVector3_to_LVecBase3f(_shape->getHalfExtentsWithMargin()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletBoxShape::make_from_solid +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +BulletBoxShape *BulletBoxShape:: +make_from_solid(const CollisionBox *solid) { + + LPoint3f p0 = solid->get_min(); + LPoint3f p1 = solid->get_max(); + + LVecBase3f extents(p1.get_x() - p0.get_x() / 2.0, + p1.get_y() - p0.get_y() / 2.0, + p1.get_z() - p0.get_z() / 2.0); + + return new BulletBoxShape(extents); +} + diff --git a/panda/src/bullet/bulletBoxShape.h b/panda/src/bullet/bulletBoxShape.h new file mode 100644 index 0000000000..87e157fe3c --- /dev/null +++ b/panda/src/bullet/bulletBoxShape.h @@ -0,0 +1,72 @@ +// Filename: bulletBoxShape.h +// Created by: enn0x (24Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_BOX_SHAPE_H__ +#define __BULLET_BOX_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bulletShape.h" + +#include "lvector3.h" + +#include "collisionBox.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletBoxShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletBoxShape : public BulletShape { + +PUBLISHED: + BulletBoxShape(const LVecBase3f &halfExtents); + INLINE ~BulletBoxShape(); + + LVecBase3f get_half_extents_without_marging() const; + LVecBase3f get_half_extents_with_marging() const; + + static BulletBoxShape *make_from_solid(const CollisionBox *solid); + +public: + virtual btCollisionShape *ptr() const; + +private: + btBoxShape *_shape; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletShape::init_type(); + register_type(_type_handle, "BulletBoxShape", + BulletShape::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletBoxShape.I" + +#endif // __BULLET_BOX_SHAPE_H__ diff --git a/panda/src/bullet/bulletCapsuleShape.I b/panda/src/bullet/bulletCapsuleShape.I new file mode 100644 index 0000000000..a9d9b7d235 --- /dev/null +++ b/panda/src/bullet/bulletCapsuleShape.I @@ -0,0 +1,47 @@ +// Filename: bulletCapsuleShape.I +// Created by: enn0x (27Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletCapsuleShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletCapsuleShape:: +~BulletCapsuleShape() { + + delete _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCapsuleShape::get_radius +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletCapsuleShape:: +get_radius() const { + + return _shape->getRadius(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCapsuleShape::get_half_height +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletCapsuleShape:: +get_half_height() const { + + return _shape->getHalfHeight(); +} + diff --git a/panda/src/bullet/bulletCapsuleShape.cxx b/panda/src/bullet/bulletCapsuleShape.cxx new file mode 100644 index 0000000000..120fdc9f8a --- /dev/null +++ b/panda/src/bullet/bulletCapsuleShape.cxx @@ -0,0 +1,55 @@ +// Filename: bulletCapsuleShape.cxx +// Created by: enn0x (27Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletCapsuleShape.h" + +TypeHandle BulletCapsuleShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletCapsuleShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletCapsuleShape:: +BulletCapsuleShape(float radius, float height, BulletUpAxis up) { + + switch (up) { + case X_up: + _shape = new btCapsuleShapeX(radius, height); + break; + case Y_up: + _shape = new btCapsuleShape(radius, height); + break; + case Z_up: + _shape = new btCapsuleShapeZ(radius, height); + break; + default: + bullet_cat.error() << "invalid up-axis:" << up << endl; + break; + } + + _shape->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCapsuleShape::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionShape *BulletCapsuleShape:: +ptr() const { + + return _shape; +} + diff --git a/panda/src/bullet/bulletCapsuleShape.h b/panda/src/bullet/bulletCapsuleShape.h new file mode 100644 index 0000000000..44b0cc019d --- /dev/null +++ b/panda/src/bullet/bulletCapsuleShape.h @@ -0,0 +1,67 @@ +// Filename: bulletCapsuleShape.h +// Created by: enn0x (27Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CAPSULE_SHAPE_H__ +#define __BULLET_CAPSULE_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletShape.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletCapsuleShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletCapsuleShape : public BulletShape { + +PUBLISHED: + BulletCapsuleShape(float radius, float height, BulletUpAxis up=Z_up); + INLINE ~BulletCapsuleShape(); + + INLINE float get_radius() const; + INLINE float get_half_height() const; + +public: + virtual btCollisionShape *ptr() const; + +private: + btCapsuleShape *_shape; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletShape::init_type(); + register_type(_type_handle, "BulletCapsuleShape", + BulletShape::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletCapsuleShape.I" + +#endif // __BULLET_CAPSULE_SHAPE_H__ diff --git a/panda/src/bullet/bulletCharacterControllerNode.I b/panda/src/bullet/bulletCharacterControllerNode.I new file mode 100644 index 0000000000..b791855a21 --- /dev/null +++ b/panda/src/bullet/bulletCharacterControllerNode.I @@ -0,0 +1,47 @@ +// Filename: bulletCharacterControllerNode.I +// Created by: enn0x (21Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletCharacterControllerNode:: +~BulletCharacterControllerNode() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::get_ghost +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE btPairCachingGhostObject *BulletCharacterControllerNode:: +get_ghost() const { + + return _ghost; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::get_character +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE btKinematicCharacterController *BulletCharacterControllerNode:: +get_character() const { + + return _character; +} + diff --git a/panda/src/bullet/bulletCharacterControllerNode.cxx b/panda/src/bullet/bulletCharacterControllerNode.cxx new file mode 100644 index 0000000000..dd2f3fcc34 --- /dev/null +++ b/panda/src/bullet/bulletCharacterControllerNode.cxx @@ -0,0 +1,434 @@ +// Filename: bulletCharacterControllerNode.cxx +// Created by: enn0x (21Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletCharacterControllerNode.h" + +TypeHandle BulletCharacterControllerNode::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletCharacterControllerNode:: +BulletCharacterControllerNode(BulletShape *shape, float step_height, const char *name) : PandaNode(name) { + + // Setup initial transform + btTransform trans = btTransform::getIdentity(); + + // Get convex shape + if (!shape->is_convex()) { + bullet_cat.error() << "a convex shape is required!" << endl; + } + + btConvexShape *convex = dynamic_cast(shape->ptr()); + + // Setup ghost object + _ghost = new btPairCachingGhostObject(); + _ghost->setUserPointer(this); + + _ghost->setWorldTransform(trans); + _ghost->setInterpolationWorldTransform(trans); + _ghost->setCollisionShape(convex); + _ghost->setCollisionFlags(btCollisionObject::CF_CHARACTER_OBJECT + | btCollisionObject::CF_KINEMATIC_OBJECT); + + // Setup up axis + _up = get_default_up_axis(); + + // Movement + _linear_velocity_is_local = false; + _linear_velocity.set(0.0f, 0.0f, 0.0f); + _angular_velocity = 0.0f; + + // Setup character controller + _character = new btKinematicCharacterController(_ghost, convex, step_height, _up); + _character->setGravity((btScalar)9.81f); + + // Retain a pointer to the shape + _shape = shape; + + // The 'transform changed' hook has to be disabled when updating the node's + // transform from inside the post_step method! + _disable_transform_changed = false; + + // Default collide mask + set_into_collide_mask(CollideMask::all_on()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::get_legal_collide_mask +// Access: Public, virtual +// Description: Returns the subset of CollideMask bits that may be +// set for this particular type of PandaNode. For +// CharacterControllerNodes this returns all bits on. +//////////////////////////////////////////////////////////////////// +CollideMask BulletCharacterControllerNode:: +get_legal_collide_mask() const { + + return CollideMask::all_on(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::safe_to_flatten +// Access: Public, Virtual +// Description: Returns true if it is generally safe to flatten out +// this particular kind of Node by duplicating +// instances, false otherwise (for instance, a Camera +// cannot be safely flattened, because the Camera +// pointer itself is meaningful). +//////////////////////////////////////////////////////////////////// +bool BulletCharacterControllerNode:: +safe_to_flatten() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::safe_to_modify_transform +// Access: Public, Virtual +// Description: Returns true if it is safe to automatically adjust +// the transform on this kind of node. Usually, this is +// only a bad idea if the user expects to find a +// particular transform on the node. +// +// ModelNodes with the preserve_transform flag set are +// presently the only kinds of nodes that should not +// have their transform even adjusted. +//////////////////////////////////////////////////////////////////// +bool BulletCharacterControllerNode:: +safe_to_modify_transform() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::safe_to_combine +// Access: Public, Virtual +// Description: Returns true if it is generally safe to combine this +// particular kind of PandaNode with other kinds of +// PandaNodes of compatible type, adding children or +// whatever. For instance, an LODNode should not be +// combined with any other PandaNode, because its set of +// children is meaningful. +//////////////////////////////////////////////////////////////////// +bool BulletCharacterControllerNode:: +safe_to_combine() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::safe_to_combine_children +// Access: Public, Virtual +// Description: Returns true if it is generally safe to combine the +// children of this PandaNode with each other. For +// instance, an LODNode's children should not be +// combined with each other, because the set of children +// is meaningful. +//////////////////////////////////////////////////////////////////// +bool BulletCharacterControllerNode:: +safe_to_combine_children() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::safe_to_flatten_below +// Access: Public, Virtual +// Description: Returns true if a flatten operation may safely +// continue past this node, or false if nodes below this +// node may not be molested. +//////////////////////////////////////////////////////////////////// +bool BulletCharacterControllerNode:: +safe_to_flatten_below() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::safe_to_transform +// Access: Public, Virtual +// Description: Returns true if it is generally safe to transform +// this particular kind of Node by calling the xform() +// method, false otherwise. For instance, it's usually +// a bad idea to attempt to xform a Character. +//////////////////////////////////////////////////////////////////// +bool BulletCharacterControllerNode:: +safe_to_transform() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::set_linear_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +set_linear_velocity(const LVector3f &velocity, bool is_local) { + + nassertv(!velocity.is_nan()); + + _linear_velocity = velocity; + _linear_velocity_is_local = is_local; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::set_angular_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +set_angular_velocity(float omega) { + + _angular_velocity = omega; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::pre_step +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +pre_step(float dt) { + + // Angular rotation + btScalar angle = dt * deg_2_rad(_angular_velocity); + + btMatrix3x3 m = _ghost->getWorldTransform().getBasis(); + btVector3 up = m[_up]; + + m *= btMatrix3x3(btQuaternion(up, angle)); + + _ghost->getWorldTransform().setBasis(m); + + // Linear movement + btVector3 v; + if (_linear_velocity_is_local) { + btTransform xform; + xform = _ghost->getWorldTransform(); + xform.setOrigin(btVector3(0.0f, 0.0f, 0.0f)); + v = xform(LVecBase3f_to_btVector3(_linear_velocity)); + } + else { + v = LVecBase3f_to_btVector3(_linear_velocity); + } + + _character->setVelocityForTimeInterval(v, dt); + //_character->setWalkDirection(v * dt); + + _angular_velocity = 0.0f; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::post_step +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +post_step() { + + btTransform& trans = _ghost->getWorldTransform(); + CPT(TransformState) ts = btTrans_to_TransformState(trans); + + _disable_transform_changed = true; + + NodePath np = NodePath::any_path((PandaNode *)this); + np.set_transform(np.get_top(), ts); + + _disable_transform_changed = false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::transform_changed +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +transform_changed() { + + if (_disable_transform_changed) return; + + // Get translation and heading + NodePath np = NodePath::any_path((PandaNode *)this); + CPT(TransformState) ts = np.get_transform(np.get_top()); + + LPoint3f pos = ts->get_pos(); + float heading = ts->get_hpr().get_x(); + + // Set translation + _character->warp(LVecBase3f_to_btVector3(pos)); + + // Set Heading + btMatrix3x3 m = _ghost->getWorldTransform().getBasis(); + btVector3 up = m[_up]; + + m = btMatrix3x3(btQuaternion(up, heading)); + + _ghost->getWorldTransform().setBasis(m); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::get_shape +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletShape *BulletCharacterControllerNode:: +get_shape() const { + + return _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::is_on_ground +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletCharacterControllerNode:: +is_on_ground() const { + + return _character->onGround(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::can_jump +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletCharacterControllerNode:: +can_jump() const { + + return _character->canJump(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::do_jump +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +do_jump() { + + _character->jump(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::set_fall_speed +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +set_fall_speed(float fall_speed) { + + _character->setFallSpeed(fall_speed); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::set_jump_speed +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +set_jump_speed(float jump_speed) { + + _character->setJumpSpeed(jump_speed); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::set_max_jump_height +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +set_max_jump_height(float max_jump_height) { + + _character->setMaxJumpHeight(max_jump_height); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::set_max_slope +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +set_max_slope(float max_slope) { + + _character->setMaxSlope(max_slope); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::get_max_slope +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletCharacterControllerNode:: +get_max_slope() const { + + return _character->getMaxSlope(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::get_gravity +// Description: +//////////////////////////////////////////////////////////////////// +float BulletCharacterControllerNode:: +get_gravity() const { + + return _character->getGravity(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::set_gravity +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +set_gravity(float gravity) { + + _character->setGravity((btScalar) gravity); +} + + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::set_use_ghost_sweep_test +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +set_use_ghost_sweep_test(bool value) { + + return _character->setUseGhostSweepTest(value); +} + +/* +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::parents_changed +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +parents_changed() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCharacterControllerNode::children_changed +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +void BulletCharacterControllerNode:: +children_changed() { + +} +*/ + diff --git a/panda/src/bullet/bulletCharacterControllerNode.h b/panda/src/bullet/bulletCharacterControllerNode.h new file mode 100644 index 0000000000..1568024d60 --- /dev/null +++ b/panda/src/bullet/bulletCharacterControllerNode.h @@ -0,0 +1,119 @@ +// Filename: bulletCharacterControllerNode.h +// Created by: enn0x (21Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CHARACTER_CONTROLLER_NODE_H__ +#define __BULLET_CHARACTER_CONTROLLER_NODE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletShape.h" + +#include "pandaNode.h" +#include "collideMask.h" +#include "luse.h" +#include "transformState.h" +#include "nodePath.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletCharacterControllerNode +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletCharacterControllerNode : public PandaNode { + +PUBLISHED: + BulletCharacterControllerNode(BulletShape *shape, float step_height, const char *name="character"); + INLINE ~BulletCharacterControllerNode(); + + void set_linear_velocity(const LVector3f &velocity, bool is_local); + void set_angular_velocity(float omega); + + BulletShape *get_shape() const; + + float get_gravity() const; + float get_max_slope() const; + + void set_fall_speed(float fall_speed); + void set_jump_speed(float jump_speed); + void set_max_jump_height(float max_jump_height); + void set_max_slope(float max_slope); + void set_gravity(float gravity); + void set_use_ghost_sweep_test(bool value); + + bool is_on_ground() const; + bool can_jump() const; + void do_jump(); + +public: + virtual CollideMask get_legal_collide_mask() const; + + virtual bool safe_to_flatten() const; + virtual bool safe_to_transform() const; + virtual bool safe_to_modify_transform() const; + virtual bool safe_to_combine() const; + virtual bool safe_to_combine_children() const; + virtual bool safe_to_flatten_below() const; + + INLINE btPairCachingGhostObject *get_ghost() const; + INLINE btKinematicCharacterController *get_character() const; + + void pre_step(float dt); + void post_step(); + +protected: + //virtual void parents_changed(); + //virtual void children_changed(); + virtual void transform_changed(); + +private: + BulletUpAxis _up; + + btKinematicCharacterController *_character; + btPairCachingGhostObject *_ghost; + + PT(BulletShape) _shape; + + LVector3f _linear_velocity; + bool _linear_velocity_is_local; + float _angular_velocity; + + bool _disable_transform_changed; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + PandaNode::init_type(); + register_type(_type_handle, "BulletCharacterControllerNode", + PandaNode::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletCharacterControllerNode.I" + +#endif // __BULLET_CHARACTER_CONTROLLER_NODE_H__ + diff --git a/panda/src/bullet/bulletClosestHitRayResult.I b/panda/src/bullet/bulletClosestHitRayResult.I new file mode 100644 index 0000000000..52f84f6fe2 --- /dev/null +++ b/panda/src/bullet/bulletClosestHitRayResult.I @@ -0,0 +1,30 @@ +// Filename: bulletClosestHitRayResult.I +// Created by: enn0x (21Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitRayResult::empty +// Access: Public +// Description: Named constructor intended to be used for asserts +// with have to return a concrete value. +//////////////////////////////////////////////////////////////////// +INLINE BulletClosestHitRayResult BulletClosestHitRayResult:: +empty() { + + btVector3 from; + btVector3 to; + + return BulletClosestHitRayResult(from, to, CollideMask::all_on()); +} + diff --git a/panda/src/bullet/bulletClosestHitRayResult.cxx b/panda/src/bullet/bulletClosestHitRayResult.cxx new file mode 100644 index 0000000000..54c8b37f58 --- /dev/null +++ b/panda/src/bullet/bulletClosestHitRayResult.cxx @@ -0,0 +1,125 @@ +// Filename: bulletClosestHitRayResult.cxx +// Created by: enn0x (21Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletClosestHitRayResult.h" + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitRayResult::Constructor +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +BulletClosestHitRayResult:: +BulletClosestHitRayResult(const btVector3 &from_pos, const btVector3 &to_pos, const CollideMask &mask) + : btCollisionWorld::ClosestRayResultCallback(from_pos, to_pos), _mask(mask) { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitRayResult::needsCollision +// Access: Protected +// Description: Override default implementation. +//////////////////////////////////////////////////////////////////// +bool BulletClosestHitRayResult:: +needsCollision(btBroadphaseProxy* proxy0) const { + + // Original implementation: + //bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0; + //collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask); + //return collides; + + btCollisionObject *obj0 = (btCollisionObject *) proxy0->m_clientObject; + PandaNode *node0 = (PandaNode *) obj0->getUserPointer(); + CollideMask mask0 = node0->get_into_collide_mask(); + + return (_mask & mask0) != 0; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitRayResult::has_hit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletClosestHitRayResult:: +has_hit() const { + + return hasHit(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitRayResult::get_hit_fraction +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletClosestHitRayResult:: +get_hit_fraction() const { + + return m_closestHitFraction; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitRayResult::get_node +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PandaNode *BulletClosestHitRayResult:: +get_node() const { + + btCollisionObject *objectPtr = m_collisionObject; + return (objectPtr) ? (PandaNode *)objectPtr->getUserPointer() : NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitRayResult::get_hit_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletClosestHitRayResult:: +get_hit_pos() const { + + return btVector3_to_LPoint3f(m_hitPointWorld); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitRayResult::get_hit_normal +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f BulletClosestHitRayResult:: +get_hit_normal() const { + + return btVector3_to_LVector3f(m_hitNormalWorld); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitRayResult::get_from_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletClosestHitRayResult:: +get_from_pos() const { + + return btVector3_to_LPoint3f(m_rayFromWorld); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitRayResult::get_to_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletClosestHitRayResult:: +get_to_pos() const { + + return btVector3_to_LPoint3f(m_rayToWorld); +} + diff --git a/panda/src/bullet/bulletClosestHitRayResult.h b/panda/src/bullet/bulletClosestHitRayResult.h new file mode 100644 index 0000000000..481164ca36 --- /dev/null +++ b/panda/src/bullet/bulletClosestHitRayResult.h @@ -0,0 +1,59 @@ +// Filename: bulletClosestHitRayResult.h +// Created by: enn0x (21Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CLOSEST_HIT_RAY_RESULT_H__ +#define __BULLET_CLOSEST_HIT_RAY_RESULT_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" + +#include "luse.h" +#include "pandaNode.h" +#include "collideMask.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletClosestHitRayResult +// Description : +//////////////////////////////////////////////////////////////////// +struct EXPCL_PANDABULLET BulletClosestHitRayResult : public btCollisionWorld::ClosestRayResultCallback { + +PUBLISHED: + INLINE static BulletClosestHitRayResult empty(); + + LPoint3f get_from_pos() const; + LPoint3f get_to_pos() const; + + bool has_hit() const; + + PandaNode *get_node() const; + LPoint3f get_hit_pos() const; + LVector3f get_hit_normal() const; + float get_hit_fraction() const; + +public: + virtual bool needsCollision(btBroadphaseProxy* proxy0) const; + +private: + BulletClosestHitRayResult(const btVector3 &from_pos, const btVector3 &to_pos, const CollideMask &mask); + + CollideMask _mask; + + friend class BulletWorld; +}; + +#include "bulletClosestHitRayResult.I" + +#endif // __BULLET_CLOSEST_HIT_RAY_RESULT_H__ diff --git a/panda/src/bullet/bulletClosestHitSweepResult.I b/panda/src/bullet/bulletClosestHitSweepResult.I new file mode 100644 index 0000000000..5c0655c0d1 --- /dev/null +++ b/panda/src/bullet/bulletClosestHitSweepResult.I @@ -0,0 +1,30 @@ +// Filename: bulletClosestHitSweepResult.I +// Created by: enn0x (01Dec10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestSweepRayResult::empty +// Access: Public +// Description: Named constructor intended to be used for asserts +// with have to return a concrete value. +//////////////////////////////////////////////////////////////////// +INLINE BulletClosestHitSweepResult BulletClosestHitSweepResult:: +empty() { + + btVector3 from; + btVector3 to; + + return BulletClosestHitSweepResult(from, to, CollideMask::all_on()); +} + diff --git a/panda/src/bullet/bulletClosestHitSweepResult.cxx b/panda/src/bullet/bulletClosestHitSweepResult.cxx new file mode 100644 index 0000000000..a49bd2fe73 --- /dev/null +++ b/panda/src/bullet/bulletClosestHitSweepResult.cxx @@ -0,0 +1,125 @@ +// Filename: bulletClosestHitSweepResult.cxx +// Created by: enn0x (01Dec10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletClosestHitSweepResult.h" + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitSweepResult::Constructor +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +BulletClosestHitSweepResult:: +BulletClosestHitSweepResult(const btVector3 &from_pos, const btVector3 &to_pos, const CollideMask &mask) + : btCollisionWorld::ClosestConvexResultCallback(from_pos, to_pos), _mask(mask) { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitSweepResult::needsCollision +// Access: Protected +// Description: Override default implementation. +//////////////////////////////////////////////////////////////////// +bool BulletClosestHitSweepResult:: +needsCollision(btBroadphaseProxy* proxy0) const { + + // Original implementation: + //bool collides = (proxy0->m_collisionFilterGroup & m_collisionFilterMask) != 0; + //collides = collides && (m_collisionFilterGroup & proxy0->m_collisionFilterMask); + //return collides; + + btCollisionObject *obj0 = (btCollisionObject *) proxy0->m_clientObject; + PandaNode *node0 = (PandaNode *) obj0->getUserPointer(); + CollideMask mask0 = node0->get_into_collide_mask(); + + return (_mask & mask0) != 0; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitSweepResult::has_hit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletClosestHitSweepResult:: +has_hit() const { + + return hasHit(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitSweepResult::get_hit_fraction +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletClosestHitSweepResult:: +get_hit_fraction() const { + + return m_closestHitFraction; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitSweepResult::get_node +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PandaNode *BulletClosestHitSweepResult:: +get_node() const { + + btCollisionObject *objectPtr = m_hitCollisionObject; + return (objectPtr) ? (PandaNode *)objectPtr->getUserPointer() : NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitSweepResult::get_hit_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletClosestHitSweepResult:: +get_hit_pos() const { + + return btVector3_to_LPoint3f(m_hitPointWorld); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitSweepResult::get_hit_normal +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f BulletClosestHitSweepResult:: +get_hit_normal() const { + + return btVector3_to_LVector3f(m_hitNormalWorld); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitSweepResult::get_from_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletClosestHitSweepResult:: +get_from_pos() const { + + return btVector3_to_LPoint3f(m_convexFromWorld); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletClosestHitSweepResult::get_to_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletClosestHitSweepResult:: +get_to_pos() const { + + return btVector3_to_LPoint3f(m_convexToWorld); +} + diff --git a/panda/src/bullet/bulletClosestHitSweepResult.h b/panda/src/bullet/bulletClosestHitSweepResult.h new file mode 100644 index 0000000000..f64ec786b2 --- /dev/null +++ b/panda/src/bullet/bulletClosestHitSweepResult.h @@ -0,0 +1,59 @@ +// Filename: bulletClosestHitSweepResult.h +// Created by: enn0x (01Dec10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CLOSEST_HIT_SWEEP_RESULT_H__ +#define __BULLET_CLOSEST_HIT_SWEEP_RESULT_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" + +#include "luse.h" +#include "pandaNode.h" +#include "collideMask.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletClosestHitSweepResult +// Description : +//////////////////////////////////////////////////////////////////// +struct EXPCL_PANDABULLET BulletClosestHitSweepResult : public btCollisionWorld::ClosestConvexResultCallback { + +PUBLISHED: + INLINE static BulletClosestHitSweepResult empty(); + + LPoint3f get_from_pos() const; + LPoint3f get_to_pos() const; + + bool has_hit() const; + + PandaNode *get_node() const; + LPoint3f get_hit_pos() const; + LVector3f get_hit_normal() const; + float get_hit_fraction() const; + +public: + virtual bool needsCollision(btBroadphaseProxy* proxy0) const; + +private: + BulletClosestHitSweepResult(const btVector3 &from_pos, const btVector3 &to_pos, const CollideMask &mask); + + CollideMask _mask; + + friend class BulletWorld; +}; + +#include "bulletClosestHitSweepResult.I" + +#endif // __BULLET_CLOSEST_HIT_SWEEP_RESULT_H__ diff --git a/panda/src/bullet/bulletConeShape.I b/panda/src/bullet/bulletConeShape.I new file mode 100644 index 0000000000..4da00766d6 --- /dev/null +++ b/panda/src/bullet/bulletConeShape.I @@ -0,0 +1,47 @@ +// Filename: bulletConeShape.I +// Created by: enn0x (24Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletConeShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletConeShape:: +~BulletConeShape() { + + delete _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConeShape::get_radius +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletConeShape:: +get_radius() const { + + return _shape->getRadius(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConeShape::get_height +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletConeShape:: +get_height() const { + + return _shape->getHeight(); +} + diff --git a/panda/src/bullet/bulletConeShape.cxx b/panda/src/bullet/bulletConeShape.cxx new file mode 100644 index 0000000000..716ac9e646 --- /dev/null +++ b/panda/src/bullet/bulletConeShape.cxx @@ -0,0 +1,55 @@ +// Filename: bulletConeShape.cxx +// Created by: enn0x (24Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletConeShape.h" + +TypeHandle BulletConeShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletConeShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletConeShape:: +BulletConeShape(float radius, float height, BulletUpAxis up) { + + switch (up) { + case X_up: + _shape = new btConeShapeX(radius, height); + break; + case Y_up: + _shape = new btConeShape(radius, height); + break; + case Z_up: + _shape = new btConeShapeZ(radius, height); + break; + default: + bullet_cat.error() << "invalid up-axis:" << up << endl; + break; + } + + _shape->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConeShape::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionShape *BulletConeShape:: +ptr() const { + + return _shape; +} + diff --git a/panda/src/bullet/bulletConeShape.h b/panda/src/bullet/bulletConeShape.h new file mode 100644 index 0000000000..a5c25d894a --- /dev/null +++ b/panda/src/bullet/bulletConeShape.h @@ -0,0 +1,67 @@ +// Filename: bulletConeShape.h +// Created by: enn0x (24Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CONE_SHAPE_H__ +#define __BULLET_CONE_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletShape.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletConeShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletConeShape : public BulletShape { + +PUBLISHED: + BulletConeShape(float radius, float height, BulletUpAxis up=Z_up); + INLINE ~BulletConeShape(); + + INLINE float get_radius() const; + INLINE float get_height() const; + +public: + virtual btCollisionShape *ptr() const; + +private: + btConeShape *_shape; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletShape::init_type(); + register_type(_type_handle, "BulletConeShape", + BulletShape::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletConeShape.I" + +#endif // __BULLET_CONE_SHAPE_H__ diff --git a/panda/src/bullet/bulletConeTwistConstraint.I b/panda/src/bullet/bulletConeTwistConstraint.I new file mode 100644 index 0000000000..05bab00710 --- /dev/null +++ b/panda/src/bullet/bulletConeTwistConstraint.I @@ -0,0 +1,25 @@ +// Filename: bulletConeTwistConstraint.I +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletConeTwistConstraint::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletConeTwistConstraint:: +~BulletConeTwistConstraint() { + + delete _constraint; +} + diff --git a/panda/src/bullet/bulletConeTwistConstraint.cxx b/panda/src/bullet/bulletConeTwistConstraint.cxx new file mode 100644 index 0000000000..d53ff48b84 --- /dev/null +++ b/panda/src/bullet/bulletConeTwistConstraint.cxx @@ -0,0 +1,95 @@ +// Filename: bulletConeTwistConstraint.cxx +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletConeTwistConstraint.h" +#include "bulletRigidBodyNode.h" + +#include "deg_2_rad.h" + +TypeHandle BulletConeTwistConstraint::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletConeTwistConstraint::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletConeTwistConstraint:: +BulletConeTwistConstraint(const BulletRigidBodyNode *node_a, + const TransformState &frame_a) { + + btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object()); + btTransform trans_a = LMatrix4f_to_btTrans(frame_a.get_mat()); + + _constraint = new btConeTwistConstraint(*ptr_a, trans_a); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConeTwistConstraint::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletConeTwistConstraint:: +BulletConeTwistConstraint(const BulletRigidBodyNode *node_a, + const BulletRigidBodyNode *node_b, + const TransformState &frame_a, + const TransformState &frame_b) { + + btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object()); + btTransform trans_a = LMatrix4f_to_btTrans(frame_a.get_mat()); + + btRigidBody *ptr_b = btRigidBody::upcast(node_b->get_object()); + btTransform trans_b = LMatrix4f_to_btTrans(frame_b.get_mat()); + + _constraint = new btConeTwistConstraint(*ptr_a, *ptr_b, trans_a, trans_b); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConeTwistConstraint::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btTypedConstraint *BulletConeTwistConstraint:: +ptr() const { + + return _constraint; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConeTwistConstraint::set_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletConeTwistConstraint:: +set_limit(int index, float value) { + + value = deg_2_rad(value); + + _constraint->setLimit(index, value); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConeTwistConstraint::set_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletConeTwistConstraint:: +set_limit(float swing1, float swing2, float twist, float softness, float bias, float relaxation) { + + swing1 = deg_2_rad(swing1); + swing2 = deg_2_rad(swing2); + twist = deg_2_rad(twist); + + _constraint->setLimit(swing1, swing2, twist, softness, bias, relaxation); +} + diff --git a/panda/src/bullet/bulletConeTwistConstraint.h b/panda/src/bullet/bulletConeTwistConstraint.h new file mode 100644 index 0000000000..fec6a2fc93 --- /dev/null +++ b/panda/src/bullet/bulletConeTwistConstraint.h @@ -0,0 +1,76 @@ +// Filename: bulletConeTwistConstraint.h +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CONE_TWIST_CONSTRAINT_H__ +#define __BULLET_CONE_TWIST_CONSTRAINT_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletConstraint.h" + +#include "transformState.h" + +class BulletRigidBodyNode; + +//////////////////////////////////////////////////////////////////// +// Class : BulletConeTwistConstraint +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletConeTwistConstraint : public BulletConstraint { + +PUBLISHED: + BulletConeTwistConstraint(const BulletRigidBodyNode *node_a, + const TransformState &frame_a); + BulletConeTwistConstraint(const BulletRigidBodyNode *node_a, + const BulletRigidBodyNode *node_b, + const TransformState &frame_a, + const TransformState &frame_b); + INLINE ~BulletConeTwistConstraint(); + + void set_limit(int index, float value); + void set_limit(float swing1, float swing2, float twist, float softness=1.0f, float bias=0.3f, float relaxation=1.0f); + +public: + virtual btTypedConstraint *ptr() const; + +private: + btConeTwistConstraint *_constraint; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletConstraint::init_type(); + register_type(_type_handle, "BulletConeTwistConstraint", + BulletConstraint::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletConeTwistConstraint.I" + +#endif // __BULLET_CONE_TWIST_CONSTRAINT_H__ diff --git a/panda/src/bullet/bulletConstraint.I b/panda/src/bullet/bulletConstraint.I new file mode 100644 index 0000000000..33de8da1cc --- /dev/null +++ b/panda/src/bullet/bulletConstraint.I @@ -0,0 +1,24 @@ +// Filename: bulletConstraint.I +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletConstraint::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletConstraint:: +~BulletConstraint() { + +} + diff --git a/panda/src/bullet/bulletConstraint.cxx b/panda/src/bullet/bulletConstraint.cxx new file mode 100644 index 0000000000..a33f2506c2 --- /dev/null +++ b/panda/src/bullet/bulletConstraint.cxx @@ -0,0 +1,85 @@ +// Filename: bulletConstraint.cxx +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletConstraint.h" +#include "bulletRigidBodyNode.h" + +TypeHandle BulletConstraint::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletConstraint::enable_feedback +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletConstraint:: +enable_feedback(bool value) { + + ptr()->enableFeedback(value); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConstraint::get_applied_impulse +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletConstraint:: +get_applied_impulse() const { + + return ptr()->getAppliedImpulse(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConstraint::set_dbg_draw_size +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletConstraint:: +set_debug_draw_size(float size) { + + ptr()->setDbgDrawSize(size); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConstraint::get_dbg_draw_size +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletConstraint:: +get_debug_draw_size() { + + return ptr()->getDbgDrawSize(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConstraint::get_rigid_body_a +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletRigidBodyNode *BulletConstraint:: +get_rigid_body_a() { + + return (BulletRigidBodyNode *)ptr()->getRigidBodyA().getUserPointer(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConstraint::get_rigid_body_b +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletRigidBodyNode *BulletConstraint:: +get_rigid_body_b() { + + return (BulletRigidBodyNode *)ptr()->getRigidBodyB().getUserPointer(); +} + diff --git a/panda/src/bullet/bulletConstraint.h b/panda/src/bullet/bulletConstraint.h new file mode 100644 index 0000000000..8d19686763 --- /dev/null +++ b/panda/src/bullet/bulletConstraint.h @@ -0,0 +1,71 @@ +// Filename: bulletConstraint.h +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CONSTRAINT_H__ +#define __BULLET_CONSTRAINT_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" + +#include "typedReferenceCount.h" + +class BulletRigidBodyNode; + +//////////////////////////////////////////////////////////////////// +// Class : BulletConstraint +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletConstraint : public TypedReferenceCount { + +PUBLISHED: + INLINE virtual ~BulletConstraint(); + + BulletRigidBodyNode *get_rigid_body_a(); + BulletRigidBodyNode *get_rigid_body_b(); + + void enable_feedback(bool value); + void set_debug_draw_size(float size); + + float get_applied_impulse() const; + float get_debug_draw_size(); + +public: + virtual btTypedConstraint *ptr() const = 0; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + TypedReferenceCount::init_type(); + register_type(_type_handle, "BulletConstraint", + TypedReferenceCount::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletConstraint.I" + +#endif // __BULLET_CONSTRAINT_H__ diff --git a/panda/src/bullet/bulletContactCallbacks.h b/panda/src/bullet/bulletContactCallbacks.h new file mode 100644 index 0000000000..397d4d1cf3 --- /dev/null +++ b/panda/src/bullet/bulletContactCallbacks.h @@ -0,0 +1,128 @@ +// Filename: bulletContactCallbacks.h +// Created by: enn0x (10Apr10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CONTACT_CALLBACKS_H__ +#define __BULLET_CONTACT_CALLBACKS_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" + +#include "config_bullet.h" // required for: bullet_cat.debug() + +#include "event.h" +#include "eventQueue.h" +#include "eventParameter.h" +#include "eventStorePandaNode.h" +#include "pandaNode.h" + +struct UserPersitentData { + PT(PandaNode) node0; + PT(PandaNode) node1; +}; + +//////////////////////////////////////////////////////////////////// +// Function: contact_added_callback +// Description: +//////////////////////////////////////////////////////////////////// +static bool +contact_added_callback(btManifoldPoint &cp, + const btCollisionObject *obj0, + int id0, + int index0, + const btCollisionObject *obj1, + int id1, + int index1) { + + if (cp.m_userPersistentData == NULL) { + PT(PandaNode) node0 = (PandaNode *)obj0->getUserPointer(); + PT(PandaNode) node1 = (PandaNode *)obj1->getUserPointer(); + + bullet_cat.debug() << "contact added: " << cp.m_userPersistentData << endl; + + // Gather persistent data + UserPersitentData *data = new UserPersitentData(); + data->node0 = node0; + data->node1 = node1; + + cp.m_userPersistentData = (void *)data; + + // Send event + if (bullet_enable_contact_events) { + + Event *event = new Event("bullet-contact-added"); + event->add_parameter(EventParameter(new EventStorePandaNode(node0))); + event->add_parameter(EventParameter(new EventStorePandaNode(node1))); + + EventQueue::get_global_event_queue()->queue_event(event); + } + } + + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: contact_processed_callback +// Description: +//////////////////////////////////////////////////////////////////// +static bool +contact_processed_callback(btManifoldPoint &cp, + void *body0, + void *body1) { + +/* + btCollisionObject *obj0 = (btCollisionObject *)body0; + btCollisionObject *colobj1Obj1 = (btCollisionObject *)body1; + + int flags0 = obj0->getCollisionFlags(); + int flags1 = obj1->getCollisionFlags(); + + if ((flags0 & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK) + || (flags1 & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK)) { + + // do something... + } +*/ + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: contact_destroyed_callback +// Description: +//////////////////////////////////////////////////////////////////// +static bool +contact_destroyed_callback(void *userPersistentData) { + + bullet_cat.debug() << "contact removed: " << userPersistentData << endl; + + UserPersitentData *data = (UserPersitentData *)userPersistentData; + + // Send event + if (bullet_enable_contact_events) { + + Event *event = new Event("bullet-contact-destroyed"); + event->add_parameter(EventParameter(new EventStorePandaNode(data->node0))); + event->add_parameter(EventParameter(new EventStorePandaNode(data->node1))); + + EventQueue::get_global_event_queue()->queue_event(event); + } + + // Delete persitent data + delete data; + + return false; +} + +#endif // __BULLET_CONTACT_CALLBACKS_H__ diff --git a/panda/src/bullet/bulletContactResult.I b/panda/src/bullet/bulletContactResult.I new file mode 100644 index 0000000000..1a60235713 --- /dev/null +++ b/panda/src/bullet/bulletContactResult.I @@ -0,0 +1,115 @@ +// Filename: bulletContactResult.I +// Created by: enn0x (08Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletContactResult::get_node0 +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE const PandaNode *BulletContact:: +get_node0() const { + + return _obj0 ? (PandaNode *)_obj0->getUserPointer() : NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletContactResult::get_node1 +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE const PandaNode *BulletContact:: +get_node1() const { + + return _obj1 ? (PandaNode *)_obj1->getUserPointer() : NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletContactResult::get_manifold_point +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE const BulletManifoldPoint *BulletContact:: +get_manifold_point() const { + + return new BulletManifoldPoint(*_mp); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletContactResult::get_idx0 +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE const int BulletContact:: +get_idx0() const { + + return _idx0; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletContactResult::get_idx1 +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE const int BulletContact:: +get_idx1() const { + + return _idx1; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletContactResult::get_part_id0 +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE const int BulletContact:: +get_part_id0() const { + + return _part_id0; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletContactResult::get_part_id1 +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE const int BulletContact:: +get_part_id1() const { + + return _part_id1; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletContactResult::get_num_contacts +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletContactResult:: +get_num_contacts() const { + + return _contacts.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletContactResult::get_contact +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE const BulletContact &BulletContactResult:: +get_contact(int idx) const { + + nassertr(idx >= 0 && idx < (int)_contacts.size(), _empty); + return _contacts[idx]; +} + diff --git a/panda/src/bullet/bulletContactResult.cxx b/panda/src/bullet/bulletContactResult.cxx new file mode 100644 index 0000000000..84aaf8c858 --- /dev/null +++ b/panda/src/bullet/bulletContactResult.cxx @@ -0,0 +1,54 @@ +// Filename: bulletContactResult.cxx +// Created by: enn0x (08Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletContactResult.h" + +BulletContact BulletContactResult::_empty; + +//////////////////////////////////////////////////////////////////// +// Function: BulletContactResult::Constructor +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +BulletContactResult:: +BulletContactResult() : btCollisionWorld::ContactResultCallback() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletContactResult::addSingleResult +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +btScalar BulletContactResult:: +addSingleResult(btManifoldPoint &mp, + const btCollisionObject *obj0, int part_id0, int idx0, + const btCollisionObject *obj1, int part_id1, int idx1) { + + + BulletContact contact; + + contact._mp = ∓ + contact._obj0 = obj0; + contact._obj1 = obj1; + contact._part_id0 = part_id0; + contact._part_id1 = part_id1; + contact._idx0 = idx0; + contact._idx1 = idx1; + + _contacts.push_back(contact); + + return 1.0f; +} + diff --git a/panda/src/bullet/bulletContactResult.h b/panda/src/bullet/bulletContactResult.h new file mode 100644 index 0000000000..6e46b8d370 --- /dev/null +++ b/panda/src/bullet/bulletContactResult.h @@ -0,0 +1,81 @@ +// Filename: bulletContactResult.h +// Created by: enn0x (08Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CONTACT_RESULT_H__ +#define __BULLET_CONTACT_RESULT_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bulletManifoldPoint.h" + +#include "pvector.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletContact +// Description : +//////////////////////////////////////////////////////////////////// +struct EXPCL_PANDABULLET BulletContact { + +PUBLISHED: + INLINE const BulletManifoldPoint *get_manifold_point() const; + INLINE const PandaNode *get_node0() const; + INLINE const PandaNode *get_node1() const; + INLINE const int get_idx0() const; + INLINE const int get_idx1() const; + INLINE const int get_part_id0() const; + INLINE const int get_part_id1() const; + +private: + btManifoldPoint *_mp; + const btCollisionObject *_obj0; + const btCollisionObject *_obj1; + int _part_id0; + int _part_id1; + int _idx0; + int _idx1; + + friend struct BulletContactResult; +}; + +//////////////////////////////////////////////////////////////////// +// Class : BulletContactResult +// Description : +//////////////////////////////////////////////////////////////////// +struct EXPCL_PANDABULLET BulletContactResult : public btCollisionWorld::ContactResultCallback { + +PUBLISHED: + INLINE int get_num_contacts() const; + INLINE const BulletContact &get_contact(int idx) const; + MAKE_SEQ(get_contacts, get_num_contacts, get_contact); + +public: + virtual btScalar addSingleResult(btManifoldPoint &mp, + const btCollisionObject *obj0, int part_id0, int idx0, + const btCollisionObject *obj1, int part_id1, int idx1); + +protected: + BulletContactResult(); + +private: + static BulletContact _empty; + + pvector _contacts; + + friend class BulletWorld; +}; + +#include "bulletContactResult.I" + +#endif // __BULLET_CONTACT_RESULT_H__ diff --git a/panda/src/bullet/bulletConvexHullShape.I b/panda/src/bullet/bulletConvexHullShape.I new file mode 100644 index 0000000000..62e3291b70 --- /dev/null +++ b/panda/src/bullet/bulletConvexHullShape.I @@ -0,0 +1,25 @@ +// Filename: bulletConvexHullShape.I +// Created by: enn0x (26Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletConvexHullShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletConvexHullShape:: +~BulletConvexHullShape() { + + delete _shape; +} + diff --git a/panda/src/bullet/bulletConvexHullShape.cxx b/panda/src/bullet/bulletConvexHullShape.cxx new file mode 100644 index 0000000000..c45f7a86d7 --- /dev/null +++ b/panda/src/bullet/bulletConvexHullShape.cxx @@ -0,0 +1,102 @@ +// Filename: bulletConvexHullShape.cxx +// Created by: enn0x (26Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletConvexHullShape.h" + +#include "nodePathCollection.h" +#include "geomNode.h" +#include "geomVertexReader.h" + +TypeHandle BulletConvexHullShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletConvexHullShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletConvexHullShape:: +BulletConvexHullShape() { + + _shape = new btConvexHullShape(NULL, 0); + _shape->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConvexHullShape::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionShape *BulletConvexHullShape:: +ptr() const { + + return _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConvexHullShape::add_point +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletConvexHullShape:: +add_point(const LPoint3f &p) { + + _shape->addPoint(LVecBase3f_to_btVector3(p)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConvexHullShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletConvexHullShape:: +add_array(const PTA_LVecBase3f &points) { + + _shape = new btConvexHullShape(NULL, 0); + _shape->setUserPointer(this); + + PTA_LVecBase3f::const_iterator it; + for (it=points.begin(); it!=points.end(); it++) { + LVecBase3f v = *it; + _shape->addPoint(LVecBase3f_to_btVector3(v)); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConvexHullShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletConvexHullShape:: +add_geom(const Geom *geom) { + + // Collect points + pvector points; + + CPT(GeomVertexData) vdata = geom->get_vertex_data(); + GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex()); + + while (!reader.is_at_end()) { + points.push_back(reader.get_data3f()); + } + + // Create shape + _shape = new btConvexHullShape(NULL, 0); + _shape->setUserPointer(this); + + pvector::const_iterator it; + for (it=points.begin(); it!=points.end(); it++) { + _shape->addPoint(LVecBase3f_to_btVector3(*it)); + } +} + diff --git a/panda/src/bullet/bulletConvexHullShape.h b/panda/src/bullet/bulletConvexHullShape.h new file mode 100644 index 0000000000..017768c85a --- /dev/null +++ b/panda/src/bullet/bulletConvexHullShape.h @@ -0,0 +1,71 @@ +// Filename: bulletConvexHullShape.h +// Created by: enn0x (26Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CONVEX_HULL_SHAPE_H__ +#define __BULLET_CONVEX_HULL_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bulletShape.h" + +#include "lpoint3.h" +#include "geom.h" +#include "pta_LVecBase3f.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletConvexHullShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletConvexHullShape : public BulletShape { + +PUBLISHED: + BulletConvexHullShape(); + INLINE ~BulletConvexHullShape(); + + void add_point(const LPoint3f &p); + void add_array(const PTA_LVecBase3f &points); + void add_geom(const Geom *geom); + +public: + virtual btCollisionShape *ptr() const; + +private: + btConvexHullShape *_shape; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletShape::init_type(); + register_type(_type_handle, "BulletConvexHullShape", + BulletShape::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletConvexHullShape.I" + +#endif // __BULLET_CONVEX_HULL_SHAPE_H__ diff --git a/panda/src/bullet/bulletConvexPointCloudShape.I b/panda/src/bullet/bulletConvexPointCloudShape.I new file mode 100644 index 0000000000..78bad86bc8 --- /dev/null +++ b/panda/src/bullet/bulletConvexPointCloudShape.I @@ -0,0 +1,36 @@ +// Filename: bulletConvexPointCloudShape.I +// Created by: enn0x (30Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletConvexPointCloudShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletConvexPointCloudShape:: +~BulletConvexPointCloudShape() { + + delete _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConvexPointCloudShape::get_num_points +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletConvexPointCloudShape:: +get_num_points() const { + + return _shape->getNumPoints(); +} + diff --git a/panda/src/bullet/bulletConvexPointCloudShape.cxx b/panda/src/bullet/bulletConvexPointCloudShape.cxx new file mode 100644 index 0000000000..0f07615f70 --- /dev/null +++ b/panda/src/bullet/bulletConvexPointCloudShape.cxx @@ -0,0 +1,91 @@ +// Filename: bulletConvexPointCloudShape.cxx +// Created by: enn0x (30Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletConvexPointCloudShape.h" + +#include "geomVertexReader.h" + +TypeHandle BulletConvexPointCloudShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletConvexPointCloudShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletConvexPointCloudShape:: +BulletConvexPointCloudShape(const PTA_LVecBase3f &points, LVecBase3f scale) { + + btVector3 btScale = LVecBase3f_to_btVector3(scale); + + // Convert points + btVector3 *btPoints = new btVector3[points.size()]; + + int i = 0; + PTA_LVecBase3f::const_iterator it; + for (it=points.begin(); it!=points.end(); it++) { + btPoints[i] = LVecBase3f_to_btVector3(*it); + i++; + } + + // Create shape + _shape = new btConvexPointCloudShape(btPoints, points.size(), btScale); + _shape->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConvexPointCloudShape::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionShape *BulletConvexPointCloudShape:: +ptr() const { + + return _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletConvexPointCloudShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletConvexPointCloudShape:: +BulletConvexPointCloudShape(const Geom *geom, LVecBase3f scale) { + + btVector3 btScale = LVecBase3f_to_btVector3(scale); + + // Collect points + pvector points; + + CPT(GeomVertexData) vdata = geom->get_vertex_data(); + GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex()); + + while (!reader.is_at_end()) { + points.push_back(reader.get_data3f()); + } + + // Convert points + btVector3 *btPoints = new btVector3[points.size()]; + + int i = 0; + pvector::const_iterator it; + for (it=points.begin(); it!=points.end(); it++) { + btPoints[i] = LVecBase3f_to_btVector3(*it); + i++; + } + + // Create + _shape = new btConvexPointCloudShape(btPoints, points.size(), btScale); + _shape->setUserPointer(this); +} + diff --git a/panda/src/bullet/bulletConvexPointCloudShape.h b/panda/src/bullet/bulletConvexPointCloudShape.h new file mode 100644 index 0000000000..1f1cd52568 --- /dev/null +++ b/panda/src/bullet/bulletConvexPointCloudShape.h @@ -0,0 +1,69 @@ +// Filename: bulletConvexPointCloudShape.h +// Created by: enn0x (30Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CONVEX_POINT_CLOUD_SHAPE_H__ +#define __BULLET_CONVEX_POINT_CLOUD_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bulletShape.h" + +#include "geom.h" +#include "pta_LVecBase3f.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletConvexPointCloudShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletConvexPointCloudShape : public BulletShape { + +PUBLISHED: + BulletConvexPointCloudShape(const PTA_LVecBase3f &points, LVecBase3f scale=LVecBase3f(1.)); + BulletConvexPointCloudShape(const Geom *geom, LVecBase3f scale=LVecBase3f(1.)); + INLINE ~BulletConvexPointCloudShape(); + + INLINE int get_num_points() const; + +public: + virtual btCollisionShape *ptr() const; + +private: + btConvexPointCloudShape *_shape; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletShape::init_type(); + register_type(_type_handle, "BulletConvexPointCloudShape", + BulletShape::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletConvexPointCloudShape.I" + +#endif // __BULLET_CONVEX_POINT_CLOUD_SHAPE_H__ diff --git a/panda/src/bullet/bulletCylinderShape.I b/panda/src/bullet/bulletCylinderShape.I new file mode 100644 index 0000000000..347614d12c --- /dev/null +++ b/panda/src/bullet/bulletCylinderShape.I @@ -0,0 +1,58 @@ +// Filename: bulletCylinderShape.I +// Created by: enn0x (17Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletCylinderShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletCylinderShape:: +~BulletCylinderShape() { + + delete _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCylinderShape::get_radius +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletCylinderShape:: +get_radius() const { + + return _shape->getRadius(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCylinderShape::get_half_extents_without_marging +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE LVecBase3f BulletCylinderShape:: +get_half_extents_without_marging() const { + + return btVector3_to_LVecBase3f(_shape->getHalfExtentsWithoutMargin()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCylinderShape::get_half_extents_with_marging +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE LVecBase3f BulletCylinderShape:: +get_half_extents_with_marging() const { + + return btVector3_to_LVecBase3f(_shape->getHalfExtentsWithMargin()); +} + diff --git a/panda/src/bullet/bulletCylinderShape.cxx b/panda/src/bullet/bulletCylinderShape.cxx new file mode 100644 index 0000000000..d656ee0e9b --- /dev/null +++ b/panda/src/bullet/bulletCylinderShape.cxx @@ -0,0 +1,83 @@ +// Filename: bulletCylinderShape.cxx +// Created by: enn0x (17Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletCylinderShape.h" + +TypeHandle BulletCylinderShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletCylinderShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletCylinderShape:: +BulletCylinderShape(const LVector3f &half_extents, BulletUpAxis up) { + + btVector3 btHalfExtents = LVecBase3f_to_btVector3(half_extents); + + switch (up) { + case X_up: + _shape = new btCylinderShapeX(btHalfExtents); + break; + case Y_up: + _shape = new btCylinderShape(btHalfExtents); + break; + case Z_up: + _shape = new btCylinderShapeZ(btHalfExtents); + break; + default: + bullet_cat.error() << "invalid up-axis:" << up << endl; + break; + } + + _shape->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCylinderShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletCylinderShape:: +BulletCylinderShape(float radius, float height, BulletUpAxis up) { + + switch (up) { + case X_up: + _shape = new btCylinderShapeX(btVector3(0.5 * height, radius, 0.0f)); + break; + case Y_up: + _shape = new btCylinderShape(btVector3(radius, 0.5 * height, 0.0f)); + break; + case Z_up: + _shape = new btCylinderShapeZ(btVector3(radius, 0.0f, 0.5 * height)); + break; + default: + bullet_cat.error() << "invalid up-axis:" << up << endl; + break; + } + + _shape->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletCylinderShape::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionShape *BulletCylinderShape:: +ptr() const { + + return _shape; +} + diff --git a/panda/src/bullet/bulletCylinderShape.h b/panda/src/bullet/bulletCylinderShape.h new file mode 100644 index 0000000000..d7f0a54389 --- /dev/null +++ b/panda/src/bullet/bulletCylinderShape.h @@ -0,0 +1,69 @@ +// Filename: bulletCylinderShape.h +// Created by: enn0x (17Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_CYLINDER_SHAPE_H__ +#define __BULLET_CYLINDER_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletShape.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletCylinderShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletCylinderShape : public BulletShape { + +PUBLISHED: + BulletCylinderShape(const LVector3f &half_extents, BulletUpAxis up=Z_up); + BulletCylinderShape(float radius, float height, BulletUpAxis up=Z_up); + INLINE ~BulletCylinderShape(); + + INLINE float get_radius() const; + INLINE LVecBase3f get_half_extents_without_marging() const; + INLINE LVecBase3f get_half_extents_with_marging() const; + +public: + virtual btCollisionShape *ptr() const; + +private: + btCylinderShape *_shape; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletShape::init_type(); + register_type(_type_handle, "BulletCylinderShape", + BulletShape::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletCylinderShape.I" + +#endif // __BULLET_CYLINDER_SHAPE_H__ diff --git a/panda/src/bullet/bulletDebugNode.I b/panda/src/bullet/bulletDebugNode.I new file mode 100644 index 0000000000..c30ae30c59 --- /dev/null +++ b/panda/src/bullet/bulletDebugNode.I @@ -0,0 +1,47 @@ +// Filename: bulletDebugNode.I +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletDebugNode:: +~BulletDebugNode() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::set_verbose +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void BulletDebugNode:: +set_verbose(bool verbose) { + + _verbose = verbose; + draw_mask_changed(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::get_verbose +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletDebugNode:: +get_verbose() const { + + return _verbose; +} + diff --git a/panda/src/bullet/bulletDebugNode.cxx b/panda/src/bullet/bulletDebugNode.cxx new file mode 100644 index 0000000000..8756ad2d55 --- /dev/null +++ b/panda/src/bullet/bulletDebugNode.cxx @@ -0,0 +1,392 @@ +// Filename: bulletDebugNode.cxx +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletDebugNode.h" + +#include "geomVertexFormat.h" +#include "geomVertexWriter.h" + +TypeHandle BulletDebugNode::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletDebugNode:: +BulletDebugNode(const char *name) : GeomNode(name), _verbose(false) { + + set_overall_hidden(true); + + _vdata = new GeomVertexData("", GeomVertexFormat::get_v3c4(), Geom::UH_stream); + + // Lines + _prim_lines = new GeomLines(Geom::UH_stream); + _prim_lines->set_shade_model(Geom::SM_uniform); + + _geom_lines = new Geom(_vdata); + _geom_lines->add_primitive(_prim_lines); + + add_geom(_geom_lines); + + // Triangles + _prim_triangles = new GeomTriangles(Geom::UH_stream); + _prim_triangles->set_shade_model(Geom::SM_uniform); + + _geom_triangles = new Geom(_vdata); + _geom_triangles->add_primitive(_prim_triangles); + + add_geom(_geom_triangles); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::safe_to_flatten +// Access: Public, Virtual +// Description: Returns true if it is generally safe to flatten out +// this particular kind of Node by duplicating +// instances, false otherwise (for instance, a Camera +// cannot be safely flattened, because the Camera +// pointer itself is meaningful). +//////////////////////////////////////////////////////////////////// +bool BulletDebugNode:: +safe_to_flatten() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::safe_to_transform +// Access: Public, Virtual +// Description: Returns true if it is generally safe to transform +// this particular kind of Node by calling the xform() +// method, false otherwise. For instance, it's usually +// a bad idea to attempt to xform a Character. +//////////////////////////////////////////////////////////////////// +bool BulletDebugNode:: +safe_to_transform() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::safe_to_modify_transform +// Access: Public, Virtual +// Description: Returns true if it is safe to automatically adjust +// the transform on this kind of node. Usually, this is +// only a bad idea if the user expects to find a +// particular transform on the node. +// +// ModelNodes with the preserve_transform flag set are +// presently the only kinds of nodes that should not +// have their transform even adjusted. +//////////////////////////////////////////////////////////////////// +bool BulletDebugNode:: +safe_to_modify_transform() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::safe_to_combine +// Access: Public, Virtual +// Description: Returns true if it is generally safe to combine this +// particular kind of PandaNode with other kinds of +// PandaNodes of compatible type, adding children or +// whatever. For instance, an LODNode should not be +// combined with any other PandaNode, because its set of +// children is meaningful. +//////////////////////////////////////////////////////////////////// +bool BulletDebugNode:: +safe_to_combine() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::safe_to_combine_children +// Access: Public, Virtual +// Description: Returns true if it is generally safe to combine the +// children of this PandaNode with each other. For +// instance, an LODNode's children should not be +// combined with each other, because the set of children +// is meaningful. +//////////////////////////////////////////////////////////////////// +bool BulletDebugNode:: +safe_to_combine_children() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::safe_to_flatten_below +// Access: Public, Virtual +// Description: Returns true if a flatten operation may safely +// continue past this node, or false if nodes below this +// node may not be molested. +//////////////////////////////////////////////////////////////////// +bool BulletDebugNode:: +safe_to_flatten_below() const { + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::draw_mask_changed +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDebugNode:: +draw_mask_changed() { + + if (is_overall_hidden()) { + _drawer.setDebugMode(DebugDraw::DBG_NoDebug); + } + else { + if (_verbose) { + _drawer.setDebugMode(DebugDraw::DBG_DrawWireframe | + DebugDraw::DBG_DrawAabb | + DebugDraw::DBG_DrawContactPoints | + DebugDraw::DBG_DrawConstraints | + DebugDraw::DBG_DrawConstraintLimits); + } + else { + _drawer.setDebugMode(DebugDraw::DBG_DrawWireframe | + DebugDraw::DBG_DrawConstraints | + DebugDraw::DBG_FastWireframe); + + } + } +} + +/* +DBG_DrawWireframe +DBG_DrawAabb +DBG_DrawText +DBG_DrawFeaturesText +DBG_DrawContactPoints +DBG_DrawConstraints +DBG_DrawConstraintLimits +DBG_NoDeactivation +DBG_NoHelpText +DBG_EnableSatComparison +DBG_DisableBulletLCP +DBG_ProfileTimings +DBG_EnableCCD +DBG_FastWireframe +*/ + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::post_step +// Access: Private +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDebugNode:: +post_step(btDynamicsWorld *world) { + + // Collect debug geometry data + world->debugDrawWorld(); + + // Render collected data + _prim_lines->clear_vertices(); + _prim_triangles->clear_vertices(); + + GeomVertexWriter vwriter(_vdata, InternalName::get_vertex()); + GeomVertexWriter cwriter(_vdata, InternalName::get_color()); + + int v = 0; + + pvector::const_iterator lit; + pvector::const_iterator tit; + + for (lit = _drawer._lines.begin(); lit != _drawer._lines.end(); lit++) { + Line line = *lit; + + vwriter.add_data3f(line._p0); + cwriter.add_data4f(line._color); + vwriter.add_data3f(line._p1); + cwriter.add_data4f(line._color); + + _prim_lines->add_vertex(v++); + _prim_lines->add_vertex(v++); + + _prim_lines->close_primitive(); + } + + for (tit = _drawer._triangles.begin(); tit != _drawer._triangles.end(); tit++) { + Triangle tri = *tit; + + vwriter.add_data3f(tri._p0); + cwriter.add_data4f(tri._color); + vwriter.add_data3f(tri._p1); + cwriter.add_data4f(tri._color); + vwriter.add_data3f(tri._p2); + cwriter.add_data4f(tri._color); + + _prim_triangles->add_vertex(v++); + _prim_triangles->add_vertex(v++); + _prim_triangles->add_vertex(v++); + + _prim_triangles->close_primitive(); + } + + // Clear the collected data again + _drawer._lines.clear(); + _drawer._triangles.clear(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::DebugDraw::setDebugMode +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDebugNode::DebugDraw:: +setDebugMode(int mode) { + + _mode = mode; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::DebugDraw::getDebugMode +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +int BulletDebugNode::DebugDraw:: +getDebugMode() const { + + return _mode; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::DebugDraw::reportErrorWarning +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDebugNode::DebugDraw:: +reportErrorWarning(const char *warning) { + + bullet_cat.error() << warning << endl; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::DebugDraw::drawLine +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDebugNode::DebugDraw:: +drawLine(const btVector3 &from, const btVector3 &to, const btVector3 &color) { + + float r = color.getX(); + float g = color.getY(); + float b = color.getZ(); + + Line line; + + line._p0 = LVecBase3f(from.getX(), from.getY(), from.getZ()); + line._p1 = LVecBase3f(to.getX(), to.getY(), to.getZ()); + line._color = Colorf(r, g, b, 1.0f); + + _lines.push_back(line); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::DebugDraw::drawTriangle +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDebugNode::DebugDraw:: +drawTriangle(const btVector3 &v0, const btVector3 &v1, const btVector3 &v2, const btVector3 &color, btScalar) { + + float r = color.getX(); + float g = color.getY(); + float b = color.getZ(); + + Triangle tri; + + tri._p0 = LVecBase3f(v0.getX(), v0.getY(), v0.getZ()); + tri._p1 = LVecBase3f(v1.getX(), v1.getY(), v1.getZ()); + tri._p2 = LVecBase3f(v2.getX(), v2.getY(), v2.getZ()); + tri._color = Colorf(r, g, b, 1.0f); + + _triangles.push_back(tri); + + + // Draw the triangle's normal +/* + btVector3 x1 = v1 - v0; + btVector3 x2 = v2 - v0; + btVector3 normal = v1.cross(v2).normalize(); + + btVector3 from = (v0 + v1 + v2) * 0.3333; + btVector3 to = from + normal; + drawLine(from, to, color); +*/ +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::DebugDraw::drawTriangle +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDebugNode::DebugDraw:: +drawTriangle(const btVector3 &v0, const btVector3 &v1, const btVector3 &v2, const btVector3 &n0, const btVector3 &n1, const btVector3 &n2, const btVector3 &color, btScalar alpha) { + + // TODO + bullet_cat.debug() << "drawTriangle(2) - not yet implemented!" << endl; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::DebugDraw::drawContactPoint +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDebugNode::DebugDraw:: +drawContactPoint(const btVector3 &point, const btVector3 &normal, btScalar distance, int lifetime, const btVector3 &color) { + + const btVector3 to = point + normal * distance; + const btVector3 &from = point; + + drawLine(from, to, color); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::DebugDraw::draw3dText +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDebugNode::DebugDraw:: +draw3dText(const btVector3 &location, const char *text) { + + // TODO + bullet_cat.debug() << "draw3dText - not yet implemented!" << endl; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDebugNode::DebugDraw::drawSphere +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDebugNode::DebugDraw:: +drawSphere(btScalar radius, const btTransform &transform, const btVector3 &color) { + + btVector3 center = transform.getOrigin(); + + const btVector3 xoffs = transform.getBasis() * btVector3(1, 0, 0); + const btVector3 yoffs = transform.getBasis() * btVector3(0, 1, 0); + const btVector3 zoffs = transform.getBasis() * btVector3(0, 0, 1); + + drawArc(center, xoffs, yoffs, radius, radius, 0, SIMD_2_PI, color, false, 10.0); + drawArc(center, yoffs, zoffs, radius, radius, 0, SIMD_2_PI, color, false, 10.0); + drawArc(center, zoffs, xoffs, radius, radius, 0, SIMD_2_PI, color, false, 10.0); +} + diff --git a/panda/src/bullet/bulletDebugNode.h b/panda/src/bullet/bulletDebugNode.h new file mode 100644 index 0000000000..497530583e --- /dev/null +++ b/panda/src/bullet/bulletDebugNode.h @@ -0,0 +1,135 @@ +// Filename: bulletDebugNode.h +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_DEBUG_NODE_H__ +#define __BULLET_DEBUG_NODE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" + +#include "geomNode.h" +#include "geom.h" +#include "geomVertexData.h" +#include "geomLines.h" +#include "geomTriangles.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletDebugNode +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletDebugNode : public GeomNode { + +PUBLISHED: + BulletDebugNode(const char *name="debug"); + INLINE ~BulletDebugNode(); + + virtual void draw_mask_changed(); + + INLINE void set_verbose(bool verbose); + INLINE bool get_verbose() const; + +public: + virtual bool safe_to_flatten() const; + virtual bool safe_to_transform() const; + virtual bool safe_to_modify_transform() const; + virtual bool safe_to_combine() const; + virtual bool safe_to_combine_children() const; + virtual bool safe_to_flatten_below() const; + +private: + void post_step(btDynamicsWorld *world); + + struct Line { + LVecBase3f _p0; + LVecBase3f _p1; + Colorf _color; + }; + + struct Triangle { + LVecBase3f _p0; + LVecBase3f _p1; + LVecBase3f _p2; + Colorf _color; + }; + + class DebugDraw : public btIDebugDraw { + + public: + DebugDraw() {}; + + virtual void setDebugMode(int mode); + virtual int getDebugMode() const; + virtual void reportErrorWarning(const char *warning); + + virtual void drawLine(const btVector3 &from, const btVector3 &to, + const btVector3 &color); + virtual void drawContactPoint(const btVector3 &point, + const btVector3 &normal, btScalar distance, int lifetime, + const btVector3 &color); + virtual void draw3dText(const btVector3 &location, const char *text); + virtual void drawTriangle(const btVector3 &v0, const btVector3 &v1, + const btVector3 &v2, const btVector3 &color, btScalar); + virtual void drawTriangle(const btVector3 &v0, const btVector3 &v1, + const btVector3 &v2, const btVector3 &n0, const btVector3 &n1, + const btVector3 &n2, const btVector3 &color, btScalar alpha); + virtual void drawSphere(btScalar radius, const btTransform &transform, + const btVector3 &color); + + public: + pvector _lines; + pvector _triangles; + + private: + int _mode; + }; + + DebugDraw _drawer; + + bool _verbose; + + PT(GeomVertexData) _vdata; + PT(Geom) _geom_lines; + PT(GeomLines) _prim_lines; + PT(Geom) _geom_triangles; + PT(GeomTriangles) _prim_triangles; + + friend class BulletWorld; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + PandaNode::init_type(); + register_type(_type_handle, "BulletDebugNode", + PandaNode::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletDebugNode.I" + +#endif // __BULLET_DEBUG_NODE_H__ + diff --git a/panda/src/bullet/bulletDistanceConstraint.I b/panda/src/bullet/bulletDistanceConstraint.I new file mode 100644 index 0000000000..c6cc7e628c --- /dev/null +++ b/panda/src/bullet/bulletDistanceConstraint.I @@ -0,0 +1,25 @@ +// Filename: bulletDistanceConstraint.I +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletDistanceConstraint::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletDistanceConstraint:: +~BulletDistanceConstraint() { + + delete _constraint; +} + diff --git a/panda/src/bullet/bulletDistanceConstraint.cxx b/panda/src/bullet/bulletDistanceConstraint.cxx new file mode 100644 index 0000000000..6cc397f218 --- /dev/null +++ b/panda/src/bullet/bulletDistanceConstraint.cxx @@ -0,0 +1,111 @@ +// Filename: bulletDistanceConstraint.cxx +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletDistanceConstraint.h" +#include "bulletRigidBodyNode.h" + +TypeHandle BulletDistanceConstraint::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletDistanceConstraint::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletDistanceConstraint:: +BulletDistanceConstraint(const BulletRigidBodyNode *node_a, + const LPoint3f &pivot_a) { + + btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object()); + btVector3 pos_a = LVecBase3f_to_btVector3(pivot_a); + + _constraint = new btPoint2PointConstraint(*ptr_a, pos_a); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDistanceConstraint::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletDistanceConstraint:: +BulletDistanceConstraint(const BulletRigidBodyNode *node_a, + const BulletRigidBodyNode *node_b, + const LPoint3f &pivot_a, + const LPoint3f &pivot_b) { + + btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object()); + btVector3 pos_a = LVecBase3f_to_btVector3(pivot_a); + + btRigidBody *ptr_b = btRigidBody::upcast(node_b->get_object()); + btVector3 pos_b = LVecBase3f_to_btVector3(pivot_b); + + _constraint = new btPoint2PointConstraint(*ptr_a, *ptr_b, pos_a, pos_b); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDistanceConstraint::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btTypedConstraint *BulletDistanceConstraint:: +ptr() const { + + return _constraint; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDistanceConstraint::set_pivot_a +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDistanceConstraint:: +set_pivot_a(const LPoint3f &pivot_a) { + + nassertv(!pivot_a.is_nan()); + _constraint->setPivotA(LVecBase3f_to_btVector3(pivot_a)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDistanceConstraint::set_pivot_b +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletDistanceConstraint:: +set_pivot_b(const LPoint3f &pivot_b) { + + nassertv(!pivot_b.is_nan()); + _constraint->setPivotA(LVecBase3f_to_btVector3(pivot_b)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDistanceConstraint::set_pivot_in_a +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletDistanceConstraint:: +get_pivot_in_a() const { + + return btVector3_to_LPoint3f(_constraint->getPivotInA()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletDistanceConstraint::set_pivot_in_b +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletDistanceConstraint:: +get_pivot_in_b() const { + + return btVector3_to_LPoint3f(_constraint->getPivotInB()); +} + diff --git a/panda/src/bullet/bulletDistanceConstraint.h b/panda/src/bullet/bulletDistanceConstraint.h new file mode 100644 index 0000000000..be209c3cd0 --- /dev/null +++ b/panda/src/bullet/bulletDistanceConstraint.h @@ -0,0 +1,79 @@ +// Filename: bulletDistanceConstraint.h +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_DISTANCE_CONSTRAINT_H__ +#define __BULLET_DISTANCE_CONSTRAINT_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletConstraint.h" + +#include "lpoint3.h" + +class BulletRigidBodyNode; + +//////////////////////////////////////////////////////////////////// +// Class : BulletDistanceConstraint +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletDistanceConstraint : public BulletConstraint { + +PUBLISHED: + BulletDistanceConstraint(const BulletRigidBodyNode *node_a, + const LPoint3f &pivot_a); + BulletDistanceConstraint(const BulletRigidBodyNode *node_a, + const BulletRigidBodyNode *node_b, + const LPoint3f &pivot_a, + const LPoint3f &pivot_b); + INLINE ~BulletDistanceConstraint(); + + void set_pivot_a(const LPoint3f &pivot_a); + void set_pivot_b(const LPoint3f &pivot_b); + + LPoint3f get_pivot_in_a() const; + LPoint3f get_pivot_in_b() const; + +public: + virtual btTypedConstraint *ptr() const; + +private: + btPoint2PointConstraint *_constraint; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletConstraint::init_type(); + register_type(_type_handle, "BulletDistanceConstraint", + BulletConstraint::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletDistanceConstraint.I" + +#endif // __BULLET_DISTANCE_CONSTRAINT_H__ diff --git a/panda/src/bullet/bulletGenericConstraint.I b/panda/src/bullet/bulletGenericConstraint.I new file mode 100644 index 0000000000..55b5f396c2 --- /dev/null +++ b/panda/src/bullet/bulletGenericConstraint.I @@ -0,0 +1,25 @@ +// Filename: bulletGenericConstraint.I +// Created by: enn0x (02Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletGenericConstraint::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletGenericConstraint:: +~BulletGenericConstraint() { + + delete _constraint; +} + diff --git a/panda/src/bullet/bulletGenericConstraint.cxx b/panda/src/bullet/bulletGenericConstraint.cxx new file mode 100644 index 0000000000..d7ddde1831 --- /dev/null +++ b/panda/src/bullet/bulletGenericConstraint.cxx @@ -0,0 +1,115 @@ +// Filename: bulletGenericConstraint.cxx +// Created by: enn0x (02Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletGenericConstraint.h" +#include "bulletRigidBodyNode.h" + +TypeHandle BulletGenericConstraint::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletGenericConstraint::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletGenericConstraint:: +BulletGenericConstraint(const BulletRigidBodyNode *node_a, + const TransformState &frame_a, + bool use_frame_a) { + + btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object()); + btTransform trans_a = LMatrix4f_to_btTrans(frame_a.get_mat()); + + _constraint = new btGeneric6DofConstraint(*ptr_a, trans_a, use_frame_a); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGenericConstraint::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletGenericConstraint:: +BulletGenericConstraint(const BulletRigidBodyNode *node_a, + const BulletRigidBodyNode *node_b, + const TransformState &frame_a, + const TransformState &frame_b, + bool use_frame_a) { + + btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object()); + btTransform trans_a = LMatrix4f_to_btTrans(frame_a.get_mat()); + + btRigidBody *ptr_b = btRigidBody::upcast(node_b->get_object()); + btTransform trans_b = LMatrix4f_to_btTrans(frame_b.get_mat()); + + _constraint = new btGeneric6DofConstraint(*ptr_a, *ptr_b, trans_a, trans_b, use_frame_a); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGenericConstraint::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btTypedConstraint *BulletGenericConstraint:: +ptr() const { + + return _constraint; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGenericConstraint::set_linear_lower_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletGenericConstraint:: +set_linear_lower_limit(const LPoint3f &limit) { + + nassertv(!limit.is_nan()); + _constraint->setLinearLowerLimit(LVecBase3f_to_btVector3(limit)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGenericConstraint::set_linear_upper_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletGenericConstraint:: +set_linear_upper_limit(const LPoint3f &limit) { + + nassertv(!limit.is_nan()); + _constraint->setLinearUpperLimit(LVecBase3f_to_btVector3(limit)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGenericConstraint::set_angular_lower_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletGenericConstraint:: +set_angular_lower_limit(const LVector3f &limit) { + + nassertv(!limit.is_nan()); + _constraint->setAngularLowerLimit(LVecBase3f_to_btVector3(limit)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGenericConstraint::set_angular_upper_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletGenericConstraint:: +set_angular_upper_limit(const LVector3f &limit) { + + nassertv(!limit.is_nan()); + _constraint->setAngularUpperLimit(LVecBase3f_to_btVector3(limit)); +} + diff --git a/panda/src/bullet/bulletGenericConstraint.h b/panda/src/bullet/bulletGenericConstraint.h new file mode 100644 index 0000000000..e27ff92abf --- /dev/null +++ b/panda/src/bullet/bulletGenericConstraint.h @@ -0,0 +1,82 @@ +// Filename: bulletGenericConstraint.h +// Created by: enn0x (02Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_GENERIC_CONSTRAINT_H__ +#define __BULLET_GENERIC_CONSTRAINT_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletConstraint.h" + +#include "transformState.h" +#include "lpoint3.h" +#include "lvector3.h" + +class BulletRigidBodyNode; + +//////////////////////////////////////////////////////////////////// +// Class : BulletGenericConstraint +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletGenericConstraint : public BulletConstraint { + +PUBLISHED: + BulletGenericConstraint(const BulletRigidBodyNode *node_a, + const TransformState &frame_a, + bool use_frame_a); + BulletGenericConstraint(const BulletRigidBodyNode *node_a, + const BulletRigidBodyNode *node_b, + const TransformState &frame_a, + const TransformState &frame_b, + bool use_frame_a); + INLINE ~BulletGenericConstraint(); + + void set_linear_lower_limit(const LPoint3f &limit); + void set_linear_upper_limit(const LPoint3f &limit); + void set_angular_lower_limit(const LVector3f &limit); + void set_angular_upper_limit(const LVector3f &limit); + +public: + virtual btTypedConstraint *ptr() const; + +private: + btGeneric6DofConstraint *_constraint; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletConstraint::init_type(); + register_type(_type_handle, "BulletGenericConstraint", + BulletConstraint::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletGenericConstraint.I" + +#endif // __BULLET_GENERIC_CONSTRAINT_H__ diff --git a/panda/src/bullet/bulletGhostNode.I b/panda/src/bullet/bulletGhostNode.I new file mode 100644 index 0000000000..01dbf73e73 --- /dev/null +++ b/panda/src/bullet/bulletGhostNode.I @@ -0,0 +1,51 @@ +// Filename: bulletGhostNode.I +// Created by: enn0x (19Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletGhostNode::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletGhostNode:: +~BulletGhostNode() { + + delete _ghost; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGhostNode::get_num_overlapping_nodes +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletGhostNode:: +get_num_overlapping_nodes() const { + + return _ghost->getNumOverlappingObjects(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGhostNode::get_overlapping_node +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PandaNode *BulletGhostNode:: +get_overlapping_node(int idx) const { + + nassertr(idx >=0 && idx < _ghost->getNumOverlappingObjects(), NULL); + + btCollisionObject *object = _ghost->getOverlappingObject(idx); + return (object) ? (PandaNode *)object->getUserPointer() : NULL; +} + diff --git a/panda/src/bullet/bulletGhostNode.cxx b/panda/src/bullet/bulletGhostNode.cxx new file mode 100644 index 0000000000..2739278d1d --- /dev/null +++ b/panda/src/bullet/bulletGhostNode.cxx @@ -0,0 +1,113 @@ +// Filename: bulletGhostNode.cxx +// Created by: enn0x (19Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletGhostNode.h" +#include "bulletShape.h" + +TypeHandle BulletGhostNode::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletGhostNode::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletGhostNode:: +BulletGhostNode(const char *name) : BulletBodyNode(name) { + + // Setup initial transform + btTransform trans = btTransform::getIdentity(); + + // Setup ghost object + _ghost = new btPairCachingGhostObject(); + _ghost->setUserPointer(this); + _ghost->setCollisionFlags(btCollisionObject::CF_NO_CONTACT_RESPONSE); + _ghost->setWorldTransform(trans); + _ghost->setInterpolationWorldTransform(trans); + _ghost->setCollisionShape(_shape); + + // Autosync is off by default + _sync_transform = false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGhostNode::get_object +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionObject *BulletGhostNode:: +get_object() const { + + return _ghost; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGhostNode::parents_changed +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +void BulletGhostNode:: +parents_changed() { + + // Enable autosync if one of the parents is suited for this + Parents parents = get_parents(); + for (int i=0; i < parents.get_num_parents(); ++i) { + PandaNode *parent = parents.get_parent(i); + TypeHandle type = parent->get_type(); + + if (BulletRigidBodyNode::get_class_type() == type || + BulletSoftBodyNode::get_class_type() == type || + BulletGhostNode::get_class_type() == type || + BulletCharacterControllerNode::get_class_type() == type) { + _sync_transform = true; + return; + } + } + + // None of the parents is suited for autosync + _sync_transform = false; + + transform_changed(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGhostNode::transform_changed +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +void BulletGhostNode:: +transform_changed() { + + if (_disable_transform_changed) return; + + btTransform trans; + get_node_transform(trans, this); + _ghost->setWorldTransform(trans); + _ghost->setInterpolationWorldTransform(trans); + + BulletBodyNode::transform_changed(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletGhostNode::pre_step +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletGhostNode:: +pre_step() { + + if (_sync_transform) { + transform_changed(); + } +} + diff --git a/panda/src/bullet/bulletGhostNode.h b/panda/src/bullet/bulletGhostNode.h new file mode 100644 index 0000000000..b3c66e9f8c --- /dev/null +++ b/panda/src/bullet/bulletGhostNode.h @@ -0,0 +1,83 @@ +// Filename: bulletGhostNode.h +// Created by: enn0x (19Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_GHOST_NODE_H__ +#define __BULLET_GHOST_NODE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletBodyNode.h" + +#include "pandaNode.h" +#include "collideMask.h" + +class BulletShape; + +//////////////////////////////////////////////////////////////////// +// Class : BulletGhostNode +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletGhostNode : public BulletBodyNode { + +PUBLISHED: + BulletGhostNode(const char *name="ghost"); + INLINE ~BulletGhostNode(); + + // Overlapping + INLINE int get_num_overlapping_nodes() const; + INLINE PandaNode *get_overlapping_node(int idx) const; + MAKE_SEQ(get_overlapping_nodes, get_num_overlapping_nodes, get_overlapping_node); + +public: + virtual btCollisionObject *get_object() const; + + void pre_step(); + +protected: + virtual void parents_changed(); + virtual void transform_changed(); + +private: + btPairCachingGhostObject *_ghost; + + bool _sync_transform; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletBodyNode::init_type(); + register_type(_type_handle, "BulletGhostNode", + BulletBodyNode::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletGhostNode.I" + +#endif // __BULLET_GHOST_NODE_H__ + diff --git a/panda/src/bullet/bulletHeightfieldShape.I b/panda/src/bullet/bulletHeightfieldShape.I new file mode 100644 index 0000000000..c11df5ea1c --- /dev/null +++ b/panda/src/bullet/bulletHeightfieldShape.I @@ -0,0 +1,26 @@ +// Filename: bulletHeightfieldShape.I +// Created by: enn0x (05Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletHeightfieldShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletHeightfieldShape:: +~BulletHeightfieldShape() { + + delete _shape; + delete _data; +} + diff --git a/panda/src/bullet/bulletHeightfieldShape.cxx b/panda/src/bullet/bulletHeightfieldShape.cxx new file mode 100644 index 0000000000..7fbc17334f --- /dev/null +++ b/panda/src/bullet/bulletHeightfieldShape.cxx @@ -0,0 +1,68 @@ +// Filename: bulletHeightfieldShape.cxx +// Created by: enn0x (05Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletHeightfieldShape.h" + +TypeHandle BulletHeightfieldShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletHeightfieldShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletHeightfieldShape:: +BulletHeightfieldShape(const PNMImage &image, float max_height, BulletUpAxis up) { + + int num_rows = image.get_x_size(); + int num_columns = image.get_y_size(); + + _data = new float[num_rows * num_columns]; + + for (int row=0; row < num_rows; row++) { + for (int column=0; column < num_columns; column++) { + _data[num_columns * row + column] = + max_height * image.get_bright(column, num_columns - row - 1); + } + } + + _shape = new btHeightfieldTerrainShape(num_rows, num_columns, + _data, + max_height, + up, + true, false); + _shape->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHeightfieldShape::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionShape *BulletHeightfieldShape:: +ptr() const { + + return _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHeightfieldShape::set_use_diamond_subdivision +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletHeightfieldShape:: +set_use_diamond_subdivision(bool flag) { + + return _shape->setUseDiamondSubdivision(flag); +} + diff --git a/panda/src/bullet/bulletHeightfieldShape.h b/panda/src/bullet/bulletHeightfieldShape.h new file mode 100644 index 0000000000..41ab906d21 --- /dev/null +++ b/panda/src/bullet/bulletHeightfieldShape.h @@ -0,0 +1,69 @@ +// Filename: bulletHeightfieldShape.h +// Created by: enn0x (05Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_HEIGHTFIELD_SHAPE_H__ +#define __BULLET_HEIGHTFIELD_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletShape.h" + +#include "pnmImage.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletHeightfieldShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletHeightfieldShape : public BulletShape { + +PUBLISHED: + BulletHeightfieldShape(const PNMImage &image, float max_height, BulletUpAxis up=Z_up); + INLINE ~BulletHeightfieldShape(); + + void set_use_diamond_subdivision(bool flag=true); + +public: + virtual btCollisionShape *ptr() const; + +private: + float *_data; + btHeightfieldTerrainShape *_shape; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletShape::init_type(); + register_type(_type_handle, "BulletHeightfieldShape", + BulletShape::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletHeightfieldShape.I" + +#endif // __BULLET_HEIGHTFIELD_SHAPE_H__ diff --git a/panda/src/bullet/bulletHelper.I b/panda/src/bullet/bulletHelper.I new file mode 100644 index 0000000000..f4ca9456d0 --- /dev/null +++ b/panda/src/bullet/bulletHelper.I @@ -0,0 +1,42 @@ +// Filename: bulletHelper.I +// Created by: enn0x (19Jan11) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletHelper::get_sb_index +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PT(InternalName) BulletHelper:: +get_sb_index() { + + if (_sb_index == (InternalName *)NULL) { + _sb_index = InternalName::make("sb_index"); + } + return _sb_index; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHelper::get_sb_flip +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE PT(InternalName) BulletHelper:: +get_sb_flip() { + + if (_sb_flip == (InternalName *)NULL) { + _sb_flip = InternalName::make("sb_flip"); + } + return _sb_flip; +} diff --git a/panda/src/bullet/bulletHelper.cxx b/panda/src/bullet/bulletHelper.cxx new file mode 100644 index 0000000000..e1ef6feebf --- /dev/null +++ b/panda/src/bullet/bulletHelper.cxx @@ -0,0 +1,307 @@ +// Filename: bulletHelper.cxx +// Created by: enn0x (19Jan11) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletHelper.h" +#include "bulletRigidBodyNode.h" +#include "bulletGhostNode.h" + +#include "geomVertexRewriter.h" + +PT(InternalName) BulletHelper::_sb_index; +PT(InternalName) BulletHelper::_sb_flip; + +//////////////////////////////////////////////////////////////////// +// Function: BulletHelper::from_collision_solids +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +NodePathCollection BulletHelper:: +from_collision_solids(NodePath &np, bool clear) { + + NodePathCollection result; + + // Iterate over all CollisionNodes below the given node + NodePathCollection npc = np.find_all_matches( "**/+CollisionNode" ); + + for (int i=0; iadd_shapes_from_collision_solids(cnode); + body->set_transform(cnp.get_transform(np)); + body->set_into_collide_mask(cnode->get_into_collide_mask()); + body->set_mass(0.0f); + body->set_name(cnode->get_name()); + + bnode = body; + } + else { + PT(BulletGhostNode) ghost; + + ghost = new BulletGhostNode(); + ghost->add_shapes_from_collision_solids(cnode); + ghost->set_transform(cnp.get_transform(np)); + ghost->set_into_collide_mask(cnode->get_into_collide_mask()); + ghost->set_name(cnode->get_name()); + + bnode = ghost; + } + + // Remove collision node if requested + if (clear) { + cnp.remove_node(); + } + + result.add_path(NodePath::any_path(bnode)); + } + + return result; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHelper::is_tangible +// Access: Private +// Description: Returns TRUE if at least one CollisionSolid of +// the given CollisionNode is tangible. Returns FALSE +// if all CollisionSolids are intangible. +//////////////////////////////////////////////////////////////////// +bool BulletHelper:: +is_tangible(CollisionNode *cnode) { + + for (int j=0; jget_num_solids(); j++) { + CPT(CollisionSolid) solid = cnode->get_solid(j); + if (solid->is_tangible()) { + return true; + } + } + + return false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHelper::add_sb_index_column +// Access: Private +// Description: +//////////////////////////////////////////////////////////////////// +CPT(GeomVertexFormat) BulletHelper:: +add_sb_index_column(const GeomVertexFormat *format) { + + PT(InternalName) name = BulletHelper::get_sb_index(); + + if (format->has_column(name)) return format; + + PT(GeomVertexArrayFormat) array; + PT(GeomVertexFormat) unregistered_format; + CPT(GeomVertexFormat) registered_format; + + array = new GeomVertexArrayFormat(); + array->add_column(name, 1, Geom::NT_uint16, Geom::C_index); + + unregistered_format = new GeomVertexFormat(*format); + unregistered_format->add_array(array); + + registered_format = GeomVertexFormat::register_format(unregistered_format); + + return registered_format; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHelper::add_sb_flip_column +// Access: Private +// Description: +//////////////////////////////////////////////////////////////////// +CPT(GeomVertexFormat) BulletHelper:: +add_sb_flip_column(const GeomVertexFormat *format) { + + PT(InternalName) name = BulletHelper::get_sb_flip(); + + if (format->has_column(name)) return format; + + PT(GeomVertexArrayFormat) array; + PT(GeomVertexFormat) unregistered_format; + CPT(GeomVertexFormat) registered_format; + + array = new GeomVertexArrayFormat(); + array->add_column(name, 1, Geom::NT_uint8, Geom::C_other); + + unregistered_format = new GeomVertexFormat(*format); + unregistered_format->add_array(array); + + registered_format = GeomVertexFormat::register_format(unregistered_format); + + return registered_format; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHelper::make_geom_from_faces +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PT(Geom) BulletHelper:: +make_geom_from_faces(BulletSoftBodyNode *node, const GeomVertexFormat *format, bool two_sided) { + + return make_geom(node, format, two_sided, true); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHelper::make_geom_from_links +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PT(Geom) BulletHelper:: +make_geom_from_links(BulletSoftBodyNode *node, const GeomVertexFormat *format) { + + return make_geom(node, format, false, false); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHelper::make_geom +// Access: Private +// Description: +//////////////////////////////////////////////////////////////////// +PT(Geom) BulletHelper:: +make_geom(BulletSoftBodyNode *node, const GeomVertexFormat *format, bool two_sided, bool use_faces) { + + btTransform trans; + get_node_transform(trans, node); + + btSoftBody *body = (btSoftBody *)node->get_object(); + + PT(Geom) geom; + PT(GeomPrimitive) prim; + PT(GeomVertexData) vdata; + + CPT(GeomVertexFormat) fmt = (format) ? format : GeomVertexFormat::get_v3n3t2(); + fmt = BulletHelper::add_sb_flip_column(fmt); + + nassertr(fmt->has_column(InternalName::get_vertex()), NULL); + nassertr(fmt->has_column(InternalName::get_normal()), NULL); + + btSoftBody::tNodeArray &nodes(body->m_nodes); + + // Vertex data + vdata = new GeomVertexData("", fmt, Geom::UH_stream); + + GeomVertexWriter vwriter(vdata, InternalName::get_vertex()); + GeomVertexWriter nwriter(vdata, InternalName::get_normal()); + GeomVertexWriter fwriter(vdata, BulletHelper::get_sb_flip()); + + for (int j=0; jm_faces); + + prim = new GeomTriangles(Geom::UH_stream); + prim->set_shade_model(Geom::SM_uniform); + + for (int j=0; jadd_vertices(int(faces[j].m_n[0] - node0), + int(faces[j].m_n[1] - node0), + int(faces[j].m_n[2] - node0)); + prim->close_primitive(); + + if (two_sided) { + prim->add_vertices(nodes.size() + int(faces[j].m_n[0] - node0), + nodes.size() + int(faces[j].m_n[2] - node0), + nodes.size() + int(faces[j].m_n[1] - node0)); + prim->close_primitive(); + } + } + } + else { + btSoftBody::tLinkArray &links(body->m_links); + + prim = new GeomLines(Geom::UH_stream); + prim->set_shade_model(Geom::SM_uniform); + + for (int j=0; jadd_vertices(int(links[j].m_n[0] - node0), + int(links[j].m_n[1] - node0)); + prim->close_primitive(); + } + } + + // Geom + geom = new Geom(vdata); + geom->add_primitive(prim); + + return geom; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHelper::make_texcoords_for_patch +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletHelper:: +make_texcoords_for_patch(Geom *geom, int resx, int resy) { + + PT(GeomVertexData) vdata = geom->modify_vertex_data(); + + nassertv(vdata->has_column(InternalName::get_texcoord())); + + GeomVertexRewriter texcoords(vdata, InternalName::get_texcoord()); + + int n = resx * resy; + int i = 0; + int ix; + int iy; + float u; + float v; + + while (!texcoords.is_at_end()) { + ix = i / resx; + iy = i % resy; + + if (i > n) ix -= 1; + + u = (float)ix/(float)(resx - 1); + v = (float)iy/(float)(resy - 1); + + texcoords.set_data2f(u, v); + i++; + } +} + diff --git a/panda/src/bullet/bulletHelper.h b/panda/src/bullet/bulletHelper.h new file mode 100644 index 0000000000..328ac22f15 --- /dev/null +++ b/panda/src/bullet/bulletHelper.h @@ -0,0 +1,70 @@ +// Filename: bulletHelper.h +// Created by: enn0x (19Jan11) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_HELPER_H__ +#define __BULLET_HELPER_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" + +#include "collisionNode.h" +#include "nodePath.h" +#include "nodePathCollection.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletHelper +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletHelper { + +PUBLISHED: + + // Collision shapes + static NodePathCollection from_collision_solids(NodePath &np, bool clear=false); + + // Internal names + INLINE static PT(InternalName) get_sb_index(); + INLINE static PT(InternalName) get_sb_flip(); + + // Geom vertex data + static CPT(GeomVertexFormat) add_sb_index_column(const GeomVertexFormat *format); + static CPT(GeomVertexFormat) add_sb_flip_column(const GeomVertexFormat *format); + + // Geom utils + static PT(Geom) make_geom_from_faces(BulletSoftBodyNode *node, + const GeomVertexFormat *format=NULL, + bool two_sided=false); + + static PT(Geom) make_geom_from_links(BulletSoftBodyNode *node, + const GeomVertexFormat *format=NULL); + + static void make_texcoords_for_patch(Geom *geom, int resx, int resy); + +private: + static PT(InternalName) _sb_index; + static PT(InternalName) _sb_flip; + + static bool is_tangible(CollisionNode *cnode); + + static PT(Geom) make_geom(BulletSoftBodyNode *node, + const GeomVertexFormat *format, + bool use_faces, + bool two_sided); +}; + +#include "bulletHelper.I" + +#endif // __BULLET_HELPER_H__ diff --git a/panda/src/bullet/bulletHingeConstraint.I b/panda/src/bullet/bulletHingeConstraint.I new file mode 100644 index 0000000000..473c050963 --- /dev/null +++ b/panda/src/bullet/bulletHingeConstraint.I @@ -0,0 +1,25 @@ +// Filename: bulletHingeConstraint.I +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletHingeConstraint::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletHingeConstraint:: +~BulletHingeConstraint() { + + delete _constraint; +} + diff --git a/panda/src/bullet/bulletHingeConstraint.cxx b/panda/src/bullet/bulletHingeConstraint.cxx new file mode 100644 index 0000000000..8464a722a6 --- /dev/null +++ b/panda/src/bullet/bulletHingeConstraint.cxx @@ -0,0 +1,158 @@ +// Filename: bulletHingeConstraint.cxx +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletHingeConstraint.h" +#include "bulletRigidBodyNode.h" + +#include "deg_2_rad.h" + +TypeHandle BulletHingeConstraint::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletHingeConstraint::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletHingeConstraint:: +BulletHingeConstraint(const BulletRigidBodyNode *node_a, + const LPoint3f &pivot_a, + const LVector3f &axis_a, + bool use_frame_a) { + + btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object()); + btVector3 pos_a = LVecBase3f_to_btVector3(pivot_a); + btVector3 vec_a = LVecBase3f_to_btVector3(axis_a); + + _constraint = new btHingeConstraint(*ptr_a, pos_a, vec_a, use_frame_a); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHingeConstraint::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletHingeConstraint:: +BulletHingeConstraint(const BulletRigidBodyNode *node_a, + const BulletRigidBodyNode *node_b, + const LPoint3f &pivot_a, + const LPoint3f &pivot_b, + const LVector3f &axis_a, + const LVector3f &axis_b, + bool use_frame_a) { + + btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object()); + btVector3 pos_a = LVecBase3f_to_btVector3(pivot_a); + btVector3 vec_a = LVecBase3f_to_btVector3(axis_a); + + btRigidBody *ptr_b = btRigidBody::upcast(node_b->get_object()); + btVector3 pos_b = LVecBase3f_to_btVector3(pivot_b); + btVector3 vec_b = LVecBase3f_to_btVector3(axis_b); + + _constraint = new btHingeConstraint(*ptr_a, *ptr_b, pos_a, pos_b, vec_a, vec_b, use_frame_a); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHingeConstraint::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btTypedConstraint *BulletHingeConstraint:: +ptr() const { + + return _constraint; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHingeConstraint::set_angular_only +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletHingeConstraint:: +set_angular_only(bool value) { + + return _constraint->setAngularOnly(value); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHingeConstraint::get_angular_only +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletHingeConstraint:: +get_angular_only() const { + + return _constraint->getAngularOnly(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHingeConstraint::set_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletHingeConstraint:: +set_limit(float low, float high, float softness, float bias, float relaxation) { + + low = deg_2_rad(low); + high = deg_2_rad(high); + + _constraint->setLimit(low, high, softness, bias, relaxation); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHingeConstraint::set_axis +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletHingeConstraint:: +set_axis(const LVector3f &axis) { + + nassertv(!axis.is_nan()); + + btVector3 v = LVecBase3f_to_btVector3(axis); + _constraint->setAxis(v); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHingeConstraint::get_lower_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletHingeConstraint:: +get_lower_limit() const { + + return rad_2_deg(_constraint->getLowerLimit()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHingeConstraint::get_upper_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletHingeConstraint:: +get_upper_limit() const { + + return rad_2_deg(_constraint->getUpperLimit()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletHingeConstraint::get_hinge_angle +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletHingeConstraint:: +get_hinge_angle() { + + return rad_2_deg(_constraint->getHingeAngle()); +} + diff --git a/panda/src/bullet/bulletHingeConstraint.h b/panda/src/bullet/bulletHingeConstraint.h new file mode 100644 index 0000000000..1ec6b2c7c0 --- /dev/null +++ b/panda/src/bullet/bulletHingeConstraint.h @@ -0,0 +1,88 @@ +// Filename: bulletHingeConstraint.h +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_HINGE_CONSTRAINT_H__ +#define __BULLET_HINGE_CONSTRAINT_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletConstraint.h" + +#include "lpoint3.h" +#include "lvector3.h" + +class BulletRigidBodyNode; + +//////////////////////////////////////////////////////////////////// +// Class : BulletHingeConstraint +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletHingeConstraint : public BulletConstraint { + +PUBLISHED: + BulletHingeConstraint(const BulletRigidBodyNode *node_a, + const LPoint3f &pivot_a, + const LVector3f &axis_a, + bool use_frame_a=false); + BulletHingeConstraint(const BulletRigidBodyNode *node_a, + const BulletRigidBodyNode *node_b, + const LPoint3f &pivot_a, + const LPoint3f &pivot_b, + const LVector3f &axis_a, + const LVector3f &axis_b, + bool use_frame_a=false); + INLINE ~BulletHingeConstraint(); + + float get_hinge_angle(); + float get_lower_limit() const; + float get_upper_limit() const; + bool get_angular_only() const; + + void set_angular_only(bool value); + void set_limit(float low, float high, float softness=0.9f, float bias=0.3f, float relaxation=1.0f); + void set_axis(const LVector3f &axis); + +public: + virtual btTypedConstraint *ptr() const; + +private: + btHingeConstraint *_constraint; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletConstraint::init_type(); + register_type(_type_handle, "BulletHingeConstraint", + BulletConstraint::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletHingeConstraint.I" + +#endif // __BULLET_HINGE_CONSTRAINT_H__ diff --git a/panda/src/bullet/bulletManifoldPoint.I b/panda/src/bullet/bulletManifoldPoint.I new file mode 100644 index 0000000000..03b8b1b9a1 --- /dev/null +++ b/panda/src/bullet/bulletManifoldPoint.I @@ -0,0 +1,24 @@ +// Filename: bulletManifoldPoint.I +// Created by: enn0x (07Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletManifoldPoint::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletManifoldPoint:: +~BulletManifoldPoint() { + +} + diff --git a/panda/src/bullet/bulletManifoldPoint.cxx b/panda/src/bullet/bulletManifoldPoint.cxx new file mode 100644 index 0000000000..52259597d5 --- /dev/null +++ b/panda/src/bullet/bulletManifoldPoint.cxx @@ -0,0 +1,103 @@ +// Filename: bulletManifoldPoint.cxx +// Created by: enn0x (07Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletManifoldPoint.h" + +//////////////////////////////////////////////////////////////////// +// Function: BulletManifoldPoint::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +BulletManifoldPoint:: +BulletManifoldPoint(btManifoldPoint &pt) : _pt(pt) { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletManifoldPoint::get_lift_time +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +int BulletManifoldPoint:: +get_lift_time() const { + + return _pt.getLifeTime(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletManifoldPoint::get_distance +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletManifoldPoint:: +get_distance() const { + + return _pt.getDistance(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletManifoldPoint::get_applied_impulse +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletManifoldPoint:: +get_applied_impulse() const { + + return _pt.getAppliedImpulse(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletManifoldPoint::get_position_world_on_a +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletManifoldPoint:: +get_position_world_on_a() const { + + return btVector3_to_LPoint3f(_pt.getPositionWorldOnA()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletManifoldPoint::get_position_world_on_b +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletManifoldPoint:: +get_position_world_on_b() const { + + return btVector3_to_LPoint3f(_pt.getPositionWorldOnB()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletManifoldPoint::get_local_point_a +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletManifoldPoint:: +get_local_point_a() const { + + return btVector3_to_LPoint3f(_pt.m_localPointA); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletManifoldPoint::get_local_point_b +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletManifoldPoint:: +get_local_point_b() const { + + return btVector3_to_LPoint3f(_pt.m_localPointB); +} + diff --git a/panda/src/bullet/bulletManifoldPoint.h b/panda/src/bullet/bulletManifoldPoint.h new file mode 100644 index 0000000000..ddd5a5b826 --- /dev/null +++ b/panda/src/bullet/bulletManifoldPoint.h @@ -0,0 +1,51 @@ +// Filename: bulletManifoldPoint.h +// Created by: enn0x (07Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_MANIFOLD_POINT_H__ +#define __BULLET_MANIFOLD_POINT_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" + +#include "lpoint3.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletManifoldPoint +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletManifoldPoint { + +PUBLISHED: + INLINE ~BulletManifoldPoint(); + + int get_lift_time() const; + float get_distance() const; + float get_applied_impulse() const; + LPoint3f get_position_world_on_a() const; + LPoint3f get_position_world_on_b() const; + LPoint3f get_local_point_a() const; + LPoint3f get_local_point_b() const; + +public: + BulletManifoldPoint(btManifoldPoint &pt); + +private: + btManifoldPoint &_pt; +}; + +#include "bulletManifoldPoint.I" + +#endif // __BULLET_MANIFOLD_POINT_H__ diff --git a/panda/src/bullet/bulletPersistentManifold.I b/panda/src/bullet/bulletPersistentManifold.I new file mode 100644 index 0000000000..70fc7bd7ef --- /dev/null +++ b/panda/src/bullet/bulletPersistentManifold.I @@ -0,0 +1,24 @@ +// Filename: bulletPersistentManifold.I +// Created by: enn0x (07Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletPersistentManifold::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletPersistentManifold:: +~BulletPersistentManifold() { + +} + diff --git a/panda/src/bullet/bulletPersistentManifold.cxx b/panda/src/bullet/bulletPersistentManifold.cxx new file mode 100644 index 0000000000..db834af366 --- /dev/null +++ b/panda/src/bullet/bulletPersistentManifold.cxx @@ -0,0 +1,110 @@ +// Filename: bulletPersistentManifold.cxx +// Created by: enn0x (07Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletPersistentManifold.h" +#include "bulletManifoldPoint.h" + +//////////////////////////////////////////////////////////////////// +// Function: BulletPersistentManifold::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +BulletPersistentManifold:: +BulletPersistentManifold(btPersistentManifold *manifold) : _manifold(manifold) { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletPersistentManifold::get_contact_breaking_threshold +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletPersistentManifold:: +get_contact_breaking_threshold() const { + + return _manifold->getContactBreakingThreshold(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletPersistentManifold::get_contact_processing_threshold +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletPersistentManifold:: +get_contact_processing_threshold() const { + + return _manifold->getContactProcessingThreshold(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletPersistentManifold::set_suspension_stiffness +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletPersistentManifold:: +clear_manifold() { + + _manifold->clearManifold(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletPersistentManifold::get_node0 +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PandaNode *BulletPersistentManifold:: +get_node0() { + + btCollisionObject *obj = static_cast(_manifold->getBody0()); + + return (obj) ? (PandaNode *)obj->getUserPointer(): NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletPersistentManifold::get_node1 +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PandaNode *BulletPersistentManifold:: +get_node1() { + + btCollisionObject *obj = static_cast(_manifold->getBody1()); + + return (obj) ? (PandaNode *)obj->getUserPointer(): NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletPersistentManifold::get_num_manifold_points +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +int BulletPersistentManifold:: +get_num_manifold_points() const { + + return _manifold->getNumContacts(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletPersistentManifold::get_manifold_point +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletManifoldPoint *BulletPersistentManifold:: +get_manifold_point(int idx) const { + + nassertr(idx < _manifold->getNumContacts(), NULL) + + return new BulletManifoldPoint(_manifold->getContactPoint(idx)); +} + diff --git a/panda/src/bullet/bulletPersistentManifold.h b/panda/src/bullet/bulletPersistentManifold.h new file mode 100644 index 0000000000..4c3faff7a0 --- /dev/null +++ b/panda/src/bullet/bulletPersistentManifold.h @@ -0,0 +1,56 @@ +// Filename: bulletPersistentManifold.h +// Created by: enn0x (07Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_PERSISTENT_MANIFOLD_H__ +#define __BULLET_PERSISTENT_MANIFOLD_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" + +#include "pandaNode.h" + +class BulletManifoldPoint; + +//////////////////////////////////////////////////////////////////// +// Class : BulletPersistentManifold +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletPersistentManifold { + +PUBLISHED: + INLINE ~BulletPersistentManifold(); + + PandaNode *get_node0(); + PandaNode *get_node1(); + + int get_num_manifold_points() const; + BulletManifoldPoint *get_manifold_point(int idx) const; + MAKE_SEQ(get_manifold_points, get_num_manifold_points, get_manifold_point); + + float get_contact_breaking_threshold() const; + float get_contact_processing_threshold() const; + + void clear_manifold(); + +public: + BulletPersistentManifold(btPersistentManifold *manifold); + +private: + btPersistentManifold *_manifold; +}; + +#include "bulletPersistentManifold.I" + +#endif // __BULLET_PERSISTENT_MANIFOLD_H__ diff --git a/panda/src/bullet/bulletPlaneShape.I b/panda/src/bullet/bulletPlaneShape.I new file mode 100644 index 0000000000..16393be127 --- /dev/null +++ b/panda/src/bullet/bulletPlaneShape.I @@ -0,0 +1,47 @@ +// Filename: bulletPlaneShape.I +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletPlaneShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletPlaneShape:: +~BulletPlaneShape() { + + delete _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletPlaneShape::get_plane_constant +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletPlaneShape:: +get_plane_constant() const { + + return _shape->getPlaneConstant(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletPlaneShape::get_plane_normal +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE LVector3f BulletPlaneShape:: +get_plane_normal() const { + + return btVector3_to_LVector3f(_shape->getPlaneNormal()); +} + diff --git a/panda/src/bullet/bulletPlaneShape.cxx b/panda/src/bullet/bulletPlaneShape.cxx new file mode 100644 index 0000000000..6243d387a1 --- /dev/null +++ b/panda/src/bullet/bulletPlaneShape.cxx @@ -0,0 +1,57 @@ +// Filename: bulletPlaneShape.cxx +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletPlaneShape.h" + +TypeHandle BulletPlaneShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletPlaneShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletPlaneShape:: +BulletPlaneShape(const LVector3f &normal, float constant) { + + btVector3 btNormal = LVecBase3f_to_btVector3(normal); + + _shape = new btStaticPlaneShape(btNormal, constant); + _shape->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletPlaneShape::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionShape *BulletPlaneShape:: +ptr() const { + + return _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletPlaneShape::make_from_solid +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +BulletPlaneShape *BulletPlaneShape:: +make_from_solid(const CollisionPlane *solid) { + + LVector3f normal = solid->get_normal(); + float constant = solid->dist_to_plane(LPoint3f(0, 0, 0)); + + return new BulletPlaneShape(normal, constant); +} + diff --git a/panda/src/bullet/bulletPlaneShape.h b/panda/src/bullet/bulletPlaneShape.h new file mode 100644 index 0000000000..56960dae66 --- /dev/null +++ b/panda/src/bullet/bulletPlaneShape.h @@ -0,0 +1,72 @@ +// Filename: bulletPlaneShape.h +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_PLANE_SHAPE_H__ +#define __BULLET_PLANE_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletShape.h" + +#include "collisionPlane.h" +#include "lvector3.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletPlaneShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletPlaneShape : public BulletShape { + +PUBLISHED: + BulletPlaneShape(const LVector3f &normal, float constant); + INLINE ~BulletPlaneShape(); + + INLINE LVector3f get_plane_normal() const; + INLINE float get_plane_constant() const; + + static BulletPlaneShape *make_from_solid(const CollisionPlane *solid); + +public: + virtual btCollisionShape *ptr() const; + +private: + btStaticPlaneShape *_shape; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletShape::init_type(); + register_type(_type_handle, "BulletPlaneShape", + BulletShape::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletPlaneShape.I" + +#endif // __BULLET_PLANE_SHAPE_H__ diff --git a/panda/src/bullet/bulletRigidBodyNode.I b/panda/src/bullet/bulletRigidBodyNode.I new file mode 100644 index 0000000000..519f2b1b5c --- /dev/null +++ b/panda/src/bullet/bulletRigidBodyNode.I @@ -0,0 +1,71 @@ +// Filename: bulletRigidBodyNode.I +// Created by: enn0x (19Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletRigidBodyNode:: +~BulletRigidBodyNode() { + + delete _body; + delete _motion; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::set_linear_damping +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void BulletRigidBodyNode:: +set_linear_damping(float value) { + + _body->setDamping(value, _body->getAngularDamping()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::set_angular_damping +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE void BulletRigidBodyNode:: +set_angular_damping(float value) { + + _body->setDamping(_body->getLinearDamping(), value); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::get_linear_damping +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletRigidBodyNode:: +get_linear_damping() const { + + return _body->getLinearDamping(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::get_angular_damping +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletRigidBodyNode:: +get_angular_damping() const { + + return _body->getAngularDamping(); +} + diff --git a/panda/src/bullet/bulletRigidBodyNode.cxx b/panda/src/bullet/bulletRigidBodyNode.cxx new file mode 100644 index 0000000000..49e815831f --- /dev/null +++ b/panda/src/bullet/bulletRigidBodyNode.cxx @@ -0,0 +1,418 @@ +// Filename: bulletRigidBodyNode.cxx +// Created by: enn0x (19Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletRigidBodyNode.h" +#include "bulletShape.h" + +TypeHandle BulletRigidBodyNode::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletRigidBodyNode:: +BulletRigidBodyNode(const char *name) : BulletBodyNode(name) { + + // Setup motion state + _motion = new MotionState(this); + + // Setup mass properties + btScalar mass(0.0); + btVector3 inertia(0, 0, 0); + + btRigidBody::btRigidBodyConstructionInfo ci(mass, _motion, _shape, inertia); + + // Enable additional damping + if (bullet_additional_damping) { + ci.m_additionalDamping = true; + ci.m_additionalDampingFactor = bullet_additional_damping_linear_factor; + ci.m_additionalLinearDampingThresholdSqr = bullet_additional_damping_linear_threshold; + ci.m_additionalAngularDampingFactor = bullet_additional_damping_angular_factor; + ci.m_additionalAngularDampingThresholdSqr = bullet_additional_damping_angular_threshold; + } + + // Setup rigid body + _body = new btRigidBody(ci); + _body->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::get_object +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionObject *BulletRigidBodyNode:: +get_object() const { + + return _body; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::shape_changed +// Access: Published +// Description: Hook which will be called whenever the total shape +// of a body changed. Used for example to update +// the mass properties (inertia) of a rigid body. +// The default implementation does nothing. +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +shape_changed() { + + set_mass(get_mass()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::set_mass +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +set_mass(float mass) { + + btVector3 inertia(0.0f, 0.0f, 0.0f); + + if (mass > 0.0f) { + _body->getCollisionShape()->calculateLocalInertia(mass, inertia); + } + + _body->setMassProps(mass, inertia); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::get_mass +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletRigidBodyNode:: +get_mass() const { + + btScalar invMass = _body->getInvMass(); + + if (invMass == 0.0f) { + return 0.0f; + } + else { + return 1.0f / invMass; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::apply_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +apply_force(const LVector3f &force, const LPoint3f &pos) { + + nassertv_always(!force.is_nan()); + nassertv_always(!pos.is_nan()); + + _body->applyForce(LVecBase3f_to_btVector3(force), + LVecBase3f_to_btVector3(pos)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::apply_central_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +apply_central_force(const LVector3f &force) { + + nassertv_always(!force.is_nan()); + + _body->applyCentralForce(LVecBase3f_to_btVector3(force)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::apply_torque +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +apply_torque(const LVector3f &torque) { + + nassertv_always(!torque.is_nan()); + + _body->applyTorque(LVecBase3f_to_btVector3(torque)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::apply_torque_impulse +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +apply_torque_impulse(const LVector3f &torque) { + + nassertv_always(!torque.is_nan()); + + _body->applyTorqueImpulse(LVecBase3f_to_btVector3(torque)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::apply_impulse +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +apply_impulse(const LVector3f &impulse, const LPoint3f &pos) { + + nassertv_always(!impulse.is_nan()); + nassertv_always(!pos.is_nan()); + + _body->applyImpulse(LVecBase3f_to_btVector3(impulse), + LVecBase3f_to_btVector3(pos)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::apply_central_impulse +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +apply_central_impulse(const LVector3f &impulse) { + + nassertv_always(!impulse.is_nan()); + + _body->applyCentralImpulse(LVecBase3f_to_btVector3(impulse)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::parents_changed +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +parents_changed() { + + transform_changed(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::transform_changed +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +transform_changed() { + + if (_disable_transform_changed) return; + + btTransform trans; + get_node_transform(trans, this); + _body->setWorldTransform(trans); + _body->setInterpolationWorldTransform(trans); + + BulletBodyNode::transform_changed(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::MotionState::getWorldTransform +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode::MotionState:: +getWorldTransform(btTransform &trans) const { + + get_node_transform(trans, _node); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::MotionState::setWorldTransform +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode::MotionState:: +setWorldTransform(const btTransform &trans) { + + if (trans.getOrigin().getX() != trans.getOrigin().getX()) { + bullet_cat.error() << "setWorldTransform: trans is NAN!" << endl; + return; + } + + LVecBase3f scale = _node->get_transform()->get_scale(); + CPT(TransformState) ts = btTrans_to_TransformState(trans, scale); + + // Disable transform_changed callback + _node->_disable_transform_changed = true; + + if (_node->get_num_parents() == 0) { + _node->set_transform(ts); + } + else { + NodePath np = NodePath::any_path(_node); + np.set_transform(np.get_top(), ts); + } + + // Re-enable transform_changed callback again + _node->_disable_transform_changed = false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::set_center_of_mass_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +set_center_of_mass_pos(const LPoint3f &pos) { + + nassertv_always(!pos.is_nan()); + + btTransform xform; + xform.setIdentity(); + xform.setOrigin(LVecBase3f_to_btVector3(pos)); + + _body->setCenterOfMassTransform(xform); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::get_center_of_mass_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletRigidBodyNode:: +get_center_of_mass_pos() const { + + return btVector3_to_LPoint3f(_body->getCenterOfMassPosition()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::get_linear_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f BulletRigidBodyNode:: +get_linear_velocity() const { + + return btVector3_to_LVector3f(_body->getLinearVelocity()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::get_angular_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f BulletRigidBodyNode:: +get_angular_velocity() const { + + return btVector3_to_LVector3f(_body->getAngularVelocity()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::set_linear_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +set_linear_velocity(const LVector3f &velocity) { + + nassertv_always(!velocity.is_nan()); + + _body->setLinearVelocity(LVecBase3f_to_btVector3(velocity)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::set_angular_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +set_angular_velocity(const LVector3f &velocity) { + + nassertv_always(!velocity.is_nan()); + + _body->setAngularVelocity(LVecBase3f_to_btVector3(velocity)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::clear_forces +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +clear_forces() { + + _body->clearForces(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::get_linear_sleep_threshold +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletRigidBodyNode:: +get_linear_sleep_threshold() const { + + return _body->getLinearSleepingThreshold(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::get_angular_sleep_threshold +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletRigidBodyNode:: +get_angular_sleep_threshold() const { + + return _body->getAngularSleepingThreshold(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::set_linear_sleep_threshold +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +set_linear_sleep_threshold(float threshold) { + + _body->setSleepingThresholds(_body->getLinearSleepingThreshold(), threshold); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::set_angular_sleep_threshold +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +set_angular_sleep_threshold(float threshold) { + + _body->setSleepingThresholds(threshold, _body->getAngularSleepingThreshold()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::set_gravity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletRigidBodyNode:: +set_gravity(const LVector3f &gravity) { + + nassertv_always(!gravity.is_nan()); + + _body->setGravity(LVecBase3f_to_btVector3(gravity)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletRigidBodyNode::get_gravity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f BulletRigidBodyNode:: +get_gravity() const { + + return btVector3_to_LVector3f(_body->getGravity()); +} + diff --git a/panda/src/bullet/bulletRigidBodyNode.h b/panda/src/bullet/bulletRigidBodyNode.h new file mode 100644 index 0000000000..91ea7d0106 --- /dev/null +++ b/panda/src/bullet/bulletRigidBodyNode.h @@ -0,0 +1,127 @@ +// Filename: bulletRigidBodyNode.h +// Created by: enn0x (19Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_RIGID_BODY_NODE_H__ +#define __BULLET_RIGID_BODY_NODE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletBodyNode.h" + +#include "pandaNode.h" +#include "collideMask.h" + +class BulletShape; + +//////////////////////////////////////////////////////////////////// +// Class : BulletRigidBodyNode +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletRigidBodyNode : public BulletBodyNode { + +PUBLISHED: + BulletRigidBodyNode(const char *name="rigid"); + INLINE ~BulletRigidBodyNode(); + + // Mass + LPoint3f get_center_of_mass_pos() const; + float get_mass() const; + void set_center_of_mass_pos(const LPoint3f &pos); + void set_mass(float mass); + + // Velocity + LVector3f get_linear_velocity() const; + LVector3f get_angular_velocity() const; + void set_linear_velocity(const LVector3f &velocity); + void set_angular_velocity(const LVector3f &velocity); + + // Damping + INLINE float get_linear_damping() const; + INLINE float get_angular_damping() const; + INLINE void set_linear_damping(float value); + INLINE void set_angular_damping(float value); + + // Forces + void clear_forces(); + void apply_force(const LVector3f &force, const LPoint3f &pos); + void apply_central_force(const LVector3f &force); + void apply_impulse(const LVector3f &impulse, const LPoint3f &pos); + void apply_central_impulse(const LVector3f &impulse); + void apply_torque(const LVector3f &torque); + void apply_torque_impulse(const LVector3f &torque); + + // Deactivation thresholds + float get_linear_sleep_threshold() const; + float get_angular_sleep_threshold() const; + void set_linear_sleep_threshold(float threshold); + void set_angular_sleep_threshold(float threshold); + + // Gravity + void set_gravity(const LVector3f &gravity); + LVector3f get_gravity() const; + +public: + virtual btCollisionObject *get_object() const; + +protected: + virtual void parents_changed(); + virtual void transform_changed(); + +private: + virtual void shape_changed(); + + class MotionState : public btMotionState { + + public: + MotionState(BulletRigidBodyNode *node) : _node(node) {}; + ~MotionState() {}; + + virtual void getWorldTransform(btTransform &trans) const; + virtual void setWorldTransform(const btTransform &trans); + + private: + BulletRigidBodyNode *_node; + }; + + btRigidBody *_body; + MotionState *_motion; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletBodyNode::init_type(); + register_type(_type_handle, "BulletRigidBodyNode", + BulletBodyNode::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletRigidBodyNode.I" + +#endif // __BULLET_RIGID_BODY_NODE_H__ + diff --git a/panda/src/bullet/bulletShape.I b/panda/src/bullet/bulletShape.I new file mode 100644 index 0000000000..8d8d463d82 --- /dev/null +++ b/panda/src/bullet/bulletShape.I @@ -0,0 +1,101 @@ +// Filename: bulletShape.I +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletShape:: +~BulletShape() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::is_polyhedral +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletShape:: +is_polyhedral() const { + + return ptr()->isPolyhedral(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::is_convex +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletShape:: +is_convex() const { + + return ptr()->isConvex(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::is_convex_2d +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletShape:: +is_convex_2d() const { + + return ptr()->isConvex2d(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::is_concave +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletShape:: +is_concave() const { + + return ptr()->isConcave(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::is_infinite +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletShape:: +is_infinite() const { + + return ptr()->isInfinite(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::is_non_moving +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletShape:: +is_non_moving() const { + + return ptr()->isNonMoving(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::is_soft_body +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletShape:: +is_soft_body() const { + + return ptr()->isSoftBody(); +} + diff --git a/panda/src/bullet/bulletShape.cxx b/panda/src/bullet/bulletShape.cxx new file mode 100644 index 0000000000..775ba34ad6 --- /dev/null +++ b/panda/src/bullet/bulletShape.cxx @@ -0,0 +1,75 @@ +// Filename: bulletShape.cxx +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletShape.h" +#include "bullet_utils.h" + +TypeHandle BulletShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::get_name +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +const char *BulletShape:: +get_name() const { + + return ptr()->getName(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::get_margin +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletShape:: +get_margin() const { + + return ptr()->getMargin(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::set_margin +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletShape:: +set_margin(float margin) { + + ptr()->setMargin(margin); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::get_local_scale +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVecBase3f BulletShape:: +get_local_scale() const { + + return btVector3_to_LVecBase3f(ptr()->getLocalScaling()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletShape::set_local_scale +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletShape:: +set_local_scale(const LVecBase3f &scale) { + + nassertv(!scale.is_nan()); + ptr()->setLocalScaling(LVecBase3f_to_btVector3(scale)); +} + diff --git a/panda/src/bullet/bulletShape.h b/panda/src/bullet/bulletShape.h new file mode 100644 index 0000000000..8c7fdc059e --- /dev/null +++ b/panda/src/bullet/bulletShape.h @@ -0,0 +1,76 @@ +// Filename: bulletShape.h +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_SHAPE_H__ +#define __BULLET_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" + +#include "typedReferenceCount.h" +//#include "lvector3.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletShape : public TypedReferenceCount { + +PUBLISHED: + INLINE virtual ~BulletShape(); + + INLINE bool is_polyhedral() const; + INLINE bool is_convex() const; + INLINE bool is_convex_2d() const; + INLINE bool is_concave() const; + INLINE bool is_infinite() const; + INLINE bool is_non_moving() const; + INLINE bool is_soft_body() const; + + void set_margin(float margin); + void set_local_scale(const LVecBase3f &scaling); + + const char *get_name() const; + float get_margin() const; + LVecBase3f get_local_scale() const; + +public: + virtual btCollisionShape *ptr() const = 0; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + TypedReferenceCount::init_type(); + register_type(_type_handle, "BulletShape", + TypedReferenceCount::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletShape.I" + +#endif // __BULLET_SHAPE_H__ diff --git a/panda/src/bullet/bulletSliderConstraint.I b/panda/src/bullet/bulletSliderConstraint.I new file mode 100644 index 0000000000..64eeb0ef21 --- /dev/null +++ b/panda/src/bullet/bulletSliderConstraint.I @@ -0,0 +1,25 @@ +// Filename: bulletSliderConstraint.I +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletSliderConstraint:: +~BulletSliderConstraint() { + + delete _constraint; +} + diff --git a/panda/src/bullet/bulletSliderConstraint.cxx b/panda/src/bullet/bulletSliderConstraint.cxx new file mode 100644 index 0000000000..c72202d3fb --- /dev/null +++ b/panda/src/bullet/bulletSliderConstraint.cxx @@ -0,0 +1,157 @@ +// Filename: bulletSliderConstraint.cxx +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletSliderConstraint.h" +#include "bulletRigidBodyNode.h" + +#include "deg_2_rad.h" + +TypeHandle BulletSliderConstraint::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSliderConstraint:: +BulletSliderConstraint(const BulletRigidBodyNode *node_a, + const TransformState &frame_a, + bool use_frame_a) { + + btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object()); + btTransform trans_a = LMatrix4f_to_btTrans(frame_a.get_mat()); + + _constraint = new btSliderConstraint(*ptr_a, trans_a, use_frame_a); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSliderConstraint:: +BulletSliderConstraint(const BulletRigidBodyNode *node_a, + const BulletRigidBodyNode *node_b, + const TransformState &frame_a, + const TransformState &frame_b, + bool use_frame_a) { + + btRigidBody *ptr_a = btRigidBody::upcast(node_a->get_object()); + btTransform trans_a = LMatrix4f_to_btTrans(frame_a.get_mat()); + + btRigidBody *ptr_b = btRigidBody::upcast(node_b->get_object()); + btTransform trans_b = LMatrix4f_to_btTrans(frame_b.get_mat()); + + _constraint = new btSliderConstraint(*ptr_a, *ptr_b, trans_a, trans_b, use_frame_a); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btTypedConstraint *BulletSliderConstraint:: +ptr() const { + + return _constraint; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::get_lower_linear_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletSliderConstraint:: +get_lower_linear_limit() { + + return _constraint->getLowerLinLimit(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::get_upper_linear_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletSliderConstraint:: +get_upper_linear_limit() { + + return _constraint->getUpperLinLimit(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::get_lower_angular_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletSliderConstraint:: +get_lower_angular_limit() { + + return rad_2_deg(_constraint->getLowerAngLimit()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::get_upper_angular_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletSliderConstraint:: +get_upper_angular_limit() { + + return rad_2_deg(_constraint->getUpperAngLimit()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::set_lower_linear_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSliderConstraint:: +set_lower_linear_limit(float value) { + + _constraint->setLowerLinLimit(value); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::set_upper_linear_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSliderConstraint:: +set_upper_linear_limit(float value) { + + _constraint->setUpperLinLimit(value); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::set_lower_angular_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSliderConstraint:: +set_lower_angular_limit(float value) { + + _constraint->setLowerAngLimit(deg_2_rad(value)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSliderConstraint::set_upper_angular_limit +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSliderConstraint:: +set_upper_angular_limit(float value) { + + _constraint->setUpperAngLimit(deg_2_rad(value)); +} + diff --git a/panda/src/bullet/bulletSliderConstraint.h b/panda/src/bullet/bulletSliderConstraint.h new file mode 100644 index 0000000000..53a43d0b62 --- /dev/null +++ b/panda/src/bullet/bulletSliderConstraint.h @@ -0,0 +1,85 @@ +// Filename: bulletSliderConstraint.h +// Created by: enn0x (01Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_SLIDER_CONSTRAINT_H__ +#define __BULLET_SLIDER_CONSTRAINT_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletConstraint.h" + +#include "transformState.h" + +class BulletRigidBodyNode; + +//////////////////////////////////////////////////////////////////// +// Class : BulletSliderConstraint +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletSliderConstraint : public BulletConstraint { + +PUBLISHED: + BulletSliderConstraint(const BulletRigidBodyNode *node_a, + const TransformState &frame_a, + bool useFrame_a); + BulletSliderConstraint(const BulletRigidBodyNode *node_a, + const BulletRigidBodyNode *node_b, + const TransformState &frame_a, + const TransformState &frame_b, + bool use_frame_a); + INLINE ~BulletSliderConstraint(); + + float get_lower_linear_limit(); + float get_upper_linear_limit(); + float get_lower_angular_limit(); + float get_upper_angular_limit(); + + void set_lower_linear_limit(float value); + void set_upper_linear_limit(float value); + void set_lower_angular_limit(float value); + void set_upper_angular_limit(float value); + +public: + virtual btTypedConstraint *ptr() const; + +private: + btSliderConstraint *_constraint; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletConstraint::init_type(); + register_type(_type_handle, "BulletSliderConstraint", + BulletConstraint::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletSliderConstraint.I" + +#endif // __BULLET_SLIDER_CONSTRAINT_H__ diff --git a/panda/src/bullet/bulletSoftBodyConfig.I b/panda/src/bullet/bulletSoftBodyConfig.I new file mode 100644 index 0000000000..f7fbc6a6fa --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyConfig.I @@ -0,0 +1,556 @@ +// Filename: bulletSoftBodyConfig.I +// Created by: enn0x (12Apr10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletSoftBodyConfig:: +~BulletSoftBodyConfig() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_velocities_correction_factor +// Access: Published +// Description: Getter for property kVCF. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_velocities_correction_factor() const { + + return _cfg.kVCF; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_velocities_correction_factor +// Access: Published +// Description: Setter for property kVCF. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_velocities_correction_factor(float value) { + + _cfg.kVCF = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_damping_coefficient +// Access: Published +// Description: Getter for property kDP. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_damping_coefficient() const { + + return _cfg.kDP; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_damping_coefficient +// Access: Published +// Description: Setter for property kDP. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_damping_coefficient(float value) { + + _cfg.kDP = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_drag_coefficient +// Access: Published +// Description: Getter for property kDG. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_drag_coefficient() const { + + return _cfg.kDG; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_drag_coefficient +// Access: Published +// Description: Setter for property kDG. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_drag_coefficient(float value) { + + _cfg.kDG = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_lift_coefficient +// Access: Published +// Description: Getter for property kLF. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_lift_coefficient() const { + + return _cfg.kLF; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_lift_coefficient +// Access: Published +// Description: Setter for property kLF. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_lift_coefficient(float value) { + + _cfg.kLF = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_pressure_coefficient +// Access: Published +// Description: Getter for property kPR. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_pressure_coefficient() const { + + return _cfg.kPR; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_pressure_coefficient +// Access: Published +// Description: Setter for property kPR. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_pressure_coefficient(float value) { + + _cfg.kPR = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_volume_conversation_coefficient +// Access: Published +// Description: Getter for property kVC. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_volume_conversation_coefficient() const { + + return _cfg.kVC; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_volume_conversation_coefficient +// Access: Published +// Description: Setter for property kVC. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_volume_conversation_coefficient(float value) { + + _cfg.kVC = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_dynamic_friction_coefficient +// Access: Published +// Description: Getter for property kDF. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_dynamic_friction_coefficient() const { + + return _cfg.kDF; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_dynamic_friction_coefficient +// Access: Published +// Description: Setter for property kDF. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_dynamic_friction_coefficient(float value) { + + _cfg.kDF = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_pose_matching_coefficient +// Access: Published +// Description: Getter for property kMT. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_pose_matching_coefficient() const { + + return _cfg.kMT; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_pose_matching_coefficient +// Access: Published +// Description: Setter for property kMT. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_pose_matching_coefficient(float value) { + + _cfg.kMT = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_rigid_contacts_hardness +// Access: Published +// Description: Getter for property kCHR. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_rigid_contacts_hardness() const { + + return _cfg.kCHR; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_rigid_contacts_hardness +// Access: Published +// Description: Setter for property kCHR. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_rigid_contacts_hardness(float value) { + + _cfg.kCHR = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_kinetic_contacts_hardness +// Access: Published +// Description: Getter for property kKHR. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_kinetic_contacts_hardness() const { + + return _cfg.kKHR; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_kinetic_contacts_hardness +// Access: Published +// Description: Setter for property kKHR. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_kinetic_contacts_hardness(float value) { + + _cfg.kKHR = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_soft_contacts_hardness +// Access: Published +// Description: Getter for property kSHR. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_soft_contacts_hardness() const { + + return _cfg.kSHR; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_soft_contacts_hardness +// Access: Published +// Description: Setter for property kSHR. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_soft_contacts_hardness(float value) { + + _cfg.kSHR = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_anchors_hardness +// Access: Published +// Description: Getter for property kAHR. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_anchors_hardness() const { + + return _cfg.kAHR; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_anchors_hardness +// Access: Published +// Description: Setter for property kAHR. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_anchors_hardness(float value) { + + _cfg.kAHR = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_soft_vs_rigid_hardness +// Access: Published +// Description: Getter for property kSRHR_CL. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_soft_vs_rigid_hardness() const { + + return _cfg.kSRHR_CL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_soft_vs_rigid_hardness +// Access: Published +// Description: Setter for property kSRHR_CL. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_soft_vs_rigid_hardness(float value) { + + _cfg.kSRHR_CL = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_soft_vs_kinetic_hardness +// Access: Published +// Description: Getter for property kSKHR_CL. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_soft_vs_kinetic_hardness() const { + + return _cfg.kSKHR_CL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_soft_vs_kinetic_hardness +// Access: Published +// Description: Setter for property kSKHR_CL. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_soft_vs_kinetic_hardness(float value) { + + _cfg.kSKHR_CL = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_soft_vs_soft_hardness +// Access: Published +// Description: Getter for property kSSHR_CL. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_soft_vs_soft_hardness() const { + + return _cfg.kSSHR_CL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_soft_vs_soft_hardness +// Access: Published +// Description: Setter for property kSSHR_CL. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_soft_vs_soft_hardness(float value) { + + _cfg.kSSHR_CL = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_soft_vs_rigid_impulse_split +// Access: Published +// Description: Getter for property kSR_SPLT_CL. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_soft_vs_rigid_impulse_split() const { + + return _cfg.kSR_SPLT_CL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_soft_vs_rigid_impulse_split +// Access: Published +// Description: Setter for property kSR_SPLT_CL. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_soft_vs_rigid_impulse_split(float value) { + + _cfg.kSR_SPLT_CL = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_soft_vs_kinetic_impulse_split +// Access: Published +// Description: Getter for property kSK_SPLT_CL. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_soft_vs_kinetic_impulse_split() const { + + return _cfg.kSK_SPLT_CL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_soft_vs_kinetic_impulse_split +// Access: Published +// Description: Setter for property kSK_SPLT_CL. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_soft_vs_kinetic_impulse_split(float value) { + + _cfg.kSK_SPLT_CL = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_soft_vs_soft_impulse_split +// Access: Published +// Description: Getter for property kSS_SPLT_CL. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_soft_vs_soft_impulse_split() const { + + return _cfg.kSS_SPLT_CL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_soft_vs_soft_impulse_split +// Access: Published +// Description: Setter for property kSS_SPLT_CL. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_soft_vs_soft_impulse_split(float value) { + + _cfg.kSS_SPLT_CL = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_maxvolume +// Access: Published +// Description: Getter for property maxvolume. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_maxvolume() const { + + return _cfg.maxvolume; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_maxvolume +// Access: Published +// Description: Setter for property maxvolume. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_maxvolume(float value) { + + _cfg.maxvolume = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_timescale +// Access: Published +// Description: Getter for property timescale. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyConfig:: +get_timescale() const { + + return _cfg.timescale; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_timescale +// Access: Published +// Description: Setter for property timescale. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_timescale(float value) { + + _cfg.timescale = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_positions_solver_iterations +// Access: Published +// Description: Getter for property piterations. +//////////////////////////////////////////////////////////////////// +INLINE int BulletSoftBodyConfig:: +get_positions_solver_iterations() const { + + return _cfg.piterations; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_positions_solver_iterations +// Access: Published +// Description: Setter for property piterations. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_positions_solver_iterations(int value) { + + nassertv(value > 0); + _cfg.piterations = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_velocities_solver_iterations +// Access: Published +// Description: Getter for property viterations. +//////////////////////////////////////////////////////////////////// +INLINE int BulletSoftBodyConfig:: +get_velocities_solver_iterations() const { + + return _cfg.viterations; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_velocities_solver_iterations +// Access: Published +// Description: Setter for property viterations. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_velocities_solver_iterations(int value) { + + nassertv(value > 0); + _cfg.viterations = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_drift_solver_iterations +// Access: Published +// Description: Getter for property diterations. +//////////////////////////////////////////////////////////////////// +INLINE int BulletSoftBodyConfig:: +get_drift_solver_iterations() const { + + return _cfg.diterations; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_drift_solver_iterations +// Access: Published +// Description: Setter for property diterations. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_drift_solver_iterations(int value) { + + nassertv(value > 0); + _cfg.diterations = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_cluster_solver_iterations +// Access: Published +// Description: Getter for property citerations. +//////////////////////////////////////////////////////////////////// +INLINE int BulletSoftBodyConfig:: +get_cluster_solver_iterations() const { + + return _cfg.citerations; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_cluster_solver_iterations +// Access: Published +// Description: Setter for property citerations. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyConfig:: +set_cluster_solver_iterations(int value) { + + nassertv(value > 0); + _cfg.citerations = value; +} + diff --git a/panda/src/bullet/bulletSoftBodyConfig.cxx b/panda/src/bullet/bulletSoftBodyConfig.cxx new file mode 100644 index 0000000000..a2480ab359 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyConfig.cxx @@ -0,0 +1,86 @@ +// Filename: bulletSoftBodyConfig.cxx +// Created by: enn0x (12Apr10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletSoftBodyConfig.h" + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyConfig:: +BulletSoftBodyConfig(btSoftBody::Config &cfg) : _cfg(cfg) { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::clear_collisions +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyConfig:: +clear_all_collision_flags() { + + _cfg.collisions = 0; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_collisions +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyConfig:: +set_collision_flag(CollisionFlag flag, bool value) { + + if (value == true) { + _cfg.collisions |= flag; + } + else { + _cfg.collisions &= ~(flag); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_collisions +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletSoftBodyConfig:: +get_collision_flag(CollisionFlag flag) const { + + return (_cfg.collisions & flag) ? true : false; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::set_aero_model +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyConfig:: +set_aero_model(AeroModel value) { + + _cfg.aeromodel = (btSoftBody::eAeroModel::_)value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyConfig::get_aero_model +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyConfig::AeroModel BulletSoftBodyConfig:: +get_aero_model() const { + + return (AeroModel)_cfg.aeromodel; +} + diff --git a/panda/src/bullet/bulletSoftBodyConfig.h b/panda/src/bullet/bulletSoftBodyConfig.h new file mode 100644 index 0000000000..743f242a78 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyConfig.h @@ -0,0 +1,115 @@ +// Filename: bulletSoftBodyConfig.h +// Created by: enn0x (12Apr10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_SOFT_BODY_CONFIG_H__ +#define __BULLET_SOFT_BODY_CONFIG_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletSoftBodyConfig +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletSoftBodyConfig { + +PUBLISHED: + INLINE ~BulletSoftBodyConfig(); + + enum CollisionFlag { + CF_rigid__vs_soft_mask = 0x000f, // RVSmask: Rigid versus soft mask + CF_sdf_rigid_soft = 0x0001, // SDF_RS: SDF based rigid vs soft + CF_cluster_rigid_soft = 0x0002, // CL_RS: Cluster vs convex rigid vs soft + CF_soft_vs_soft_mask = 0x0030, // SVSmask: Soft versus soft mask + CF_vertex_face_soft_soft = 0x0010, // VF_SS: Vertex vs face soft vs soft handling + CF_cluster_soft_soft = 0x0020, // CL_SS: Cluster vs cluster soft vs soft handling + CF_cluster_self = 0x0040, // CL_SELF: Cluster soft body self collision + }; + + enum AeroModel { + AM_vertex_point, // V_Point: Vertex normals are oriented toward velocity + AM_vertex_two_sided, // V_TwoSided: Vertex normals are fliped to match velocity + AM_vertex_one_sided, // V_OneSided: Vertex normals are taken as it is + AM_face_two_sided, // F_TwoSided: Face normals are fliped to match velocity + AM_face_one_sided, // F_OneSided: Face normals are taken as it is + }; + + void clear_all_collision_flags(); + void set_collision_flag(CollisionFlag flag, bool value); + bool get_collision_flag(CollisionFlag flag) const; + + void set_aero_model(AeroModel value); + AeroModel get_aero_model() const; + + INLINE void set_velocities_correction_factor(float value); + INLINE void set_damping_coefficient(float value); + INLINE void set_drag_coefficient(float value); + INLINE void set_lift_coefficient(float value); + INLINE void set_pressure_coefficient(float value); + INLINE void set_volume_conversation_coefficient(float value); + INLINE void set_dynamic_friction_coefficient(float value); + INLINE void set_pose_matching_coefficient(float value); + INLINE void set_rigid_contacts_hardness(float value); + INLINE void set_kinetic_contacts_hardness(float value); + INLINE void set_soft_contacts_hardness(float value); + INLINE void set_anchors_hardness(float value); + INLINE void set_soft_vs_rigid_hardness(float value); + INLINE void set_soft_vs_kinetic_hardness(float value); + INLINE void set_soft_vs_soft_hardness(float value); + INLINE void set_soft_vs_rigid_impulse_split(float value); + INLINE void set_soft_vs_kinetic_impulse_split(float value); + INLINE void set_soft_vs_soft_impulse_split(float value); + INLINE void set_maxvolume(float value); + INLINE void set_timescale(float value); + INLINE void set_positions_solver_iterations(int value); + INLINE void set_velocities_solver_iterations(int value); + INLINE void set_drift_solver_iterations( int value); + INLINE void set_cluster_solver_iterations(int value); + + INLINE float get_velocities_correction_factor() const; + INLINE float get_damping_coefficient() const; + INLINE float get_drag_coefficient() const; + INLINE float get_lift_coefficient() const; + INLINE float get_pressure_coefficient() const; + INLINE float get_volume_conversation_coefficient() const; + INLINE float get_dynamic_friction_coefficient() const; + INLINE float get_pose_matching_coefficient() const; + INLINE float get_rigid_contacts_hardness() const; + INLINE float get_kinetic_contacts_hardness() const; + INLINE float get_soft_contacts_hardness() const; + INLINE float get_anchors_hardness() const; + INLINE float get_soft_vs_rigid_hardness() const; + INLINE float get_soft_vs_kinetic_hardness() const; + INLINE float get_soft_vs_soft_hardness() const; + INLINE float get_soft_vs_rigid_impulse_split() const; + INLINE float get_soft_vs_kinetic_impulse_split() const; + INLINE float get_soft_vs_soft_impulse_split() const; + INLINE float get_maxvolume() const; + INLINE float get_timescale() const; + INLINE int get_positions_solver_iterations() const; + INLINE int get_velocities_solver_iterations() const; + INLINE int get_drift_solver_iterations() const; + INLINE int get_cluster_solver_iterations() const; + +public: + BulletSoftBodyConfig(btSoftBody::Config &cfg); + +private: + btSoftBody::Config &_cfg; +}; + +#include "bulletSoftBodyConfig.I" + +#endif // __BULLET_SOFT_BODY_CONFIG_H__ diff --git a/panda/src/bullet/bulletSoftBodyMaterial.I b/panda/src/bullet/bulletSoftBodyMaterial.I new file mode 100644 index 0000000000..a13f9ed6a9 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyMaterial.I @@ -0,0 +1,115 @@ +// Filename: bulletSoftBodyMaterial.I +// Created by: enn0x (19Mar11) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyMaterial::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletSoftBodyMaterial:: +~BulletSoftBodyMaterial() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyMaterial::empty +// Access: Published +// Description: Named constructor intended to be used for asserts +// with have to return a concrete value. +//////////////////////////////////////////////////////////////////// +INLINE BulletSoftBodyMaterial BulletSoftBodyMaterial:: +empty() { + + static btSoftBody::Material material; + + return BulletSoftBodyMaterial(material); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::get_material +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE btSoftBody::Material &BulletSoftBodyMaterial:: +get_material() const { + + return _material; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyMaterial::getLinearStiffness +// Access: Published +// Description: Getter for the property m_kLST. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyMaterial:: +getLinearStiffness() const { + + return _material.m_kLST; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyMaterial::setLinearStiffness +// Access: Published +// Description: Setter for the property m_kLST. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyMaterial:: +setLinearStiffness(float value) { + + _material.m_kLST = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyMaterial::getAngularStiffness +// Access: Published +// Description: Getter for the property m_kAST. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyMaterial:: +getAngularStiffness() const { + + return _material.m_kAST; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyMaterial::setAngularStiffness +// Access: Published +// Description: Setter for the property m_kAST. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyMaterial:: +setAngularStiffness(float value) { + + _material.m_kAST = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyMaterial::getVolumePreservation +// Access: Published +// Description: Getter for the property m_kVST. +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyMaterial:: +getVolumePreservation() const { + + return _material.m_kVST; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyMaterial::setVolumePreservation +// Access: Published +// Description: Setter for the property m_kVST. +//////////////////////////////////////////////////////////////////// +INLINE void BulletSoftBodyMaterial:: +setVolumePreservation(float value) { + + _material.m_kVST = value; +} + diff --git a/panda/src/bullet/bulletSoftBodyMaterial.cxx b/panda/src/bullet/bulletSoftBodyMaterial.cxx new file mode 100644 index 0000000000..d05212ffc8 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyMaterial.cxx @@ -0,0 +1,26 @@ +// Filename: bulletSoftBodyMaterial.cxx +// Created by: enn0x (19Mar11) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletSoftBodyMaterial.h" + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyMaterial::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyMaterial:: +BulletSoftBodyMaterial(btSoftBody::Material &material) : _material(material) { + +} + diff --git a/panda/src/bullet/bulletSoftBodyMaterial.h b/panda/src/bullet/bulletSoftBodyMaterial.h new file mode 100644 index 0000000000..0bd097e89f --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyMaterial.h @@ -0,0 +1,51 @@ +// Filename: bulletSoftBodyMaterial.h +// Created by: enn0x (19Mar11) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_SOFT_BODY_MATERIAL_H__ +#define __BULLET_SOFT_BODY_MATERIAL_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletSoftBodyMaterial +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletSoftBodyMaterial { + +PUBLISHED: + INLINE ~BulletSoftBodyMaterial(); + INLINE static BulletSoftBodyMaterial empty(); + + INLINE void setLinearStiffness(float value); + INLINE void setAngularStiffness(float value); + INLINE void setVolumePreservation(float value); + + INLINE float getLinearStiffness() const; + INLINE float getAngularStiffness() const; + INLINE float getVolumePreservation() const; + +public: + BulletSoftBodyMaterial(btSoftBody::Material &material); + + INLINE btSoftBody::Material &get_material() const; + +private: + btSoftBody::Material &_material; +}; + +#include "bulletSoftBodyMaterial.I" + +#endif // __BULLET_SOFT_BODY_MATERIAL_H__ diff --git a/panda/src/bullet/bulletSoftBodyNode.I b/panda/src/bullet/bulletSoftBodyNode.I new file mode 100644 index 0000000000..0624c1f0e8 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyNode.I @@ -0,0 +1,116 @@ +// Filename: bulletSoftBodyNode.I +// Created by: enn0x (27Dec10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletSoftBodyNode:: +~BulletSoftBodyNode() { + + delete _body; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNodeElement::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletSoftBodyNodeElement:: +~BulletSoftBodyNodeElement() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNodeElement::empty +// Access: Published +// Description: Named constructor intended to be used for asserts +// with have to return a concrete value. +//////////////////////////////////////////////////////////////////// +INLINE BulletSoftBodyNodeElement BulletSoftBodyNodeElement:: +empty() { + + static btSoftBody::Node node; + + return BulletSoftBodyNodeElement(node); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNodeElement::get_pos +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE LPoint3f BulletSoftBodyNodeElement:: +get_pos() const { + + return btVector3_to_LPoint3f(_node.m_x); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNodeElement::get_normal +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE LVector3f BulletSoftBodyNodeElement:: +get_normal() const { + + return btVector3_to_LVector3f(_node.m_n); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNodeElement::get_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE LVector3f BulletSoftBodyNodeElement:: +get_velocity() const { + + return btVector3_to_LVector3f(_node.m_v); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNodeElement::get_inv_mass +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyNodeElement:: +get_inv_mass() const { + + return _node.m_im; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNodeElement::get_area +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletSoftBodyNodeElement:: +get_area() const { + + return _node.m_area; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNodeElement::is_attached +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletSoftBodyNodeElement:: +is_attached() const { + + return _node.m_battach; +} + diff --git a/panda/src/bullet/bulletSoftBodyNode.cxx b/panda/src/bullet/bulletSoftBodyNode.cxx new file mode 100644 index 0000000000..ca585a159a --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyNode.cxx @@ -0,0 +1,1039 @@ +// Filename: bulletSoftBodyNode.cxx +// Created by: enn0x (27Dec10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletSoftBodyNode.h" +#include "bulletSoftBodyConfig.h" +#include "bulletSoftBodyMaterial.h" +#include "bulletSoftBodyShape.h" +#include "bulletSoftBodyWorldInfo.h" +#include "bulletHelper.h" + +#include "geomVertexRewriter.h" +#include "geomVertexReader.h" + +TypeHandle BulletSoftBodyNode::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyNode:: +BulletSoftBodyNode(btSoftBody *body, const char *name) : BulletBodyNode(name) { + + // Setup body + _body = body; + _body->setUserPointer(this); + + // Shape + btCollisionShape *shape_ptr = _body->getCollisionShape(); + + nassertv(shape_ptr != NULL); + nassertv(shape_ptr->getShapeType() == SOFTBODY_SHAPE_PROXYTYPE); + + _shapes.push_back(new BulletSoftBodyShape((btSoftBodyCollisionShape *)shape_ptr)); + + // Rendering + _geom = NULL; + _curve = NULL; + _surface = NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_object +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionObject *BulletSoftBodyNode:: +get_object() const { + + return _body; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_cfg +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyConfig BulletSoftBodyNode:: +get_cfg() { + + return BulletSoftBodyConfig(_body->m_cfg); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_world_info +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyWorldInfo BulletSoftBodyNode:: +get_world_info() { + + return BulletSoftBodyWorldInfo(*(_body->m_worldInfo)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_num_materials +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +int BulletSoftBodyNode:: +get_num_materials() const { + + return _body->m_materials.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_material +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyMaterial BulletSoftBodyNode:: +get_material(int idx) const { + + nassertr(idx >= 0 && idx < get_num_materials(), BulletSoftBodyMaterial::empty()); + + btSoftBody::Material *material = _body->m_materials[idx]; + return BulletSoftBodyMaterial(*material); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_num_nodes +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +int BulletSoftBodyNode:: +get_num_nodes() const { + + return _body->m_nodes.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_node +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyNodeElement BulletSoftBodyNode:: +get_node(int idx) const { + + nassertr(idx >=0 && idx < get_num_nodes(), BulletSoftBodyNodeElement::empty()); + return BulletSoftBodyNodeElement(_body->m_nodes[idx]); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::append_material +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyMaterial BulletSoftBodyNode:: +append_material() { + + btSoftBody::Material *material = _body->appendMaterial(); + nassertr(material, BulletSoftBodyMaterial::empty()); + + return BulletSoftBodyMaterial(*material); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::generate_bending_constraints +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +generate_bending_constraints(int distance, BulletSoftBodyMaterial *material) { + + if (material) { + _body->generateBendingConstraints(distance, &(material->get_material())); + } + else { + _body->generateBendingConstraints(distance); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::randomize_constraints +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +randomize_constraints() { + + _body->randomizeConstraints(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::parents_changed +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +parents_changed() { + + transform_changed(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::transform_changed +// Access: Protected +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +transform_changed() { + + if (_disable_transform_changed) return; + + btTransform trans; + get_node_transform(trans, this); + trans *= _body->m_initialWorldTransform.inverse(); + _body->transform(trans); + + BulletBodyNode::transform_changed(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::post_step +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +post_step() { + + if (_geom) { + btTransform trans; + get_node_transform(trans, this); + + PT(GeomVertexData) vdata = _geom->modify_vertex_data(); + + GeomVertexRewriter vertices(vdata, InternalName::get_vertex()); + GeomVertexRewriter normals(vdata, InternalName::get_normal()); + GeomVertexReader indices(vdata, BulletHelper::get_sb_index()); + GeomVertexReader flips(vdata, BulletHelper::get_sb_flip()); + + while (!vertices.is_at_end()) { + btSoftBody::Node node = _body->m_nodes[indices.get_data1i()]; + btVector3 v = trans.invXform(node.m_x); + btVector3 n = node.m_n; + + if (flips.get_data1i() > 0) n *= -1; + + vertices.set_data3f(v.getX(), v.getY(), v.getZ()); + normals.set_data3f(n.getX(), n.getY(), n.getZ()); + } + } + + if (_curve) { + btSoftBody::tNodeArray &nodes(_body->m_nodes); + + for (int i=0; i < nodes.size(); i++) { + btVector3 pos = nodes[i].m_x; + _curve->set_vertex(i, btVector3_to_LPoint3f(pos)); + } + } + + if (_surface) { + btSoftBody::tNodeArray &nodes(_body->m_nodes); + + int num_u = _surface->get_num_u_vertices(); + int num_v = _surface->get_num_v_vertices(); + nassertv(num_u * num_v == nodes.size()); + + for (int u=0; u < num_u; u++) { + for (int v=0; v < num_v; v++) { + btVector3 pos = nodes[u * num_u + v].m_x; + _surface->set_vertex(u, v, btVector3_to_LPoint3f(pos)); + } + } + } + + // It is ok to pass the address of a temporary object here, because + // set_bounds does not store the pointer - it makes a copy using + // volume->make_copy(). + BoundingBox bb = this->get_aabb(); + CPT(TransformState) xform = TransformState::make_pos(bb.get_approx_center()); + + _disable_transform_changed = true; + this->set_transform(xform); + _disable_transform_changed = false; + + Thread *current_thread = Thread::get_current_thread(); + this->r_mark_geom_bounds_stale(current_thread); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_closest_node_index +// Access: Published +// Description: Returns the index of the node which is closest +// to the given point. The distance between each node +// and the given point is computed in world space +// if local=false, and in local space if local=true. +//////////////////////////////////////////////////////////////////// +int BulletSoftBodyNode:: +get_closest_node_index(LVecBase3f point, bool local) { + + btScalar max_dist_sqr = 1e30; + btVector3 point_x = LVecBase3f_to_btVector3(point); + + btTransform trans; + if (local == true) { + get_node_transform(trans, this); + } + else { + trans.setIdentity(); + } + + btSoftBody::tNodeArray &nodes(_body->m_nodes); + int node_idx = 0; + + for (int i=0; iget_vertex_data()->has_column(InternalName::get_vertex())); + nassertv(geom->get_vertex_data()->has_column(InternalName::get_normal())); + + _geom = geom; + + PT(GeomVertexData) vdata = _geom->modify_vertex_data(); + + if (!vdata->has_column(BulletHelper::get_sb_index())) { + CPT(GeomVertexFormat) format = vdata->get_format(); + format = BulletHelper::add_sb_index_column(format); + vdata->set_format(format); + } + + if (!vdata->has_column(BulletHelper::get_sb_flip())) { + CPT(GeomVertexFormat) format = vdata->get_format(); + format = BulletHelper::add_sb_flip_column(format); + vdata->set_format(format); + } + + GeomVertexReader vertices(vdata, InternalName::get_vertex()); + GeomVertexRewriter indices(vdata, BulletHelper::get_sb_index()); + + while (!vertices.is_at_end()) { + LVecBase3f point = vertices.get_data3f(); + int node_idx = get_closest_node_index(point, true); + indices.set_data1i(node_idx); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::unlink_geom +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +unlink_geom() { + + _geom = NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::link_curve +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +link_curve(NurbsCurveEvaluator *curve) { + + nassertv(curve->get_num_vertices() == _body->m_nodes.size()); + + _curve = curve; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::unlink_curve +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +unlink_curve() { + + _curve = NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::link_surface +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +link_surface(NurbsSurfaceEvaluator *surface) { + + nassertv(surface->get_num_u_vertices() * surface->get_num_v_vertices() == _body->m_nodes.size()); + + _surface = surface; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::unlink_surface +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +unlink_surface() { + + _surface = NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_aabb +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BoundingBox BulletSoftBodyNode:: +get_aabb() const { + + btVector3 pMin; + btVector3 pMax; + + _body->getAabb(pMin, pMax); + + return BoundingBox( + btVector3_to_LPoint3f(pMin), + btVector3_to_LPoint3f(pMax) + ); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::set_volume_mass +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +set_volume_mass(float mass) { + + _body->setVolumeMass(mass); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::set_total_mass +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +set_total_mass(float mass, bool fromfaces) { + + _body->setTotalMass(mass, fromfaces); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::set_volume_density +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +set_volume_density(float density) { + + _body->setVolumeDensity(density); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::set_total_density +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +set_total_density(float density) { + + _body->setTotalDensity(density); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::set_mass +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +set_mass(int node, float mass) { + + _body->setMass(node, mass); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_mass +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletSoftBodyNode:: +get_mass(int node) const { + + return _body->getMass(node); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_total_mass +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletSoftBodyNode:: +get_total_mass() const { + + return _body->getTotalMass(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_volume +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletSoftBodyNode:: +get_volume() const { + + return _body->getVolume(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::add_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +add_force(const LVector3f &force) { + + nassertv(!force.is_nan()); + _body->addForce(LVecBase3f_to_btVector3(force)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::add_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +add_force(const LVector3f &force, int node) { + + nassertv(!force.is_nan()); + _body->addForce(LVecBase3f_to_btVector3(force), node); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::set_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +set_velocity(const LVector3f &velocity) { + + nassertv(!velocity.is_nan()); + _body->setVelocity(LVecBase3f_to_btVector3(velocity)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::add_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +add_velocity(const LVector3f &velocity) { + + nassertv(!velocity.is_nan()); + _body->addVelocity(LVecBase3f_to_btVector3(velocity)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::add_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +add_velocity(const LVector3f &velocity, int node) { + + nassertv(!velocity.is_nan()); + _body->addVelocity(LVecBase3f_to_btVector3(velocity), node); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::generate_clusters +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +generate_clusters(int k, int maxiterations) { + + _body->generateClusters(k, maxiterations); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::release_clusters +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +release_clusters() { + + _body->releaseClusters(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::release_cluster +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +release_cluster(int index) { + + _body->releaseCluster(index); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_num_clusters +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +int BulletSoftBodyNode:: +get_num_clusters() const { + + return _body->clusterCount(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::cluster_com +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVecBase3f BulletSoftBodyNode:: +cluster_com(int cluster) const { + + return btVector3_to_LVecBase3f(_body->clusterCom(cluster)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::set_pose +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +set_pose(bool bvolume, bool bframe) { + + _body->setPose(bvolume, bframe); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::append_anchor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +append_anchor(int node, BulletRigidBodyNode *body, bool disable) { + + nassertv(node < _body->m_nodes.size()) + nassertv(body); + + btRigidBody *ptr =(btRigidBody *)body->get_object(); + _body->appendAnchor(node, ptr, disable); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::append_anchor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyNode:: +append_anchor(int node, BulletRigidBodyNode *body, const LVector3f &pivot, bool disable) { + + nassertv(node < _body->m_nodes.size()) + nassertv(body); + nassertv(!pivot.is_nan()); + + btRigidBody *ptr =(btRigidBody *)body->get_object(); + _body->appendAnchor(node, ptr, LVecBase3f_to_btVector3(pivot), disable); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNodeElement::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyNodeElement:: +BulletSoftBodyNodeElement(btSoftBody::Node &node) : _node(node) { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::get_point_index +// Access: Private +// Description: Returns the index of the first point within an +// array of points which has about the same +// coordinates as the given point. If no points +// is found -1 is returned. +//////////////////////////////////////////////////////////////////// +int BulletSoftBodyNode:: +get_point_index(LVecBase3f p, PTA_LVecBase3f points) { + + float eps = 1.0e-6f; // TODO make this a config option + + for (PTA_LVecBase3f::size_type i=0; i mapping; + + for (PTA_LVecBase3f::size_type i=0; iget_vertex_data(); + + nassertr(vdata->has_column(InternalName::get_vertex()), NULL); + + GeomVertexReader vreader(vdata, InternalName::get_vertex()); + + while (!vreader.is_at_end()) { + LVecBase3f v = vreader.get_data3f(); + points.push_back(v); + } + + // Read indices + for (int i=0; iget_num_primitives(); i++) { + + CPT(GeomPrimitive) prim = geom->get_primitive(i); + prim = prim->decompose(); + + for (int j=0; jget_num_primitives(); j++) { + + int s = prim->get_primitive_start(j); + int e = prim->get_primitive_end(j); + + for (int k=s; kget_vertex(k)); + } + } + } + + // Create body + return make_tri_mesh(info, points, indices, randomizeConstraints); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::make_tet_mesh +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PT(BulletSoftBodyNode) BulletSoftBodyNode:: +make_tet_mesh(BulletSoftBodyWorldInfo &info, PTA_LVecBase3f points, PTA_int indices, bool tetralinks) { + + // Points + btAlignedObjectArray pos; + pos.resize(points.size()); + for (PTA_LVecBase3f::size_type i=0; iappendTetra(ni[0],ni[1],ni[2],ni[3]); + + if (tetralinks) { + body->appendLink(ni[0], ni[1], 0, true); + body->appendLink(ni[1], ni[2], 0, true); + body->appendLink(ni[2], ni[0], 0, true); + body->appendLink(ni[0], ni[3], 0, true); + body->appendLink(ni[1], ni[3], 0, true); + body->appendLink(ni[2], ni[3], 0, true); + } + } + + // Node + PT(BulletSoftBodyNode) node = new BulletSoftBodyNode(body); + + return node; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyNode::make_tet_mesh +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PT(BulletSoftBodyNode) BulletSoftBodyNode:: +make_tet_mesh(BulletSoftBodyWorldInfo &info, const char *ele, const char *face, const char *node) { + + nassertr(node && node[0], NULL); + + // Nodes + btAlignedObjectArray pos; + + int npos = 0; + int ndims = 0; // not used + int nattrb = 0; // not used + int hasbounds = 0; // not used + + sscanf(node, "%d %d %d %d", &npos, &ndims, &nattrb, &hasbounds); + node += next_line(node); + + pos.resize(npos); + + for (int i=0; iappendFace(ni[0], ni[1], ni[2]); + } + } + + // Links + if (ele && ele[0]) { + int ntetra = 0; + int ncorner = 0; + int neattrb = 0; + + sscanf(ele, "%d %d %d", &ntetra, &ncorner, &neattrb); + ele += next_line(ele); + + for (int i=0; iappendTetra(ni[0], ni[1], ni[2], ni[3]); + + body->appendLink(ni[0], ni[1], 0, true); + body->appendLink(ni[1], ni[2], 0, true); + body->appendLink(ni[2], ni[0], 0, true); + body->appendLink(ni[0], ni[3], 0, true); + body->appendLink(ni[1], ni[3], 0, true); + body->appendLink(ni[2], ni[3], 0, true); + } + } + + // Node + PT(BulletSoftBodyNode) sbnode = new BulletSoftBodyNode(body); + + return sbnode; +} + diff --git a/panda/src/bullet/bulletSoftBodyNode.h b/panda/src/bullet/bulletSoftBodyNode.h new file mode 100644 index 0000000000..649e4f5e82 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyNode.h @@ -0,0 +1,232 @@ +// Filename: bulletSoftBodyNode.h +// Created by: enn0x (27Dec10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_SOFT_BODY_NODE_H__ +#define __BULLET_SOFT_BODY_NODE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletBodyNode.h" + +#include "collideMask.h" +#include "geom.h" +#include "geomNode.h" +#include "geomVertexFormat.h" +#include "boundingBox.h" +#include "nurbsCurveEvaluator.h" +#include "nurbsSurfaceEvaluator.h" +#include "pta_LVecBase3f.h" + +class BulletSoftBodyConfig; +class BulletSoftBodyMaterial; +class BulletSoftBodyWorldInfo; + +//////////////////////////////////////////////////////////////////// +// Class : BulletSoftBodyNodeElement +// Description : +//////////////////////////////////////////////////////////////////// +class BulletSoftBodyNodeElement { + +PUBLISHED: + INLINE ~BulletSoftBodyNodeElement(); + INLINE static BulletSoftBodyNodeElement empty(); + + INLINE LPoint3f get_pos() const; + INLINE LVector3f get_velocity() const; + INLINE LVector3f get_normal() const; + INLINE float get_inv_mass() const; + INLINE float get_area() const; + INLINE int is_attached() const; + +public: + BulletSoftBodyNodeElement(btSoftBody::Node &node); + +private: + btSoftBody::Node &_node; +}; + +//////////////////////////////////////////////////////////////////// +// Class : BulletSoftBodyNode +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletSoftBodyNode : public BulletBodyNode { + +public: + BulletSoftBodyNode(btSoftBody *body, const char *name="softbody"); + +PUBLISHED: + INLINE ~BulletSoftBodyNode(); + + BulletSoftBodyConfig get_cfg(); + BulletSoftBodyWorldInfo get_world_info(); + + void generate_bending_constraints(int distance, BulletSoftBodyMaterial *material=NULL); + void randomize_constraints(); + + // Mass, volume, density + void set_volume_mass(float mass); + void set_volume_density(float density); + void set_total_mass(float mass, bool fromfaces=false); + void set_total_density(float density); + void set_mass(int node, float mass); + + float get_mass(int node) const; + float get_total_mass() const; + float get_volume() const; + + // Force + void add_force(const LVector3f &force); + void add_force(const LVector3f &force, int node); + + void set_velocity(const LVector3f &velocity); + void add_velocity(const LVector3f &velocity); + void add_velocity(const LVector3f &velocity, int node); + + void set_pose(bool bvolume, bool bframe); + + BoundingBox get_aabb() const; + + // Cluster + void generate_clusters(int k, int maxiterations=8192); + void release_cluster(int index); + void release_clusters(); + int get_num_clusters() const; + LVecBase3f cluster_com(int cluster) const; + + // Rendering + void link_geom(Geom *geom); + void unlink_geom(); + + void link_curve(NurbsCurveEvaluator *curve); + void unlink_curve(); + + void link_surface(NurbsSurfaceEvaluator *surface); + void unlink_surface(); + + // Anchors + void append_anchor(int node, BulletRigidBodyNode *body, + bool disable=false); + void append_anchor(int node, BulletRigidBodyNode *body, + const LVector3f &pivot, + bool disable=false); + + // Materials + int get_num_materials() const; + BulletSoftBodyMaterial get_material(int idx) const; + MAKE_SEQ(get_materials, get_num_materials, get_material); + + BulletSoftBodyMaterial append_material(); + + // Nodes + int get_num_nodes() const; + BulletSoftBodyNodeElement get_node(int idx) const; + MAKE_SEQ(get_nodes, get_num_nodes, get_node); + + int get_closest_node_index(LVecBase3f point, bool local); + + // Factory + static PT(BulletSoftBodyNode) make_rope( + BulletSoftBodyWorldInfo &info, + const LPoint3f &from, + const LPoint3f &to, + int res, + int fixeds); + + static PT(BulletSoftBodyNode) make_patch( + BulletSoftBodyWorldInfo &info, + const LPoint3f &corner00, + const LPoint3f &corner10, + const LPoint3f &corner01, + const LPoint3f &corner11, + int resx, + int resy, + int fixeds, + bool gendiags); + + static PT(BulletSoftBodyNode) make_ellipsoid( + BulletSoftBodyWorldInfo &info, + const LPoint3f ¢er, + const LVecBase3f &radius, + int res); + + static PT(BulletSoftBodyNode) make_tri_mesh( + BulletSoftBodyWorldInfo &info, + const Geom *geom, + bool randomizeConstraints=true); + + static PT(BulletSoftBodyNode) make_tri_mesh( + BulletSoftBodyWorldInfo &info, + PTA_LVecBase3f points, + PTA_int indices, + bool randomizeConstraints=true); + + static PT(BulletSoftBodyNode) make_tet_mesh( + BulletSoftBodyWorldInfo &info, + PTA_LVecBase3f points, + PTA_int indices, + bool tetralinks=true); + + static PT(BulletSoftBodyNode) make_tet_mesh( + BulletSoftBodyWorldInfo &info, + const char *ele, + const char *face, + const char *node); + +public: + virtual btCollisionObject *get_object() const; + + void post_step(); + +protected: + virtual void parents_changed(); + virtual void transform_changed(); + +private: + btSoftBody *_body; + + PT(Geom) _geom; + PT(NurbsCurveEvaluator) _curve; + PT(NurbsSurfaceEvaluator) _surface; + + static int get_point_index(LVecBase3f p, PTA_LVecBase3f points); + static int next_line(const char *buffer); + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletBodyNode::init_type(); + register_type(_type_handle, "BulletSoftBodyNode", + BulletBodyNode::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletSoftBodyNode.I" + +#endif // __BULLET_SOFT_BODY_NODE_H__ + diff --git a/panda/src/bullet/bulletSoftBodyShape.I b/panda/src/bullet/bulletSoftBodyShape.I new file mode 100644 index 0000000000..4b84077830 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyShape.I @@ -0,0 +1,28 @@ +// Filename: bulletSoftBodyShape.I +// Created by: enn0x (06May10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletSoftBodyShape:: +~BulletSoftBodyShape() { + + // The btSoftBodyShape _shape is created and deleted internally + // by the owning btSoftBody object. It must not be deleted + // here! +} + diff --git a/panda/src/bullet/bulletSoftBodyShape.cxx b/panda/src/bullet/bulletSoftBodyShape.cxx new file mode 100644 index 0000000000..f3ee4c06a0 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyShape.cxx @@ -0,0 +1,59 @@ +// Filename: bulletSoftBodyShape.cxx +// Created by: enn0x (06May10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletSoftBodyShape.h" +#include "bulletSoftBodyNode.h" + +TypeHandle BulletSoftBodyShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyShape::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyShape:: +BulletSoftBodyShape(btSoftBodyCollisionShape *shapePtr) { + + _shape = shapePtr; + _shape->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyShape::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionShape *BulletSoftBodyShape:: +ptr() const { + + return _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyShape::get_body +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyNode *BulletSoftBodyShape:: +get_body() const { + + if (_shape->m_body) { + return (BulletSoftBodyNode *)_shape->m_body->getUserPointer(); + } + else + { + return NULL; + } +} + diff --git a/panda/src/bullet/bulletSoftBodyShape.h b/panda/src/bullet/bulletSoftBodyShape.h new file mode 100644 index 0000000000..ef8637eca2 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyShape.h @@ -0,0 +1,68 @@ +// Filename: bulletSoftBodyShape.h +// Created by: enn0x (06May10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_SOFT_BODY_SHAPE_H__ +#define __BULLET_SOFT_BODY_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bulletShape.h" + +class BulletSoftBodyNode; + +//////////////////////////////////////////////////////////////////// +// Class : BulletSoftBodyShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletSoftBodyShape : public BulletShape { + +PUBLISHED: + INLINE ~BulletSoftBodyShape(); + + BulletSoftBodyNode *get_body() const; + +public: + BulletSoftBodyShape(btSoftBodyCollisionShape *shapePtr); + + virtual btCollisionShape *ptr() const; + +private: + btSoftBodyCollisionShape *_shape; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletShape::init_type(); + register_type(_type_handle, "BulletSoftBodyShape", + BulletShape::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletSoftBodyShape.I" + +#endif // __BULLET_SOFT_BODY_SHAPE_H__ diff --git a/panda/src/bullet/bulletSoftBodyWorldInfo.I b/panda/src/bullet/bulletSoftBodyWorldInfo.I new file mode 100644 index 0000000000..1960b31403 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyWorldInfo.I @@ -0,0 +1,35 @@ +// Filename: bulletSoftBodyWorldInfo.I +// Created by: enn0x (04Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletSoftBodyWorldInfo:: +~BulletSoftBodyWorldInfo() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::get_info +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE btSoftBodyWorldInfo &BulletSoftBodyWorldInfo:: +get_info() const { + + return _info; +} + diff --git a/panda/src/bullet/bulletSoftBodyWorldInfo.cxx b/panda/src/bullet/bulletSoftBodyWorldInfo.cxx new file mode 100644 index 0000000000..ec540c07e6 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyWorldInfo.cxx @@ -0,0 +1,149 @@ +// Filename: bulletSoftBodyWorldInfo.cxx +// Created by: enn0x (04Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletSoftBodyWorldInfo.h" + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyWorldInfo:: +BulletSoftBodyWorldInfo(btSoftBodyWorldInfo &info) : _info(info) { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::garbage_collect +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyWorldInfo:: +garbage_collect(int lifetime) { + + _info.m_sparsesdf.GarbageCollect(lifetime); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::set_air_density +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyWorldInfo:: +set_air_density(float density) { + + _info.air_density = density; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::set_water_density +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyWorldInfo:: +set_water_density(float density) { + + _info.water_density = density; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::set_water_offset +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyWorldInfo:: +set_water_offset(float offset) { + + _info.water_offset = offset; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::set_water_normal +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyWorldInfo:: +set_water_normal(const LVector3f &normal) { + + nassertv(!normal.is_nan()); + _info.water_normal = LVecBase3f_to_btVector3(normal); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::set_gravity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletSoftBodyWorldInfo:: +set_gravity(const LVector3f &gravity) { + + nassertv(!gravity.is_nan()); + _info.m_gravity = LVecBase3f_to_btVector3(gravity); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::get_air_density +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletSoftBodyWorldInfo:: +get_air_density() const { + + return _info.air_density; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::get_water_density +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletSoftBodyWorldInfo:: +get_water_density() const { + + return _info.water_density; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::get_water_offset +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletSoftBodyWorldInfo:: +get_water_offset() const { + + return _info.water_offset; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::get_water_normal +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f BulletSoftBodyWorldInfo:: +get_water_normal() const { + + return btVector3_to_LVector3f(_info.water_normal); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSoftBodyWorldInfo::get_gravity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f BulletSoftBodyWorldInfo:: +get_gravity() const { + + return btVector3_to_LVector3f(_info.m_gravity); +} + diff --git a/panda/src/bullet/bulletSoftBodyWorldInfo.h b/panda/src/bullet/bulletSoftBodyWorldInfo.h new file mode 100644 index 0000000000..b1b18c9dd6 --- /dev/null +++ b/panda/src/bullet/bulletSoftBodyWorldInfo.h @@ -0,0 +1,59 @@ +// Filename: bulletSoftBodyWorldInfo.h +// Created by: enn0x (04Mar10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_SOFT_BODY_WORLD_INFO_H__ +#define __BULLET_SOFT_BODY_WORLD_INFO_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" + +#include "lvector3.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletSoftBodyWorldInfo +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletSoftBodyWorldInfo { + +PUBLISHED: + INLINE ~BulletSoftBodyWorldInfo(); + + void set_air_density(float density); + void set_water_density(float density); + void set_water_offset(float offset); + void set_water_normal(const LVector3f &normal); + void set_gravity(const LVector3f &gravity); + + float get_air_density() const; + float get_water_density() const; + float get_water_offset() const; + LVector3f get_water_normal() const; + LVector3f get_gravity() const; + + void garbage_collect(int lifetime=256); + +public: + BulletSoftBodyWorldInfo(btSoftBodyWorldInfo &_info); + + INLINE btSoftBodyWorldInfo &get_info() const; + +private: + btSoftBodyWorldInfo &_info; +}; + +#include "bulletSoftBodyWorldInfo.I" + +#endif // __BULLET_SOFT_BODY_WORLD_INFO_H__ diff --git a/panda/src/bullet/bulletSphereShape.I b/panda/src/bullet/bulletSphereShape.I new file mode 100644 index 0000000000..e3bdfbc8c7 --- /dev/null +++ b/panda/src/bullet/bulletSphereShape.I @@ -0,0 +1,36 @@ +// Filename: bulletSphereShape.I +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletSphereShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletSphereShape:: +~BulletSphereShape() { + + delete _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSphereShape::get_radius +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE float BulletSphereShape:: +get_radius() const { + + return _shape->getRadius(); +} + diff --git a/panda/src/bullet/bulletSphereShape.cxx b/panda/src/bullet/bulletSphereShape.cxx new file mode 100644 index 0000000000..adeb2ef6f7 --- /dev/null +++ b/panda/src/bullet/bulletSphereShape.cxx @@ -0,0 +1,52 @@ +// Filename: bulletSphereShape.cxx +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletSphereShape.h" + +TypeHandle BulletSphereShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletSphereShape::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSphereShape:: +BulletSphereShape(float radius) { + + _shape = new btSphereShape(radius); + _shape->setUserPointer(this); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSphereShape::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionShape *BulletSphereShape:: +ptr() const { + + return _shape; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletSphereShape::make_from_solid +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +BulletSphereShape *BulletSphereShape:: +make_from_solid(const CollisionSphere *solid) { + + return new BulletSphereShape(solid->get_radius()); +} + diff --git a/panda/src/bullet/bulletSphereShape.h b/panda/src/bullet/bulletSphereShape.h new file mode 100644 index 0000000000..7e0b293729 --- /dev/null +++ b/panda/src/bullet/bulletSphereShape.h @@ -0,0 +1,70 @@ +// Filename: bulletSphereShape.h +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_SPHERE_SHAPE_H__ +#define __BULLET_SPHERE_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" +#include "bulletShape.h" + +#include "collisionSphere.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletSphereShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletSphereShape : public BulletShape { + +PUBLISHED: + BulletSphereShape(float radius); + INLINE ~BulletSphereShape(); + + INLINE float get_radius() const; + + static BulletSphereShape *make_from_solid(const CollisionSphere *solid); + +public: + virtual btCollisionShape *ptr() const; + +private: + btSphereShape *_shape; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletShape::init_type(); + register_type(_type_handle, "BulletSphereShape", + BulletShape::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletSphereShape.I" + +#endif // __BULLET_SPHERE_SHAPE_H__ diff --git a/panda/src/bullet/bulletTriangleMesh.I b/panda/src/bullet/bulletTriangleMesh.I new file mode 100644 index 0000000000..533bea1e93 --- /dev/null +++ b/panda/src/bullet/bulletTriangleMesh.I @@ -0,0 +1,48 @@ +// Filename: bulletTriangleMesh.I +// Created by: enn0x (09Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletTriangleMesh:: +~BulletTriangleMesh() { + + delete _mesh; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btTriangleMesh *BulletTriangleMesh:: +ptr() const { + + return _mesh; +} + +//////////////////////////////////////////////////////////////////// +// Function: ostream::Put To Operator +// Description: +//////////////////////////////////////////////////////////////////// +INLINE ostream & +operator << (ostream &out, const BulletTriangleMesh &obj) { + + obj.output(out); + return out; +} + diff --git a/panda/src/bullet/bulletTriangleMesh.cxx b/panda/src/bullet/bulletTriangleMesh.cxx new file mode 100644 index 0000000000..35e040358f --- /dev/null +++ b/panda/src/bullet/bulletTriangleMesh.cxx @@ -0,0 +1,220 @@ +// Filename: bulletTriangleMesh.cxx +// Created by: enn0x (09Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletTriangleMesh.h" + +#include "pvector.h" +#include "geomVertexData.h" +#include "geomVertexReader.h" + +TypeHandle BulletTriangleMesh::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletTriangleMesh:: +BulletTriangleMesh() { + + _mesh = new btTriangleMesh(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::get_num_triangles +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +int BulletTriangleMesh:: +get_num_triangles() const { + + return _mesh->getNumTriangles(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::preallocate +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletTriangleMesh:: +preallocate(int num_verts, int num_indices) { + + _mesh->preallocateVertices(num_verts); + _mesh->preallocateIndices(num_indices); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::add_triangle +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletTriangleMesh:: +add_triangle(const LPoint3f &p0, const LPoint3f &p1, const LPoint3f &p2, bool remove_duplicate_vertices) { + + nassertv(!p0.is_nan()); + nassertv(!p1.is_nan()); + nassertv(!p2.is_nan()); + + _mesh->addTriangle( + LVecBase3f_to_btVector3(p0), + LVecBase3f_to_btVector3(p1), + LVecBase3f_to_btVector3(p2), + remove_duplicate_vertices); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::set_welding_distance +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletTriangleMesh:: +set_welding_distance(float distance) { + + _mesh->m_weldingThreshold = distance; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::get_welding_distance +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletTriangleMesh:: +get_welding_distance() const { + + return _mesh->m_weldingThreshold; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::add_geom +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletTriangleMesh:: +add_geom(const Geom *geom, bool remove_duplicate_vertices) { + + // Collect points + pvector points; + + CPT(GeomVertexData) vdata = geom->get_vertex_data(); + GeomVertexReader reader = GeomVertexReader(vdata, InternalName::get_vertex()); + + while (!reader.is_at_end()) { + points.push_back(reader.get_data3f()); + } + + // Convert points + btVector3 *vertices = new btVector3[points.size()]; + + int i = 0; + pvector::const_iterator it; + for (it=points.begin(); it!=points.end(); it++) { + LPoint3f v = *it; + vertices[i] = LVecBase3f_to_btVector3(v); + i++; + } + + // Add triangles + for (int k=0; kget_num_primitives(); k++) { + + CPT(GeomPrimitive) prim = geom->get_primitive(k); + prim = prim->decompose(); + + for (int l=0; lget_num_primitives(); l++) { + + int s = prim->get_primitive_start(l); + int e = prim->get_primitive_end(l); + + nassertv(e - s == 3); + + btVector3 v0 = vertices[prim->get_vertex(s)]; + btVector3 v1 = vertices[prim->get_vertex(s+1)]; + btVector3 v2 = vertices[prim->get_vertex(s+2)]; + + _mesh->addTriangle(v0, v1, v2, remove_duplicate_vertices); + } + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::add_array +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletTriangleMesh:: +add_array(const PTA_LVecBase3f &points, const PTA_int &indices, bool remove_duplicate_vertices) { + + // Convert vertices + btVector3 *vertices = new btVector3[points.size()]; + + int i = 0; + PTA_LVecBase3f::const_iterator it; + for (it=points.begin(); it!=points.end(); it++) { + LVecBase3f v = *it; + vertices[i] = LVecBase3f_to_btVector3(v); + i++; + } + + // Add triangles + int j = 0; + while (j+2 < (int)indices.size()) { + + btVector3 v0 = vertices[indices[j++]]; + btVector3 v1 = vertices[indices[j++]]; + btVector3 v2 = vertices[indices[j++]]; + + _mesh->addTriangle(v0, v1, v2, remove_duplicate_vertices); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::output +// Access: Published, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +void BulletTriangleMesh:: +output(ostream &out) const { + + out << get_type() << ", " << _mesh->getNumTriangles(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMesh::write +// Access: Published, Virtual +// Description: +//////////////////////////////////////////////////////////////////// +void BulletTriangleMesh:: +write(ostream &out, int indent_level) const { + + indent(out, indent_level) << get_type() << ":" << endl; + + IndexedMeshArray& array = _mesh->getIndexedMeshArray(); + for (int i=0; i < array.size(); i++) { + indent(out, indent_level + 2) << "IndexedMesh " << i << ":" << endl; + btIndexedMesh meshPart = array.at(i); + + indent(out, indent_level + 4) << "num triangles:" << meshPart.m_numTriangles << endl; + indent(out, indent_level + 4) << "num vertices:" << meshPart.m_numVertices << endl; + + // TODO print imesh info... +/* +int m_numTriangles +const unsigned char * m_triangleIndexBase +int m_triangleIndexStride +int m_numVertices +const unsigned char * m_vertexBase +int m_vertexStride +*/ + } +} + diff --git a/panda/src/bullet/bulletTriangleMesh.h b/panda/src/bullet/bulletTriangleMesh.h new file mode 100644 index 0000000000..c48e5f648a --- /dev/null +++ b/panda/src/bullet/bulletTriangleMesh.h @@ -0,0 +1,91 @@ +// Filename: bulletTriangleMesh.h +// Created by: enn0x (09Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_TRIANGLE_MESH_H__ +#define __BULLET_TRIANGLE_MESH_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" + +#include "typedReferenceCount.h" +#include "nodePath.h" +#include "lpoint3.h" +#include "geom.h" +#include "pta_LVecBase3f.h" +#include "pta_int.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletTriangleMesh +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletTriangleMesh : public TypedReferenceCount { + +PUBLISHED: + BulletTriangleMesh(); + INLINE ~BulletTriangleMesh(); + + void add_triangle(const LPoint3f &p0, + const LPoint3f &p1, + const LPoint3f &p2, + bool remove_duplicate_vertices=false); + void add_array(const PTA_LVecBase3f &points, + const PTA_int &indices, + bool remove_duplicate_vertices=false); + void add_geom(const Geom *geom, + bool remove_duplicate_vertices=false); + + void set_welding_distance(float distance); + void preallocate(int num_verts, int num_indices); + + int get_num_triangles() const; + float get_welding_distance() const; + + virtual void output(ostream &out) const; + virtual void write(ostream &out, int indent_level) const; + +public: + INLINE btTriangleMesh *ptr() const; + +private: + btTriangleMesh *_mesh; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + TypedReferenceCount::init_type(); + register_type(_type_handle, "BulletTriangleMesh", + TypedReferenceCount::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +INLINE ostream &operator << (ostream &out, const BulletTriangleMesh &obj); + +#include "bulletTriangleMesh.I" + +#endif // __BULLET_TRIANGLE_MESH_H__ diff --git a/panda/src/bullet/bulletTriangleMeshShape.I b/panda/src/bullet/bulletTriangleMeshShape.I new file mode 100644 index 0000000000..fed85746ef --- /dev/null +++ b/panda/src/bullet/bulletTriangleMeshShape.I @@ -0,0 +1,53 @@ +// Filename: bulletTriangleMeshShape.I +// Created by: enn0x (09Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMeshShape::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletTriangleMeshShape:: +~BulletTriangleMeshShape() { + + if (_bvh_shape) { + delete _bvh_shape; + } + + if (_gimpact_shape) { + delete _gimpact_shape; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMeshShape::is_static +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletTriangleMeshShape:: +is_static() const { + + return (_bvh_shape != NULL); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMeshShape::is_dynamic +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE bool BulletTriangleMeshShape:: +is_dynamic() const { + + return (_gimpact_shape != NULL); +} + diff --git a/panda/src/bullet/bulletTriangleMeshShape.cxx b/panda/src/bullet/bulletTriangleMeshShape.cxx new file mode 100644 index 0000000000..890302d248 --- /dev/null +++ b/panda/src/bullet/bulletTriangleMeshShape.cxx @@ -0,0 +1,91 @@ +// Filename: bulletTriangleMeshShape.cxx +// Created by: enn0x (09Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletTriangleMeshShape.h" +#include "bulletTriangleMesh.h" + +#include "nodePathCollection.h" +#include "geomNode.h" +#include "geomVertexReader.h" + +TypeHandle BulletTriangleMeshShape::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMeshShape::Constructor +// Access: Published +// Description: The parameters 'compress' and 'bvh' are only used +// if 'dynamic' is set to FALSE. +//////////////////////////////////////////////////////////////////// +BulletTriangleMeshShape:: +BulletTriangleMeshShape(BulletTriangleMesh *mesh, bool dynamic, bool compress, bool bvh) { + + // Retain a pointer to the mesh, to prevent it from being deleted + _mesh = mesh; + + // Dynamic will create a GImpact mesh shape + if (dynamic) { + + _gimpact_shape = new btGImpactMeshShape(mesh->ptr()); + _gimpact_shape->updateBound(); + _gimpact_shape->setUserPointer(this); + + _bvh_shape = NULL; + } + + // Static will create a Bvh mesh shape + else { + + _bvh_shape = new btBvhTriangleMeshShape(mesh->ptr(), compress, bvh); + _bvh_shape->setUserPointer(this); + + _gimpact_shape = NULL; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMeshShape::ptr +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionShape *BulletTriangleMeshShape:: +ptr() const { + + if (_bvh_shape) { + return _bvh_shape; + } + + if (_gimpact_shape) { + return _gimpact_shape; + } + + return NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletTriangleMeshShape::refit_tree +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletTriangleMeshShape:: +refit_tree(const LPoint3f &aabb_min, const LPoint3f &aabb_max) { + + nassertv(!aabb_max.is_nan()); + nassertv(!aabb_max.is_nan()); + + nassertv(this->is_static()); + + _bvh_shape->refitTree(LVecBase3f_to_btVector3(aabb_min), + LVecBase3f_to_btVector3(aabb_max)); +} + diff --git a/panda/src/bullet/bulletTriangleMeshShape.h b/panda/src/bullet/bulletTriangleMeshShape.h new file mode 100644 index 0000000000..e6556fd236 --- /dev/null +++ b/panda/src/bullet/bulletTriangleMeshShape.h @@ -0,0 +1,75 @@ +// Filename: bulletTriangleMeshShape.h +// Created by: enn0x (09Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_TRIANGLE_MESH_SHAPE_H__ +#define __BULLET_TRIANGLE_MESH_SHAPE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bulletShape.h" + +#include "lpoint3.h" + +class BulletTriangleMesh; + +//////////////////////////////////////////////////////////////////// +// Class : BulletTriangleMeshShape +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletTriangleMeshShape : public BulletShape { + +PUBLISHED: + BulletTriangleMeshShape(BulletTriangleMesh *mesh, bool dynamic, bool compress=true, bool bvh=true); + INLINE ~BulletTriangleMeshShape(); + + void refit_tree(const LPoint3f &aabb_min, const LPoint3f &aabb_max); + + INLINE bool is_static() const; + INLINE bool is_dynamic() const; + +public: + virtual btCollisionShape *ptr() const; + +private: + btBvhTriangleMeshShape *_bvh_shape; + btGImpactMeshShape *_gimpact_shape; + + PT(BulletTriangleMesh) _mesh; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + BulletShape::init_type(); + register_type(_type_handle, "BulletTriangleMeshShape", + BulletShape::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletTriangleMeshShape.I" + +#endif // __BULLET_TRIANGLE_MESH_SHAPE_H__ diff --git a/panda/src/bullet/bulletVehicle.I b/panda/src/bullet/bulletVehicle.I new file mode 100644 index 0000000000..a6cf237aef --- /dev/null +++ b/panda/src/bullet/bulletVehicle.I @@ -0,0 +1,191 @@ +// Filename: bulletVehicle.I +// Created by: enn0x (16Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletVehicle:: +~BulletVehicle() { + + delete _vehicle; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::get_vehicle +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btRaycastVehicle *BulletVehicle:: +get_vehicle() const { + + return _vehicle; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::get_tuning +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletVehicleTuning &BulletVehicle:: +get_tuning() { + + return _tuning; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::get_num_wheels +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletVehicle:: +get_num_wheels() const { + + return _vehicle->getNumWheels(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::set_suspension_stiffness +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicleTuning:: +set_suspension_stiffness(float value) { + + _.m_suspensionStiffness = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::set_suspension_compression +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicleTuning:: +set_suspension_compression(float value) { + + _.m_suspensionCompression = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::set_suspension_damping +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicleTuning:: +set_suspension_damping(float value) { + + _.m_suspensionDamping = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::set_max_suspension_travel_cm +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicleTuning:: +set_max_suspension_travel_cm(float value) { + + _.m_maxSuspensionTravelCm = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::set_friction_slip +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicleTuning:: +set_friction_slip(float value) { + + _.m_frictionSlip = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::set_max_suspension_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicleTuning:: +set_max_suspension_force(float value) { + + _.m_maxSuspensionForce = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::get_suspension_stiffness +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletVehicleTuning:: +get_suspension_stiffness() const { + + return _.m_suspensionStiffness; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::get_suspension_compression +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletVehicleTuning:: +get_suspension_compression() const { + + return _.m_suspensionCompression; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::get_suspension_damping +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletVehicleTuning:: +get_suspension_damping() const { + + return _.m_suspensionDamping; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::get_max_suspension_travel_cm +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletVehicleTuning:: +get_max_suspension_travel_cm() const { + + return _.m_maxSuspensionTravelCm; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::get_friction_slip +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletVehicleTuning:: +get_friction_slip() const { + + return _.m_frictionSlip; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicleTuning::get_max_suspension_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletVehicleTuning:: +get_max_suspension_force() const { + + return _.m_maxSuspensionForce; +} + diff --git a/panda/src/bullet/bulletVehicle.cxx b/panda/src/bullet/bulletVehicle.cxx new file mode 100644 index 0000000000..62a8427514 --- /dev/null +++ b/panda/src/bullet/bulletVehicle.cxx @@ -0,0 +1,245 @@ +// Filename: bulletVehicle.cxx +// Created by: enn0x (16Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletVehicle.h" +#include "bulletWorld.h" +#include "bulletRigidBodyNode.h" +#include "bulletWheel.h" + +TypeHandle BulletVehicle::_type_handle; + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletVehicle:: +BulletVehicle(BulletWorld *world, BulletRigidBodyNode *chassis) { + + btRigidBody *body = btRigidBody::upcast(chassis->get_object()); + + _raycaster = new btDefaultVehicleRaycaster(world->get_world()); + _vehicle = new btRaycastVehicle(_tuning._, body, _raycaster); + + set_coordinate_system(get_default_up_axis()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::set_coordinate_system +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicle:: +set_coordinate_system(BulletUpAxis up) { + + switch (up) { + case X_up: + _vehicle->setCoordinateSystem(1, 0, 2); + break; + case Y_up: + _vehicle->setCoordinateSystem(0, 1, 2); + break; + case Z_up: + _vehicle->setCoordinateSystem(0, 2, 1); + break; + default: + bullet_cat.error() << "invalid up axis:" << up << endl; + break; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::get_forward_vector +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f BulletVehicle:: +get_forward_vector() const { + + return btVector3_to_LVector3f(_vehicle->getForwardVector()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::get_chassis +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletRigidBodyNode *BulletVehicle:: +get_chassis() { + + btRigidBody *bodyPtr = _vehicle->getRigidBody(); + return (bodyPtr) ? (BulletRigidBodyNode *)bodyPtr->getUserPointer() : NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::get_current_speed_km_hour +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletVehicle:: +get_current_speed_km_hour() const { + + return _vehicle->getCurrentSpeedKmHour(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::reset_suspension +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicle:: +reset_suspension() { + + _vehicle->resetSuspension(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::get_steering_value +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletVehicle:: +get_steering_value(int idx) const { + + nassertr(idx < get_num_wheels(), 0.0f); + return rad_2_deg(_vehicle->getSteeringValue(idx)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::set_steering_value +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicle:: +set_steering_value(float steering, int idx) { + + nassertv(idx < get_num_wheels()); + _vehicle->setSteeringValue(deg_2_rad(steering), idx); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::apply_engine_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicle:: +apply_engine_force(float force, int idx) { + + nassertv(idx < get_num_wheels()); + _vehicle->applyEngineForce(force, idx); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::set_brake +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicle:: +set_brake(float brake, int idx) { + + nassertv(idx < get_num_wheels()); + _vehicle->setBrake(brake, idx); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::set_pitch_control +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicle:: +set_pitch_control(float pitch) { + + _vehicle->setPitchControl(pitch); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::create_wheel +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletWheel BulletVehicle:: +create_wheel() { + + btVector3 pos(0.0, 0.0, 0.0); + btVector3 direction = get_axis(_vehicle->getUpAxis()); + btVector3 axle = get_axis(_vehicle->getRightAxis()); + + btScalar suspension(0.4); + btScalar radius(0.3); + + btWheelInfo &info = _vehicle->addWheel(pos, direction, axle, suspension, radius, _tuning._, false); + + info.m_clientInfo = NULL; + + return BulletWheel(info); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::get_axis +// Access: Private +// Description: +//////////////////////////////////////////////////////////////////// +btVector3 BulletVehicle:: +get_axis(int idx) { + + switch (idx) { + case 0: + return btVector3(1.0, 0.0, 0.0); + case 1: + return btVector3(0.0, 1.0, 0.0); + case 2: + return btVector3(0.0, 0.0, 1.0); + default: + return btVector3(0.0, 0.0, 0.0); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::get_wheel +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletWheel BulletVehicle:: +get_wheel(int idx) const { + + nassertr(idx < get_num_wheels(), BulletWheel::empty()); + return BulletWheel(_vehicle->getWheelInfo(idx)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletVehicle::post_step +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +void BulletVehicle:: +post_step() { + + for (int i=0; i < get_num_wheels(); i++) { + btWheelInfo info = _vehicle->getWheelInfo(i); + + PandaNode *node = (PandaNode *)info.m_clientInfo; + if (node) { + + CPT(TransformState) ts = btTrans_to_TransformState(info.m_worldTransform); + + // Transform relative to wheel node's parent + //node->set_transform(ts); + + // Transform absolute + NodePath np = NodePath::any_path(node); + np.set_transform(np.get_top(), ts); + } + } +} + diff --git a/panda/src/bullet/bulletVehicle.h b/panda/src/bullet/bulletVehicle.h new file mode 100644 index 0000000000..ff0415626f --- /dev/null +++ b/panda/src/bullet/bulletVehicle.h @@ -0,0 +1,128 @@ +// Filename: bulletVehicle.h +// Created by: enn0x (16Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_VEHICLE_H__ +#define __BULLET_VEHICLE_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" + +#include "typedReferenceCount.h" +#include "lpoint3.h" +#include "lvector3.h" + +class BulletWorld; +class BulletRigidBodyNode; +class BulletWheel; + +//////////////////////////////////////////////////////////////////// +// Class : BulletVehicleTuning +// Description : +//////////////////////////////////////////////////////////////////// +class BulletVehicleTuning { + +PUBLISHED: + INLINE void set_suspension_stiffness(float value); + INLINE void set_suspension_compression(float value); + INLINE void set_suspension_damping(float value); + INLINE void set_max_suspension_travel_cm(float value); + INLINE void set_friction_slip(float value); + INLINE void set_max_suspension_force(float value); + + INLINE float get_suspension_stiffness() const; + INLINE float get_suspension_compression() const; + INLINE float get_suspension_damping() const; + INLINE float get_max_suspension_travel_cm() const; + INLINE float get_friction_slip() const; + INLINE float get_max_suspension_force() const; + +private: + btRaycastVehicle::btVehicleTuning _; + + friend class BulletVehicle; +}; + +//////////////////////////////////////////////////////////////////// +// Class : BulletVehicle +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletVehicle : public TypedReferenceCount { + +PUBLISHED: + BulletVehicle(BulletWorld *world, BulletRigidBodyNode *chassis); + INLINE ~BulletVehicle(); + + void set_coordinate_system(BulletUpAxis up); + void set_steering_value(float steering, int idx); + void set_brake(float brake, int idx); + void set_pitch_control(float pitch); + + BulletRigidBodyNode *get_chassis(); + float get_current_speed_km_hour() const; + float get_steering_value(int idx) const; + LVector3f get_forward_vector() const; + + void reset_suspension(); + void apply_engine_force(float force, int idx); + + // Wheels + BulletWheel create_wheel(); + + INLINE int get_num_wheels() const; + BulletWheel get_wheel(int idx) const; + MAKE_SEQ(get_wheels, get_num_wheels, get_wheel); + + // Tuning + INLINE BulletVehicleTuning &get_tuning(); + +public: + INLINE btRaycastVehicle *get_vehicle() const; + + void post_step(); + +private: + btRaycastVehicle *_vehicle; + btVehicleRaycaster *_raycaster; + + BulletVehicleTuning _tuning; + + static btVector3 get_axis(int idx); + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + TypedReferenceCount::init_type(); + register_type(_type_handle, "BulletVehicle", + TypedReferenceCount::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +#include "bulletVehicle.I" + +#endif // __BULLET_VEHICLE_H__ diff --git a/panda/src/bullet/bulletWheel.I b/panda/src/bullet/bulletWheel.I new file mode 100644 index 0000000000..ddba951a7c --- /dev/null +++ b/panda/src/bullet/bulletWheel.I @@ -0,0 +1,39 @@ +// Filename: bulletWheel.I +// Created by: enn0x (17Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletWheel:: +~BulletWheel() { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::empty +// Access: Public +// Description: Named constructor intended to be used for asserts +// with have to return a concrete value. +//////////////////////////////////////////////////////////////////// +INLINE BulletWheel BulletWheel:: +empty() { + + btWheelInfoConstructionInfo ci; + btWheelInfo info(ci); + + return BulletWheel(info); +} + diff --git a/panda/src/bullet/bulletWheel.cxx b/panda/src/bullet/bulletWheel.cxx new file mode 100644 index 0000000000..838dee61d2 --- /dev/null +++ b/panda/src/bullet/bulletWheel.cxx @@ -0,0 +1,547 @@ +// Filename: bulletWheel.cxx +// Created by: enn0x (17Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletWheel.h" + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::Constructor +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +BulletWheel:: +BulletWheel(btWheelInfo &info) : _info(info) { + +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_suspension_rest_length +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_suspension_rest_length() const { + + return _info.getSuspensionRestLength(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_suspension_stiffness +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_suspension_stiffness(float value) { + + _info.m_suspensionStiffness = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_suspension_stiffness +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_suspension_stiffness() const { + + return _info.m_suspensionStiffness; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_max_suspension_travel_cm +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_max_suspension_travel_cm(float value) { + + _info.m_maxSuspensionTravelCm = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_max_suspension_travel_cm +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_max_suspension_travel_cm() const { + + return _info.m_maxSuspensionTravelCm; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_friction_slip +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_friction_slip(float value) { + + _info.m_frictionSlip = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_friction_slip +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_friction_slip() const { + + return _info.m_frictionSlip; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_max_suspension_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_max_suspension_force(float value) { + + _info.m_maxSuspensionForce = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_max_suspension_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_max_suspension_force() const { + + return _info.m_maxSuspensionForce; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_wheels_damping_compression +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_wheels_damping_compression(float value) { + + _info.m_wheelsDampingCompression = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_wheels_damping_compression +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_wheels_damping_compression() const { + + return _info.m_wheelsDampingCompression; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_wheels_damping_relaxation +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_wheels_damping_relaxation(float value) { + + _info.m_wheelsDampingRelaxation = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_wheels_damping_relaxation +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_wheels_damping_relaxation() const { + + return _info.m_wheelsDampingRelaxation; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_roll_influence +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_roll_influence(float value) { + + _info.m_rollInfluence = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_roll_influence +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_roll_influence() const { + + return _info.m_rollInfluence; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_wheel_radius +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_wheel_radius(float value) { + + _info.m_wheelsRadius = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_wheel_radius +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_wheel_radius() const { + + return _info.m_wheelsRadius; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_steering +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_steering(float value) { + + _info.m_steering = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_steering +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_steering() const { + + return _info.m_steering; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_rotation +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_rotation(float value) { + + _info.m_rotation = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_rotation +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_rotation() const { + + return _info.m_rotation; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_delta_rotation +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_delta_rotation(float value) { + + _info.m_deltaRotation = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_delta_rotation +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_delta_rotation() const { + + return _info.m_deltaRotation; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_engine_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_engine_force(float value) { + + _info.m_engineForce = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_engine_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_engine_force() const { + + return _info.m_engineForce; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_brake +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_brake(float value) { + + _info.m_brake = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_brake +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_brake() const { + + return _info.m_brake; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_skid_info +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_skid_info(float value) { + + _info.m_skidInfo = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_skid_info +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_skid_info() const { + + return _info.m_skidInfo; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_wheels_suspension_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_wheels_suspension_force(float value) { + + _info.m_wheelsSuspensionForce = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_wheels_suspension_force +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_wheels_suspension_force() const { + + return _info.m_wheelsSuspensionForce; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_suspension_relative_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_suspension_relative_velocity(float value) { + + _info.m_suspensionRelativeVelocity = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_suspension_relative_velocity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_suspension_relative_velocity() const { + + return _info.m_suspensionRelativeVelocity; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_clipped_inv_connection_point_cs +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_clipped_inv_connection_point_cs(float value) { + + _info.m_clippedInvContactDotSuspension = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_clipped_inv_connection_point_cs +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +float BulletWheel:: +get_clipped_inv_connection_point_cs() const { + + return _info.m_clippedInvContactDotSuspension; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_chassis_connection_point_cs +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_chassis_connection_point_cs(const LPoint3f &pos) { + + nassertv(!pos.is_nan()); + _info.m_chassisConnectionPointCS = LVecBase3f_to_btVector3(pos); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_chassis_connection_point_cs +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f BulletWheel:: +get_chassis_connection_point_cs() const { + + return btVector3_to_LPoint3f(_info.m_chassisConnectionPointCS); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_wheel_direction_cs +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_wheel_direction_cs(const LVector3f &dir) { + + nassertv(!dir.is_nan()); + _info.m_wheelDirectionCS = LVecBase3f_to_btVector3(dir); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_wheel_direction_cs +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f BulletWheel:: +get_wheel_direction_cs() const { + + return btVector3_to_LVector3f(_info.m_wheelDirectionCS); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_wheel_axle_cs +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_wheel_axle_cs(const LVector3f &axle) { + + nassertv(!axle.is_nan()); + _info.m_wheelAxleCS = LVecBase3f_to_btVector3(axle); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_wheel_axle_cs +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f BulletWheel:: +get_wheel_axle_cs() const { + + return btVector3_to_LVector3f(_info.m_wheelAxleCS); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_world_transform +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_world_transform(const LMatrix4f &mat) { + + nassertv(!mat.is_nan()); + _info.m_worldTransform = LMatrix4f_to_btTrans(mat); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_world_transform +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +LMatrix4f BulletWheel:: +get_world_transform() const { + + return btTrans_to_LMatrix4f(_info.m_worldTransform); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_front_wheel +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_front_wheel(bool value) { + + _info.m_bIsFrontWheel = value; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::is_front_wheel +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletWheel:: +is_front_wheel() const { + + return _info.m_bIsFrontWheel; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::set_node +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWheel:: +set_node(PandaNode *node) { + + _info.m_clientInfo = (void *)node; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWheel::get_node +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +PandaNode *BulletWheel:: +get_node() const { + + return (_info.m_clientInfo == NULL) ? NULL : (PandaNode *)_info.m_clientInfo; +} + diff --git a/panda/src/bullet/bulletWheel.h b/panda/src/bullet/bulletWheel.h new file mode 100644 index 0000000000..137be17213 --- /dev/null +++ b/panda/src/bullet/bulletWheel.h @@ -0,0 +1,97 @@ +// Filename: bulletWheel.h +// Created by: enn0x (17Feb10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_WHEEL_H__ +#define __BULLET_WHEEL_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" + +#include "lvector3.h" +#include "lpoint3.h" +#include "lmatrix.h" +#include "pandaNode.h" + +//////////////////////////////////////////////////////////////////// +// Class : BulletWheel +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletWheel { + +PUBLISHED: + INLINE ~BulletWheel(); + + void set_suspension_stiffness(float value); + void set_max_suspension_travel_cm(float value); + void set_friction_slip(float value); + void set_max_suspension_force(float value); + void set_wheels_damping_compression(float value); + void set_wheels_damping_relaxation(float value); + void set_roll_influence(float value); + void set_wheel_radius(float value); + void set_steering(float value); + void set_rotation(float value); + void set_delta_rotation(float value); + void set_engine_force(float value); + void set_brake(float value); + void set_skid_info(float value); + void set_wheels_suspension_force(float value); + void set_suspension_relative_velocity(float value); + void set_clipped_inv_connection_point_cs(float value); + void set_chassis_connection_point_cs(const LPoint3f &pos); + void set_wheel_direction_cs(const LVector3f &dir); + void set_wheel_axle_cs(const LVector3f &axle); + void set_world_transform(const LMatrix4f &mat); + void set_front_wheel(bool value); + void set_node(PandaNode *node); + + float get_suspension_rest_length() const; + float get_suspension_stiffness() const; + float get_max_suspension_travel_cm() const; + float get_friction_slip() const; + float get_max_suspension_force() const; + float get_wheels_damping_compression() const; + float get_wheels_damping_relaxation() const; + float get_roll_influence() const; + float get_wheel_radius() const; + float get_steering() const; + float get_rotation() const; + float get_delta_rotation() const; + float get_engine_force() const; + float get_brake() const; + float get_skid_info() const; + float get_wheels_suspension_force() const; + float get_suspension_relative_velocity() const; + float get_clipped_inv_connection_point_cs() const; + LPoint3f get_chassis_connection_point_cs() const; + LVector3f get_wheel_direction_cs() const; + LVector3f get_wheel_axle_cs() const; + LMatrix4f get_world_transform() const; + bool is_front_wheel() const; + PandaNode *get_node() const; + +public: + BulletWheel(btWheelInfo &info); + + INLINE static BulletWheel empty(); + +private: + btWheelInfo &_info; +}; + +#include "bulletWheel.I" + +#endif // __BULLET_WHEEL_H__ diff --git a/panda/src/bullet/bulletWorld.I b/panda/src/bullet/bulletWorld.I new file mode 100644 index 0000000000..e2d5ca2d5e --- /dev/null +++ b/panda/src/bullet/bulletWorld.I @@ -0,0 +1,242 @@ +// Filename: bulletWorld.I +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::Destructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletWorld:: +~BulletWorld() { + + while (get_num_characters() > 0) { + remove_character(get_character(0)); + } + + while (get_num_vehicles() > 0) { + remove_vehicle(get_vehicle(0)); + } + + while (get_num_constraints() > 0) { + remove_constraint(get_constraint(0)); + } + + while (get_num_rigid_bodies() > 0) { + remove_rigid_body(get_rigid_body(0)); + } + + while (get_num_soft_bodies() > 0) { + remove_soft_body(get_soft_body(0)); + } + + while (get_num_ghosts() > 0) { + remove_ghost(get_ghost(0)); + } + + _info.m_sparsesdf.Reset(); + + delete _ghost_cb; + delete _filter_cb; + + delete _world; + delete _solver; + delete _configuration; + delete _dispatcher; + delete _broadphase; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_world +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE btDynamicsWorld *BulletWorld:: +get_world() const { + + return _world; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_broadphase +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE btBroadphaseInterface *BulletWorld:: +get_broadphase() const { + + return _broadphase; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_dispatcher +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +INLINE btDispatcher *BulletWorld:: +get_dispatcher() const { + + return _dispatcher; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_num_rigid_bodies +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletWorld:: +get_num_rigid_bodies() const { + + return _bodies.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_rigid_body +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletRigidBodyNode *BulletWorld:: +get_rigid_body(int idx) const { + + nassertr(idx >= 0 && idx < (int)_bodies.size(), NULL); + return _bodies[idx]; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_num_soft_bodies +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletWorld:: +get_num_soft_bodies() const { + + return _softbodies.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_soft_body +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletSoftBodyNode *BulletWorld:: +get_soft_body(int idx) const { + + nassertr(idx >= 0 && idx < (int)_softbodies.size(), NULL); + return _softbodies[idx]; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_num_ghosts +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletWorld:: +get_num_ghosts() const { + + return _ghosts.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_ghost +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletGhostNode *BulletWorld:: +get_ghost(int idx) const { + + nassertr(idx >= 0 && idx < (int)_ghosts.size(), NULL); + return _ghosts[idx]; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_num_characters +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletWorld:: +get_num_characters() const { + + return _characters.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_character +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletCharacterControllerNode *BulletWorld:: +get_character(int idx) const { + + nassertr(idx >= 0 && idx < (int)_characters.size(), NULL); + return _characters[idx]; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_num_vehicles +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletWorld:: +get_num_vehicles() const { + + return _vehicles.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_vehicle +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletVehicle *BulletWorld:: +get_vehicle(int idx) const { + + nassertr(idx >= 0 && idx < (int)_vehicles.size(), NULL); + return _vehicles[idx]; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_num_constraints +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletWorld:: +get_num_constraints() const { + + return _constraints.size(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_constraint +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE BulletConstraint *BulletWorld:: +get_constraint(int idx) const { + + nassertr(idx >= 0 && idx < (int)_constraints.size(), NULL); + return _constraints[idx]; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_num_manifolds +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +INLINE int BulletWorld:: +get_num_manifolds() const { + + return _world->getDispatcher()->getNumManifolds(); +} + + diff --git a/panda/src/bullet/bulletWorld.cxx b/panda/src/bullet/bulletWorld.cxx new file mode 100644 index 0000000000..50821b3a5e --- /dev/null +++ b/panda/src/bullet/bulletWorld.cxx @@ -0,0 +1,736 @@ +// Filename: bulletWorld.cxx +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bulletWorld.h" +#include "bulletPersistentManifold.h" +#include "bulletShape.h" +#include "bulletSoftBodyWorldInfo.h" + +#include "collideMask.h" + +TypeHandle BulletWorld::_type_handle; + +PStatCollector BulletWorld::_pstat_physics("App:Bullet:DoPhysics"); +PStatCollector BulletWorld::_pstat_simulation("App:Bullet:DoPhysics:Simulation"); +PStatCollector BulletWorld::_pstat_debug("App:Bullet:DoPhysics:Debug"); +PStatCollector BulletWorld::_pstat_sb("App:Bullet:DoPhysics:SoftBodies"); + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::Constructor +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletWorld:: +BulletWorld() { + + // Callbacks + _ghost_cb = new btGhostPairCallback(); + _filter_cb = new btFilterCallback(); + + // Broadphase + btScalar dx(bullet_sap_extents); + btVector3 extents(dx, dx, dx); + + switch (bullet_broadphase_algorithm) { + case BA_sweep_and_prune: + _broadphase = new btAxisSweep3(extents, extents, 1024); + break; + case BA_dynamic_aabb_tree: + _broadphase = new btDbvtBroadphase(); + break; + default: + bullet_cat.error() << "no proper broadphase algorithm!" << endl; + } + + // Configuration + _configuration = new btSoftBodyRigidBodyCollisionConfiguration(); + + // Dispatcher + _dispatcher = new btCollisionDispatcher(_configuration); + + // Solver + _solver = new btSequentialImpulseConstraintSolver; + + // World + _world = new btSoftRigidDynamicsWorld(_dispatcher, _broadphase, _solver, _configuration); + _world->getPairCache()->setInternalGhostPairCallback(_ghost_cb); + _world->getPairCache()->setOverlapFilterCallback(_filter_cb); + _world->setGravity(btVector3(0.0f, 0.0f, 0.0f)); + + // SoftBodyWorldInfo + _info.m_dispatcher = _dispatcher; + _info.m_broadphase = _broadphase; + _info.m_gravity = _world->getGravity(); + _info.m_sparsesdf.Initialize(); + + // Register GIMPACT algorithm + btGImpactCollisionAlgorithm::registerAlgorithm(_dispatcher); + + // Some prefered settings + _world->getDispatchInfo().m_enableSPU = true; // default: true + _world->getDispatchInfo().m_useContinuous = true; // default: true + _world->getSolverInfo().m_splitImpulse = false; // default: false + _world->getSolverInfo().m_numIterations = bullet_solver_iterations; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_world_info +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletSoftBodyWorldInfo BulletWorld:: +get_world_info() { + + return BulletSoftBodyWorldInfo(_info); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::set_gravity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +set_gravity(const LVector3f &gravity) { + + _world->setGravity(LVecBase3f_to_btVector3(gravity)); + _info.m_gravity = _world->getGravity(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::set_gravity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +set_gravity(float gx, float gy, float gz) { + + _world->setGravity(btVector3(gx, gy, gz)); + _info.m_gravity = _world->getGravity(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_gravity +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +const LVector3f BulletWorld:: +get_gravity() const { + + return btVector3_to_LVector3f(_world->getGravity()); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::do_physics +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +do_physics(float dt, int substeps, float stepsize) { + + _pstat_physics.start(); + + // Preprocess characters + for (int i=0; i < get_num_characters(); i++) { + get_character(i)->pre_step(dt); + } + + // Preprocess ghosts (autosync) + for (int i=0; i < get_num_ghosts(); i++) { + get_ghost(i)->pre_step(); + } + + // Simulation + _pstat_simulation.start(); + _world->stepSimulation(dt, substeps, stepsize); + _pstat_simulation.stop(); + + // Postprocess characters + for (int i=0; i < get_num_characters(); i++) { + get_character(i)->post_step(); + } + + // Postprocess vehicles + for (int i=0; i < get_num_vehicles(); i++) { + get_vehicle(i)->post_step(); + } + + // Render debug + _pstat_debug.start(); + if (_debug) { + _debug->post_step(_world); + } + _pstat_debug.stop(); + + // Render soft bodies + _pstat_sb.start(); + for (int i=0; i < get_num_soft_bodies(); i++) { + get_soft_body(i)->post_step(); + } + _info.m_sparsesdf.GarbageCollect(bullet_gc_lifetime); + _pstat_sb.stop(); + + _pstat_physics.stop(); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::set_debug_node +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +set_debug_node(BulletDebugNode *node) { + + nassertv(node); + + _debug = node; + _world->setDebugDrawer(&(_debug->_drawer)); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::clear_debug_node +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +clear_debug_node() { + + _debug = NULL; + _world->setDebugDrawer(NULL); +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::attach_rigid_body +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +attach_rigid_body(BulletRigidBodyNode *node) { + + nassertv(node); + + btRigidBody *ptr = btRigidBody::upcast(node->get_object()); + + BulletRigidBodies::iterator found; + PT(BulletRigidBodyNode) ptnode = node; + found = find(_bodies.begin(), _bodies.end(), ptnode); + + if (found == _bodies.end()) { + _bodies.push_back(node); + _world->addRigidBody(ptr); + } + else { + bullet_cat.warning() << "rigid body already attached" << endl; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::remove_rigid_body +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +remove_rigid_body(BulletRigidBodyNode *node) { + + nassertv(node); + + btRigidBody *ptr = btRigidBody::upcast(node->get_object()); + + BulletRigidBodies::iterator found; + PT(BulletRigidBodyNode) ptnode = node; + found = find(_bodies.begin(), _bodies.end(), ptnode); + + if (found == _bodies.end()) { + bullet_cat.warning() << "rigid body not attached" << endl; + } + else { + _bodies.erase(found); + _world->removeRigidBody(ptr); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::attach_soft_body +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +attach_soft_body(BulletSoftBodyNode *node) { + + nassertv(node); + + btSoftBody *ptr = btSoftBody::upcast(node->get_object()); + + // TODO: group/filter settings (see ghost objects too) + short group = btBroadphaseProxy::DefaultFilter; + short mask = btBroadphaseProxy::AllFilter; + + BulletSoftBodies::iterator found; + PT(BulletSoftBodyNode) ptnode = node; + found = find(_softbodies.begin(), _softbodies.end(), ptnode); + + if (found == _softbodies.end()) { + _softbodies.push_back(node); + _world->addSoftBody(ptr, group, mask); + } + else { + bullet_cat.warning() << "soft body already attached" << endl; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::remove_soft_body +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +remove_soft_body(BulletSoftBodyNode *node) { + + nassertv(node); + + btSoftBody *ptr = btSoftBody::upcast(node->get_object()); + + BulletSoftBodies::iterator found; + PT(BulletSoftBodyNode) ptnode = node; + found = find(_softbodies.begin(), _softbodies.end(), ptnode); + + if (found == _softbodies.end()) { + bullet_cat.warning() << "soft body not attached" << endl; + } + else { + _softbodies.erase(found); + _world->removeSoftBody(ptr); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::attach_ghost +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +attach_ghost(BulletGhostNode *node) { + + nassertv(node); + + // TODO group/filter settings... +/* +enum CollisionFilterGroups { + DefaultFilter = 1, + StaticFilter = 2, + KinematicFilter = 4, + DebrisFilter = 8, + SensorTrigger = 16, + CharacterFilter = 32, + AllFilter = -1 +} +*/ + + short group = btBroadphaseProxy::SensorTrigger; + short mask = btBroadphaseProxy::AllFilter + & ~btBroadphaseProxy::StaticFilter + & ~btBroadphaseProxy::SensorTrigger; + + btGhostObject *ptr = btGhostObject::upcast(node->get_object()); + + BulletGhosts::iterator found; + PT(BulletGhostNode) ptnode = node; + found = find(_ghosts.begin(), _ghosts.end(), ptnode); + + if (found == _ghosts.end()) { + _ghosts.push_back(node); + _world->addCollisionObject(ptr, group, mask); + } + else { + bullet_cat.warning() << "ghost already attached" << endl; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::remove_ghost +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +remove_ghost(BulletGhostNode *node) { + + nassertv(node); + + btGhostObject *ptr = btGhostObject::upcast(node->get_object()); + + BulletGhosts::iterator found; + PT(BulletGhostNode) ptnode = node; + found = find(_ghosts.begin(), _ghosts.end(), ptnode); + + if (found == _ghosts.end()) { + bullet_cat.warning() << "ghost not attached" << endl; + } + else { + _ghosts.erase(found); + _world->removeCollisionObject(ptr); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::attach_character +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +attach_character(BulletCharacterControllerNode *node) { + + nassertv(node); + + BulletCharacterControllers::iterator found; + PT(BulletCharacterControllerNode) ptnode = node; + found = find(_characters.begin(), _characters.end(), ptnode); + + if (found == _characters.end()) { + _characters.push_back(node); + + _world->addCollisionObject(node->get_ghost(), + btBroadphaseProxy::CharacterFilter, + btBroadphaseProxy::StaticFilter|btBroadphaseProxy::DefaultFilter); + + _world->addCharacter(node->get_character()); + } + else { + bullet_cat.warning() << "character already attached" << endl; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::remove_character +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +remove_character(BulletCharacterControllerNode *node) { + + nassertv(node); + + BulletCharacterControllers::iterator found; + PT(BulletCharacterControllerNode) ptnode = node; + found = find(_characters.begin(), _characters.end(), ptnode); + + if (found == _characters.end()) { + bullet_cat.warning() << "character not attached" << endl; + } + else { + _characters.erase(found); + _world->removeCollisionObject(node->get_ghost()); + _world->removeCharacter(node->get_character()); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::attach_vehicle +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +attach_vehicle(BulletVehicle *vehicle) { + + nassertv(vehicle); + + BulletVehicles::iterator found; + PT(BulletVehicle) ptvehicle = vehicle; + found = find(_vehicles.begin(), _vehicles.end(), ptvehicle); + + if (found == _vehicles.end()) { + _vehicles.push_back(vehicle); + _world->addVehicle(vehicle->get_vehicle()); + } + else { + bullet_cat.warning() << "vehicle already attached" << endl; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::remove_vehicle +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +remove_vehicle(BulletVehicle *vehicle) { + + nassertv(vehicle); + + remove_rigid_body(vehicle->get_chassis()); + + BulletVehicles::iterator found; + PT(BulletVehicle) ptvehicle = vehicle; + found = find(_vehicles.begin(), _vehicles.end(), ptvehicle); + + if (found == _vehicles.end()) { + bullet_cat.warning() << "vehicle not attached" << endl; + } + else { + _vehicles.erase(found); + _world->removeVehicle(vehicle->get_vehicle()); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::attach_constraint +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +attach_constraint(BulletConstraint *constraint) { + + nassertv(constraint); + + BulletConstraints::iterator found; + PT(BulletConstraint) ptconstraint = constraint; + found = find(_constraints.begin(), _constraints.end(), ptconstraint); + + if (found == _constraints.end()) { + _constraints.push_back(constraint); + _world->addConstraint(constraint->ptr()); + } + else { + bullet_cat.warning() << "constraint already attached" << endl; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::remove_constraint +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +void BulletWorld:: +remove_constraint(BulletConstraint *constraint) { + + nassertv(constraint); + + BulletConstraints::iterator found; + PT(BulletConstraint) ptconstraint = constraint; + found = find(_constraints.begin(), _constraints.end(), ptconstraint); + + if (found == _constraints.end()) { + bullet_cat.warning() << "constraint not attached" << endl; + } + else { + _constraints.erase(found); + _world->removeConstraint(constraint->ptr()); + } +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::ray_test_closest +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletClosestHitRayResult BulletWorld:: +ray_test_closest(const LPoint3f &from_pos, const LPoint3f &to_pos, const CollideMask &mask) const { + + nassertr(!from_pos.is_nan(), BulletClosestHitRayResult::empty()); + nassertr(!to_pos.is_nan(), BulletClosestHitRayResult::empty()); + + const btVector3 from = LVecBase3f_to_btVector3(from_pos); + const btVector3 to = LVecBase3f_to_btVector3(to_pos); + + BulletClosestHitRayResult cb(from, to, mask); + _world->rayTest(from, to, cb); + return cb; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::ray_test_all +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletAllHitsRayResult BulletWorld:: +ray_test_all(const LPoint3f &from_pos, const LPoint3f &to_pos, const CollideMask &mask) const { + + nassertr(!from_pos.is_nan(), BulletAllHitsRayResult::empty()); + nassertr(!to_pos.is_nan(), BulletAllHitsRayResult::empty()); + + const btVector3 from = LVecBase3f_to_btVector3(from_pos); + const btVector3 to = LVecBase3f_to_btVector3(to_pos); + + BulletAllHitsRayResult cb(from, to, mask); + _world->rayTest(from, to, cb); + return cb; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::sweep_test_closest +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletClosestHitSweepResult BulletWorld:: +sweep_test_closest(BulletShape *shape, const TransformState &from_ts, const TransformState &to_ts, const CollideMask &mask, float penetration) const { + + nassertr(shape, BulletClosestHitSweepResult::empty()); + nassertr(shape->is_convex(), BulletClosestHitSweepResult::empty()); + nassertr(!from_ts.is_invalid(), BulletClosestHitSweepResult::empty()); + nassertr(!to_ts.is_invalid(), BulletClosestHitSweepResult::empty()); + + const btConvexShape *convex = (const btConvexShape *) shape->ptr(); + const btVector3 from_pos = LVecBase3f_to_btVector3(from_ts.get_pos()); + const btVector3 to_pos = LVecBase3f_to_btVector3(to_ts.get_pos()); + const btTransform from_trans = LMatrix4f_to_btTrans(from_ts.get_mat()); + const btTransform to_trans = LMatrix4f_to_btTrans(to_ts.get_mat()); + + BulletClosestHitSweepResult cb(from_pos, to_pos, mask); + _world->convexSweepTest(convex, from_trans, to_trans, cb, penetration); + return cb; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::contact_test +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletContactResult BulletWorld:: +contact_test(PandaNode *node) const { + + btCollisionObject *obj = get_collision_object(node); + + BulletContactResult cb; + + if (obj) { + _world->contactTest(obj, cb); + } + + return cb; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::contact_pair_test +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletContactResult BulletWorld:: +contact_test_pair(PandaNode *node0, PandaNode *node1) const { + + btCollisionObject *obj0 = get_collision_object(node0); + btCollisionObject *obj1 = get_collision_object(node1); + + BulletContactResult cb; + + if (obj0 && obj1) { + _world->contactPairTest(obj0, obj1, cb); + } + + return cb; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_manifold +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +BulletPersistentManifold *BulletWorld:: +get_manifold(int idx) const { + + nassertr(idx < get_num_manifolds(), NULL); + + btPersistentManifold *ptr = _dispatcher->getManifoldByIndexInternal(idx); + return (ptr) ? new BulletPersistentManifold(ptr) : NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::get_collision_object +// Access: Public +// Description: +//////////////////////////////////////////////////////////////////// +btCollisionObject *BulletWorld:: +get_collision_object(PandaNode *node) { + + if (node->is_of_type(BulletRigidBodyNode::get_class_type())) { + return ((BulletRigidBodyNode *)node)->get_object(); + } + else if (node->is_of_type(BulletGhostNode::get_class_type())) { + return ((BulletGhostNode *)node)->get_object(); + } + else if (node->is_of_type(BulletCharacterControllerNode::get_class_type())) { + return ((BulletCharacterControllerNode *)node)->get_ghost(); + } + else if (node->is_of_type(BulletSoftBodyNode::get_class_type())) { + return ((BulletSoftBodyNode *)node)->get_object(); + } + + return NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::FilterCallback::needBroadphaseCollision +// Access: Published +// Description: +//////////////////////////////////////////////////////////////////// +bool BulletWorld::btFilterCallback:: +needBroadphaseCollision(btBroadphaseProxy* proxy0, btBroadphaseProxy* proxy1) const { + + btCollisionObject *obj0 = (btCollisionObject *) proxy0->m_clientObject; + btCollisionObject *obj1 = (btCollisionObject *) proxy1->m_clientObject; + + nassertr(obj0, false); + nassertr(obj1, false); + + PandaNode *node0 = (PandaNode *) obj0->getUserPointer(); + PandaNode *node1 = (PandaNode *) obj1->getUserPointer(); + + nassertr(node0, false); + nassertr(node1, false); + + CollideMask mask0 = node0->get_into_collide_mask(); + CollideMask mask1 = node1->get_into_collide_mask(); + + return (mask0 & mask1) != 0; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::BroadphaseAlgorithm ostream operator +// Description: +//////////////////////////////////////////////////////////////////// +ostream & +operator << (ostream &out, BulletWorld::BroadphaseAlgorithm algorithm) { + + switch (algorithm) { + case BulletWorld::BA_sweep_and_prune: + return out << "sap"; + + case BulletWorld::BA_dynamic_aabb_tree: + return out << "aabb"; + }; + + return out << "**invalid BulletWorld::BroadphaseAlgorithm(" << (int)algorithm << ")**"; +} + +//////////////////////////////////////////////////////////////////// +// Function: BulletWorld::BroadphaseAlgorithm istream operator +// Description: +//////////////////////////////////////////////////////////////////// +istream & +operator >> (istream &in, BulletWorld::BroadphaseAlgorithm &algorithm) { + string word; + in >> word; + + if (word == "sap") { + algorithm = BulletWorld::BA_sweep_and_prune; + } + else if (word == "aabb") { + algorithm = BulletWorld::BA_dynamic_aabb_tree; + } + else { + bullet_cat.error() + << "Invalid BulletWorld::BroadphaseAlgorithm: " << word << "\n"; + algorithm = BulletWorld::BA_dynamic_aabb_tree; + } + + return in; +} + diff --git a/panda/src/bullet/bulletWorld.h b/panda/src/bullet/bulletWorld.h new file mode 100644 index 0000000000..15de6f5a4d --- /dev/null +++ b/panda/src/bullet/bulletWorld.h @@ -0,0 +1,221 @@ +// Filename: bulletWorld.h +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_WORLD_H__ +#define __BULLET_WORLD_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" +#include "bullet_utils.h" + +#include "bulletClosestHitRayResult.h" +#include "bulletAllHitsRayResult.h" +#include "bulletClosestHitSweepResult.h" +#include "bulletContactResult.h" +#include "bulletDebugNode.h" +#include "bulletCharacterControllerNode.h" +#include "bulletConstraint.h" +#include "bulletGhostNode.h" +#include "bulletRigidBodyNode.h" +#include "bulletSoftBodyNode.h" +#include "bulletVehicle.h" + +#include "typedReferenceCount.h" +#include "transformState.h" +#include "pandaNode.h" +#include "collideMask.h" +#include "luse.h" + +class BulletPersistentManifold; +class BulletShape; +class BulletSoftBodyWorldInfo; + +//////////////////////////////////////////////////////////////////// +// Class : BulletWorld +// Description : +//////////////////////////////////////////////////////////////////// +class EXPCL_PANDABULLET BulletWorld : public TypedReferenceCount { + +PUBLISHED: + BulletWorld(); + INLINE ~BulletWorld(); + + void set_gravity(const LVector3f &gravity); + void set_gravity(float gx, float gy, float gz); + const LVector3f get_gravity() const; + + void do_physics(float dt, int substeps=10, float stepsize=1.0f/60.0f); + + void set_debug_node(BulletDebugNode *node); + void clear_debug_node(); + + BulletSoftBodyWorldInfo get_world_info(); + + // Ghost object + void attach_ghost(BulletGhostNode *node); + void remove_ghost(BulletGhostNode *node); + + INLINE int get_num_ghosts() const; + INLINE BulletGhostNode *get_ghost(int idx) const; + MAKE_SEQ(get_ghosts, get_num_ghosts, get_ghost); + + // Rigid body + void attach_rigid_body(BulletRigidBodyNode *node); + void remove_rigid_body(BulletRigidBodyNode *node); + + INLINE int get_num_rigid_bodies() const; + INLINE BulletRigidBodyNode *get_rigid_body(int idx) const; + MAKE_SEQ(get_rigid_bodies, get_num_rigid_bodies, get_rigid_body); + + // Soft body + void attach_soft_body(BulletSoftBodyNode *node); + void remove_soft_body(BulletSoftBodyNode *node); + + INLINE int get_num_soft_bodies() const; + INLINE BulletSoftBodyNode *get_soft_body(int idx) const; + MAKE_SEQ(get_soft_bodies, get_num_soft_bodies, get_soft_body); + + // Character controller + void attach_character(BulletCharacterControllerNode *node); + void remove_character(BulletCharacterControllerNode *node); + + INLINE int get_num_characters() const; + INLINE BulletCharacterControllerNode *get_character(int idx) const; + MAKE_SEQ(get_characters, get_num_characters, get_character); + + // Vehicle + void attach_vehicle(BulletVehicle *vehicle); + void remove_vehicle(BulletVehicle *vehicle); + + INLINE int get_num_vehicles() const; + INLINE BulletVehicle *get_vehicle(int idx) const; + MAKE_SEQ(get_vehicles, get_num_vehicles, get_vehicle); + + // Constraint + void attach_constraint(BulletConstraint *constraint); + void remove_constraint(BulletConstraint *constraint); + + INLINE int get_num_constraints() const; + INLINE BulletConstraint *get_constraint(int idx) const; + MAKE_SEQ(get_constraints, get_num_constraints, get_constraint); + + // Raycast and other queries + BulletClosestHitRayResult ray_test_closest( + const LPoint3f &from_pos, + const LPoint3f &to_pos, + const CollideMask &mask=CollideMask::all_on()) const; + + BulletAllHitsRayResult ray_test_all( + const LPoint3f &from_pos, + const LPoint3f &to_pos, + const CollideMask &mask=CollideMask::all_on()) const; + + BulletClosestHitSweepResult sweep_test_closest( + BulletShape *shape, + const TransformState &from_ts, + const TransformState &to_ts, + const CollideMask &mask=CollideMask::all_on(), + float penetration=0.0f) const; + + BulletContactResult contact_test(PandaNode *node) const; + BulletContactResult contact_test_pair(PandaNode *node0, PandaNode *node1) const; + + // Manifolds + INLINE int get_num_manifolds() const; + BulletPersistentManifold *get_manifold(int idx) const; + MAKE_SEQ(get_manifolds, get_num_manifolds, get_manifold); + + // Configuration + enum BroadphaseAlgorithm { + BA_sweep_and_prune, + BA_dynamic_aabb_tree, + }; + +public: + static btCollisionObject *get_collision_object(PandaNode *node); + + INLINE btDynamicsWorld *get_world() const; + INLINE btBroadphaseInterface *get_broadphase() const; + INLINE btDispatcher *get_dispatcher() const; + +private: + typedef PTA(PT(BulletRigidBodyNode)) BulletRigidBodies; + typedef PTA(PT(BulletSoftBodyNode)) BulletSoftBodies; + typedef PTA(PT(BulletGhostNode)) BulletGhosts; + typedef PTA(PT(BulletCharacterControllerNode)) BulletCharacterControllers; + typedef PTA(PT(BulletVehicle)) BulletVehicles; + typedef PTA(PT(BulletConstraint)) BulletConstraints; + + static PStatCollector _pstat_physics; + static PStatCollector _pstat_simulation; + static PStatCollector _pstat_debug; + static PStatCollector _pstat_sb; + + struct btFilterCallback : public btOverlapFilterCallback { + virtual bool needBroadphaseCollision( + btBroadphaseProxy* proxy0, + btBroadphaseProxy* proxy1) const; + }; + + btBroadphaseInterface *_broadphase; + btCollisionConfiguration *_configuration; + btCollisionDispatcher *_dispatcher; + btConstraintSolver *_solver; + btSoftRigidDynamicsWorld *_world; + + btGhostPairCallback *_ghost_cb; + btFilterCallback *_filter_cb; + + btSoftBodyWorldInfo _info; + + PT(BulletDebugNode) _debug; + + BulletRigidBodies _bodies; + BulletSoftBodies _softbodies; + BulletGhosts _ghosts; + BulletCharacterControllers _characters; + BulletVehicles _vehicles; + BulletConstraints _constraints; + +//////////////////////////////////////////////////////////////////// +public: + static TypeHandle get_class_type() { + return _type_handle; + } + static void init_type() { + TypedReferenceCount::init_type(); + register_type(_type_handle, "BulletWorld", + TypedReferenceCount::get_class_type()); + } + virtual TypeHandle get_type() const { + return get_class_type(); + } + virtual TypeHandle force_init_type() { + init_type(); + return get_class_type(); + } + +private: + static TypeHandle _type_handle; +}; + +EXPCL_PANDABULLET ostream & +operator << (ostream &out, BulletWorld::BroadphaseAlgorithm algorithm); +EXPCL_PANDABULLET istream & +operator >> (istream &in, BulletWorld::BroadphaseAlgorithm &algorithm); + +#include "bulletWorld.I" + +#endif // __BULLET_WORLD_H__ diff --git a/panda/src/bullet/bullet_composite.cxx b/panda/src/bullet/bullet_composite.cxx new file mode 100644 index 0000000000..bd713d5733 --- /dev/null +++ b/panda/src/bullet/bullet_composite.cxx @@ -0,0 +1,40 @@ +#include "config_bullet.cxx" +#include "bullet_utils.cxx" +#include "bulletAllHitsRayResult.cxx" +#include "bulletBodyNode.cxx" +#include "bulletBoxShape.cxx" +#include "bulletCapsuleShape.cxx" +#include "bulletClosestHitRayResult.cxx" +#include "bulletClosestHitSweepResult.cxx" +#include "bulletConeShape.cxx" +#include "bulletConeTwistConstraint.cxx" +#include "bulletConstraint.cxx" +#include "bulletContactResult.cxx" +#include "bulletConvexHullShape.cxx" +#include "bulletConvexPointCloudShape.cxx" +#include "bulletCharacterControllerNode.cxx" +#include "bulletCylinderShape.cxx" +#include "bulletDebugNode.cxx" +#include "bulletDistanceConstraint.cxx" +#include "bulletGenericConstraint.cxx" +#include "bulletGhostNode.cxx" +#include "bulletHelper.cxx" +#include "bulletHeightfieldShape.cxx" +#include "bulletHingeConstraint.cxx" +#include "bulletManifoldPoint.cxx" +#include "bulletPersistentManifold.cxx" +#include "bulletPlaneShape.cxx" +#include "bulletRigidBodyNode.cxx" +#include "bulletShape.cxx" +#include "bulletSliderConstraint.cxx" +#include "bulletSphereShape.cxx" +#include "bulletSoftBodyNode.cxx" +#include "bulletSoftBodyConfig.cxx" +#include "bulletSoftBodyMaterial.cxx" +#include "bulletSoftBodyShape.cxx" +#include "bulletSoftBodyWorldInfo.cxx" +#include "bulletTriangleMesh.cxx" +#include "bulletTriangleMeshShape.cxx" +#include "bulletVehicle.cxx" +#include "bulletWheel.cxx" +#include "bulletWorld.cxx" diff --git a/panda/src/bullet/bullet_includes.h b/panda/src/bullet/bullet_includes.h new file mode 100644 index 0000000000..5ea9af67f1 --- /dev/null +++ b/panda/src/bullet/bullet_includes.h @@ -0,0 +1,38 @@ +// Filename: bullet_includes.h +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_INCLUDES_H__ +#define __BULLET_INCLUDES_H__ + +#include "pandabase.h" + +#include "btBulletDynamicsCommon.h" + +#ifndef CPPPARSER +#include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h" +#include "BulletCollision/CollisionDispatch/btGhostObject.h" +#include "BulletCollision/CollisionDispatch/btManifoldResult.h" +#include "BulletCollision/CollisionShapes/btConvexPointCloudShape.h" +#include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h" +#include "BulletCollision/Gimpact/btGImpactCollisionAlgorithm.h" +#include "BulletCollision/Gimpact/btGImpactShape.h" +#include "BulletDynamics/Character/btKinematicCharacterController.h" +#include "BulletDynamics/Vehicle/btRaycastVehicle.h" +#include "BulletSoftBody/btSoftBodyHelpers.h" +#include "BulletSoftBody/btSoftBodyInternals.h" +#include "BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h" +#include "BulletSoftBody/btSoftRigidDynamicsWorld.h" +#endif + +#endif // __BULLET_INCLUDES_H__ diff --git a/panda/src/bullet/bullet_utils.I b/panda/src/bullet/bullet_utils.I new file mode 100644 index 0000000000..9ae3206782 --- /dev/null +++ b/panda/src/bullet/bullet_utils.I @@ -0,0 +1,15 @@ +// Filename: bullet_utils.I +// Created by: enn0x (25Nov10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + + diff --git a/panda/src/bullet/bullet_utils.cxx b/panda/src/bullet/bullet_utils.cxx new file mode 100644 index 0000000000..f043b6a195 --- /dev/null +++ b/panda/src/bullet/bullet_utils.cxx @@ -0,0 +1,182 @@ +// Filename: bullet_utils.h +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "bullet_utils.h" + +#include "transformState.h" + +//////////////////////////////////////////////////////////////////// +// Function: LVecBase3f_to_btVector3 +// Description: +//////////////////////////////////////////////////////////////////// +btVector3 LVecBase3f_to_btVector3(const LVecBase3f &v) { + + return btVector3(v.get_x(), v.get_y(), v.get_z()); +} + +//////////////////////////////////////////////////////////////////// +// Function: btVector3_to_LVecBase3f +// Description: +//////////////////////////////////////////////////////////////////// +LVecBase3f btVector3_to_LVecBase3f(const btVector3 &v) { + + return LVecBase3f(v.getX(), v.getY(), v.getZ()); +} + +//////////////////////////////////////////////////////////////////// +// Function: btVector3_to_LVector3f +// Description: +//////////////////////////////////////////////////////////////////// +LVector3f btVector3_to_LVector3f(const btVector3 &v) { + + return LVector3f(v.getX(), v.getY(), v.getZ()); +} + +//////////////////////////////////////////////////////////////////// +// Function: btVector3_to_LPoint3f +// Description: +//////////////////////////////////////////////////////////////////// +LPoint3f btVector3_to_LPoint3f(const btVector3 &p) { + + return LPoint3f(p.getX(), p.getY(), p.getZ()); +} + +//////////////////////////////////////////////////////////////////// +// Function: LMatrix3f_to_btMatrix3x3 +// Description: +//////////////////////////////////////////////////////////////////// +btMatrix3x3 LMatrix3f_to_btMatrix3x3(const LMatrix3f &m) { + + btMatrix3x3 result; + result.setFromOpenGLSubMatrix(m.get_data()); + return result; +} + +//////////////////////////////////////////////////////////////////// +// Function: btMatrix3x3_to_LMatrix3f +// Description: +//////////////////////////////////////////////////////////////////// +LMatrix3f btMatrix3x3_to_LMatrix3f(const btMatrix3x3 &m) { + + float cells[9]; + m.getOpenGLSubMatrix(cells); + return LMatrix3f(cells[0], cells[1], cells[2], + cells[3], cells[4], cells[5], + cells[6], cells[7], cells[8]); +} + +//////////////////////////////////////////////////////////////////// +// Function: LQuaternionf_to_btQuat +// Description: +//////////////////////////////////////////////////////////////////// +btQuaternion LQuaternionf_to_btQuat(const LQuaternionf &q) { + + return btQuaternion(q.get_i(), q.get_j(), q.get_k(), q.get_r()); +} + +//////////////////////////////////////////////////////////////////// +// Function: btQuat_to_LQuaternionf +// Description: +//////////////////////////////////////////////////////////////////// +LQuaternionf btQuat_to_LQuaternionf(const btQuaternion &q) { + + return LQuaternionf(q.getW(), q.getX(), q.getY(), q.getZ()); +} + +//////////////////////////////////////////////////////////////////// +// Function: LMatrix4f_to_btTrans +// Description: +//////////////////////////////////////////////////////////////////// +btTransform LMatrix4f_to_btTrans(const LMatrix4f &m) { + + LQuaternionf quat; + quat.set_from_matrix(m.get_upper_3()); + + btQuaternion btq = LQuaternionf_to_btQuat(quat); + btVector3 btv = LVecBase3f_to_btVector3(m.get_row3(3)); + + return btTransform(btq, btv); +} + +//////////////////////////////////////////////////////////////////// +// Function: btTrans_to_LMatrix4f +// Description: +//////////////////////////////////////////////////////////////////// +LMatrix4f btTrans_to_LMatrix4f(const btTransform &trans) { + + return TransformState::make_pos_quat_scale( + btVector3_to_LVector3f(trans.getOrigin()), + btQuat_to_LQuaternionf(trans.getRotation()), + LVector3f(1.0f, 1.0f, 1.0f))->get_mat(); +} + +//////////////////////////////////////////////////////////////////// +// Function: btTrans_to_TransformState +// Description: +//////////////////////////////////////////////////////////////////// +CPT(TransformState) btTrans_to_TransformState(const btTransform &trans, const LVecBase3f &scale) { + + LVecBase3f pos = btVector3_to_LVector3f(trans.getOrigin()); + LQuaternionf quat = btQuat_to_LQuaternionf(trans.getRotation()); + + return TransformState::make_pos_quat_scale(pos, quat, scale); +} + +//////////////////////////////////////////////////////////////////// +// Function: get_default_up_axis +// Description: +//////////////////////////////////////////////////////////////////// +BulletUpAxis get_default_up_axis() { + + switch (get_default_coordinate_system()) { + + case CS_yup_right: + case CS_yup_left: + return Y_up; + + case CS_zup_right: + case CS_zup_left: + return Z_up; + + default: + return Z_up; + } +} + +//////////////////////////////////////////////////////////////////// +// Function: get_node_transform +// Description: +//////////////////////////////////////////////////////////////////// +void get_node_transform(btTransform &trans, PandaNode *node) { + + LMatrix4f m; + + if (node->get_num_parents() == 0) { + m = node->get_transform()->get_mat(); + } + else { + NodePath np = NodePath::any_path(node); + m = np.get_net_transform()->get_mat(); + } + + LQuaternionf quat; + quat.set_from_matrix(m.get_upper_3()); + + btQuaternion btq = LQuaternionf_to_btQuat(quat); + btVector3 btv = LVecBase3f_to_btVector3(m.get_row3(3)); + + trans.setRotation(btq); + trans.setOrigin(btv); +} + diff --git a/panda/src/bullet/bullet_utils.h b/panda/src/bullet/bullet_utils.h new file mode 100644 index 0000000000..fc6d3e2cd4 --- /dev/null +++ b/panda/src/bullet/bullet_utils.h @@ -0,0 +1,61 @@ +// Filename: bullet_utils.h +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __BULLET_UTILS_H__ +#define __BULLET_UTILS_H__ + +#include "pandabase.h" + +#include "bullet_includes.h" + +#include "lpoint3.h" +#include "lvector3.h" +#include "lmatrix.h" +#include "lquaternion.h" +#include "pandaNode.h" +#include "nodePath.h" + +// Conversion from Panda3D to Bullet +EXPCL_PANDABULLET btVector3 LVecBase3f_to_btVector3(const LVecBase3f &v); +EXPCL_PANDABULLET btMatrix3x3 LMatrix3f_to_btMatrix3x3(const LMatrix3f &m); +EXPCL_PANDABULLET btTransform LMatrix4f_to_btTrans(const LMatrix4f &m); +EXPCL_PANDABULLET btQuaternion LQuaternionf_to_btQuat(const LQuaternionf &q); + +// Conversion from Bullet to Panda3D +EXPCL_PANDABULLET LVecBase3f btVector3_to_LVecBase3f(const btVector3 &v); +EXPCL_PANDABULLET LVector3f btVector3_to_LVector3f(const btVector3 &v); +EXPCL_PANDABULLET LPoint3f btVector3_to_LPoint3f(const btVector3 &p); +EXPCL_PANDABULLET LMatrix3f btMatrix3x3_to_LMatrix3f(const btMatrix3x3 &m); +EXPCL_PANDABULLET LMatrix4f btTrans_to_LMatrix4f(const btTransform &tf); +EXPCL_PANDABULLET LQuaternionf btQuat_to_LQuaternionf(const btQuaternion &q); + +EXPCL_PANDABULLET CPT(TransformState) btTrans_to_TransformState( + const btTransform &tf, + const LVecBase3f &scale=LVecBase3f(1.0f, 1.0f, 1.0f)); + +// UpAxis +BEGIN_PUBLISH + +enum BulletUpAxis { + X_up = 0, + Y_up = 1, + Z_up = 2, +}; + +EXPCL_PANDABULLET BulletUpAxis get_default_up_axis(); +END_PUBLISH + +#include "bullet_utils.I" + +#endif // __BULLET_UTILS_H__ diff --git a/panda/src/bullet/config_bullet.cxx b/panda/src/bullet/config_bullet.cxx new file mode 100644 index 0000000000..41a633c062 --- /dev/null +++ b/panda/src/bullet/config_bullet.cxx @@ -0,0 +1,185 @@ +// Filename: config_bullet.cxx +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#include "config_bullet.h" + +#include "bulletBodyNode.h" +#include "bulletBoxShape.h" +#include "bulletCapsuleShape.h" +#include "bulletCharacterControllerNode.h" +#include "bulletConeShape.h" +#include "bulletConeTwistConstraint.h" +#include "bulletConstraint.h" +#include "bulletConvexHullShape.h" +#include "bulletConvexPointCloudShape.h" +#include "bulletCylinderShape.h" +#include "bulletDebugNode.h" +#include "bulletDistanceConstraint.h" +#include "bulletGenericConstraint.h" +#include "bulletGhostNode.h" +#include "bulletHeightfieldShape.h" +#include "bulletHingeConstraint.h" +#include "bulletPlaneShape.h" +#include "bulletRigidBodyNode.h" +#include "bulletShape.h" +#include "bulletSliderConstraint.h" +#include "bulletSphereShape.h" +#include "bulletSoftBodyNode.h" +#include "bulletSoftBodyShape.h" +#include "bulletTriangleMesh.h" +#include "bulletTriangleMeshShape.h" +#include "bulletVehicle.h" +#include "bulletWorld.h" + +#include "bulletContactCallbacks.h" + +extern ContactAddedCallback gContactAddedCallback; +extern ContactProcessedCallback gContactProcessedCallback; +extern ContactDestroyedCallback gContactDestroyedCallback; + +#include "dconfig.h" +#include "pandaSystem.h" + +Configure(config_bullet); +NotifyCategoryDef(bullet, ""); + +ConfigureFn(config_bullet) { + init_libbullet(); +} + +ConfigVariableInt bullet_max_objects +("bullet-max-objects", 1024, +PRC_DESC("Specifies the maximum number of individual objects within a " + "bullet physics world. Default value is 1024.")); + +ConfigVariableInt bullet_gc_lifetime +("bullet-gc-lifetime", 256, +PRC_DESC("Specifies the lifetime of data clean up be the soft body world " + "info garbage collector. Default value is 256.")); + +ConfigVariableEnum bullet_broadphase_algorithm +("bullet-broadphase-algorithm", BulletWorld::BA_dynamic_aabb_tree, +PRC_DESC("Specifies the broadphase algorithm to be used by the physics " + "engine. Default value is 'aabb' (dynamic aabb tree).")); + +ConfigVariableDouble bullet_sap_extents +("bullet-sap-extents", 1000.0, +PRC_DESC("Specifies the world extent in all directions. The config variable " + "is only used if bullet-broadphase-algorithm is set to 'sap' " + "(sweep and prune). Default value is 1000.0.")); + +ConfigVariableBool bullet_enable_contact_events +("bullet-enable-contact-events", false, +PRC_DESC("Specifies if events should be send when new contacts are " + "created or existing contacts get remove. Warning: enabling " + "contact events might create more load on the event queue " + "then you might want! Default value is FALSE.")); + +ConfigVariableInt bullet_solver_iterations +("bullet-solver-iterations", 10, +PRC_DESC("Specifies the number of iterations for the Bullet contact " + "solver. This is the native Bullet property " + "btContactSolverInfo::m_numIterations. Default value is 10.")); + +ConfigVariableBool bullet_additional_damping +("bullet-additional-damping", false, +PRC_DESC("Enables additional damping on eachrigid body, in order to reduce " + "jitter. Default value is FALSE. Additional damping is an " + "experimental feature of the Bullet physics engine. Use with " + "care.")); + +ConfigVariableDouble bullet_additional_damping_linear_factor +("bullet-additional-damping-linear-factor", 0.005, +PRC_DESC("Only used when bullet-additional-damping is set to TRUE. " + "Default value is 0.005")); + +ConfigVariableDouble bullet_additional_damping_angular_factor +("bullet-additional-damping-angular-factor", 0.01, +PRC_DESC("Only used when bullet-additional-damping is set to TRUE. " + "Default value is 0.01")); + +ConfigVariableDouble bullet_additional_damping_linear_threshold +("bullet-additional-damping-linear-threshold", 0.01, +PRC_DESC("Only used when bullet-additional-damping is set to TRUE. " + "Default value is 0.01")); + +ConfigVariableDouble bullet_additional_damping_angular_threshold +("bullet-additional-damping-angular-threshold", 0.01, +PRC_DESC("Only used when bullet-additional-damping is set to TRUE. " + "Default value is 0.01.")); + +//////////////////////////////////////////////////////////////////// +// Function: init_libbullet +// Description: Initializes the library. This must be called at +// least once before any of the functions or classes in +// this library can be used. Normally it will be +// called by the static initializers and need not be +// called explicitly, but special cases exist. +//////////////////////////////////////////////////////////////////// +void +init_libbullet() { + + static bool initialized = false; + if (initialized) { + return; + } + initialized = true; + + // Initialize types + // + BulletBodyNode::init_type(); + BulletBoxShape::init_type(); + BulletCapsuleShape::init_type(); + BulletCharacterControllerNode::init_type(); + BulletConeShape::init_type(); + BulletConeTwistConstraint::init_type(); + BulletConstraint::init_type(); + BulletConvexHullShape::init_type(); + BulletConvexPointCloudShape::init_type(); + BulletCylinderShape::init_type(); + BulletDebugNode::init_type(); + BulletDistanceConstraint::init_type(); + BulletGenericConstraint::init_type(); + BulletGhostNode::init_type(); + BulletHeightfieldShape::init_type(); + BulletHingeConstraint::init_type(); + BulletPlaneShape::init_type(); + BulletRigidBodyNode::init_type(); + BulletShape::init_type(); + BulletSliderConstraint::init_type(); + BulletSphereShape::init_type(); + BulletSoftBodyNode::init_type(); + BulletSoftBodyShape::init_type(); + BulletTriangleMesh::init_type(); + BulletTriangleMeshShape::init_type(); + BulletVehicle::init_type(); + BulletWorld::init_type(); + + // Custom contact callbacks + // + gContactAddedCallback = contact_added_callback; + gContactProcessedCallback = contact_processed_callback; + gContactDestroyedCallback = contact_destroyed_callback; + + // Initialize notification category + // + bullet_cat.init(); + bullet_cat.debug() << "initialize module" << endl; + + // Register the Bullet system + // + PandaSystem *ps = PandaSystem::get_global_ptr(); + ps->add_system("Bullet"); +} + diff --git a/panda/src/bullet/config_bullet.h b/panda/src/bullet/config_bullet.h new file mode 100644 index 0000000000..a99640a1e4 --- /dev/null +++ b/panda/src/bullet/config_bullet.h @@ -0,0 +1,45 @@ +// Filename: config_bullet.h +// Created by: enn0x (23Jan10) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) Carnegie Mellon University. All rights reserved. +// +// All use of this software is subject to the terms of the revised BSD +// license. You should have received a copy of this license along +// with this source code in a file named "LICENSE." +// +//////////////////////////////////////////////////////////////////// + +#ifndef __CONFIG_BULLET_H__ +#define __CONFIG_BULLET_H__ + +#include "pandabase.h" +#include "notifyCategoryProxy.h" + +#include "configVariableSearchPath.h" +#include "configVariableBool.h" +#include "configVariableEnum.h" +#include "configVariableDouble.h" +#include "configVariableInt.h" + +#include "bulletWorld.h" + +NotifyCategoryDecl(bullet, EXPCL_PANDABULLET, EXPTP_PANDABULLET); + +extern ConfigVariableInt bullet_max_objects; +extern ConfigVariableInt bullet_gc_lifetime; +extern ConfigVariableEnum bullet_broadphase_algorithm; +extern ConfigVariableDouble bullet_sap_extents; +extern ConfigVariableBool bullet_enable_contact_events; +extern ConfigVariableInt bullet_solver_iterations; +extern ConfigVariableBool bullet_additional_damping; +extern ConfigVariableDouble bullet_additional_damping_linear_factor; +extern ConfigVariableDouble bullet_additional_damping_angular_factor; +extern ConfigVariableDouble bullet_additional_damping_linear_threshold; +extern ConfigVariableDouble bullet_additional_damping_angular_threshold; + +extern EXPCL_PANDABULLET void init_libbullet(); + +#endif // __CONFIG_BULLET_H__ diff --git a/panda/src/pandabase/pandasymbols.h b/panda/src/pandabase/pandasymbols.h index 9022f2f313..7472e3df83 100644 --- a/panda/src/pandabase/pandasymbols.h +++ b/panda/src/pandabase/pandasymbols.h @@ -96,6 +96,14 @@ #define EXPTP_PANDAAWESOMIUM extern #endif +#ifdef BUILDING_PANDABULLET + #define EXPCL_PANDABULLET __declspec(dllexport) + #define EXPTP_PANDABULLET +#else + #define EXPCL_PANDABULLET __declspec(dllimport) + #define EXPTP_PANDABULLET extern +#endif + #ifdef BUILDING_PANDACR #define EXPCL_PANDACR __declspec(dllexport) #define EXPTP_PANDACR @@ -277,6 +285,9 @@ #define EXPCL_PANDAAWESOMIUM #define EXPTP_PANDAAWESOMIUM +#define EXPCL_PANDABULLET +#define EXPTP_PANDABULLET + #define EXPCL_PANDACR #define EXPTP_PANDACR @@ -307,7 +318,6 @@ #define EXPCL_PANDAODE #define EXPTP_PANDAODE - #define EXPCL_PANDAPHYSICS #define EXPTP_PANDAPHYSICS