Panda3D
 All Classes Functions Variables Enumerations
bulletWorld.h
1 // Filename: bulletWorld.h
2 // Created by: enn0x (23Jan10)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef __BULLET_WORLD_H__
16 #define __BULLET_WORLD_H__
17 
18 #include "pandabase.h"
19 
20 #include "bullet_includes.h"
21 #include "bullet_utils.h"
22 
23 #include "bulletClosestHitRayResult.h"
24 #include "bulletAllHitsRayResult.h"
25 #include "bulletClosestHitSweepResult.h"
26 #include "bulletContactResult.h"
27 #include "bulletDebugNode.h"
28 #include "bulletBaseCharacterControllerNode.h"
29 #include "bulletConstraint.h"
30 #include "bulletGhostNode.h"
31 #include "bulletRigidBodyNode.h"
32 #include "bulletSoftBodyNode.h"
33 #include "bulletVehicle.h"
34 
35 #include "typedReferenceCount.h"
36 #include "transformState.h"
37 #include "pandaNode.h"
38 #include "callbackObject.h"
39 #include "collideMask.h"
40 #include "luse.h"
41 
43 class BulletShape;
45 
46 extern PT(CallbackObject) bullet_contact_added_callback;
47 
48 ////////////////////////////////////////////////////////////////////
49 // Class : BulletWorld
50 // Description :
51 ////////////////////////////////////////////////////////////////////
52 class EXPCL_PANDABULLET BulletWorld : public TypedReferenceCount {
53 
54 PUBLISHED:
55  BulletWorld();
56  INLINE ~BulletWorld();
57 
58  void set_gravity(const LVector3 &gravity);
59  void set_gravity(PN_stdfloat gx, PN_stdfloat gy, PN_stdfloat gz);
60  const LVector3 get_gravity() const;
61 
62  int do_physics(PN_stdfloat dt, int max_substeps=1, PN_stdfloat stepsize=1.0f/60.0f);
63 
64  BulletSoftBodyWorldInfo get_world_info();
65 
66  // Debug
67  INLINE void set_debug_node(BulletDebugNode *node);
68  INLINE void clear_debug_node();
69  INLINE BulletDebugNode *get_debug_node() const;
70 
71  // Attach/Remove
72  void attach(TypedObject *object);
73  void attach_constraint(BulletConstraint *constraint, bool linked_collision=false);
74 
75  void remove(TypedObject *object);
76 
77  // Ghost object
78  INLINE int get_num_ghosts() const;
79  INLINE BulletGhostNode *get_ghost(int idx) const;
80  MAKE_SEQ(get_ghosts, get_num_ghosts, get_ghost);
81 
82  // Rigid body
83  INLINE int get_num_rigid_bodies() const;
84  INLINE BulletRigidBodyNode *get_rigid_body(int idx) const;
85  MAKE_SEQ(get_rigid_bodies, get_num_rigid_bodies, get_rigid_body);
86 
87  // Soft body
88  INLINE int get_num_soft_bodies() const;
89  INLINE BulletSoftBodyNode *get_soft_body(int idx) const;
90  MAKE_SEQ(get_soft_bodies, get_num_soft_bodies, get_soft_body);
91 
92  // Character controller
93  INLINE int get_num_characters() const;
94  INLINE BulletBaseCharacterControllerNode *get_character(int idx) const;
95  MAKE_SEQ(get_characters, get_num_characters, get_character);
96 
97  // Vehicle
98  void attach_vehicle(BulletVehicle *vehicle);
99  void remove_vehicle(BulletVehicle *vehicle);
100 
101  INLINE int get_num_vehicles() const;
102  INLINE BulletVehicle *get_vehicle(int idx) const;
103  MAKE_SEQ(get_vehicles, get_num_vehicles, get_vehicle);
104 
105  // Constraint
106  INLINE int get_num_constraints() const;
107  INLINE BulletConstraint *get_constraint(int idx) const;
108  MAKE_SEQ(get_constraints, get_num_constraints, get_constraint);
109 
110  // Raycast and other queries
111  BulletClosestHitRayResult ray_test_closest(
112  const LPoint3 &from_pos,
113  const LPoint3 &to_pos,
114  const CollideMask &mask=CollideMask::all_on()) const;
115 
116  BulletAllHitsRayResult ray_test_all(
117  const LPoint3 &from_pos,
118  const LPoint3 &to_pos,
119  const CollideMask &mask=CollideMask::all_on()) const;
120 
121  BulletClosestHitSweepResult sweep_test_closest(
122  BulletShape *shape,
123  const TransformState &from_ts,
124  const TransformState &to_ts,
125  const CollideMask &mask=CollideMask::all_on(),
126  PN_stdfloat penetration=0.0f) const;
127 
128  BulletContactResult contact_test(PandaNode *node, bool use_filter=false) const;
129  BulletContactResult contact_test_pair(PandaNode *node0, PandaNode *node1) const;
130 
131  bool filter_test(PandaNode *node0, PandaNode *node1) const;
132 
133  // Manifolds
134  INLINE int get_num_manifolds() const;
135  BulletPersistentManifold *get_manifold(int idx) const;
136  MAKE_SEQ(get_manifolds, get_num_manifolds, get_manifold);
137 
138  // Collision filtering
139  void set_group_collision_flag(unsigned int group1, unsigned int group2, bool enable);
140  bool get_group_collision_flag(unsigned int group1, unsigned int group2) const;
141 
142  // Callbacks
143  void set_contact_added_callback(CallbackObject *obj);
144  void clear_contact_added_callback();
145 
146  void set_tick_callback(CallbackObject *obj, bool is_pretick=false);
147  void clear_tick_callback();
148 
149  void set_filter_callback(CallbackObject *obj);
150  void clear_filter_callback();
151 
152  // Configuration
153  enum BroadphaseAlgorithm {
154  BA_sweep_and_prune,
155  BA_dynamic_aabb_tree,
156  };
157 
158  enum FilterAlgorithm {
159  FA_mask,
160  FA_groups_mask,
161  FA_callback,
162  };
163 
164 PUBLISHED: // Deprecated methods, will become private soon
165  void attach_ghost(BulletGhostNode *node);
166  void remove_ghost(BulletGhostNode *node);
167 
168  void attach_rigid_body(BulletRigidBodyNode *node);
169  void remove_rigid_body(BulletRigidBodyNode *node);
170 
171  void attach_soft_body(BulletSoftBodyNode *node);
172  void remove_soft_body(BulletSoftBodyNode *node);
173 
174  void attach_character(BulletBaseCharacterControllerNode *node);
175  void remove_character(BulletBaseCharacterControllerNode *node);
176 
177  void remove_constraint(BulletConstraint *constraint);
178 
179 public:
180  static btCollisionObject *get_collision_object(PandaNode *node);
181 
182  INLINE btDynamicsWorld *get_world() const;
183  INLINE btBroadphaseInterface *get_broadphase() const;
184  INLINE btDispatcher *get_dispatcher() const;
185 
186 private:
187  void sync_p2b(PN_stdfloat dt, int num_substeps);
188  void sync_b2p();
189 
190  static void tick_callback(btDynamicsWorld *world, btScalar timestep);
191 
192  typedef PTA(PT(BulletRigidBodyNode)) BulletRigidBodies;
193  typedef PTA(PT(BulletSoftBodyNode)) BulletSoftBodies;
194  typedef PTA(PT(BulletGhostNode)) BulletGhosts;
195  typedef PTA(PT(BulletBaseCharacterControllerNode)) BulletCharacterControllers;
196  typedef PTA(PT(BulletVehicle)) BulletVehicles;
197  typedef PTA(PT(BulletConstraint)) BulletConstraints;
198 
199  static PStatCollector _pstat_physics;
200  static PStatCollector _pstat_simulation;
201  static PStatCollector _pstat_debug;
202  static PStatCollector _pstat_p2b;
203  static PStatCollector _pstat_b2p;
204 
205  struct btFilterCallback1 : public btOverlapFilterCallback {
206  virtual bool needBroadphaseCollision(
207  btBroadphaseProxy* proxy0,
208  btBroadphaseProxy* proxy1) const;
209  };
210 
211  struct btFilterCallback2 : public btOverlapFilterCallback {
212  virtual bool needBroadphaseCollision(
213  btBroadphaseProxy* proxy0,
214  btBroadphaseProxy* proxy1) const;
215 
216  CollideMask _collide[32];
217  };
218 
219  struct btFilterCallback3 : public btOverlapFilterCallback {
220  virtual bool needBroadphaseCollision(
221  btBroadphaseProxy* proxy0,
222  btBroadphaseProxy* proxy1) const;
223 
224  PT(CallbackObject) _filter_callback_obj;
225  };
226 
227  btBroadphaseInterface *_broadphase;
228  btCollisionConfiguration *_configuration;
229  btCollisionDispatcher *_dispatcher;
230  btConstraintSolver *_solver;
231  btSoftRigidDynamicsWorld *_world;
232 
233  btGhostPairCallback _ghost_cb;
234 
235  btFilterCallback1 _filter_cb1;
236  btFilterCallback2 _filter_cb2;
237  btFilterCallback3 _filter_cb3;
238  btOverlapFilterCallback *_filter_cb;
239 
240  PT(CallbackObject) _tick_callback_obj;
241 
242  PT(BulletDebugNode) _debug;
243 
244  btSoftBodyWorldInfo _info;
245 
246  BulletRigidBodies _bodies;
247  BulletSoftBodies _softbodies;
248  BulletGhosts _ghosts;
249  BulletCharacterControllers _characters;
250  BulletVehicles _vehicles;
251  BulletConstraints _constraints;
252 
253 ////////////////////////////////////////////////////////////////////
254 public:
255  static TypeHandle get_class_type() {
256  return _type_handle;
257  }
258  static void init_type() {
259  TypedReferenceCount::init_type();
260  register_type(_type_handle, "BulletWorld",
261  TypedReferenceCount::get_class_type());
262  }
263  virtual TypeHandle get_type() const {
264  return get_class_type();
265  }
266  virtual TypeHandle force_init_type() {
267  init_type();
268  return get_class_type();
269  }
270 
271 private:
272  static TypeHandle _type_handle;
273 };
274 
275 EXPCL_PANDABULLET ostream &
276 operator << (ostream &out, BulletWorld::BroadphaseAlgorithm algorithm);
277 EXPCL_PANDABULLET istream &
278 operator >> (istream &in, BulletWorld::BroadphaseAlgorithm &algorithm);
279 
280 EXPCL_PANDABULLET ostream &
281 operator << (ostream &out, BulletWorld::FilterAlgorithm algorithm);
282 EXPCL_PANDABULLET istream &
283 operator >> (istream &in, BulletWorld::FilterAlgorithm &algorithm);
284 
285 #include "bulletWorld.I"
286 
287 #endif // __BULLET_WORLD_H__
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
static BitMask< WType, nbits > all_on()
Returns a BitMask whose bits are all on.
Definition: bitMask.I:73
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
This is a three-component vector distance (as opposed to a three-component point, which represents a ...
Definition: lvector3.h:100
This is a three-component point in space (as opposed to a three-component vector, which represents a ...
Definition: lpoint3.h:99
Simulates a raycast vehicle which casts a ray per wheel at the ground as a cheap replacement for comp...
Definition: bulletVehicle.h:65
A lightweight class that represents a single element that may be timed and/or counted via stats...
This is a generic object that can be assigned to a callback at various points in the rendering proces...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85