PageRenderTime 32ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/source/gameengine/Ketsji/KX_TrackToActuator.cpp

https://bitbucket.org/brita/blender-gl_debug
C++ | 491 lines | 348 code | 77 blank | 66 comment | 39 complexity | adfaac0f695d22c5dba4a25d6a1e79c8 MD5 | raw file
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): none yet.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. /** \file gameengine/Ketsji/KX_TrackToActuator.cpp
  28. * \ingroup ketsji
  29. *
  30. * Replace the mesh for this actuator's parent
  31. */
  32. /* todo: not all trackflags / upflags are implemented/tested !
  33. * m_trackflag is used to determine the forward tracking direction
  34. * m_upflag for the up direction
  35. * normal situation is +y for forward, +z for up */
  36. #include "MT_Scalar.h"
  37. #include "SCA_IActuator.h"
  38. #include "KX_TrackToActuator.h"
  39. #include "SCA_IScene.h"
  40. #include "SCA_LogicManager.h"
  41. #include <math.h>
  42. #include <iostream>
  43. #include "KX_GameObject.h"
  44. #include "PyObjectPlus.h"
  45. /* ------------------------------------------------------------------------- */
  46. /* Native functions */
  47. /* ------------------------------------------------------------------------- */
  48. KX_TrackToActuator::KX_TrackToActuator(SCA_IObject *gameobj,
  49. SCA_IObject *ob,
  50. int time,
  51. bool allow3D,
  52. int trackflag,
  53. int upflag)
  54. : SCA_IActuator(gameobj, KX_ACT_TRACKTO)
  55. {
  56. m_time = time;
  57. m_allow3D = allow3D;
  58. m_object = ob;
  59. m_trackflag = trackflag;
  60. m_upflag = upflag;
  61. m_parentobj = 0;
  62. if (m_object)
  63. m_object->RegisterActuator(this);
  64. {
  65. // if the object is vertex parented, don't check parent orientation as the link is broken
  66. if (!((KX_GameObject*)gameobj)->IsVertexParent()) {
  67. m_parentobj = ((KX_GameObject*)gameobj)->GetParent(); // check if the object is parented
  68. if (m_parentobj) {
  69. // if so, store the initial local rotation
  70. // this is needed to revert the effect of the parent inverse node (TBC)
  71. m_parentlocalmat = m_parentobj->GetSGNode()->GetLocalOrientation();
  72. // use registration mechanism rather than AddRef, it creates zombie objects
  73. m_parentobj->RegisterActuator(this);
  74. // GetParent did AddRef, undo here
  75. m_parentobj->Release();
  76. }
  77. }
  78. }
  79. } /* End of constructor */
  80. /* old function from Blender */
  81. static MT_Matrix3x3 EulToMat3(float eul[3])
  82. {
  83. MT_Matrix3x3 mat;
  84. float ci, cj, ch, si, sj, sh, cc, cs, sc, ss;
  85. ci = cos(eul[0]);
  86. cj = cos(eul[1]);
  87. ch = cos(eul[2]);
  88. si = sin(eul[0]);
  89. sj = sin(eul[1]);
  90. sh = sin(eul[2]);
  91. cc = ci*ch;
  92. cs = ci*sh;
  93. sc = si*ch;
  94. ss = si*sh;
  95. mat[0][0] = cj*ch;
  96. mat[1][0] = sj*sc-cs;
  97. mat[2][0] = sj*cc+ss;
  98. mat[0][1] = cj*sh;
  99. mat[1][1] = sj*ss+cc;
  100. mat[2][1] = sj*cs-sc;
  101. mat[0][2] = -sj;
  102. mat[1][2] = cj*si;
  103. mat[2][2] = cj*ci;
  104. return mat;
  105. }
  106. /* old function from Blender */
  107. static void Mat3ToEulOld(MT_Matrix3x3 mat, float eul[3])
  108. {
  109. const float cy = sqrtf(mat[0][0] * mat[0][0] + mat[0][1] * mat[0][1]);
  110. if (cy > (float)(16.0f * FLT_EPSILON)) {
  111. eul[0] = atan2f( mat[1][2], mat[2][2]);
  112. eul[1] = atan2f(-mat[0][2], cy);
  113. eul[2] = atan2f( mat[0][1], mat[0][0]);
  114. }
  115. else {
  116. eul[0] = atan2f(-mat[2][1], mat[1][1]);
  117. eul[1] = atan2f(-mat[0][2], cy);
  118. eul[2] = 0.0;
  119. }
  120. }
  121. /* old function from Blender */
  122. static void compatible_eulFast(float *eul, float *oldrot)
  123. {
  124. float dx, dy, dz;
  125. /* angular difference of 360 degrees */
  126. dx = eul[0] - oldrot[0];
  127. dy = eul[1] - oldrot[1];
  128. dz = eul[2] - oldrot[2];
  129. if (fabsf(dx) > (float)MT_PI) {
  130. if (dx > 0.0f) eul[0] -= (float)MT_2_PI; else eul[0] += (float)MT_2_PI;
  131. }
  132. if (fabsf(dy) > (float)MT_PI) {
  133. if (dy > 0.0f) eul[1] -= (float)MT_2_PI; else eul[1] += (float)MT_2_PI;
  134. }
  135. if (fabsf(dz) > (float)MT_PI) {
  136. if (dz > 0.0f) eul[2] -= (float)MT_2_PI; else eul[2] += (float)MT_2_PI;
  137. }
  138. }
  139. static MT_Matrix3x3 matrix3x3_interpol(MT_Matrix3x3 oldmat, MT_Matrix3x3 mat, int m_time)
  140. {
  141. float eul[3], oldeul[3];
  142. Mat3ToEulOld(oldmat, oldeul);
  143. Mat3ToEulOld(mat, eul);
  144. compatible_eulFast(eul, oldeul);
  145. eul[0] = (m_time * oldeul[0] + eul[0]) / (1.0f + m_time);
  146. eul[1] = (m_time * oldeul[1] + eul[1]) / (1.0f + m_time);
  147. eul[2] = (m_time * oldeul[2] + eul[2]) / (1.0f + m_time);
  148. return EulToMat3(eul);
  149. }
  150. KX_TrackToActuator::~KX_TrackToActuator()
  151. {
  152. if (m_object)
  153. m_object->UnregisterActuator(this);
  154. if (m_parentobj)
  155. m_parentobj->UnregisterActuator(this);
  156. } /* end of destructor */
  157. void KX_TrackToActuator::ProcessReplica()
  158. {
  159. // the replica is tracking the same object => register it
  160. if (m_object)
  161. m_object->RegisterActuator(this);
  162. if (m_parentobj)
  163. m_parentobj->RegisterActuator(this);
  164. SCA_IActuator::ProcessReplica();
  165. }
  166. bool KX_TrackToActuator::UnlinkObject(SCA_IObject* clientobj)
  167. {
  168. if (clientobj == m_object)
  169. {
  170. // this object is being deleted, we cannot continue to track it.
  171. m_object = NULL;
  172. return true;
  173. }
  174. if (clientobj == m_parentobj)
  175. {
  176. m_parentobj = NULL;
  177. return true;
  178. }
  179. return false;
  180. }
  181. void KX_TrackToActuator::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
  182. {
  183. void **h_obj = (*obj_map)[m_object];
  184. if (h_obj) {
  185. if (m_object)
  186. m_object->UnregisterActuator(this);
  187. m_object = (SCA_IObject*)(*h_obj);
  188. m_object->RegisterActuator(this);
  189. }
  190. void **h_parobj = (*obj_map)[m_parentobj];
  191. if (h_parobj) {
  192. if (m_parentobj)
  193. m_parentobj->UnregisterActuator(this);
  194. m_parentobj= (KX_GameObject*)(*h_parobj);
  195. m_parentobj->RegisterActuator(this);
  196. }
  197. }
  198. bool KX_TrackToActuator::Update(double curtime, bool frame)
  199. {
  200. bool result = false;
  201. bool bNegativeEvent = IsNegativeEvent();
  202. RemoveAllEvents();
  203. if (bNegativeEvent)
  204. {
  205. // do nothing on negative events
  206. }
  207. else if (m_object)
  208. {
  209. KX_GameObject* curobj = (KX_GameObject*) GetParent();
  210. MT_Vector3 dir = ((KX_GameObject*)m_object)->NodeGetWorldPosition() - curobj->NodeGetWorldPosition();
  211. if (dir.length2())
  212. dir.normalize();
  213. MT_Vector3 up(0,0,1);
  214. #ifdef DSADSA
  215. switch (m_upflag)
  216. {
  217. case 0:
  218. {
  219. up.setValue(1.0,0,0);
  220. break;
  221. }
  222. case 1:
  223. {
  224. up.setValue(0,1.0,0);
  225. break;
  226. }
  227. case 2:
  228. default:
  229. {
  230. up.setValue(0,0,1.0);
  231. }
  232. }
  233. #endif
  234. if (m_allow3D)
  235. {
  236. up = (up - up.dot(dir) * dir).safe_normalized();
  237. }
  238. else
  239. {
  240. dir = (dir - up.dot(dir)*up).safe_normalized();
  241. }
  242. MT_Vector3 left;
  243. MT_Matrix3x3 mat;
  244. switch (m_trackflag)
  245. {
  246. case 0: // TRACK X
  247. {
  248. // (1.0 , 0.0 , 0.0 ) x direction is forward, z (0.0 , 0.0 , 1.0 ) up
  249. left = dir.safe_normalized();
  250. dir = up.cross(left).safe_normalized();
  251. mat.setValue (
  252. left[0], dir[0],up[0],
  253. left[1], dir[1],up[1],
  254. left[2], dir[2],up[2]
  255. );
  256. break;
  257. };
  258. case 1: // TRACK Y
  259. {
  260. // (0.0 , 1.0 , 0.0 ) y direction is forward, z (0.0 , 0.0 , 1.0 ) up
  261. left = (dir.cross(up)).safe_normalized();
  262. mat.setValue (
  263. left[0], dir[0],up[0],
  264. left[1], dir[1],up[1],
  265. left[2], dir[2],up[2]
  266. );
  267. break;
  268. }
  269. case 2: // track Z
  270. {
  271. left = up.safe_normalized();
  272. up = dir.safe_normalized();
  273. dir = left;
  274. left = (dir.cross(up)).safe_normalized();
  275. mat.setValue (
  276. left[0], dir[0],up[0],
  277. left[1], dir[1],up[1],
  278. left[2], dir[2],up[2]
  279. );
  280. break;
  281. }
  282. case 3: // TRACK -X
  283. {
  284. // (1.0 , 0.0 , 0.0 ) x direction is forward, z (0.0 , 0.0 , 1.0 ) up
  285. left = -dir.safe_normalized();
  286. dir = up.cross(left).safe_normalized();
  287. mat.setValue (
  288. left[0], dir[0],up[0],
  289. left[1], dir[1],up[1],
  290. left[2], dir[2],up[2]
  291. );
  292. break;
  293. };
  294. case 4: // TRACK -Y
  295. {
  296. // (0.0 , -1.0 , 0.0 ) -y direction is forward, z (0.0 , 0.0 , 1.0 ) up
  297. left = (-dir.cross(up)).safe_normalized();
  298. mat.setValue (
  299. left[0], -dir[0],up[0],
  300. left[1], -dir[1],up[1],
  301. left[2], -dir[2],up[2]
  302. );
  303. break;
  304. }
  305. case 5: // track -Z
  306. {
  307. left = up.safe_normalized();
  308. up = -dir.safe_normalized();
  309. dir = left;
  310. left = (dir.cross(up)).safe_normalized();
  311. mat.setValue (
  312. left[0], dir[0],up[0],
  313. left[1], dir[1],up[1],
  314. left[2], dir[2],up[2]
  315. );
  316. break;
  317. }
  318. default:
  319. {
  320. // (1.0 , 0.0 , 0.0 ) -x direction is forward, z (0.0 , 0.0 , 1.0 ) up
  321. left = -dir.safe_normalized();
  322. dir = up.cross(left).safe_normalized();
  323. mat.setValue (
  324. left[0], dir[0],up[0],
  325. left[1], dir[1],up[1],
  326. left[2], dir[2],up[2]
  327. );
  328. }
  329. }
  330. MT_Matrix3x3 oldmat;
  331. oldmat= curobj->NodeGetWorldOrientation();
  332. /* erwin should rewrite this! */
  333. mat= matrix3x3_interpol(oldmat, mat, m_time);
  334. if (m_parentobj) { // check if the model is parented and calculate the child transform
  335. MT_Point3 localpos;
  336. localpos = curobj->GetSGNode()->GetLocalPosition();
  337. // Get the inverse of the parent matrix
  338. MT_Matrix3x3 parentmatinv;
  339. parentmatinv = m_parentobj->NodeGetWorldOrientation ().inverse ();
  340. // transform the local coordinate system into the parents system
  341. mat = parentmatinv * mat;
  342. // append the initial parent local rotation matrix
  343. mat = m_parentlocalmat * mat;
  344. // set the models tranformation properties
  345. curobj->NodeSetLocalOrientation(mat);
  346. curobj->NodeSetLocalPosition(localpos);
  347. //curobj->UpdateTransform();
  348. }
  349. else
  350. {
  351. curobj->NodeSetLocalOrientation(mat);
  352. }
  353. result = true;
  354. }
  355. return result;
  356. }
  357. #ifdef WITH_PYTHON
  358. /* ------------------------------------------------------------------------- */
  359. /* Python functions */
  360. /* ------------------------------------------------------------------------- */
  361. /* Integration hooks ------------------------------------------------------- */
  362. PyTypeObject KX_TrackToActuator::Type = {
  363. PyVarObject_HEAD_INIT(NULL, 0)
  364. "KX_TrackToActuator",
  365. sizeof(PyObjectPlus_Proxy),
  366. 0,
  367. py_base_dealloc,
  368. 0,
  369. 0,
  370. 0,
  371. 0,
  372. py_base_repr,
  373. 0,0,0,0,0,0,0,0,0,
  374. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
  375. 0,0,0,0,0,0,0,
  376. Methods,
  377. 0,
  378. 0,
  379. &SCA_IActuator::Type,
  380. 0,0,0,0,0,0,
  381. py_base_new
  382. };
  383. PyMethodDef KX_TrackToActuator::Methods[] = {
  384. {NULL,NULL} //Sentinel
  385. };
  386. PyAttributeDef KX_TrackToActuator::Attributes[] = {
  387. KX_PYATTRIBUTE_INT_RW("time",0,1000,true,KX_TrackToActuator,m_time),
  388. KX_PYATTRIBUTE_BOOL_RW("use3D",KX_TrackToActuator,m_allow3D),
  389. KX_PYATTRIBUTE_RW_FUNCTION("object", KX_TrackToActuator, pyattr_get_object, pyattr_set_object),
  390. { NULL } //Sentinel
  391. };
  392. PyObject *KX_TrackToActuator::pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
  393. {
  394. KX_TrackToActuator* actuator = static_cast<KX_TrackToActuator*>(self);
  395. if (!actuator->m_object)
  396. Py_RETURN_NONE;
  397. else
  398. return actuator->m_object->GetProxy();
  399. }
  400. int KX_TrackToActuator::pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
  401. {
  402. KX_TrackToActuator* actuator = static_cast<KX_TrackToActuator*>(self);
  403. KX_GameObject *gameobj;
  404. if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: KX_TrackToActuator"))
  405. return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
  406. if (actuator->m_object != NULL)
  407. actuator->m_object->UnregisterActuator(actuator);
  408. actuator->m_object = (SCA_IObject*) gameobj;
  409. if (actuator->m_object)
  410. actuator->m_object->RegisterActuator(actuator);
  411. return PY_SET_ATTR_SUCCESS;
  412. }
  413. #endif // WITH_PYTHON
  414. /* eof */