quiet double promotion warnings for the game engine.
[blender.git] / source / gameengine / Ketsji / KX_TrackToActuator.cpp
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
28 /** \file gameengine/Ketsji/KX_TrackToActuator.cpp
29  *  \ingroup ketsji
30  *
31  * Replace the mesh for this actuator's parent
32  */
33
34 /* todo: not all trackflags / upflags are implemented/tested !
35  * m_trackflag is used to determine the forward tracking direction
36  * m_upflag for the up direction
37  * normal situation is +y for forward, +z for up */
38
39 #include "MT_Scalar.h"
40 #include "SCA_IActuator.h"
41 #include "KX_TrackToActuator.h"
42 #include "SCA_IScene.h"
43 #include "SCA_LogicManager.h"
44 #include <math.h>
45 #include <iostream>
46 #include "KX_GameObject.h"
47
48 #include "PyObjectPlus.h"
49
50 /* ------------------------------------------------------------------------- */
51 /* Native functions                                                          */
52 /* ------------------------------------------------------------------------- */
53
54 KX_TrackToActuator::KX_TrackToActuator(SCA_IObject *gameobj, 
55                                        SCA_IObject *ob,
56                                        int time,
57                                        bool allow3D,
58                                        int trackflag,
59                                        int upflag)
60     : SCA_IActuator(gameobj, KX_ACT_TRACKTO)
61 {
62         m_time = time;
63         m_allow3D = allow3D;
64         m_object = ob;
65         m_trackflag = trackflag;
66         m_upflag = upflag;
67         m_parentobj = 0;
68         
69         if (m_object)
70                 m_object->RegisterActuator(this);
71
72         {
73                 // if the object is vertex parented, don't check parent orientation as the link is broken
74                 if (!((KX_GameObject*)gameobj)->IsVertexParent()) {
75                         m_parentobj = ((KX_GameObject*)gameobj)->GetParent(); // check if the object is parented 
76                         if (m_parentobj) {  
77                                 // if so, store the initial local rotation
78                                 // this is needed to revert the effect of the parent inverse node (TBC)
79                                 m_parentlocalmat = m_parentobj->GetSGNode()->GetLocalOrientation();
80                                 // use registration mechanism rather than AddRef, it creates zombie objects
81                                 m_parentobj->RegisterActuator(this);
82                                 // GetParent did AddRef, undo here
83                                 m_parentobj->Release();
84                         }
85                 }
86         }
87
88 } /* End of constructor */
89
90
91
92 /* old function from Blender */
93 MT_Matrix3x3 EulToMat3(float *eul)
94 {
95         MT_Matrix3x3 mat;
96         float ci, cj, ch, si, sj, sh, cc, cs, sc, ss;
97         
98         ci = cos(eul[0]); 
99         cj = cos(eul[1]); 
100         ch = cos(eul[2]);
101         si = sin(eul[0]); 
102         sj = sin(eul[1]); 
103         sh = sin(eul[2]);
104         cc = ci*ch; 
105         cs = ci*sh; 
106         sc = si*ch; 
107         ss = si*sh;
108
109         mat[0][0] = cj*ch; 
110         mat[1][0] = sj*sc-cs; 
111         mat[2][0] = sj*cc+ss;
112         mat[0][1] = cj*sh; 
113         mat[1][1] = sj*ss+cc; 
114         mat[2][1] = sj*cs-sc;
115         mat[0][2] = -sj;         
116         mat[1][2] = cj*si;    
117         mat[2][2] = cj*ci;
118
119         return mat;
120 }
121
122
123
124 /* old function from Blender */
125 void Mat3ToEulOld(MT_Matrix3x3 mat, float eul[3])
126 {
127         const float cy = sqrtf(mat[0][0] * mat[0][0] + mat[0][1] * mat[0][1]);
128
129         if (cy > (float)(16.0f * FLT_EPSILON)) {
130                 eul[0] = atan2f( mat[1][2], mat[2][2]);
131                 eul[1] = atan2f(-mat[0][2], cy);
132                 eul[2] = atan2f( mat[0][1], mat[0][0]);
133         }
134         else {
135                 eul[0] = atan2f(-mat[2][1], mat[1][1]);
136                 eul[1] = atan2f(-mat[0][2], cy);
137                 eul[2] = 0.0;
138         }
139 }
140
141
142
143 /* old function from Blender */
144 void compatible_eulFast(float *eul, float *oldrot)
145 {
146         float dx, dy, dz;
147         
148         /* angular difference of 360 degrees */
149
150         dx = eul[0] - oldrot[0];
151         dy = eul[1] - oldrot[1];
152         dz = eul[2] - oldrot[2];
153
154         if (fabsf(dx) > (float)MT_PI) {
155                 if (dx > 0.0f) eul[0] -= (float)MT_2_PI; else eul[0] += (float)MT_2_PI;
156         }
157         if (fabsf(dy) > (float)MT_PI) {
158                 if (dy > 0.0f) eul[1] -= (float)MT_2_PI; else eul[1] += (float)MT_2_PI;
159         }
160         if (fabsf(dz) > (float)MT_PI) {
161                 if (dz > 0.0f) eul[2] -= (float)MT_2_PI; else eul[2] += (float)MT_2_PI;
162         }
163 }
164
165
166
167 MT_Matrix3x3 matrix3x3_interpol(MT_Matrix3x3 oldmat, MT_Matrix3x3 mat, int m_time)
168 {
169         float eul[3], oldeul[3];        
170
171         Mat3ToEulOld(oldmat, oldeul);
172         Mat3ToEulOld(mat, eul);
173         compatible_eulFast(eul, oldeul);
174         
175         eul[0] = (m_time * oldeul[0] + eul[0]) / (1.0f + m_time);
176         eul[1] = (m_time * oldeul[1] + eul[1]) / (1.0f + m_time);
177         eul[2] = (m_time * oldeul[2] + eul[2]) / (1.0f + m_time);
178         
179         return EulToMat3(eul);
180 }
181
182
183
184 KX_TrackToActuator::~KX_TrackToActuator()
185 {
186         if (m_object)
187                 m_object->UnregisterActuator(this);
188         if (m_parentobj)
189                 m_parentobj->UnregisterActuator(this);
190 } /* end of destructor */
191
192 void KX_TrackToActuator::ProcessReplica()
193 {
194         // the replica is tracking the same object => register it
195         if (m_object)
196                 m_object->RegisterActuator(this);
197         if (m_parentobj)
198                 m_parentobj->RegisterActuator(this);
199         SCA_IActuator::ProcessReplica();
200 }
201
202
203 bool KX_TrackToActuator::UnlinkObject(SCA_IObject* clientobj)
204 {
205         if (clientobj == m_object)
206         {
207                 // this object is being deleted, we cannot continue to track it.
208                 m_object = NULL;
209                 return true;
210         }
211         if (clientobj == m_parentobj)
212         {
213                 m_parentobj = NULL;
214                 return true;
215         }
216         return false;
217 }
218
219 void KX_TrackToActuator::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
220 {
221         void **h_obj = (*obj_map)[m_object];
222         if (h_obj) {
223                 if (m_object)
224                         m_object->UnregisterActuator(this);
225                 m_object = (SCA_IObject*)(*h_obj);
226                 m_object->RegisterActuator(this);
227         }
228
229         void **h_parobj = (*obj_map)[m_parentobj];
230         if (h_parobj) {
231                 if (m_parentobj)
232                         m_parentobj->UnregisterActuator(this);
233                 m_parentobj= (KX_GameObject*)(*h_parobj);
234                 m_parentobj->RegisterActuator(this);
235         }
236 }
237
238
239 bool KX_TrackToActuator::Update(double curtime, bool frame)
240 {
241         bool result = false;    
242         bool bNegativeEvent = IsNegativeEvent();
243         RemoveAllEvents();
244
245         if (bNegativeEvent)
246         {
247                 // do nothing on negative events
248         }
249         else if (m_object)
250         {
251                 KX_GameObject* curobj = (KX_GameObject*) GetParent();
252                 MT_Vector3 dir = ((KX_GameObject*)m_object)->NodeGetWorldPosition() - curobj->NodeGetWorldPosition();
253                 if (dir.length2())
254                         dir.normalize();
255                 MT_Vector3 up(0,0,1);
256                 
257                 
258 #ifdef DSADSA
259                 switch (m_upflag)
260                 {
261                 case 0:
262                         {
263                                 up.setValue(1.0,0,0);
264                                 break;
265                         } 
266                 case 1:
267                         {
268                                 up.setValue(0,1.0,0);
269                                 break;
270                         }
271                 case 2:
272                 default:
273                         {
274                                 up.setValue(0,0,1.0);
275                         }
276                 }
277 #endif 
278                 if (m_allow3D)
279                 {
280                         up = (up - up.dot(dir) * dir).safe_normalized();
281                         
282                 }
283                 else
284                 {
285                         dir = (dir - up.dot(dir)*up).safe_normalized();
286                 }
287                 
288                 MT_Vector3 left;
289                 MT_Matrix3x3 mat;
290                 
291                 switch (m_trackflag)
292                 {
293                 case 0: // TRACK X
294                         {
295                                 // (1.0 , 0.0 , 0.0 ) x direction is forward, z (0.0 , 0.0 , 1.0 ) up
296                                 left  = dir.safe_normalized();
297                                 dir = (left.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                                 
304                                 break;
305                         };
306                 case 1: // TRACK Y
307                         {
308                                 // (0.0 , 1.0 , 0.0 ) y direction is forward, z (0.0 , 0.0 , 1.0 ) up
309                                 left  = (dir.cross(up)).safe_normalized();
310                                 mat.setValue (
311                                         left[0], dir[0],up[0], 
312                                         left[1], dir[1],up[1],
313                                         left[2], dir[2],up[2]
314                                         );
315                                 
316                                 break;
317                         }
318                         
319                 case 2: // track Z
320                         {
321                                 left = up.safe_normalized();
322                                 up = dir.safe_normalized();
323                                 dir = left;
324                                 left  = (dir.cross(up)).safe_normalized();
325                                 mat.setValue (
326                                         left[0], dir[0],up[0], 
327                                         left[1], dir[1],up[1],
328                                         left[2], dir[2],up[2]
329                                         );
330                                 break;
331                         }
332                         
333                 case 3: // TRACK -X
334                         {
335                                 // (1.0 , 0.0 , 0.0 ) x direction is forward, z (0.0 , 0.0 , 1.0 ) up
336                                 left  = -dir.safe_normalized();
337                                 dir = -(left.cross(up)).safe_normalized();
338                                 mat.setValue (
339                                         left[0], dir[0],up[0], 
340                                         left[1], dir[1],up[1],
341                                         left[2], dir[2],up[2]
342                                         );
343                                 
344                                 break;
345                         };
346                 case 4: // TRACK -Y
347                         {
348                                 // (0.0 , -1.0 , 0.0 ) -y direction is forward, z (0.0 , 0.0 , 1.0 ) up
349                                 left  = (-dir.cross(up)).safe_normalized();
350                                 mat.setValue (
351                                         left[0], -dir[0],up[0], 
352                                         left[1], -dir[1],up[1],
353                                         left[2], -dir[2],up[2]
354                                         );
355                                 break;
356                         }
357                 case 5: // track -Z
358                         {
359                                 left = up.safe_normalized();
360                                 up = -dir.safe_normalized();
361                                 dir = left;
362                                 left  = (dir.cross(up)).safe_normalized();
363                                 mat.setValue (
364                                         left[0], dir[0],up[0], 
365                                         left[1], dir[1],up[1],
366                                         left[2], dir[2],up[2]
367                                         );
368                                 
369                                 break;
370                         }
371                         
372                 default:
373                         {
374                                 // (1.0 , 0.0 , 0.0 ) -x direction is forward, z (0.0 , 0.0 , 1.0 ) up
375                                 left  = -dir.safe_normalized();
376                                 dir = -(left.cross(up)).safe_normalized();
377                                 mat.setValue (
378                                         left[0], dir[0],up[0], 
379                                         left[1], dir[1],up[1],
380                                         left[2], dir[2],up[2]
381                                         );
382                         }
383                 }
384                 
385                 MT_Matrix3x3 oldmat;
386                 oldmat= curobj->NodeGetWorldOrientation();
387                 
388                 /* erwin should rewrite this! */
389                 mat= matrix3x3_interpol(oldmat, mat, m_time);
390                 
391
392                 if (m_parentobj) { // check if the model is parented and calculate the child transform
393                                 
394                         MT_Point3 localpos;
395                         localpos = curobj->GetSGNode()->GetLocalPosition();
396                         // Get the inverse of the parent matrix
397                         MT_Matrix3x3 parentmatinv;
398                         parentmatinv = m_parentobj->NodeGetWorldOrientation ().inverse ();                              
399                         // transform the local coordinate system into the parents system
400                         mat = parentmatinv * mat;
401                         // append the initial parent local rotation matrix
402                         mat = m_parentlocalmat * mat;
403
404                         // set the models tranformation properties
405                         curobj->NodeSetLocalOrientation(mat);
406                         curobj->NodeSetLocalPosition(localpos);
407                         //curobj->UpdateTransform();
408                 }
409                 else
410                 {
411                         curobj->NodeSetLocalOrientation(mat);
412                 }
413
414                 result = true;
415         }
416
417         return result;
418 }
419
420 #ifdef WITH_PYTHON
421
422 /* ------------------------------------------------------------------------- */
423 /* Python functions                                                          */
424 /* ------------------------------------------------------------------------- */
425
426 /* Integration hooks ------------------------------------------------------- */
427 PyTypeObject KX_TrackToActuator::Type = {
428         PyVarObject_HEAD_INIT(NULL, 0)
429         "KX_TrackToActuator",
430         sizeof(PyObjectPlus_Proxy),
431         0,
432         py_base_dealloc,
433         0,
434         0,
435         0,
436         0,
437         py_base_repr,
438         0,0,0,0,0,0,0,0,0,
439         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
440         0,0,0,0,0,0,0,
441         Methods,
442         0,
443         0,
444         &SCA_IActuator::Type,
445         0,0,0,0,0,0,
446         py_base_new
447 };
448
449 PyMethodDef KX_TrackToActuator::Methods[] = {
450         {NULL,NULL} //Sentinel
451 };
452
453 PyAttributeDef KX_TrackToActuator::Attributes[] = {
454         KX_PYATTRIBUTE_INT_RW("time",0,1000,true,KX_TrackToActuator,m_time),
455         KX_PYATTRIBUTE_BOOL_RW("use3D",KX_TrackToActuator,m_allow3D),
456         KX_PYATTRIBUTE_RW_FUNCTION("object", KX_TrackToActuator, pyattr_get_object, pyattr_set_object),
457
458         { NULL }        //Sentinel
459 };
460
461 PyObject* KX_TrackToActuator::pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
462 {
463         KX_TrackToActuator* actuator = static_cast<KX_TrackToActuator*>(self);
464         if (!actuator->m_object)        
465                 Py_RETURN_NONE;
466         else
467                 return actuator->m_object->GetProxy();
468 }
469
470 int KX_TrackToActuator::pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
471 {
472         KX_TrackToActuator* actuator = static_cast<KX_TrackToActuator*>(self);
473         KX_GameObject *gameobj;
474                 
475         if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: KX_TrackToActuator"))
476                 return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
477                 
478         if (actuator->m_object != NULL)
479                 actuator->m_object->UnregisterActuator(actuator);       
480
481         actuator->m_object = (SCA_IObject*) gameobj;
482                 
483         if (actuator->m_object)
484                 actuator->m_object->RegisterActuator(actuator);
485                 
486         return PY_SET_ATTR_SUCCESS;
487 }
488
489 #endif // WITH_PYTHON
490
491 /* eof */