Panda3D
Loading...
Searching...
No Matches
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 */
21btVector3 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 */
31LVecBase3 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 */
41LVector3 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 */
51LPoint3 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 */
61btMatrix3x3 LMatrix3_to_btMatrix3x3(const LMatrix3 &m) {
62
63 LMatrix4 m4(m);
64 btMatrix3x3 result;
65 result.setFromOpenGLSubMatrix((const btScalar *)m4.get_data());
66 return result;
67}
68
69/**
70 *
71 */
72LMatrix3 btMatrix3x3_to_LMatrix3(const btMatrix3x3 &m) {
73
74 btScalar cells[12];
75 m.getOpenGLSubMatrix(cells);
76 return LMatrix3((PN_stdfloat)cells[0], (PN_stdfloat)cells[1], (PN_stdfloat)cells[2],
77 (PN_stdfloat)cells[4], (PN_stdfloat)cells[5], (PN_stdfloat)cells[6],
78 (PN_stdfloat)cells[8], (PN_stdfloat)cells[9], (PN_stdfloat)cells[10]);
79}
80
81/**
82 *
83 */
84btQuaternion LQuaternion_to_btQuat(const LQuaternion &q) {
85
86 return btQuaternion((btScalar)q.get_i(),
87 (btScalar)q.get_j(),
88 (btScalar)q.get_k(),
89 (btScalar)q.get_r());
90}
91
92/**
93 *
94 */
95LQuaternion btQuat_to_LQuaternion(const btQuaternion &q) {
96
97 return LQuaternion((PN_stdfloat)q.getW(),
98 (PN_stdfloat)q.getX(),
99 (PN_stdfloat)q.getY(),
100 (PN_stdfloat)q.getZ());
101}
102
103/**
104 *
105 */
106btTransform LMatrix4_to_btTrans(const LMatrix4 &m) {
107
108 LQuaternion quat;
109 quat.set_from_matrix(m.get_upper_3());
110
111 btQuaternion btq = LQuaternion_to_btQuat(quat);
112 btVector3 btv = LVecBase3_to_btVector3(m.get_row3(3));
113
114 return btTransform(btq, btv);
115}
116
117/**
118 *
119 */
120LMatrix4 btTrans_to_LMatrix4(const btTransform &trans) {
121
122 return TransformState::make_pos_quat_scale(
123 btVector3_to_LVector3(trans.getOrigin()),
124 btQuat_to_LQuaternion(trans.getRotation()),
125 LVector3(1.0f, 1.0f, 1.0f))->get_mat();
126}
127
128/**
129 *
130 */
131CPT(TransformState) btTrans_to_TransformState(const btTransform &trans, const LVecBase3 &scale) {
132
133 LVecBase3 pos = btVector3_to_LVector3(trans.getOrigin());
134 LQuaternion quat = btQuat_to_LQuaternion(trans.getRotation());
135
136 return TransformState::make_pos_quat_scale(pos, quat, scale);
137}
138
139/**
140 *
141 */
142btTransform TransformState_to_btTrans(CPT(TransformState) ts) {
143
144 ts = ts->set_scale(1.0);
145
146 LMatrix4 m = ts->get_mat();
147
148 LQuaternion quat;
149 quat.set_from_matrix(m.get_upper_3());
150
151 btQuaternion btq = LQuaternion_to_btQuat(quat);
152 btVector3 btv = LVecBase3_to_btVector3(m.get_row3(3));
153
154 return btTransform(btq, btv);
155}
156
157/**
158 *
159 */
160BulletUpAxis get_default_up_axis() {
161
162 switch (get_default_coordinate_system()) {
163
164 case CS_yup_right:
165 case CS_yup_left:
166 return Y_up;
167
168 case CS_zup_right:
169 case CS_zup_left:
170 return Z_up;
171
172 default:
173 return Z_up;
174 }
175}
176
177/**
178 *
179 */
180void get_node_transform(btTransform &trans, PandaNode *node) {
181
182 // Get TS
183 CPT(TransformState) ts;
184 if (node->get_num_parents() == 0) {
185 ts = node->get_transform();
186 }
187 else {
188 NodePath np = NodePath::any_path(node);
189 ts = np.get_net_transform();
190 }
191
192 // Remove scale from TS, since scale fudges the orientation
193 ts = ts->set_scale(1.0);
194
195 // Convert
196 LMatrix4 m = ts->get_mat();
197
198 LQuaternion quat;
199 quat.set_from_matrix(m.get_upper_3());
200
201 btQuaternion btq = LQuaternion_to_btQuat(quat);
202 btVector3 btv = LVecBase3_to_btVector3(m.get_row3(3));
203
204 trans.setRotation(btq);
205 trans.setOrigin(btv);
206}
207
208/**
209 * Returns the version of the linked Bullet library.
210 */
212
213 return BT_BULLET_VERSION;
214}
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:159
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
A basic node of the scene graph or data graph.
Definition pandaNode.h:65
get_num_parents
Returns the number of parent nodes this node has.
Definition pandaNode.h:118
Indicates a coordinate-system transform on vertices.
get_mat
Returns the matrix that describes the transform.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.