Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

KX_GameObject.cpp

Go to the documentation of this file.
00001 
00002 #define KX_INERTIA_INFINITE 10000
00003 
00004 #include "KX_GameObject.h"
00005 #include "RAS_MeshObject.h"
00006 #include <stdio.h> // printf
00007 #include "solid.h"
00008 #include "SM_Object.h"
00009 #include "SG_Controller.h"
00010 #include "KX_PhysicsController.h"
00011 #include "SG_Node.h"
00012 #include "SM_ClientObjectInfo.h"
00013 
00014 PyTypeObject KX_GameObject::Type = {
00015         PyObject_HEAD_INIT(&PyType_Type)
00016         0,
00017         "KX_GameObject",
00018         sizeof(KX_GameObject),
00019         0,
00020         PyDestructor,
00021         0,
00022         __getattr,
00023         __setattr,
00024         0, //&MyPyCompare,
00025         __repr,
00026         0, //&cvalue_as_number,
00027         0,
00028         0,
00029         0,
00030         0
00031 };
00032 
00033 PyParentObject KX_GameObject::Parents[] = {
00034         &KX_GameObject::Type,
00035         &SCA_IObject::Type,
00036         &CValue::Type,
00037         NULL
00038 };
00039 
00040 PyMethodDef KX_GameObject::Methods[] = {
00041 {"setVisible",(PyCFunction) KX_GameObject::sPySetVisible, METH_VARARGS},  
00042 {"setPosition", (PyCFunction) KX_GameObject::sPySetPosition, METH_VARARGS},
00043   {"getPosition", (PyCFunction) KX_GameObject::sPyGetPosition, METH_VARARGS},
00044   {"getLinearVelocity", (PyCFunction) KX_GameObject::sPyGetLinearVelocity, METH_VARARGS},
00045   {"getVelocity", (PyCFunction) KX_GameObject::sPyGetVelocity, METH_VARARGS},
00046   {"getOrientation", (PyCFunction) KX_GameObject::sPyGetOrientation, METH_VARARGS},
00047   {"setOrientation", (PyCFunction) KX_GameObject::sPySetOrientation, METH_VARARGS},
00048   {"getMass", (PyCFunction) KX_GameObject::sPyGetMass, METH_VARARGS},
00049   {"getReactionForce", (PyCFunction) KX_GameObject::sPyGetReactionForce, METH_VARARGS},
00050 {"suspendDynamics", (PyCFunction)KX_GameObject::sPySuspendDynamics,METH_VARARGS},
00051 {"restoreDynamics", (PyCFunction)KX_GameObject::sPyRestoreDynamics,METH_VARARGS},
00052 
00053   {NULL,NULL} //Sentinel
00054 };
00055 
00056 PyObject*
00057 KX_GameObject::_getattr(char* attr)

00058 {
00059   _getattr_up(SCA_IObject);
00060 }
00061 
00062 
00063 
00064 KX_GameObject::KX_GameObject(void* sgReplicationInfo,SG_Callbacks callbacks,PyTypeObject* T)
00065   : SCA_IObject(T),
00066         m_DynamicsScaling(1,1,1),
00067         m_bDyna(false),
00068         m_bInLayer(true),
00069         m_bSuspendDynamics(false),
00070         m_pPhysicsController(NULL)

00071         

00072 {
00073         m_pClient_info = new SM_ClientObjectInfo();
00074         m_pSGNode = new SG_Node(this,sgReplicationInfo,callbacks);
00075 };
00076 
00077 KX_GameObject::~KX_GameObject()

00078 {
00079         // is this delete somewhere ?
00080         //if (m_sumoObj)
00081         //      delete m_sumoObj;
00082         delete m_pClient_info;
00083 //      if (m_pSGNode)
00084 //              delete m_pSGNode;
00085         
00086 }
00087 CValue* KX_GameObject::	Calc(VALUE_OPERATOR op, CValue *val) 

00088 {
00089         return NULL;
00090 }
00091         
00092 
00093 CValue*         KX_GameObject::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)

00094 {
00095         return NULL;
00096 }
00097 
00098 const CCString &        KX_GameObject::GetText()

