RioEngine  0.1
My first attempt to create a 3D WYSIWYG Game Engine
transform.cpp
Go to the documentation of this file.
1 //--------------------------------------------------------------- @License begins
2 // RioEngine: The late night Coke -without whores- debugging sessions
3 // 2012-2015 Leopoldo Lomas Flores. Torreon, Coahuila. MEXICO
4 // leopoldolomas [at] gmail
5 // www.rioengine.com
6 // www.leopoldolomas.info
7 // "You have a problem, you face it like a man."
8 //
9 // This is free and unencumbered software released into the public domain.
10 //
11 // Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
12 // software, either in source code form or as a compiled binary, for any purpose,
13 // commercial or non-commercial, and by any means.
14 //
15 // In jurisdictions that recognize copyright laws, the author or authors of this
16 // software dedicate any and all copyright interest in the software to the public
17 // domain. We make this dedication for the benefit of the public at large and to
18 // the detriment of our heirs and successors. We intend this dedication to be
19 // an overt act of relinquishment in perpetuity of all present and future
20 // rights to this software under copyright law.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS
24 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //--------------------------------------------------------------- @License ends
29 
30 #include "constants.h"
31 #include "misc/btcustom.h"
32 #include "cengine/cgameobject.h"
33 #include "transform.h"
34 
35 //-----------------------------------------------------------------------------
36 
38  m_localTransform = btCustomTransform();
39 }
40 
41 //-----------------------------------------------------------------------------
42 
43 btRigidBody* Transform::parentsBody() const {
44  if (!parent()) {
45  return NULL;
46  }
47 
48  CGameObject* parent_as_game_object = qobject_cast<CGameObject*>(parent());
49  RE_ASSERT(parent_as_game_object);
50 
51  return parent_as_game_object->bulletProperties.rigidBody();
52 }
53 
54 //-----------------------------------------------------------------------------
55 
56 CGameScene* Transform::parentsScene() const {
57  if (!parent()) {
58  return NULL;
59  }
60 
61  CGameObject* parent_as_game_object = qobject_cast<CGameObject*>(parent());
62  RE_ASSERT(parent_as_game_object);
63 
64  return parent_as_game_object->parentScene();
65 }
66 
67 //-----------------------------------------------------------------------------
68 
69 btVector3 Transform::position() const {
70  if(parent() && parent()->parent()) { // TODO Wat? Improve this
71  CGameObject* parent_object = qobject_cast<CGameObject*>(parent()->parent());
72  if(parent_object) {
73  return parent_object->transform().position() + localPosition();
74  }
75  }
76  return localPosition();
77 }
78 
79 //-----------------------------------------------------------------------------
80 
81 btVector3 Transform::scale() const {
82  if(parent() && parent()->parent()) { // TODO improve this
83  CGameObject* parent_object = qobject_cast<CGameObject*>(parent()->parent());
84  if(parent_object) {
85  return parent_object->transform().scale() * localScale();
86  }
87  }
88  return localScale();
89 }
90 
91 //-----------------------------------------------------------------------------
92 
93 btQuaternion Transform::rotation() const {
94  if(parent() && parent()->parent()) { // TODO improve this
95  CGameObject* parent_object = qobject_cast<CGameObject*>(parent()->parent());
96  if(parent_object) {
97  return parent_object->transform().rotation() * localRotation();
98  }
99  }
100  return localRotation();
101 }
102 
103 //-----------------------------------------------------------------------------
104 
105 const btVector3& Transform::localPosition(UnitType unit_type) const {
106  if (!parentsBody()) {
107  const btVector3& origin = m_localTransform.getInnerTransform().getOrigin();
108  return unit_type == WORLD ? origin : origin * k_QGLGameEditorViewport_SizeFactor;
109  }
110 
111  btTransform t;
112  parentsBody()->getMotionState()->getWorldTransform(t);
113 
114  btTransform& transform = m_localTransform.getInnerTransform();
115  transform.setOrigin(
116  unit_type == WORLD ? t.getOrigin() / k_QGLGameEditorViewport_SizeFactor : t.getOrigin()
117  );
118  return transform.getOrigin();
119 }
120 
121 //-----------------------------------------------------------------------------
122 
123 btVector3 Transform::localScale() const {
124  btMatrix3x3 scale = m_localTransform.getScale();
125  return btVector3(scale[0][0],
126  scale[1][1],
127  scale[2][2]);
128 }
129 
130 //-----------------------------------------------------------------------------
131 
132 btQuaternion Transform::localRotation() const {
133  if (!parentsBody()) {
134  return m_localTransform.getInnerTransform().getRotation();
135  }
136 
137  btTransform t;
138  parentsBody()->getMotionState()->getWorldTransform(t);
139 
140  btTransform transform = m_localTransform.getInnerTransform();
141  transform.setRotation(t.getRotation());
142  return transform.getRotation();
143 }
144 
145 //-----------------------------------------------------------------------------
146 
147 void Transform::setLocalPosition(const btVector3& p) {
148  if (!parentsBody()) {
149  m_localTransform.getInnerTransform().setOrigin(p);
150  return;
151  }
152 
153  btTransform t = parentsBody()->getWorldTransform();
154  t.setOrigin(p * k_QGLGameEditorViewport_SizeFactor);
155 
156  parentsBody()->setWorldTransform(t);
157 
158  // TODO what is the difference between the two following calls?
159  parentsBody()->setCenterOfMassTransform(t);
160  parentsBody()->getMotionState()->setWorldTransform(t);
161 }
162 
163 //-----------------------------------------------------------------------------
164 
165 void Transform::setLocalPosition(float x, float y, float z) {
166  setLocalPosition(btVector3(x, y, z));
167 }
168 
169 //-----------------------------------------------------------------------------
170 
171 void Transform::setLocalScale(const btVector3& s) {
172  btMatrix3x3* scale = &m_localTransform.getScale();
173  scale->setValue(s.x(), 0, 0,
174  0, s.y(), 0,
175  0, 0, s.z());
176 }
177 
178 //-----------------------------------------------------------------------------
179 
180 void Transform::setLocalScale(float x, float y, float z) {
181  setLocalScale(btVector3(x, y, z));
182 }
183 
184 //-----------------------------------------------------------------------------
185 
186 void Transform::setLocalScale(float scale) {
187  setLocalScale(scale, scale, scale);
188 }
189 
190 //-----------------------------------------------------------------------------
191 
192 void Transform::setLocalRotation(const btQuaternion& r) {
193  if (!parentsBody()) {
194  m_localTransform.getInnerTransform().setRotation(r);
195  return;
196  }
197 
198  btTransform t;
199  parentsBody()->getMotionState()->getWorldTransform(t);
200  t.setRotation(r);
201 
202  // TODO what is the difference between the two following calls?
203  parentsBody()->setWorldTransform(t);
204  parentsBody()->getMotionState()->setWorldTransform(t);
205 }
206 
207 //-----------------------------------------------------------------------------
208 
209 void Transform::setLocalRotation(float w, float x, float y, float z) {
210  setLocalRotation(btQuaternion(w, x, y, z));
211 }
212 
213 //-----------------------------------------------------------------------------
214 
216  if(transform_mode == Transform::GLOBAL) {
217  return btCustom::right();
218  }
219  return btCustom::right(localRotation());
220 }
221 
222 //-----------------------------------------------------------------------------
223 
225  if(transform_mode == Transform::GLOBAL) {
226  return btCustom::up();
227  }
228  return btCustom::up(localRotation());
229 }
230 
231 //-----------------------------------------------------------------------------
232 
234  if(transform_mode == Transform::GLOBAL) {
235  return btCustom::forward();
236  }
238 }
239 
240 //-----------------------------------------------------------------------------
241 
242 void Transform::moveX(float dx, TransformMode transform_mode) {
243  btVector3 right_vec = getRightVector(transform_mode);
244  btVector3 new_position = localPosition() + (right_vec * dx);
245  setLocalPosition(new_position);
246 }
247 
248 //-----------------------------------------------------------------------------
249 
250 void Transform::moveY(float dy, TransformMode transform_mode) {
251  btVector3 up_vec = getUpVector(transform_mode);
252  btVector3 new_position = localPosition() + (up_vec * dy);
253  setLocalPosition(new_position);
254 }
255 
256 //-----------------------------------------------------------------------------
257 
258 void Transform::moveZ(float dz, TransformMode transform_mode) {
259  dz *= -1.0f;
260  btVector3 forward_vec = getForwardVector(transform_mode);
261  btVector3 new_position = localPosition() + (forward_vec * dz);
262  setLocalPosition(new_position);
263 }
264 
265 //-----------------------------------------------------------------------------
266 
267 void Transform::rotate(btQuaternion &q, TransformMode transform_mode) {
268  if (transform_mode == LOCAL) {
270  } else {
272  }
273 }
274 
275 //-----------------------------------------------------------------------------
276 
277 void Transform::rotateX(float angle, TransformMode transform_mode) {
278  btQuaternion x_rot(btVector3(1.0f, 0, 0), DEGTORAD(angle));
279  rotate(x_rot, transform_mode);
280 }
281 
282 //-----------------------------------------------------------------------------
283 
284 void Transform::rotateY(float angle, TransformMode transform_mode) {
285  btQuaternion y_rot(btVector3(0, 1.0f, 0), DEGTORAD(angle));
286  if (transform_mode == LOCAL) {
287  setLocalRotation(y_rot * localRotation());
288  } else {
289  setLocalRotation(localRotation() * y_rot);
290  }
291 }
292 
293 //-----------------------------------------------------------------------------
294 
295 void Transform::rotateZ(float angle, TransformMode transform_mode) {
296  btQuaternion z_rot(btVector3(0, 0, 1.0f), angle);
297  rotate(z_rot, transform_mode);
298 }
299 
300 //-----------------------------------------------------------------------------
301 
303  btMatrix3x3 scale_transform;
304  scale_transform.setIdentity();
305 
306  if(scale) {
307  scale_transform = m_localTransform.getScale();
308  }
309 
310  if (!parentsBody()) {
311  return m_localTransform;
312  }
313 
314  m_localTransform.setInnerTransform(parentsBody()->getWorldTransform());
315  m_localTransform.scale(scale_transform);
316  m_localTransform.getInnerTransform().setOrigin(m_localTransform.getInnerTransform().getOrigin() / k_QGLGameEditorViewport_SizeFactor);
317  return m_localTransform;
318 }
319 
320 //-----------------------------------------------------------------------------
321 
323  if(parent() && parent()->parent()) { // TODO improve this
324  CGameObject* parent_object = qobject_cast<CGameObject*>(parent()->parent());
325  if(parent_object) {
326  return parent_object->transform().local(scale) * local(scale);
327  }
328  }
329  return local(scale);
330 }
331 
332 //------------------------------------------- CameraTransform overridden methods
333 
334 btVector3 CameraTransform::getRightVector(Transform::TransformMode transform_mode) {
335  if(transform_mode == Transform::GLOBAL) {
336  return btCustom::right();
337  }
339 }
340 
341 //-----------------------------------------------------------------------------
342 
343 btVector3 CameraTransform::getUpVector(Transform::TransformMode transform_mode) {
344  if(transform_mode == Transform::GLOBAL) {
345  return btCustom::up();
346  }
348 }
349 
350 //-----------------------------------------------------------------------------
351 
352 btVector3 CameraTransform::getForwardVector(Transform::TransformMode transform_mode) {
353  if(transform_mode == Transform::GLOBAL) {
354  return btCustom::forward();
355  }
357 }
btCustomTransform & local(bool scale=true) const
Definition: transform.cpp:302
void setInnerTransform(const btTransform &inner_transform)
btQuaternion localRotation() const
Definition: transform.cpp:132
btCustomTransform global(bool scale=true) const
Definition: transform.cpp:322
btQuaternion rotation() const
Definition: transform.cpp:93
btTransform + Scale Matrix
btVector3 scale() const
Definition: transform.cpp:81
void setLocalScale(float x, float y, float z)
Definition: transform.cpp:180
btMatrix3x3 & getScale()
const btVector3 & localPosition(UnitType unit_type=WORLD) const
Definition: transform.cpp:105
void moveX(float dx, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:242
void rotate(btQuaternion &q, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:267
void setLocalRotation(float w, float x, float y, float z)
Definition: transform.cpp:209
virtual btVector3 getRightVector(TransformMode transform_mode=LOCAL)
Definition: transform.cpp:215
void moveY(float dy, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:250
void moveZ(float dz, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:258
virtual btVector3 getUpVector(TransformMode transform_mode=LOCAL)
Definition: transform.cpp:224
btVector3 up()
Definition: btcustom.cpp:49
btVector3 right()
Definition: btcustom.cpp:32
btVector3 getAxisZ(btMatrix3x3 &m)
Definition: btcustom.cpp:20
void rotateZ(float angle, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:295
btVector3 localScale() const
Definition: transform.cpp:123
virtual btVector3 getForwardVector(TransformMode transform_mode=LOCAL)
Definition: transform.cpp:233
void rotateX(float angle, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:277
btVector3 getAxisY(btMatrix3x3 &m)
Definition: btcustom.cpp:14
#define RE_ASSERT
Definition: macro.h:57
btTransform & getInnerTransform()
#define DEGTORAD(x)
Definition: macro.h:52
btVector3 position() const
Definition: transform.cpp:69
btRigidBody * rigidBody() const
btVector3 forward()
Definition: btcustom.cpp:66
void setLocalPosition(float x, float y, float z)
Definition: transform.cpp:165
void scale(btMatrix3x3 m)
void rotateY(float angle, TransformMode transform_mode=GLOBAL)
Definition: transform.cpp:284
Transform & transform()
Definition: clnode.cpp:69
btVector3 getAxisX(btMatrix3x3 &m)
Definition: btcustom.cpp:8