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 parents_changed();
116  virtual void transform_changed();
117 
118 private:
119  virtual void do_shape_changed();
120  void do_transform_changed();
121 
122  void do_set_mass(PN_stdfloat mass);
123  PN_stdfloat do_get_mass() const;
124 
125  // The motion state is used for synchronisation between Bullet and the
126  // Panda3D scene graph.
127  class MotionState : public btMotionState {
128 
129  public:
130  MotionState();
131 
132  virtual void getWorldTransform(btTransform &trans) const;
133  virtual void setWorldTransform(const btTransform &trans);
134 
135  void set_net_transform(const TransformState *ts);
136 
137  void sync_b2p(PandaNode *node);
138  bool sync_disabled() const;
139 
140  bool pick_dirty_flag();
141 
142  private:
143  btTransform _trans;
144  bool _disabled;
145  bool _dirty;
146  bool _was_dirty;
147  };
148 
149  MotionState _motion;
150  btRigidBody *_rigid;
151 
152 public:
153  static void register_with_read_factory();
154  virtual void write_datagram(BamWriter *manager, Datagram &dg);
155  virtual PandaNode *make_copy() const;
156 
157 protected:
159  static TypedWritable *make_from_bam(const FactoryParams &params);
160  void fillin(DatagramIterator &scan, BamReader *manager);
161 
162 public:
163  static TypeHandle get_class_type() {
164  return _type_handle;
165  }
166  static void init_type() {
167  BulletBodyNode::init_type();
168  register_type(_type_handle, "BulletRigidBodyNode",
169  BulletBodyNode::get_class_type());
170  }
171  virtual TypeHandle get_type() const {
172  return get_class_type();
173  }
174  virtual TypeHandle force_init_type() {
175  init_type();
176  return get_class_type();
177  }
178 
179 private:
180  static TypeHandle _type_handle;
181 };
182 
183 #include "bulletRigidBodyNode.I"
184 
185 #endif // __BULLET_RIGID_BODY_NODE_H__
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 the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
virtual void write_datagram(BamWriter *manager, Datagram &dg)
Writes the contents of this object to the datagram for shipping out to a Bam file.
A class to retrieve the individual data elements previously stored in a Datagram.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
A basic node of the scene graph or data graph.
Definition: pandaNode.h:65
static void register_with_read_factory()
Tells the BamReader how to create objects of type PandaNode.
Definition: pandaNode.cxx:3574
virtual PandaNode * make_copy() const
Returns a newly-allocated PandaNode that is a shallow copy of this one.
Definition: pandaNode.cxx:481
Indicates a coordinate-system transform on vertices.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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