00099 {
00100         return m_text;
00101 }
00102 
00103 float           KX_GameObject::GetNumber()

00104 {
00105         return 0;
00106 }
00107 
00108 CCString        KX_GameObject::GetName()

00109 {
00110         return m_name;
00111 }
00112 
00113 void            KX_GameObject::SetName(CCString name)

00114 {
00115         m_name = name;
00116 };                                                              // Set the name of the value
00117         
00118 void    KX_GameObject::ReplicaSetName(CCString name)

00119 {
00120 
00121 }
00122 
00123 SM_Object*              KX_GameObject::GetSumoObject()

00124 {
00125         SM_Object* sumoObj=NULL;
00126 
00127         if (m_pPhysicsController)
00128         {
00129                 sumoObj = m_pPhysicsController->GetSumoObject();
00130         } 
00131         return sumoObj;
00132 }
00133 CValue*         KX_GameObject::GetReplica()

00134 {
00135 
00136         KX_GameObject* replica = new KX_GameObject(*this);//m_float,GetName());
00137         
00138         // this will copy properties and so on...
00139         CValue::AddDataToReplica(replica);
00140         replica->m_bDyna = false;
00141         replica->m_pPhysicsController= NULL;
00142         replica->m_pSGNode = NULL;
00143 
00144         // these need special threatment
00145         // a new sumo object needs to be made by the one who replicates this object
00146         // using a KX_Scene
00147         
00148         //replica->m_sumoObj = NULL;
00149         
00150         //replica->m_meshes.clear();
00151         //replica->m_tempClientObject=NULL;//->m_blenderobject = NULL;
00152 
00153         //class SM_Object*      m_sumoObj;
00154         
00155         //vector                                <class RAS_MeshObject*>         m_meshes;
00156 
00157         replica->m_pClient_info = new SM_ClientObjectInfo(*m_pClient_info);
00158         replica->m_pClient_info->m_clientobject = replica;
00159 
00160         return replica;
00161 
00162 }
00163 
00164 
00165 
00166 void KX_GameObject::ApplyForce(const MT_Vector3& force,bool local)

00167 {
00168 
00169         if (m_pPhysicsController)
00170         {
00171                 m_pPhysicsController->ApplyForce(force,local);
00172         }
00173 
00174 }
00175 
00176 
00177 void KX_GameObject::ApplyTorque(const MT_Vector3& torque,bool local)

00178 {
00179         if (m_pPhysicsController)
00180         {
00181                 m_pPhysicsController->ApplyTorque(torque,local);
00182         }
00183 }
00184 
00185 void KX_GameObject::ApplyMovement(const MT_Vector3& dloc,bool local)

00186 {
00187 <<<<<<< KX_GameObject.cpp
00188         if (this->IsDynamic()) // assume m_pPhysicsController exists now
00189 =======
00190         if (this->IsDynamic()) //m_pPhysicsController)
00191 >>>>>>> 1.68
00192         {
00193                 m_pPhysicsController->RelativeTranslate(dloc,local);
00194         } else
00195         {
00196                 GetSGNode()->RelativeTranslate(dloc,local);
00197         }
00198 //      SetSumoMotionState();
00199 }
00200 
00201 void KX_GameObject::ApplyRotation(const MT_Vector3& drot,bool local)

00202 {
00203         MT_Matrix3x3 rotmat(drot);
00204 
00205 <<<<<<< KX_GameObject.cpp
00206         if (this->IsDynamic()) // assume m_pPhysicsController exists now
00207 =======
00208         if (this->IsDynamic()) //m_pPhysicsController)
00209 >>>>>>> 1.68
00210         {
00211                 m_pPhysicsController->RelativeRotate(rotmat.transposed(),local);
00212         } else
00213         {
00214                 // in worldspace
00215                 GetSGNode()->RelativeRotate(rotmat.transposed(),local);
00216         }
00217 }
00218 
00222 
00223 double* KX_GameObject::GetOpenGLMatrix()

