Blender V2.61 - r43446

KX_TrackToActuator.cpp

Go to the documentation of this file.
00001 
00004 //
00005 // Replace the mesh for this actuator's parent
00006 //
00007 //
00008 // ***** BEGIN GPL LICENSE BLOCK *****
00009 //
00010 // This program is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public License
00012 // as published by the Free Software Foundation; either version 2
00013 // of the License, or (at your option) any later version.
00014 //
00015 // This program is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU General Public License
00021 // along with this program; if not, write to the Free Software Foundation,
00022 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00023 //
00024 // The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00025 // All rights reserved.
00026 //
00027 // The Original Code is: all of this file.
00028 //
00029 // Contributor(s): none yet.
00030 //
00031 // ***** END GPL LICENSE BLOCK *****
00032 
00033 // todo: not all trackflags / upflags are implemented/tested !
00034 // m_trackflag is used to determine the forward tracking direction
00035 // m_upflag for the up direction
00036 // normal situation is +y for forward, +z for up
00037 
00038 #include "MT_Scalar.h"
00039 #include "SCA_IActuator.h"
00040 #include "KX_TrackToActuator.h"
00041 #include "SCA_IScene.h"
00042 #include "SCA_LogicManager.h"
00043 #include <math.h>
00044 #include <iostream>
00045 #include "KX_GameObject.h"
00046 
00047 #include "PyObjectPlus.h"
00048 
00049 /* ------------------------------------------------------------------------- */
00050 /* Native functions                                                          */
00051 /* ------------------------------------------------------------------------- */
00052 
00053 
00054 
00055 KX_TrackToActuator::KX_TrackToActuator(SCA_IObject *gameobj, 
00056                                        SCA_IObject *ob,
00057                                        int time,
00058                                        bool allow3D,
00059                                        int trackflag,
00060                                        int upflag)
00061     : SCA_IActuator(gameobj, KX_ACT_TRACKTO)
00062 {
00063     m_time = time;
00064     m_allow3D = allow3D;
00065     m_object = ob;
00066     m_trackflag = trackflag;
00067     m_upflag = upflag;
00068     m_parentobj = 0;
00069     
00070     if (m_object)
00071         m_object->RegisterActuator(this);
00072 
00073     {
00074         // if the object is vertex parented, don't check parent orientation as the link is broken
00075         if (!((KX_GameObject*)gameobj)->IsVertexParent()){
00076             m_parentobj = ((KX_GameObject*)gameobj)->GetParent(); // check if the object is parented 
00077             if (m_parentobj) {  
00078                 // if so, store the initial local rotation
00079                 // this is needed to revert the effect of the parent inverse node (TBC)
00080                 m_parentlocalmat = m_parentobj->GetSGNode()->GetLocalOrientation();
00081                 // use registration mechanism rather than AddRef, it creates zombie objects
00082                 m_parentobj->RegisterActuator(this);
00083                 // GetParent did AddRef, undo here
00084                 m_parentobj->Release();
00085             }
00086         }
00087     }
00088 
00089 } /* End of constructor */
00090 
00091 
00092 
00093 /* old function from Blender */
00094 MT_Matrix3x3 EulToMat3(float *eul)
00095 {
00096     MT_Matrix3x3 mat;
00097     float ci, cj, ch, si, sj, sh, cc, cs, sc, ss;
00098     
00099     ci = cos(eul[0]); 
00100     cj = cos(eul[1]); 
00101     ch = cos(eul[2]);
00102     si = sin(eul[0]); 
00103     sj = sin(eul[1]); 
00104     sh = sin(eul[2]);
00105     cc = ci*ch; 
00106     cs = ci*sh; 
00107     sc = si*ch; 
00108     ss = si*sh;
00109 
00110     mat[0][0] = cj*ch; 
00111     mat[1][0] = sj*sc-cs; 
00112     mat[2][0] = sj*cc+ss;
00113     mat[0][1] = cj*sh; 
00114     mat[1][1] = sj*ss+cc; 
00115     mat[2][1] = sj*cs-sc;
00116     mat[0][2] = -sj;     
00117     mat[1][2] = cj*si;    
00118     mat[2][2] = cj*ci;
00119 
00120     return mat;
00121 }
00122 
00123 
00124 
00125 /* old function from Blender */
00126 void Mat3ToEulOld(MT_Matrix3x3 mat, float *eul)
00127 {
00128     MT_Scalar cy;
00129     
00130     cy = sqrt(mat[0][0]*mat[0][0] + mat[0][1]*mat[0][1]);
00131 
00132     if (cy > 16.0*FLT_EPSILON) {
00133         eul[0] = atan2(mat[1][2], mat[2][2]);
00134         eul[1] = atan2(-mat[0][2], cy);
00135         eul[2] = atan2(mat[0][1], mat[0][0]);
00136     } else {
00137         eul[0] = atan2(-mat[2][1], mat[1][1]);
00138         eul[1] = atan2(-mat[0][2], cy);
00139         eul[2] = 0.0;
00140     }
00141 }
00142 
00143 
00144 
00145 /* old function from Blender */
00146 void compatible_eulFast(float *eul, float *oldrot)
00147 {
00148     float dx, dy, dz;
00149     
00150     /* angular difference of 360 degrees */
00151 
00152     dx= eul[0] - oldrot[0];
00153     dy= eul[1] - oldrot[1];
00154     dz= eul[2] - oldrot[2];
00155 
00156     if( fabs(dx) > MT_PI) {
00157         if(dx > 0.0) eul[0] -= MT_2_PI; else eul[0]+= MT_2_PI;
00158     }
00159     if( fabs(dy) > MT_PI) {
00160         if(dy > 0.0) eul[1] -= MT_2_PI; else eul[1]+= MT_2_PI;
00161     }
00162     if( fabs(dz) > MT_PI ) {
00163         if(dz > 0.0) eul[2] -= MT_2_PI; else eul[2]+= MT_2_PI;
00164     }
00165 }
00166 
00167 
00168 
00169 MT_Matrix3x3 matrix3x3_interpol(MT_Matrix3x3 oldmat, MT_Matrix3x3 mat, int m_time)
00170 {
00171     float eul[3], oldeul[3];    
00172 
00173     Mat3ToEulOld(oldmat, oldeul);
00174     Mat3ToEulOld(mat, eul);
00175     compatible_eulFast(eul, oldeul);
00176     
00177     eul[0]= (m_time*oldeul[0] + eul[0])/(1.0+m_time);
00178     eul[1]= (m_time*oldeul[1] + eul[1])/(1.0+m_time);
00179     eul[2]= (m_time*oldeul[2] + eul[2])/(1.0+m_time);
00180     
00181     return EulToMat3(eul);
00182 }
00183 
00184 
00185 
00186 KX_TrackToActuator::~KX_TrackToActuator()
00187 {
00188     if (m_object)
00189         m_object->UnregisterActuator(this);
00190     if (m_parentobj)
00191         m_parentobj->UnregisterActuator(this);
00192 } /* end of destructor */
00193 
00194 void KX_TrackToActuator::ProcessReplica()
00195 {
00196     // the replica is tracking the same object => register it
00197     if (m_object)
00198         m_object->RegisterActuator(this);
00199     if (m_parentobj)
00200         m_parentobj->RegisterActuator(this);
00201     SCA_IActuator::ProcessReplica();
00202 }
00203 
00204 
00205 bool KX_TrackToActuator::UnlinkObject(SCA_IObject* clientobj)
00206 {
00207     if (clientobj == m_object)
00208     {
00209         // this object is being deleted, we cannot continue to track it.
00210         m_object = NULL;
00211         return true;
00212     }
00213     if (clientobj == m_parentobj)
00214     {
00215         m_parentobj = NULL;
00216         return true;
00217     }
00218     return false;
00219 }
00220 
00221 void KX_TrackToActuator::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
00222 {
00223     void **h_obj = (*obj_map)[m_object];
00224     if (h_obj) {
00225         if (m_object)
00226             m_object->UnregisterActuator(this);
00227         m_object = (SCA_IObject*)(*h_obj);
00228         m_object->RegisterActuator(this);
00229     }
00230 
00231     void **h_parobj = (*obj_map)[m_parentobj];
00232     if (h_parobj) {
00233         if (m_parentobj)
00234             m_parentobj->UnregisterActuator(this);
00235         m_parentobj= (KX_GameObject*)(*h_parobj);
00236         m_parentobj->RegisterActuator(this);
00237     }
00238 }
00239 
00240 
00241 bool KX_TrackToActuator::Update(double curtime, bool frame)
00242 {
00243     bool result = false;    
00244     bool bNegativeEvent = IsNegativeEvent();
00245     RemoveAllEvents();
00246 
00247     if (bNegativeEvent)
00248     {
00249         // do nothing on negative events
00250     }
00251     else if (m_object)
00252     {
00253         KX_GameObject* curobj = (KX_GameObject*) GetParent();
00254         MT_Vector3 dir = ((KX_GameObject*)m_object)->NodeGetWorldPosition() - curobj->NodeGetWorldPosition();
00255         if (dir.length2())
00256             dir.normalize();
00257         MT_Vector3 up(0,0,1);
00258         
00259         
00260 #ifdef DSADSA
00261         switch (m_upflag)
00262         {
00263         case 0:
00264             {
00265                 up.setValue(1.0,0,0);
00266                 break;
00267             } 
00268         case 1:
00269             {
00270                 up.setValue(0,1.0,0);
00271                 break;
00272             }
00273         case 2:
00274         default:
00275             {
00276                 up.setValue(0,0,1.0);
00277             }
00278         }
00279 #endif 
00280         if (m_allow3D)
00281         {
00282             up = (up - up.dot(dir) * dir).safe_normalized();
00283             
00284         }
00285         else
00286         {
00287             dir = (dir - up.dot(dir)*up).safe_normalized();
00288         }
00289         
00290         MT_Vector3 left;
00291         MT_Matrix3x3 mat;
00292         
00293         switch (m_trackflag)
00294         {
00295         case 0: // TRACK X
00296             {
00297                 // (1.0 , 0.0 , 0.0 ) x direction is forward, z (0.0 , 0.0 , 1.0 ) up
00298                 left  = dir.safe_normalized();
00299                 dir = (left.cross(up)).safe_normalized();
00300                 mat.setValue (
00301                     left[0], dir[0],up[0], 
00302                     left[1], dir[1],up[1],
00303                     left[2], dir[2],up[2]
00304                     );
00305                 
00306                 break;
00307             };
00308         case 1: // TRACK Y
00309             {
00310                 // (0.0 , 1.0 , 0.0 ) y direction is forward, z (0.0 , 0.0 , 1.0 ) up
00311                 left  = (dir.cross(up)).safe_normalized();
00312                 mat.setValue (
00313                     left[0], dir[0],up[0], 
00314                     left[1], dir[1],up[1],
00315                     left[2], dir[2],up[2]
00316                     );
00317                 
00318                 break;
00319             }
00320             
00321         case 2: // track Z
00322             {
00323                 left = up.safe_normalized();
00324                 up = dir.safe_normalized();
00325                 dir = left;
00326                 left  = (dir.cross(up)).safe_normalized();
00327                 mat.setValue (
00328                     left[0], dir[0],up[0], 
00329                     left[1], dir[1],up[1],
00330                     left[2], dir[2],up[2]
00331                     );
00332                 break;
00333             }
00334             
00335         case 3: // TRACK -X
00336             {
00337                 // (1.0 , 0.0 , 0.0 ) x direction is forward, z (0.0 , 0.0 , 1.0 ) up
00338                 left  = -dir.safe_normalized();
00339                 dir = -(left.cross(up)).safe_normalized();
00340                 mat.setValue (
00341                     left[0], dir[0],up[0], 
00342                     left[1], dir[1],up[1],
00343                     left[2], dir[2],up[2]
00344                     );
00345                 
00346                 break;
00347             };
00348         case 4: // TRACK -Y
00349             {
00350                 // (0.0 , -1.0 , 0.0 ) -y direction is forward, z (0.0 , 0.0 , 1.0 ) up
00351                 left  = (-dir.cross(up)).safe_normalized();
00352                 mat.setValue (
00353                     left[0], -dir[0],up[0], 
00354                     left[1], -dir[1],up[1],
00355                     left[2], -dir[2],up[2]
00356                     );
00357                 break;
00358             }
00359         case 5: // track -Z
00360             {
00361                 left = up.safe_normalized();
00362                 up = -dir.safe_normalized();
00363                 dir = left;
00364                 left  = (dir.cross(up)).safe_normalized();
00365                 mat.setValue (
00366                     left[0], dir[0],up[0], 
00367                     left[1], dir[1],up[1],
00368                     left[2], dir[2],up[2]
00369                     );
00370                 
00371                 break;
00372             }
00373             
00374         default:
00375             {
00376                 // (1.0 , 0.0 , 0.0 ) -x direction is forward, z (0.0 , 0.0 , 1.0 ) up
00377                 left  = -dir.safe_normalized();
00378                 dir = -(left.cross(up)).safe_normalized();
00379                 mat.setValue (
00380                     left[0], dir[0],up[0], 
00381                     left[1], dir[1],up[1],
00382                     left[2], dir[2],up[2]
00383                     );
00384             }
00385         }
00386         
00387         MT_Matrix3x3 oldmat;
00388         oldmat= curobj->NodeGetWorldOrientation();
00389         
00390         /* erwin should rewrite this! */
00391         mat= matrix3x3_interpol(oldmat, mat, m_time);
00392         
00393 
00394         if(m_parentobj){ // check if the model is parented and calculate the child transform
00395                 
00396             MT_Point3 localpos;
00397             localpos = curobj->GetSGNode()->GetLocalPosition();
00398             // Get the inverse of the parent matrix
00399             MT_Matrix3x3 parentmatinv;
00400             parentmatinv = m_parentobj->NodeGetWorldOrientation ().inverse ();              
00401             // transform the local coordinate system into the parents system
00402             mat = parentmatinv * mat;
00403             // append the initial parent local rotation matrix
00404             mat = m_parentlocalmat * mat;
00405 
00406             // set the models tranformation properties
00407             curobj->NodeSetLocalOrientation(mat);
00408             curobj->NodeSetLocalPosition(localpos);
00409             //curobj->UpdateTransform();
00410         }
00411         else
00412         {
00413             curobj->NodeSetLocalOrientation(mat);
00414         }
00415 
00416         result = true;
00417     }
00418 
00419     return result;
00420 }
00421 
00422 #ifdef WITH_PYTHON
00423 
00424 /* ------------------------------------------------------------------------- */
00425 /* Python functions                                                          */
00426 /* ------------------------------------------------------------------------- */
00427 
00428 /* Integration hooks ------------------------------------------------------- */
00429 PyTypeObject KX_TrackToActuator::Type = {
00430     PyVarObject_HEAD_INIT(NULL, 0)
00431     "KX_TrackToActuator",
00432     sizeof(PyObjectPlus_Proxy),
00433     0,
00434     py_base_dealloc,
00435     0,
00436     0,
00437     0,
00438     0,
00439     py_base_repr,
00440     0,0,0,0,0,0,0,0,0,
00441     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00442     0,0,0,0,0,0,0,
00443     Methods,
00444     0,
00445     0,
00446     &SCA_IActuator::Type,
00447     0,0,0,0,0,0,
00448     py_base_new
00449 };
00450 
00451 PyMethodDef KX_TrackToActuator::Methods[] = {
00452     {NULL,NULL} //Sentinel
00453 };
00454 
00455 PyAttributeDef KX_TrackToActuator::Attributes[] = {
00456     KX_PYATTRIBUTE_INT_RW("time",0,1000,true,KX_TrackToActuator,m_time),
00457     KX_PYATTRIBUTE_BOOL_RW("use3D",KX_TrackToActuator,m_allow3D),
00458     KX_PYATTRIBUTE_RW_FUNCTION("object", KX_TrackToActuator, pyattr_get_object, pyattr_set_object),
00459 
00460     { NULL }    //Sentinel
00461 };
00462 
00463 PyObject* KX_TrackToActuator::pyattr_get_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
00464 {
00465     KX_TrackToActuator* actuator = static_cast<KX_TrackToActuator*>(self);
00466     if (!actuator->m_object)    
00467         Py_RETURN_NONE;
00468     else
00469         return actuator->m_object->GetProxy();
00470 }
00471 
00472 int KX_TrackToActuator::pyattr_set_object(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
00473 {
00474     KX_TrackToActuator* actuator = static_cast<KX_TrackToActuator*>(self);
00475     KX_GameObject *gameobj;
00476         
00477     if (!ConvertPythonToGameObject(value, &gameobj, true, "actuator.object = value: KX_TrackToActuator"))
00478         return PY_SET_ATTR_FAIL; // ConvertPythonToGameObject sets the error
00479         
00480     if (actuator->m_object != NULL)
00481         actuator->m_object->UnregisterActuator(actuator);   
00482 
00483     actuator->m_object = (SCA_IObject*) gameobj;
00484         
00485     if (actuator->m_object)
00486         actuator->m_object->RegisterActuator(actuator);
00487         
00488     return PY_SET_ATTR_SUCCESS;
00489 }
00490 
00491 #endif // WITH_PYTHON
00492 
00493 /* eof */