Panda3D
bullet_utils.cxx
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 bullet_utils.cxx
10  * @author enn0x
11  * @date 2010-01-23
12  */
13 
14 #include "bullet_utils.h"
15 
16 #include "transformState.h"
17 
18 /**
19  *
20  */
21 btVector3 LVecBase3_to_btVector3(const LVecBase3 &v) {
22 
23  return btVector3((btScalar)v.get_x(),
24  (btScalar)v.get_y(),
25  (btScalar)v.get_z());
26 }
27 
28 /**
29  *
30  */
31 LVecBase3 btVector3_to_LVecBase3(const btVector3 &v) {
32 
33  return LVecBase3((PN_stdfloat)v.getX(),
34  (PN_stdfloat)v.getY(),
35  (PN_stdfloat)v.getZ());
36 }
37 
38 /**
39  *
40  */
41 LVector3 btVector3_to_LVector3(const btVector3 &v) {
42 
43  return LVector3((PN_stdfloat)v.getX(),
44  (PN_stdfloat)v.getY(),
45  (PN_stdfloat)v.getZ());
46 }
47 
48 /**
49  *
50  */
51 LPoint3 btVector3_to_LPoint3(const btVector3 &p) {
52 
53  return LPoint3((PN_stdfloat)p.getX(),
54  (PN_stdfloat)p.getY(),
55  (PN_stdfloat)p.getZ());
56 }
57 
58 /**
59  *
60  */
61 btMatrix3x3 LMatrix3_to_btMatrix3x3(const LMatrix3 &m) {
62 
63  btMatrix3x3 result;
64  result.setFromOpenGLSubMatrix((const btScalar *)m.get_data());
65  return result;
66 }
67 
68 /**
69  *
70  */
71 LMatrix3 btMatrix3x3_to_LMatrix3(const btMatrix3x3 &m) {
72 
73  btScalar cells[9];
74  m.getOpenGLSubMatrix(cells);
75  return LMatrix3((PN_stdfloat)cells[0], (PN_stdfloat)cells[1], (PN_stdfloat)cells[2],
76  (PN_stdfloat)cells[3], (PN_stdfloat)cells[4], (PN_stdfloat)cells[5],
77  (PN_stdfloat)cells[6], (PN_stdfloat)cells[7], (PN_stdfloat)cells[8]);
78 }
79 
80 /**
81  *
82  */
83 btQuaternion LQuaternion_to_btQuat(const LQuaternion &q) {
84 
85  return btQuaternion((btScalar)q.get_i(),
86  (btScalar)q.get_j(),
87  (btScalar)q.get_k(),
88  (btScalar)q.get_r());
89 }
90 
91 /**
92  *
93  */
94 LQuaternion btQuat_to_LQuaternion(const btQuaternion &q) {
95 
96  return LQuaternion((PN_stdfloat)q.getW(),
97  (PN_stdfloat)q.getX(),
98  (PN_stdfloat)q.getY(),
99  (PN_stdfloat)q.getZ());
100 }
101 
102 /**
103  *
104  */
105 btTransform LMatrix4_to_btTrans(const LMatrix4 &m) {
106 
107  LQuaternion quat;
108  quat.set_from_matrix(m.get_upper_3());
109 
110  btQuaternion btq = LQuaternion_to_btQuat(quat);
111  btVector3 btv = LVecBase3_to_btVector3(m.get_row3(3));
112 
113  return btTransform(btq, btv);
114 }
115 
116 /**
117  *
118  */
119 LMatrix4 btTrans_to_LMatrix4(const btTransform &trans) {
120 
121  return TransformState::make_pos_quat_scale(
122  btVector3_to_LVector3(trans.getOrigin()),
123  btQuat_to_LQuaternion(trans.getRotation()),
124  LVector3(1.0f, 1.0f, 1.0f))->get_mat();
125 }
126 
127 /**
128  *
129  */
130 CPT(TransformState) btTrans_to_TransformState(const btTransform &trans, const LVecBase3 &scale) {
131 
132  LVecBase3 pos = btVector3_to_LVector3(trans.getOrigin());
133  LQuaternion quat = btQuat_to_LQuaternion(trans.getRotation());
134 
135  return TransformState::make_pos_quat_scale(pos, quat, scale);
136 }
137 
138 /**
139  *
140  */
141 btTransform TransformState_to_btTrans(CPT(TransformState) ts) {
142 
143  ts = ts->set_scale(1.0);
144 
145  LMatrix4 m = ts->get_mat();
146 
147  LQuaternion quat;
148  quat.set_from_matrix(m.get_upper_3());
149 
150  btQuaternion btq = LQuaternion_to_btQuat(quat);
151  btVector3 btv = LVecBase3_to_btVector3(m.get_row3(3));
152 
153  return btTransform(btq, btv);
154 }
155 
156 /**
157  *
158  */
159 BulletUpAxis get_default_up_axis() {
160 
161  switch (get_default_coordinate_system()) {
162 
163  case CS_yup_right:
164  case CS_yup_left:
165  return Y_up;
166 
167  case CS_zup_right:
168  case CS_zup_left:
169  return Z_up;
170 
171  default:
172  return Z_up;
173  }
174 }
175 
176 /**
177  *
178  */
179 void get_node_transform(btTransform &trans, PandaNode *node) {
180 
181  // Get TS
182  CPT(TransformState) ts;
183  if (node->get_num_parents() == 0) {
184  ts = node->get_transform();
185  }
186  else {
187  NodePath np = NodePath::any_path(node);
188  ts = np.get_net_transform();
189  }
190 
191  // Remove scale from TS, since scale fudges the orientation
192  ts = ts->set_scale(1.0);
193 
194  // Convert
195  LMatrix4 m = ts->get_mat();
196 
197  LQuaternion quat;
198  quat.set_from_matrix(m.get_upper_3());
199 
200  btQuaternion btq = LQuaternion_to_btQuat(quat);
201  btVector3 btv = LVecBase3_to_btVector3(m.get_row3(3));
202 
203  trans.setRotation(btq);
204  trans.setOrigin(btv);
205 }
206 
207 /**
208  * Returns the version of the linked Bullet library.
209  */
211 
212  return BT_BULLET_VERSION;
213 }
A basic node of the scene graph or data graph.
Definition: pandaNode.h:64
Indicates a coordinate-system transform on vertices.
get_num_parents
Returns the number of parent nodes this node has.
Definition: pandaNode.h:118
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
get_mat
Returns the matrix that describes the transform.
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:62
int get_bullet_version()
Returns the version of the linked Bullet library.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:161