00224 {
00225         // todo: optimize and only update if necessary
00226         double* fl = m_OpenGL_4x4Matrix.getPointer();
00227         MT_Transform trans;
00228         trans.setOrigin(GetSGNode()->GetWorldPosition());
00229         trans.setBasis(GetSGNode()->GetWorldOrientation());
00230         MT_Vector3 scaling = GetSGNode()->GetWorldScaling();
00231         trans.scale(scaling[0],scaling[1],scaling[2]);
00232         trans.getValue(fl);
00233         return fl;
00234 }
00235 
00236 void KX_GameObject::Bucketize()

00237 {
00238         double* fl = GetOpenGLMatrix();
00239         for (int i=0;i<m_meshes.size();i++)
00240         {
00241                 m_meshes[i]->Bucketize(
00242                         fl,this
00243                 );
00244         }
00245 }
00246 
00247 void KX_GameObject::RemoveMeshes()

00248 {
00249         double* fl = GetOpenGLMatrix();
00250         for (int i=0;i<m_meshes.size();i++)
00251         {
00252                 m_meshes[i]->RemoveFromBuckets(
00253                         fl,this
00254                 );
00255         }
00256         // todo: memleak! add refcounting to meshes!
00257         m_meshes.clear();
00258 }
00259 
00260 <<<<<<< KX_GameObject.cpp
00261 =======
00262 
00263 
00264 >>>>>>> 1.68
00265 void KX_GameObject::UpdateTransform(bool i_am_ipo)

00266 {
00267         if (i_am_ipo)
00268         {
00269                 if (m_pPhysicsController)
00270                 {
00271                         m_pPhysicsController->SetSumoTransform();
00272                 }
00273         } else
00274         {
00275                 int i=0;
00276         }
00277 <<<<<<< KX_GameObject.cpp
00278 
00279 =======
00280 >>>>>>> 1.68
00281 }
00282 
00283 
00284 void KX_GameObject::SetDebugColor(unsigned int bgra)

00285 {
00286         for (int i=0;i<m_meshes.size();i++)
00287         {
00288                 m_meshes[i]->DebugColor(bgra);  
00289         }
00290 }
00291 
00292 void KX_GameObject::ResetDebugColor()

00293 {
00294         SetDebugColor(0xff000000);
00295 }
00296 
00297 void KX_GameObject::UpdateIPO(float curframetime) 

00298 {
00299         
00300         GetSGNode()->SetSimulatedTime(curframetime);
00301         
00302         UpdateTransform(true);
00303                 
00304 }
00305 
00306 
00307 void KX_GameObject::SetSumoMotionState()

00308 {
00309 /*      if (m_sumoObj)

00310         {

00311                 SM_SetPosition(m_sumoObj,m_trans        1.getOrigin().getValue());

00312                 SM_SetOrientation(m_sumoObj,m_trans1.getRotation().getValue());

00313                 SM_SetScaling(m_sumoObj,m_scaling.getValue());

00314         }

00315         */
00316 }
00317 
00318 void KX_GameObject::RegisterSumoObject(class SM_Scene* sumoScene,class SM_Object* sumoObj,const char* matname,bool isDynamic,bool isActor,bool bInLayer) {
00319 
00320 
00321                 m_bInLayer = bInLayer;
00322                 m_bDyna = isDynamic;
00323                 // need easy access, not via 'node' etc.
00324                 m_pPhysicsController = new KX_PhysicsController(sumoScene,sumoObj,isDynamic);
00325                                 
00326                 GetSGNode()->AddSGController(m_pPhysicsController);
00327 
00328                 m_pClient_info->m_type = (isActor ? 1 : 0);
00329                 m_pClient_info->m_clientobject = this;
00330                 // store materialname in auxinfo, needed for touchsensors
00331                 m_pClient_info->m_auxilary_info = (void*)matname;
00332 
00333                 m_pPhysicsController->SetObject(this->GetSGNode());
00334                                 
00335 
00336                 m_DynamicsScaling.setValue(1.0, 1.0, 1.0);
00337 
00338 };
00339 
00340 
00341 
00342 void    KX_GameObject::MarkVisible(bool visible)

