Panda3D
bulletRigidBodyNode.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 bulletRigidBodyNode.h
10  * @author enn0x
11  * @date 2010-11-19
12  */
13 
14 #ifndef __BULLET_RIGID_BODY_NODE_H__
15 #define __BULLET_RIGID_BODY_NODE_H__
16 
17 #include "pandabase.h"
18 
19 #include "bullet_includes.h"
20 #include "bullet_utils.h"
21 #include "bulletBodyNode.h"
22 
23 #include "pandaNode.h"
24 #include "collideMask.h"
25 
26 class BulletShape;
27 
28 /**
29  *
30  */
31 class EXPCL_PANDABULLET BulletRigidBodyNode : public BulletBodyNode {
32 
33 PUBLISHED:
34  explicit BulletRigidBodyNode(const char *name="rigid");
35  INLINE ~BulletRigidBodyNode();
36 
37  // Mass & inertia
38  void set_mass(PN_stdfloat mass);
39  PN_stdfloat get_mass() const;
40  PN_stdfloat get_inv_mass() const;
41  void set_inertia(const LVecBase3 &inertia);
42  LVector3 get_inertia() const;
43  LVector3 get_inv_inertia_diag_local() const;
44  LMatrix3 get_inv_inertia_tensor_world() const;
45 
46  // Velocity
47  LVector3 get_linear_velocity() const;
48  LVector3 get_angular_velocity() const;
49  void set_linear_velocity(const LVector3 &velocity);
50  void set_angular_velocity(const LVector3 &velocity);
51 
52  // Damping
53  PN_stdfloat get_linear_damping() const;
54  PN_stdfloat get_angular_damping() const;
55  void set_linear_damping(PN_stdfloat value);
56  void set_angular_damping(PN_stdfloat value);
57 
58  // Forces
59  void clear_forces();
60  void apply_force(const LVector3 &force, const LPoint3 &pos);
61  void apply_central_force(const LVector3 &force);
62  void apply_impulse(const LVector3 &impulse, const LPoint3 &pos);
63  void apply_central_impulse(const LVector3 &impulse);
64  void apply_torque(const LVector3 &torque);
65  void apply_torque_impulse(const LVector3 &torque);
66 
67  LVector3 get_total_force() const;
68  LVector3 get_total_torque() const;
69 
70  // Deactivation thresholds
71  PN_stdfloat get_linear_sleep_threshold() const;
72  PN_stdfloat get_angular_sleep_threshold() const;
73  void set_linear_sleep_threshold(PN_stdfloat threshold);
74  void set_angular_sleep_threshold(PN_stdfloat threshold);
75 
76  // Gravity
77  void set_gravity(const LVector3 &gravity);
78  LVector3 get_gravity() const;
79 
80  // Restrict movement
81  LVector3 get_linear_factor() const;
82  LVector3 get_angular_factor() const;
83  void set_linear_factor(const LVector3 &factor);
84  void set_angular_factor(const LVector3 &factor);
85 
86  // Special
87  bool pick_dirty_flag();
88 
89  MAKE_PROPERTY(mass, get_mass, set_mass);
90  MAKE_PROPERTY(inv_mass, get_inv_mass);
91  MAKE_PROPERTY(inertia, get_inertia, set_inertia);
92  MAKE_PROPERTY(inv_inertia_diag_local, get_inv_inertia_diag_local);
93  MAKE_PROPERTY(inv_inertia_tensor_world, get_inv_inertia_tensor_world);
94  MAKE_PROPERTY(linear_velocity, get_linear_velocity, set_linear_velocity);
95  MAKE_PROPERTY(angular_velocity, get_angular_velocity, set_angular_velocity);
96  MAKE_PROPERTY(linear_damping, get_linear_damping, set_linear_damping);
97  MAKE_PROPERTY(angular_damping, get_angular_damping, set_angular_damping);
98  MAKE_PROPERTY(total_force, get_total_force);
99  MAKE_PROPERTY(total_torque, get_total_torque);
100  MAKE_PROPERTY(linear_sleep_threshold, get_linear_sleep_threshold, set_linear_sleep_threshold);
101  MAKE_PROPERTY(angular_sleep_threshold, get_angular_sleep_threshold, set_angular_sleep_threshold);
102  MAKE_PROPERTY(gravity, get_gravity, set_gravity);
103  MAKE_PROPERTY(linear_factor, get_linear_factor, set_linear_factor);
104  MAKE_PROPERTY(angular_factor, get_angular_factor, set_angular_factor);
105 
106 public:
107  virtual btCollisionObject *get_object() const;
108 
109  virtual void output(std::ostream &out) const;
110 
111  void do_sync_p2b();
112  void do_sync_b2p();
113 
114 protected:
115  virtual void transform_changed();
116 
117 private:
118  virtual void do_shape_changed();
119  void do_transform_changed();
120 
121  void do_set_mass(PN_stdfloat mass);
122  PN_stdfloat do_get_mass() const;
123 
124  // The motion state is used for synchronisation between Bullet and the
125  // Panda3D scene graph.
126  class MotionState : public btMotionState {
127 
128  public:
129  MotionState();
130 
131  virtual void getWorldTransform(btTransform &trans) const;
132  virtual void setWorldTransform(const btTransform &trans);
133 
134  void set_net_transform(const TransformState *ts);
135 
136  void sync_b2p(PandaNode *node);
137  bool sync_disabled() const;
138 
139  bool pick_dirty_flag();
140 
141  private:
142  btTransform _trans;
143  bool _disabled;
144  bool _dirty;
145  bool _was_dirty;
146  };
147 
148  MotionState _motion;
149  btRigidBody *_rigid;
150 
151 public:
152  static void register_with_read_factory();
153  virtual void write_datagram(BamWriter *manager, Datagram &dg);
154  virtual PandaNode *make_copy() const;
155 
156 protected:
158  static TypedWritable *make_from_bam(const FactoryParams &params);
159  void fillin(DatagramIterator &scan, BamReader *manager);
160 
161 public:
162  static TypeHandle get_class_type() {
163  return _type_handle;
164  }
165  static void init_type() {
166  BulletBodyNode::init_type();
167  register_type(_type_handle, "BulletRigidBodyNode",
168  BulletBodyNode::get_class_type());
169  }
170  virtual TypeHandle get_type() const {
171  return get_class_type();
172  }
173  virtual TypeHandle force_init_type() {
174  init_type();
175  return get_class_type();
176  }
177 
178 private:
179  static TypeHandle _type_handle;
180 };
181 
182 #include "bulletRigidBodyNode.I"
183 
184 #endif // __BULLET_RIGID_BODY_NODE_H__
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
Indicates a coordinate-system transform on vertices.
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
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.
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
static void register_with_read_factory()
Tells the BamReader how to create objects of type PandaNode.
Definition: pandaNode.cxx:3580
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
virtual PandaNode * make_copy() const
Returns a newly-allocated PandaNode that is a shallow copy of this one.
Definition: pandaNode.cxx:487
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.