Panda3D
 All Classes Functions Variables Enumerations
bulletGhostNode.cxx
1 // Filename: bulletGhostNode.cxx
2 // Created by: enn0x (19Nov10)
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 #include "bulletGhostNode.h"
16 #include "bulletShape.h"
17 
18 TypeHandle BulletGhostNode::_type_handle;
19 
20 ////////////////////////////////////////////////////////////////////
21 // Function: BulletGhostNode::Constructor
22 // Access: Published
23 // Description:
24 ////////////////////////////////////////////////////////////////////
25 BulletGhostNode::
26 BulletGhostNode(const char *name) : BulletBodyNode(name) {
27 
28  // Synchronised transform
29  _sync = TransformState::make_identity();
30  _sync_disable = false;
31  _sync_local = false;
32 
33  // Initial transform
34  btTransform trans = btTransform::getIdentity();
35 
36  // Ghost object
37  _ghost = new btPairCachingGhostObject();
38  _ghost->setUserPointer(this);
39  _ghost->setCollisionFlags(btCollisionObject::CF_NO_CONTACT_RESPONSE);
40  _ghost->setWorldTransform(trans);
41  _ghost->setInterpolationWorldTransform(trans);
42  _ghost->setCollisionShape(_shape);
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: BulletGhostNode::get_object
47 // Access: Published
48 // Description:
49 ////////////////////////////////////////////////////////////////////
50 btCollisionObject *BulletGhostNode::
51 get_object() const {
52 
53  return _ghost;
54 }
55 
56 ////////////////////////////////////////////////////////////////////
57 // Function: BulletGhostNode::parents_changed
58 // Access: Protected
59 // Description:
60 ////////////////////////////////////////////////////////////////////
61 void BulletGhostNode::
62 parents_changed() {
63 
64  Parents parents = get_parents();
65  for (int i=0; i < parents.get_num_parents(); ++i) {
66  PandaNode *parent = parents.get_parent(i);
67  TypeHandle type = parent->get_type();
68 
69  if (BulletRigidBodyNode::get_class_type() == type ||
70  BulletSoftBodyNode::get_class_type() == type ||
71  BulletGhostNode::get_class_type() == type ||
72  type.is_derived_from(BulletBaseCharacterControllerNode::get_class_type())) {
73 
74  _sync_local = true;
75  return;
76  }
77  }
78 
79  _sync_local = false;
80 }
81 
82 ////////////////////////////////////////////////////////////////////
83 // Function: BulletGhostNode::transform_changed
84 // Access: Protected
85 // Description:
86 ////////////////////////////////////////////////////////////////////
87 void BulletGhostNode::
88 transform_changed() {
89 
90  if (_sync_disable) return;
91 
93  CPT(TransformState) ts = np.get_net_transform();
94 
95  LMatrix4 m_sync = _sync->get_mat();
96  LMatrix4 m_ts = ts->get_mat();
97 
98  if (!m_sync.almost_equal(m_ts)) {
99  _sync = ts;
100 
101  btTransform trans = TransformState_to_btTrans(ts);
102  _ghost->setWorldTransform(trans);
103  _ghost->setInterpolationWorldTransform(trans);
104 
105  if (ts->has_scale()) {
106  LVecBase3 scale = ts->get_scale();
107  if (!scale.almost_equal(LVecBase3(1.0f, 1.0f, 1.0f))) {
108  for (int i=0; i<get_num_shapes(); i++) {
109  PT(BulletShape) shape = _shapes[i];
110  shape->set_local_scale(scale);
111  }
112  }
113  }
114  }
115 }
116 
117 ////////////////////////////////////////////////////////////////////
118 // Function: BulletGhostNode::sync_p2b
119 // Access: Public
120 // Description:
121 ////////////////////////////////////////////////////////////////////
122 void BulletGhostNode::
123 sync_p2b() {
124 
125  transform_changed();
126 }
127 
128 ////////////////////////////////////////////////////////////////////
129 // Function: BulletGhostNode::sync_b2p
130 // Access: Public
131 // Description:
132 ////////////////////////////////////////////////////////////////////
133 void BulletGhostNode::
134 sync_b2p() {
135 
136  NodePath np = NodePath::any_path((PandaNode *)this);
137  LVecBase3 scale = np.get_net_transform()->get_scale();
138 
139  btTransform trans = _ghost->getWorldTransform();
140  CPT(TransformState) ts = btTrans_to_TransformState(trans, scale);
141 
142  LMatrix4 m_sync = _sync->get_mat();
143  LMatrix4 m_ts = ts->get_mat();
144 
145  if (!m_sync.almost_equal(m_ts)) {
146  _sync = ts;
147  _sync_disable = true;
148  np.set_transform(NodePath(), ts);
149  _sync_disable = false;
150  }
151 }
152 
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
bool is_derived_from(TypeHandle parent, TypedObject *object=(TypedObject *) NULL) const
Returns true if this type is derived from the indicated type, false otherwise.
Definition: typeHandle.I:152
This is the base class for all three-component vectors and points.
Definition: lvecBase3.h:105
PandaNode * get_parent(int n, Thread *current_thread=Thread::get_current_thread()) const
Returns the nth parent node of this node.
Definition: pandaNode.I:40
Parents get_parents(Thread *current_thread=Thread::get_current_thread()) const
Returns an object that can be used to walk through the list of parents of the node, similar to get_children() and get_stashed().
Definition: pandaNode.I:809
void set_transform(const TransformState *transform, Thread *current_thread=Thread::get_current_thread())
Changes the complete transform object on this node.
Definition: nodePath.I:718
static NodePath any_path(PandaNode *node, Thread *current_thread=Thread::get_current_thread())
Returns a new NodePath that represents any arbitrary path from the root to the indicated node...
Definition: nodePath.I:77
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:451
LVecBase3 get_scale() const
Retrieves the scale component of the transform.
Definition: nodePath.cxx:1331
bool almost_equal(const LVecBase3f &other, float threshold) const
Returns true if two vectors are memberwise equal within a specified tolerance.
Definition: lvecBase3.h:1264
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165