00343 {
00344         for (int i=0;i<m_meshes.size();i++)
00345         {
00346                 double* fl = GetOpenGLMatrix();
00347                 m_meshes[i]->MarkVisible(fl,this,visible);//m_blenderobject,visible);
00348         }
00349 }
00350 
00351 void KX_GameObject::setLinearVelocity(const MT_Vector3& lin_vel,bool local)

00352 {
00353         if (m_pPhysicsController)
00354         {
00355                 m_pPhysicsController->SetLinearVelocity(lin_vel,local);
00356         }
00357 }
00358 
00359 void KX_GameObject::setAngularVelocity(const MT_Vector3& ang_vel,bool local)

00360 {
00361         if (m_pPhysicsController)
00362         {
00363                 m_pPhysicsController->SetAngularVelocity(ang_vel,local);
00364         }
00365 }
00366         
00367 PyObject* KX_GameObject::PyGetPosition(PyObject* self, 
00368                                PyObject* args, 
00369                                PyObject* kwds)

00370 

00371 {
00372         float x=0,y=0,z=0;
00373                         
00374         MT_Point3 pos = NodeGetWorldPosition();
00375         
00376         PyObject* resultlist = PyList_New(3);
00377         int index;
00378         for (index=0;index<3;index++)
00379         {
00380                 PyList_SetItem(resultlist,index,PyFloat_FromDouble(pos[index]));
00381         }
00382 
00383         return resultlist;
00384 
00385 }
00386 
00387 PyObject* KX_GameObject::PyGetLinearVelocity(PyObject* self, 
00388                                PyObject* args, 
00389                                PyObject* kwds)

00390 

00391 {
00392         float x=0,y=0,z=0;
00393         // only can get the velocity if we have a physics object connected to us...
00394         MT_Vector3 velocity(0.0,0.0,0.0);
00395 
00396         if (m_pPhysicsController)
00397         {
00398                 velocity = m_pPhysicsController->GetLinearVelocity();
00399         }
00400         
00401         PyObject* resultlist = PyList_New(3);
00402         int index;
00403         for (index=0;index<3;index++)
00404         {
00405                 PyList_SetItem(resultlist,index,PyFloat_FromDouble(velocity[index]));
00406         }
00407         return resultlist;
00408 }
00409 PyObject* KX_GameObject::PySetVisible(PyObject* self,
00410                                         PyObject* args,
00411                                         PyObject* kwds)

00412 {
00413         int visible = 1;
00414 
00415         if (PyArg_ParseTuple(args,"i",&visible))
00416         {
00417              MarkVisible(visible!=0);   
00418         }
00419         else
00420         {
00421            return NULL;      
00422         }
00423    Py_Return;
00424                 
00425 }
00426 PyObject* KX_GameObject::PyGetVelocity(PyObject* self, 
00427                                PyObject* args, 
00428                                PyObject* kwds)

00429 

00430 {
00431         float x=0,y=0,z=0;
00432         // only can get the velocity if we have a physics object connected to us...
00433         MT_Vector3 velocity(0.0,0.0,0.0);
00434         MT_Point3 point(0.0,0.0,0.0);
00435         
00436         
00437         MT_Point3 pos;
00438         PyObject* pylist;
00439         bool    error = false;
00440 
00441         int len = PyTuple_Size(args);
00442 
00443         if ((len > 0) && PyArg_ParseTuple(args,"O",&pylist))
00444         {
00445                 if (pylist->ob_type == &CListValue::Type)
00446                 {
00447                         CListValue* listval = (CListValue*) pylist;
00448                         if (listval->GetCount() == 3)
00449                         {
00450                                 int index;
00451                                 for (index=0;index<3;index++)
00452                                 {
00453                                         pos[index] = listval->GetValue(index)->GetNumber();
00454                                 }
00455                         }       else
00456                         {
00457                                 error = true;
00458                         }
00459                         
00460                 } else
00461                 {
00462                         
00463                         // assert the list is long enough...
00464                         int numitems = PyList_Size(pylist);
00465                         if (numitems == 3)
00466                         {
00467                                 int index;
00468                                 for (index=0;index<3;index++)
00469                                 {
00470                                         pos[index] = PyFloat_AsDouble(PyList_GetItem(pylist,index));
00471                                 }
00472                         }
00473                         else
00474                         {
00475                                 error = true;
00476                         }
00477                 }
00478                 
00479                 if (!error)
00480                         point = pos;    
00481         }
00482         
00483 
00484         if (m_pPhysicsController)
00485         {
00486                 velocity = m_pPhysicsController->GetVelocity(point);
00487         }
00488         
00489         PyObject* resultlist = PyList_New(3);
00490         int index;
00491         for (index=0;index<3;index++)
00492         {
00493                 PyList_SetItem(resultlist,index,PyFloat_FromDouble(velocity[index]));
00494         }
00495         return resultlist;
00496 }
00497 
00498 
00499 PyObject* KX_GameObject::PyGetMass(PyObject* self, 
00500                                PyObject* args, 
00501                                PyObject* kwds)

