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