Panda3D
 All Classes Functions Variables Enumerations
bulletWorld.h
00001 // Filename: bulletWorld.h
00002 // Created by:  enn0x (23Jan10)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef __BULLET_WORLD_H__
00016 #define __BULLET_WORLD_H__
00017 
00018 #include "pandabase.h"
00019 
00020 #include "bullet_includes.h"
00021 #include "bullet_utils.h"
00022 
00023 #include "bulletClosestHitRayResult.h"
00024 #include "bulletAllHitsRayResult.h"
00025 #include "bulletClosestHitSweepResult.h"
00026 #include "bulletContactResult.h"
00027 #include "bulletDebugNode.h"
00028 #include "bulletBaseCharacterControllerNode.h"
00029 #include "bulletConstraint.h"
00030 #include "bulletGhostNode.h"
00031 #include "bulletRigidBodyNode.h"
00032 #include "bulletSoftBodyNode.h"
00033 #include "bulletVehicle.h"
00034 
00035 #include "typedReferenceCount.h"
00036 #include "transformState.h"
00037 #include "pandaNode.h"
00038 #include "collideMask.h"
00039 #include "luse.h"
00040 
00041 class BulletPersistentManifold;
00042 class BulletShape;
00043 class BulletSoftBodyWorldInfo;
00044 
00045 ////////////////////////////////////////////////////////////////////
00046 //       Class : BulletWorld
00047 // Description : 
00048 ////////////////////////////////////////////////////////////////////
00049 class EXPCL_PANDABULLET BulletWorld : public TypedReferenceCount {
00050 
00051 PUBLISHED:
00052   BulletWorld();
00053   INLINE ~BulletWorld();
00054 
00055   void set_gravity(const LVector3 &gravity);
00056   void set_gravity(PN_stdfloat gx, PN_stdfloat gy, PN_stdfloat gz);
00057   const LVector3 get_gravity() const;
00058 
00059   int do_physics(PN_stdfloat dt, int max_substeps=1, PN_stdfloat stepsize=1.0f/60.0f);
00060 
00061   void set_debug_node(BulletDebugNode *node);
00062   void clear_debug_node();
00063 
00064   BulletSoftBodyWorldInfo get_world_info();
00065 
00066   // Ghost object
00067   void attach_ghost(BulletGhostNode *node);
00068   void remove_ghost(BulletGhostNode *node);
00069 
00070   INLINE int get_num_ghosts() const;
00071   INLINE BulletGhostNode *get_ghost(int idx) const;
00072   MAKE_SEQ(get_ghosts, get_num_ghosts, get_ghost);
00073 
00074   // Rigid body
00075   void attach_rigid_body(BulletRigidBodyNode *node);
00076   void remove_rigid_body(BulletRigidBodyNode *node);
00077 
00078   INLINE int get_num_rigid_bodies() const;
00079   INLINE BulletRigidBodyNode *get_rigid_body(int idx) const;
00080   MAKE_SEQ(get_rigid_bodies, get_num_rigid_bodies, get_rigid_body);
00081 
00082   // Soft body
00083   void attach_soft_body(BulletSoftBodyNode *node);
00084   void remove_soft_body(BulletSoftBodyNode *node);
00085 
00086   INLINE int get_num_soft_bodies() const;
00087   INLINE BulletSoftBodyNode *get_soft_body(int idx) const;
00088   MAKE_SEQ(get_soft_bodies, get_num_soft_bodies, get_soft_body);
00089 
00090   // Character controller
00091   void attach_character(BulletBaseCharacterControllerNode *node);
00092   void remove_character(BulletBaseCharacterControllerNode *node);
00093 
00094   INLINE int get_num_characters() const;
00095   INLINE BulletBaseCharacterControllerNode *get_character(int idx) const;
00096   MAKE_SEQ(get_characters, get_num_characters, get_character);
00097 
00098   // Vehicle
00099   void attach_vehicle(BulletVehicle *vehicle);
00100   void remove_vehicle(BulletVehicle *vehicle);
00101 
00102   INLINE int get_num_vehicles() const;
00103   INLINE BulletVehicle *get_vehicle(int idx) const;
00104   MAKE_SEQ(get_vehicles, get_num_vehicles, get_vehicle);
00105 
00106   // Constraint
00107   void attach_constraint(BulletConstraint *constraint);
00108   void remove_constraint(BulletConstraint *constraint);
00109 
00110   INLINE int get_num_constraints() const;
00111   INLINE BulletConstraint *get_constraint(int idx) const;
00112   MAKE_SEQ(get_constraints, get_num_constraints, get_constraint);
00113 
00114   // Raycast and other queries
00115   BulletClosestHitRayResult ray_test_closest(
00116     const LPoint3 &from_pos,
00117     const LPoint3 &to_pos,
00118     const CollideMask &mask=CollideMask::all_on()) const;
00119 
00120   BulletAllHitsRayResult ray_test_all(
00121     const LPoint3 &from_pos,
00122     const LPoint3 &to_pos,
00123     const CollideMask &mask=CollideMask::all_on()) const;
00124 
00125   BulletClosestHitSweepResult sweep_test_closest(
00126     BulletShape *shape,
00127     const TransformState &from_ts,
00128     const TransformState &to_ts,
00129     const CollideMask &mask=CollideMask::all_on(),
00130     PN_stdfloat penetration=0.0f) const;
00131 
00132   BulletContactResult contact_test(PandaNode *node) const;
00133   BulletContactResult contact_test_pair(PandaNode *node0, PandaNode *node1) const;
00134 
00135   // Manifolds
00136   INLINE int get_num_manifolds() const;
00137   BulletPersistentManifold *get_manifold(int idx) const;
00138   MAKE_SEQ(get_manifolds, get_num_manifolds, get_manifold);
00139 
00140   // Collision filtering
00141   void set_group_collision_flag(unsigned int group1, unsigned int group2, bool enable);
00142   bool get_group_collision_flag(unsigned int group1, unsigned int group2) const;
00143 
00144   // Configuration
00145   enum BroadphaseAlgorithm {
00146     BA_sweep_and_prune,
00147     BA_dynamic_aabb_tree,
00148   };
00149 
00150   enum FilterAlgorithm {
00151     FA_mask,
00152     FA_groups_mask,
00153   };
00154 
00155 public:
00156   static btCollisionObject *get_collision_object(PandaNode *node);
00157 
00158   INLINE btDynamicsWorld *get_world() const;
00159   INLINE btBroadphaseInterface *get_broadphase() const;
00160   INLINE btDispatcher *get_dispatcher() const;
00161 
00162 private:
00163   void sync_p2b(PN_stdfloat dt, int num_substeps);
00164   void sync_b2p();
00165 
00166   typedef PTA(PT(BulletRigidBodyNode)) BulletRigidBodies;
00167   typedef PTA(PT(BulletSoftBodyNode)) BulletSoftBodies;
00168   typedef PTA(PT(BulletGhostNode)) BulletGhosts;
00169   typedef PTA(PT(BulletBaseCharacterControllerNode)) BulletCharacterControllers;
00170   typedef PTA(PT(BulletVehicle)) BulletVehicles;
00171   typedef PTA(PT(BulletConstraint)) BulletConstraints;
00172 
00173   static PStatCollector _pstat_physics;
00174   static PStatCollector _pstat_simulation;
00175   static PStatCollector _pstat_debug;
00176   static PStatCollector _pstat_p2b;
00177   static PStatCollector _pstat_b2p;
00178 
00179   struct btFilterCallback1 : public btOverlapFilterCallback {
00180     virtual bool needBroadphaseCollision(
00181       btBroadphaseProxy* proxy0,
00182       btBroadphaseProxy* proxy1) const;
00183   };
00184 
00185   struct btFilterCallback2 : public btOverlapFilterCallback {
00186     virtual bool needBroadphaseCollision(
00187       btBroadphaseProxy* proxy0,
00188       btBroadphaseProxy* proxy1) const;
00189 
00190     CollideMask _collide[32];
00191   };
00192 
00193   btBroadphaseInterface *_broadphase;
00194   btCollisionConfiguration *_configuration;
00195   btCollisionDispatcher *_dispatcher;
00196   btConstraintSolver *_solver;
00197   btSoftRigidDynamicsWorld *_world;
00198 
00199   btGhostPairCallback _ghost_cb;
00200 
00201   btFilterCallback1 _filter_cb1;
00202   btFilterCallback2 _filter_cb2;
00203 
00204   btSoftBodyWorldInfo _info;
00205 
00206   PT(BulletDebugNode) _debug;
00207 
00208   BulletRigidBodies _bodies;
00209   BulletSoftBodies _softbodies;
00210   BulletGhosts _ghosts;
00211   BulletCharacterControllers _characters;
00212   BulletVehicles _vehicles;
00213   BulletConstraints _constraints;
00214 
00215 ////////////////////////////////////////////////////////////////////
00216 public:
00217   static TypeHandle get_class_type() {
00218     return _type_handle;
00219   }
00220   static void init_type() {
00221     TypedReferenceCount::init_type();
00222     register_type(_type_handle, "BulletWorld", 
00223                   TypedReferenceCount::get_class_type());
00224   }
00225   virtual TypeHandle get_type() const {
00226     return get_class_type();
00227   }
00228   virtual TypeHandle force_init_type() {
00229     init_type();
00230     return get_class_type();
00231   }
00232 
00233 private:
00234   static TypeHandle _type_handle;
00235 };
00236 
00237 EXPCL_PANDABULLET ostream &
00238 operator << (ostream &out, BulletWorld::BroadphaseAlgorithm algorithm);
00239 EXPCL_PANDABULLET istream &
00240 operator >> (istream &in, BulletWorld::BroadphaseAlgorithm &algorithm);
00241 
00242 EXPCL_PANDABULLET ostream &
00243 operator << (ostream &out, BulletWorld::FilterAlgorithm algorithm);
00244 EXPCL_PANDABULLET istream &
00245 operator >> (istream &in, BulletWorld::FilterAlgorithm &algorithm);
00246 
00247 #include "bulletWorld.I"
00248 
00249 #endif // __BULLET_WORLD_H__
 All Classes Functions Variables Enumerations