00502 

00503 {
00504         PyObject* pymass = NULL;
00505 /*
00506         if (m_sumoObj)

00507         {

00508                 if (m_sumoObj->isDynamic())

00509                 {

00510                         const SM_ShapeProps *shapeprops = m_sumoObj->getShapeProps();

00511                         pymass = PyFloat_FromDouble(shapeprops->m_mass);

00512                 }

00513         } else

00514         {

00515         }

00516         */
00517         if (pymass)
00518                 return pymass;
00519 
00520         Py_Return;
00521 }
00522 
00523 PyObject* KX_GameObject::PyGetReactionForce(PyObject* self, 
00524                                                                                         PyObject* args, 
00525                                                                                         PyObject* kwds)

00526 

00527 {
00528     //  float x=0,y=0,z=0;
00529         // only can get the velocity if we have a physics object connected to us...
00530 
00531         MT_Vector3 reaction_force = MT_Vector3(0.0, 0.0, 0.0);
00532         //      m_sumoObj ?     m_sumoObj->getReactionForce() : MT_Vector3(0.0, 0.0, 0.0);
00533         
00534         PyObject* resultlist = PyList_New(3);
00535         int index;
00536         for (index=0;index<3;index++)
00537         {
00538                 PyList_SetItem(resultlist,index,
00539                                            PyFloat_FromDouble(reaction_force[index]));
00540         }
00541         return resultlist;
00542 }
00543 
00544 
00545 
00546 PyObject* KX_GameObject::PySuspendDynamics(PyObject* self, 
00547                                PyObject* args, 
00548                                PyObject* kwds)          {
00549 
00550         
00551         if (m_bSuspendDynamics)
00552         {
00553                 Py_Return;
00554         }
00555 
00556         if (m_pPhysicsController)
00557         {
00558                 m_pPhysicsController->SuspendDynamics();
00559         }
00560         m_bSuspendDynamics = true;
00561 
00562         Py_Return;
00563 }
00564 
00565 PyObject* KX_GameObject::PyRestoreDynamics(PyObject* self, 
00566                                PyObject* args, 
00567                                PyObject* kwds)          {
00568         //if (m_sumoObj)
00569         //              m_sumoObj->restoreDynamics();
00570 
00571         if (!m_bSuspendDynamics)
00572         {
00573                 Py_Return;
00574         }
00575 
00576         if (m_pPhysicsController)
00577         {
00578                 m_pPhysicsController->RestoreDynamics();
00579         }
00580         m_bSuspendDynamics = false;
00581 
00582         Py_Return;
00583 }
00584 
00585 
00586 PyObject* KX_GameObject::PyGetOrientation(PyObject* self, PyObject* args, PyObject* kwds) //keywords
00587 {
00588         // do the conversion of a C++ matrix to a python list
00589 
00590         PyObject* resultlist = PyList_New(3);
00591 
00592 
00593         int row,col;
00594         const MT_Matrix3x3& orient = NodeGetWorldOrientation();
00595 
00596         int index = 0;
00597         for (row=0;row<3;row++)
00598         {
00599                 PyObject* veclist = PyList_New(3);
00600 
00601                 for (col=0;col<3;col++)
00602                 {
00603                         const MT_Scalar fl = orient[row][col];
00604                         PyList_SetItem(veclist,col,PyFloat_FromDouble(fl));
00605                 }
00606                 PyList_SetItem(resultlist,row,veclist);
00607 
00608         }
00609         return resultlist;
00610 }
00611 
00612 
00613 
00614 PyObject* KX_GameObject::PySetOrientation(PyObject* self, 
00615                                PyObject* args, 
00616                                PyObject* kwds)

00617 {
00618         MT_Matrix3x3 matrix;
00619 
00620         PyObject* pylist;
00621         bool    error = false;
00622         int row,col;
00623 
00624         PyArg_ParseTuple(args,"O",&pylist);
00625 
00626         if (pylist->ob_type == &CListValue::Type)
00627         {
00628                 CListValue* listval = (CListValue*) pylist;
00629                 if (listval->GetCount() == 3)
00630                 {
00631                         for (row=0;row<3;row++) // each row has a 3-vector [x,y,z]
00632                         {
00633                                 CListValue* vecval = (CListValue*)listval->GetValue(row);
00634                                 for (col=0;col<3;col++)
00635                                 {
00636                                                 matrix[row][col] = vecval->GetValue(col)->GetNumber();
00637 
00638                                 }
00639                         }
00640 
00641                 }       else
00642                 {
00643                         error = true;
00644                 }
00645 
00646         } else
00647         {
00648                 
00649                 // assert the list is long enough...
00650                 int numitems = PyList_Size(pylist);
00651                 if (numitems == 3)
00652                 {
00653                         for (row=0;row<3;row++) // each row has a 3-vector [x,y,z]
00654                         {
00655 
00656                                 PyObject* veclist = PyList_GetItem(pylist,row); // here we have a vector3 list
00657                                 for (col=0;col<3;col++)
00658                                 {
00659                                                 matrix[row][col] =  PyFloat_AsDouble(PyList_GetItem(veclist,col));
00660 
00661                                 }
00662                         }
00663                         
00664                 }
00665                 else
00666                 {
00667                         error = true;
00668                 }
00669         }
00670 
00671         if (!error)
00672                 NodeSetLocalOrientation(matrix);
00673 
00674         Py_INCREF(Py_None);
00675         return Py_None;
00676 }
00677 
00678 
00679 
00680 // scenegraph node stuff
00681 
00682 
00683 void                                                    KX_GameObject::NodeSetVertexParent(bool isvertexparent)

00684 {
00685         GetSGNode()->SetVertexParent(isvertexparent);
00686 }
00687 void                                                    KX_GameObject::NodeSetLocalPosition(const MT_Point3& trans)

00688 {
00689         GetSGNode()->SetLocalPosition(trans);
00690 }
00691 
00692 void                                                    KX_GameObject::NodeSetLocalOrientation(const MT_Matrix3x3& rot)

00693 {
00694         GetSGNode()->SetLocalOrientation(rot);
00695 }
00696 
00697 void                                                    KX_GameObject::NodeSetLocalScale(const MT_Vector3& scale)

00698 {
00699         GetSGNode()->SetLocalScale(scale);
00700 }
00701 void                                                    KX_GameObject::NodeUpdateGS(double time,bool bInitiator)

00702 {
00703         GetSGNode()->UpdateGS(time,bInitiator);
00704 }
00705 
00706 const MT_Matrix3x3&                             KX_GameObject::NodeGetWorldOrientation() const

00707 {
00708         return GetSGNode()->GetWorldOrientation();
00709 }
00710 
00711 const MT_Vector3&                               KX_GameObject::NodeGetWorldScaling() const

00712 {
00713         return GetSGNode()->GetWorldScaling();
00714 }
00715 
00716 const MT_Point3&                                KX_GameObject::NodeGetWorldPosition() const

00717 {
00718         return GetSGNode()->GetWorldPosition();
00719 }
00720 void                                                    KX_GameObject::NodeAddChild(class SG_Spatial* child)

00721 {
00722                 GetSGNode()->AddChild(child);
00723 }

Generated at Thu Feb 1 13:03:04 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000