6a35dfc048492877bddc2297f8558fbff220890a
[blender.git] / source / gameengine / Ketsji / KX_GameObject.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  * Game object wrapper
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #ifdef WIN32
39 // This warning tells us about truncation of __long__ stl-generated names.
40 // It can occasionally cause DevStudio to have internal compiler warnings.
41 #pragma warning( disable : 4786 )     
42 #endif
43
44
45 #define KX_INERTIA_INFINITE 10000
46 #include "RAS_IPolygonMaterial.h"
47 #include "KX_GameObject.h"
48 #include "RAS_MeshObject.h"
49 #include "KX_MeshProxy.h"
50 #include <stdio.h> // printf
51 #include "SG_Controller.h"
52 #include "KX_IPhysicsController.h"
53 #include "SG_Node.h"
54 #include "SG_Controller.h"
55 #include "KX_ClientObjectInfo.h"
56 #include "RAS_BucketManager.h"
57
58 #include "KX_PyMath.h"
59
60 // This file defines relationships between parents and children
61 // in the game engine.
62
63 #include "KX_SG_NodeRelationships.h"
64
65 KX_GameObject::KX_GameObject(
66         void* sgReplicationInfo,
67         SG_Callbacks callbacks,
68         PyTypeObject* T
69 ) : 
70         SCA_IObject(T),
71         m_bDyna(false),
72         m_bSuspendDynamics(false),
73         m_bUseObjectColor(false),
74         m_bVisible(true),
75         m_pPhysicsController1(NULL)
76 {
77         m_ignore_activity_culling = false;
78         m_pClient_info = new KX_ClientObjectInfo(this, KX_ClientObjectInfo::ACTOR);
79         m_pSGNode = new SG_Node(this,sgReplicationInfo,callbacks);
80         
81         // define the relationship between this node and it's parent.
82         
83         KX_NormalParentRelation * parent_relation = 
84                 KX_NormalParentRelation::New();
85         m_pSGNode->SetParentRelation(parent_relation);
86         
87
88 };
89
90
91
92 KX_GameObject::~KX_GameObject()
93 {
94         // is this delete somewhere ?
95         //if (m_sumoObj)
96         //      delete m_sumoObj;
97         delete m_pClient_info;
98         //if (m_pSGNode)
99         //      delete m_pSGNode;
100         
101 }
102
103
104
105 CValue* KX_GameObject:: Calc(VALUE_OPERATOR op, CValue *val) 
106 {
107         return NULL;
108 }
109
110
111
112 CValue* KX_GameObject::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
113 {
114         return NULL;
115 }
116
117
118
119 const STR_String & KX_GameObject::GetText()
120 {
121         return m_text;
122 }
123
124
125
126 float KX_GameObject::GetNumber()
127 {
128         return 0;
129 }
130
131
132
133 STR_String KX_GameObject::GetName()
134 {
135         return m_name;
136 }
137
138
139
140 void KX_GameObject::SetName(STR_String name)
141 {
142         m_name = name;
143 };                                                              // Set the name of the value
144
145
146
147 void KX_GameObject::ReplicaSetName(STR_String name)
148 {
149 }
150
151
152
153
154
155
156 KX_IPhysicsController* KX_GameObject::GetPhysicsController()
157 {
158         return m_pPhysicsController1;
159 }
160
161
162
163
164
165 KX_GameObject* KX_GameObject::GetParent()
166 {
167         KX_GameObject* result = NULL;
168         SG_Node* node = m_pSGNode;
169         
170         while (node && !result)
171         {
172                 node = node->GetSGParent();
173                 if (node)
174                         result = (KX_GameObject*)node->GetSGClientObject();
175         }
176         
177         if (result)
178                 result->AddRef();
179
180         return result;
181         
182 }
183
184
185
186 void KX_GameObject::ProcessReplica(KX_GameObject* replica)
187 {
188         replica->m_pPhysicsController1 = NULL;
189         replica->m_pSGNode = NULL;
190         replica->m_pClient_info = new KX_ClientObjectInfo(*m_pClient_info);
191         replica->m_pClient_info->m_clientobject = replica;
192 }
193
194
195
196 CValue* KX_GameObject::GetReplica()
197 {
198         KX_GameObject* replica = new KX_GameObject(*this);
199         
200         // this will copy properties and so on...
201         CValue::AddDataToReplica(replica);
202         ProcessReplica(replica);
203         
204         return replica;
205 }
206
207
208
209 void KX_GameObject::ApplyForce(const MT_Vector3& force,bool local)
210 {
211         if (m_pPhysicsController1)
212                 m_pPhysicsController1->ApplyForce(force,local);
213 }
214
215
216
217 void KX_GameObject::ApplyTorque(const MT_Vector3& torque,bool local)
218 {
219         if (m_pPhysicsController1)
220                 m_pPhysicsController1->ApplyTorque(torque,local);
221 }
222
223
224
225 void KX_GameObject::ApplyMovement(const MT_Vector3& dloc,bool local)
226 {
227         if (m_pPhysicsController1) // (IsDynamic())
228         {
229                 m_pPhysicsController1->RelativeTranslate(dloc,local);
230         }
231         GetSGNode()->RelativeTranslate(dloc,GetSGNode()->GetSGParent(),local);
232 }
233
234
235
236 void KX_GameObject::ApplyRotation(const MT_Vector3& drot,bool local)
237 {
238         MT_Matrix3x3 rotmat(drot);
239         rotmat.transpose();
240         
241         if (m_pPhysicsController1) // (IsDynamic())
242                 m_pPhysicsController1->RelativeRotate(rotmat,local); 
243         // in worldspace
244         GetSGNode()->RelativeRotate(rotmat,local);
245 }
246
247
248
249 /**
250 GetOpenGL Matrix, returns an OpenGL 'compatible' matrix
251 */
252 double* KX_GameObject::GetOpenGLMatrix()
253 {
254         // todo: optimize and only update if necessary
255         double* fl = m_OpenGL_4x4Matrix.getPointer();
256         MT_Transform trans;
257         
258         trans.setOrigin(GetSGNode()->GetWorldPosition());
259         trans.setBasis(GetSGNode()->GetWorldOrientation());
260         
261         MT_Vector3 scaling = GetSGNode()->GetWorldScaling();
262         
263         trans.scale(scaling[0], scaling[1], scaling[2]);
264         trans.getValue(fl);
265
266         return fl;
267 }
268
269
270
271 void KX_GameObject::Bucketize()
272 {
273         double* fl = GetOpenGLMatrix();
274
275         for (size_t i=0;i<m_meshes.size();i++)
276                 m_meshes[i]->Bucketize(fl, this, m_bUseObjectColor, m_objectColor);
277 }
278
279
280
281 void KX_GameObject::RemoveMeshes()
282 {
283         double* fl = GetOpenGLMatrix();
284
285         for (size_t i=0;i<m_meshes.size();i++)
286                 m_meshes[i]->RemoveFromBuckets(fl, this);
287
288         //note: meshes can be shared, and are deleted by KX_BlenderSceneConverter
289
290         m_meshes.clear();
291 }
292
293
294
295 void KX_GameObject::UpdateNonDynas()
296 {
297         if (m_pPhysicsController1)
298         {
299                 m_pPhysicsController1->SetSumoTransform(true);
300         }
301 }
302
303
304
305 void KX_GameObject::UpdateTransform()
306 {
307         if (m_pPhysicsController1)
308                 m_pPhysicsController1->SetSumoTransform(false);
309 }
310
311 void KX_GameObject::UpdateTransformFunc(SG_IObject* node, void* gameobj, void* scene)
312 {
313         ((KX_GameObject*)gameobj)->UpdateTransform();
314 }
315
316
317 void KX_GameObject::SetDebugColor(unsigned int bgra)
318 {
319         for (size_t i=0;i<m_meshes.size();i++)
320                 m_meshes[i]->DebugColor(bgra);  
321 }
322
323
324
325 void KX_GameObject::ResetDebugColor()
326 {
327         SetDebugColor(0xff000000);
328 }
329
330
331
332 void KX_GameObject::UpdateIPO(float curframetime,
333                                                           bool recurse,
334                                                           bool ipo_as_force,
335                                                           bool force_local) 
336 {
337
338         // The ipo-actuator needs a sumo reference... this is retrieved (unfortunately)
339         // by the iposgcontr itself...
340 //              ipocontr->SetSumoReference(gameobj->GetSumoScene(), 
341 //                                                                 gameobj->GetSumoObject());
342
343
344         // The ipo has to be treated as a force, and not a displacement!
345         // For this case, we send some settings to the controller. This
346         // may need some caching...
347         if (ipo_as_force) {
348                 SGControllerList::iterator it = GetSGNode()->GetSGControllerList().begin();
349
350                 while (it != GetSGNode()->GetSGControllerList().end()) {
351                         (*it)->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, ipo_as_force);
352                         (*it)->SetOption(SG_Controller::SG_CONTR_IPO_FORCES_ACT_LOCAL, force_local);
353                         it++;
354                 }
355         } 
356
357         // The rest is the 'normal' update procedure.
358         GetSGNode()->SetSimulatedTime(curframetime,recurse);
359         GetSGNode()->UpdateWorldData(curframetime);
360         UpdateTransform();
361 }
362
363
364 /*
365 void KX_GameObject::RegisterSumoObject(class SM_Scene* sumoScene,
366                                                                            DT_SceneHandle solidscene,
367                                                                            class SM_Object* sumoObj,
368                                                                            const char* matname,
369                                                                            bool isDynamic,
370                                                                            bool isActor)
371 {
372         m_bDyna = isDynamic;
373
374         // need easy access, not via 'node' etc.
375         m_pPhysicsController = new KX_PhysicsController(sumoScene,solidscene,sumoObj,isDynamic);
376                                 
377         GetSGNode()->AddSGController(m_pPhysicsController);
378         
379         m_pClient_info->m_type = (isActor ? 1 : 0);
380         m_pClient_info->m_clientobject = this;
381
382         // store materialname in auxinfo, needed for touchsensors
383         m_pClient_info->m_auxilary_info = (matname? (void*)(matname+2) : NULL);
384         m_pPhysicsController->SetObject(this->GetSGNode());
385 }
386 */
387
388 bool
389 KX_GameObject::GetVisible(
390         void
391         )
392 {
393         return m_bVisible;
394 }
395
396 void
397 KX_GameObject::SetVisible(
398         bool v
399         )
400 {
401         m_bVisible = v;
402 }
403
404 // used by Python, and the actuatorshould _not_ be misused by the
405 // scene!
406 void 
407 KX_GameObject::MarkVisible(
408         bool visible
409         )
410 {
411         /* If explicit visibility settings are used, this is
412          * determined on this level. Maybe change this to mesh level
413          * later on? */
414         
415         double* fl = GetOpenGLMatrixPtr()->getPointer();
416         for (size_t i=0;i<m_meshes.size();i++)
417         {
418                 m_meshes[i]->MarkVisible(fl,this,visible,m_bUseObjectColor,m_objectColor);
419         }
420 }
421
422
423 // Always use the flag?
424 void 
425 KX_GameObject::MarkVisible(
426         void
427         )
428 {
429         double* fl = GetOpenGLMatrixPtr()->getPointer();
430         for (size_t i=0;i<m_meshes.size();i++)
431         {
432                 m_meshes[i]->MarkVisible(fl,
433                                          this,
434                                          m_bVisible,
435                                          m_bUseObjectColor,
436                                          m_objectColor
437                         );
438         }
439 }
440
441 void KX_GameObject::addLinearVelocity(const MT_Vector3& lin_vel,bool local)
442 {
443         if (m_pPhysicsController1)
444                 m_pPhysicsController1->SetLinearVelocity(lin_vel + m_pPhysicsController1->GetLinearVelocity(),local);
445 }
446
447
448
449 void KX_GameObject::setLinearVelocity(const MT_Vector3& lin_vel,bool local)
450 {
451         if (m_pPhysicsController1)
452                 m_pPhysicsController1->SetLinearVelocity(lin_vel,local);
453 }
454
455
456
457 void KX_GameObject::setAngularVelocity(const MT_Vector3& ang_vel,bool local)
458 {
459         if (m_pPhysicsController1)
460                 m_pPhysicsController1->SetAngularVelocity(ang_vel,local);
461 }
462
463 void KX_GameObject::ResolveCombinedVelocities(
464         const MT_Vector3 & lin_vel,
465         const MT_Vector3 & ang_vel,
466         bool lin_vel_local,
467         bool ang_vel_local
468 ){
469         if (m_pPhysicsController1)
470         {
471                 m_pPhysicsController1->resolveCombinedVelocities(
472                         lin_vel_local ? NodeGetWorldOrientation() * lin_vel : lin_vel,
473                         ang_vel_local ? NodeGetWorldOrientation() * ang_vel : ang_vel                   
474                 );              
475         }
476 }
477
478
479 void KX_GameObject::SetObjectColor(const MT_Vector4& rgbavec)
480 {
481         m_bUseObjectColor = true;
482         m_objectColor = rgbavec;
483 }
484
485
486
487 MT_Vector3 KX_GameObject::GetLinearVelocity()
488 {
489         MT_Vector3 velocity(0.0,0.0,0.0);
490         
491         if (m_pPhysicsController1)
492         {
493                 velocity = m_pPhysicsController1->GetLinearVelocity();
494         }
495         return velocity;
496         
497 }
498
499
500 // scenegraph node stuff
501
502 void KX_GameObject::NodeSetLocalPosition(const MT_Point3& trans)
503 {
504         if (m_pPhysicsController1)
505         {
506                 m_pPhysicsController1->setPosition(trans);
507         }
508
509         GetSGNode()->SetLocalPosition(trans);
510 }
511
512
513
514 void KX_GameObject::NodeSetLocalOrientation(const MT_Matrix3x3& rot)
515 {
516         if (m_pPhysicsController1)
517         {
518                 m_pPhysicsController1->setOrientation(rot.getRotation());
519         }
520         
521         GetSGNode()->SetLocalOrientation(rot);
522 }
523
524
525
526 void KX_GameObject::NodeSetLocalScale(const MT_Vector3& scale)
527 {
528         if (m_pPhysicsController1)
529         {
530                 m_pPhysicsController1->setScaling(scale);
531         }
532         
533         GetSGNode()->SetLocalScale(scale);
534 }
535
536
537
538 void KX_GameObject::NodeSetRelativeScale(const MT_Vector3& scale)
539 {
540         GetSGNode()->RelativeScale(scale);
541 }
542
543
544
545 void KX_GameObject::NodeUpdateGS(double time,bool bInitiator)
546 {
547         GetSGNode()->UpdateWorldData(time);
548 }
549
550
551
552 const MT_Matrix3x3& KX_GameObject::NodeGetWorldOrientation() const
553 {
554         return GetSGNode()->GetWorldOrientation();
555 }
556
557
558
559 const MT_Vector3& KX_GameObject::NodeGetWorldScaling() const
560 {
561         return GetSGNode()->GetWorldScaling();
562 }
563
564
565
566 const MT_Point3& KX_GameObject::NodeGetWorldPosition() const
567 {
568         return GetSGNode()->GetWorldPosition();
569 }
570
571 /* Suspend/ resume: for the dynamic behaviour, there is a simple
572  * method. For the residual motion, there is not. I wonder what the
573  * correct solution is for Sumo. Remove from the motion-update tree?
574  *
575  * So far, only switch the physics and logic.
576  * */
577
578 void KX_GameObject::Resume(void)
579 {
580         if (m_suspended) {
581                 SCA_IObject::Resume();
582                 GetPhysicsController()->RestoreDynamics();
583
584                 m_suspended = false;
585         }
586 }
587
588 void KX_GameObject::Suspend(void)
589 {
590         if ((!m_ignore_activity_culling) 
591                 && (!m_suspended))  {
592                 SCA_IObject::Suspend();
593                 GetPhysicsController()->SuspendDynamics();
594                 m_suspended = true;
595         }
596 }
597
598
599
600
601 /* ------- python stuff ---------------------------------------------------*/
602
603
604
605
606 PyMethodDef KX_GameObject::Methods[] = {
607         {"setVisible",(PyCFunction) KX_GameObject::sPySetVisible, METH_VARARGS},  
608         {"setPosition", (PyCFunction) KX_GameObject::sPySetPosition, METH_VARARGS},
609         {"getPosition", (PyCFunction) KX_GameObject::sPyGetPosition, METH_VARARGS},
610         {"getOrientation", (PyCFunction) KX_GameObject::sPyGetOrientation, METH_VARARGS},
611         {"setOrientation", (PyCFunction) KX_GameObject::sPySetOrientation, METH_VARARGS},
612         {"getLinearVelocity", (PyCFunction) KX_GameObject::sPyGetLinearVelocity, METH_VARARGS},
613         {"getVelocity", (PyCFunction) KX_GameObject::sPyGetVelocity, METH_VARARGS},
614         {"getMass", (PyCFunction) KX_GameObject::sPyGetMass, METH_VARARGS},
615         {"getReactionForce", (PyCFunction) KX_GameObject::sPyGetReactionForce, METH_VARARGS},
616         {"applyImpulse", (PyCFunction) KX_GameObject::sPyApplyImpulse, METH_VARARGS},
617         {"suspendDynamics", (PyCFunction)KX_GameObject::sPySuspendDynamics,METH_VARARGS},
618         {"restoreDynamics", (PyCFunction)KX_GameObject::sPyRestoreDynamics,METH_VARARGS},
619         {"enableRigidBody", (PyCFunction)KX_GameObject::sPyEnableRigidBody,METH_VARARGS},
620         {"disableRigidBody", (PyCFunction)KX_GameObject::sPyDisableRigidBody,METH_VARARGS},
621         {"getParent", (PyCFunction)KX_GameObject::sPyGetParent,METH_VARARGS},
622         {"getMesh", (PyCFunction)KX_GameObject::sPyGetMesh,METH_VARARGS},
623         {"getPhysicsId", (PyCFunction)KX_GameObject::sPyGetPhysicsId,METH_VARARGS},
624         
625         
626         {NULL,NULL} //Sentinel
627 };
628
629
630
631
632 bool KX_GameObject::ConvertPythonVectorArgs(PyObject* args,
633                                                                                         MT_Vector3& pos,
634                                                                                         MT_Vector3& pos2)
635 {
636         PyObject* pylist;
637         PyObject* pylist2;
638         bool error = (PyArg_ParseTuple(args,"OO",&pylist,&pylist2)) != 0;
639
640         pos = ConvertPythonPylist(pylist);
641         pos2 = ConvertPythonPylist(pylist2);
642                 
643         return error;
644 }
645
646
647
648 PyObject* KX_GameObject::sPySetPosition(PyObject* self,
649                                                                                 PyObject* args,
650                                                                                 PyObject* kwds)
651 {
652         return ((KX_GameObject*) self)->PySetPosition(self, args, kwds);
653 }
654         
655
656
657 PyObject* KX_GameObject::PyGetPosition(PyObject* self,
658                                                                            PyObject* args, 
659                                                                            PyObject* kwds)
660 {
661         return PyObjectFromMT_Point3(NodeGetWorldPosition());
662 }
663
664
665
666 PyTypeObject KX_GameObject::Type = {
667         PyObject_HEAD_INIT(&PyType_Type)
668                 0,
669                 "KX_GameObject",
670                 sizeof(KX_GameObject),
671                 0,
672                 PyDestructor,
673                 0,
674                 __getattr,
675                 __setattr,
676                 0, //&MyPyCompare,
677                 __repr,
678                 0, //&cvalue_as_number,
679                 0,
680                 0,
681                 0,
682                 0
683 };
684
685
686
687 PyParentObject KX_GameObject::Parents[] = {
688         &KX_GameObject::Type,
689                 &SCA_IObject::Type,
690                 &CValue::Type,
691                 NULL
692 };
693
694
695
696
697 PyObject* KX_GameObject::_getattr(const STR_String& attr)
698 {
699         if (m_pPhysicsController1)
700         {
701                 if (attr == "mass")
702                         return PyFloat_FromDouble(GetPhysicsController()->GetMass());
703         }
704
705         if (attr == "parent")
706         {       
707                 KX_GameObject* parent = GetParent();
708                 if (parent)
709                         return parent;
710                 Py_Return;
711         }
712         
713         if (attr == "visible")
714                 return PyInt_FromLong(m_bVisible);
715         
716         if (attr == "position")
717                 return PyObjectFromMT_Point3(NodeGetWorldPosition());
718         
719         if (attr == "orientation")
720                 return PyObjectFromMT_Matrix3x3(NodeGetWorldOrientation());
721         
722         if (attr == "scaling")
723                 return PyObjectFromMT_Vector3(NodeGetWorldScaling());
724         
725         _getattr_up(SCA_IObject);
726 }
727
728 int KX_GameObject::_setattr(const STR_String& attr, PyObject *value)    // _setattr method
729 {
730         if (attr == "mass")
731                 return 1;
732         
733         if (attr == "parent")
734                 return 1;
735                 
736         if (PyInt_Check(value))
737         {
738                 int val = PyInt_AsLong(value);
739                 if (attr == "visible")
740                 {
741                         SetVisible(val != 0);
742                         return 0;
743                 }
744         }
745         
746         if (PySequence_Check(value))
747         {
748                 if (attr == "orientation")
749                 {
750                         MT_Matrix3x3 rot;
751                         if (PyObject_IsMT_Matrix(value, 3))
752                         {
753                                 rot = MT_Matrix3x3FromPyObject(value);
754                                 NodeSetLocalOrientation(rot);
755                                 return 0;
756                         }
757                         
758                         if (PySequence_Size(value) == 4)
759                         {
760                                 MT_Quaternion qrot = MT_QuaternionFromPyList(value);
761                                 rot.setRotation(qrot);
762                                 NodeSetLocalOrientation(rot);
763                                 return 0;
764                         }
765                         
766                         if (PySequence_Size(value) == 3)
767                         {
768                                 MT_Vector3 erot = MT_Vector3FromPyList(value);
769                                 rot.setEuler(erot);
770                                 NodeSetLocalOrientation(rot);
771                                 return 0;
772                         }
773                         
774                         return 1;
775                 }
776                 
777                 if (attr == "position")
778                 {
779                         MT_Point3 pos(MT_Point3FromPyList(value));
780                         NodeSetLocalPosition(pos);
781                         return 0;
782                 }
783                 
784                 if (attr == "scaling")
785                 {
786                         MT_Vector3 scale(MT_Vector3FromPyList(value));
787                         NodeSetLocalScale(scale);
788                         return 0;
789                 }
790         }
791         
792         return SCA_IObject::_setattr(attr, value);
793 }
794
795
796 PyObject* KX_GameObject::PyGetLinearVelocity(PyObject* self, 
797                                                                                          PyObject* args, 
798                                                                                          PyObject* kwds)
799 {
800         // only can get the velocity if we have a physics object connected to us...
801         return PyObjectFromMT_Vector3( GetLinearVelocity());
802 }
803
804
805
806 PyObject* KX_GameObject::PySetVisible(PyObject* self,
807                                                                           PyObject* args,
808                                                                           PyObject* kwds)
809 {
810         int visible = 1;
811         
812         if (PyArg_ParseTuple(args,"i",&visible))
813         {
814                 MarkVisible(visible!=0);
815                 m_bVisible = (visible!=0);
816         }
817         else
818         {
819                 return NULL;         
820         }
821         Py_Return;
822         
823 }
824
825
826
827 PyObject* KX_GameObject::PyGetVelocity(PyObject* self, 
828                                                                            PyObject* args, 
829                                                                            PyObject* kwds)
830 {
831         // only can get the velocity if we have a physics object connected to us...
832         MT_Vector3 velocity(0.0,0.0,0.0);
833         MT_Point3 point(0.0,0.0,0.0);
834         
835         
836         PyObject* pypos = NULL;
837         if (PyArg_ParseTuple(args, "|O", &pypos))
838         {
839                 if (pypos)
840                         point = MT_Point3FromPyList(pypos);
841         }
842         
843         if (m_pPhysicsController1)
844         {
845                 velocity = m_pPhysicsController1->GetVelocity(point);
846         }
847         
848         return PyObjectFromMT_Vector3(velocity);
849 }
850
851
852
853 PyObject* KX_GameObject::PyGetMass(PyObject* self, 
854                                                                    PyObject* args, 
855                                                                    PyObject* kwds)
856 {
857         PyObject* pymass = NULL;
858         
859         float mass = GetPhysicsController()->GetMass();
860         pymass = PyFloat_FromDouble(mass);
861
862         if (pymass)
863                 return pymass;
864         
865         Py_Return;
866 }
867
868
869
870 PyObject* KX_GameObject::PyGetReactionForce(PyObject* self, 
871                                                                                         PyObject* args, 
872                                                                                         PyObject* kwds)
873 {
874         // only can get the velocity if we have a physics object connected to us...
875         return PyObjectFromMT_Vector3(GetPhysicsController()->getReactionForce());
876 }
877
878
879
880 PyObject* KX_GameObject::PyEnableRigidBody(PyObject* self, 
881                                                                                    PyObject* args, 
882                                                                                    PyObject* kwds)
883 {
884         
885         GetPhysicsController()->setRigidBody(true);
886
887         Py_Return;
888 }
889
890
891
892 PyObject* KX_GameObject::PyDisableRigidBody(PyObject* self, 
893                                                                                         PyObject* args, 
894                                                                                         PyObject* kwds)
895 {
896         GetPhysicsController()->setRigidBody(false);
897
898         Py_Return;
899 }
900
901
902
903 PyObject* KX_GameObject::PyGetParent(PyObject* self, 
904                                                                          PyObject* args, 
905                                                                          PyObject* kwds)
906 {
907         KX_GameObject* parent = this->GetParent();
908         if (parent)
909                 return parent;
910         Py_Return;
911 }
912
913
914
915 PyObject* KX_GameObject::PyGetMesh(PyObject* self, 
916                                                                    PyObject* args, 
917                                                                    PyObject* kwds)
918 {
919         int mesh = 0;
920         
921         if (PyArg_ParseTuple(args, "|i", &mesh))
922         {
923                 if (mesh < m_meshes.size() && mesh >= 0)
924                 {
925                         KX_MeshProxy* meshproxy = new KX_MeshProxy(m_meshes[mesh]);
926                         return meshproxy;
927                 }
928         }
929         Py_Return;
930 }
931
932
933
934 PyObject* KX_GameObject::PyApplyImpulse(PyObject* self, 
935                                                                                 PyObject* args, 
936                                                                                 PyObject* kwds)
937 {
938         
939         
940         PyObject* pyattach;
941         PyObject* pyimpulse;
942         
943         if (PyArg_ParseTuple(args, "OO", &pyattach, &pyimpulse))
944         {
945                 MT_Point3  attach(MT_Point3FromPyList(pyattach));
946                 MT_Vector3 impulse(MT_Vector3FromPyList(pyimpulse));
947         
948                 if (m_pPhysicsController1)
949                 {
950                         m_pPhysicsController1->applyImpulse(attach, impulse);
951                 }
952
953         }
954         
955         Py_Return;
956 }
957
958
959
960 PyObject* KX_GameObject::PySuspendDynamics(PyObject* self, 
961                                                                                    PyObject* args, 
962                                                                                    PyObject* kwds)
963 {
964         if (m_bSuspendDynamics)
965         {
966                 Py_Return;
967         }
968         
969         if (m_pPhysicsController1)
970         {
971                 m_pPhysicsController1->SuspendDynamics();
972         }
973         m_bSuspendDynamics = true;
974         
975         Py_Return;
976 }
977
978
979
980 PyObject* KX_GameObject::PyRestoreDynamics(PyObject* self, 
981                                                                                    PyObject* args, 
982                                                                                    PyObject* kwds)
983 {
984         
985         if (!m_bSuspendDynamics)
986         {
987                 Py_Return;
988         }
989         
990         if (m_pPhysicsController1)
991         {
992                 m_pPhysicsController1->RestoreDynamics();
993         }
994         m_bSuspendDynamics = false;
995         
996         Py_Return;
997 }
998
999
1000
1001 PyObject* KX_GameObject::PyGetOrientation(PyObject* self,
1002                                                                                   PyObject* args,
1003                                                                                   PyObject* kwds) //keywords
1004 {
1005         const MT_Matrix3x3& orient = NodeGetWorldOrientation();
1006         return PyObjectFromMT_Matrix3x3(orient);
1007 }
1008
1009
1010
1011 PyObject* KX_GameObject::PySetOrientation(PyObject* self, 
1012                                                                                   PyObject* args, 
1013                                                                                   PyObject* kwds)
1014 {
1015         PyObject* pylist;
1016         bool    error = false;
1017         int row,col;
1018         
1019         PyArg_ParseTuple(args,"O",&pylist);
1020         
1021         MT_Matrix3x3 matrix;
1022         if (PyObject_IsMT_Matrix(pylist, 3))
1023         {
1024                 matrix = MT_Matrix3x3FromPyObject(pylist);
1025                         
1026                 if (!PyErr_Occurred())
1027                 {
1028                         NodeSetLocalOrientation(matrix);
1029                 }
1030                 
1031                 Py_Return;
1032         }
1033         
1034         MT_Quaternion quat = MT_QuaternionFromPyList(pylist);
1035         if (!PyErr_Occurred())
1036         {
1037                 matrix.setRotation(quat);
1038                 NodeSetLocalOrientation(matrix);
1039         }
1040         
1041         Py_Return;
1042 }
1043
1044
1045
1046 PyObject* KX_GameObject::PySetPosition(PyObject* self, 
1047                                                                            PyObject* args, 
1048                                                                            PyObject* kwds)
1049 {
1050         // make a general function for this, it's needed many times
1051         PyObject *pypos;
1052         if (PyArg_ParseTuple(args, "O", &pypos))
1053         {
1054                 MT_Point3 pos = MT_Point3FromPyList(pypos);
1055                 NodeSetLocalPosition(pos);
1056         }
1057         Py_Return;
1058 }
1059
1060 PyObject* KX_GameObject::PyGetPhysicsId(PyObject* self,
1061                                                                                            PyObject* args,
1062                                                                                            PyObject* kwds)
1063 {
1064         KX_IPhysicsController* ctrl = GetPhysicsController();
1065         int physid=0;
1066         if (ctrl)
1067         {
1068                 physid= (int)ctrl->GetUserData();
1069         }
1070         return PyInt_FromLong(physid);
1071 }
1072
1073 /* --------------------------------------------------------------------- 
1074  * Some stuff taken from the header
1075  * --------------------------------------------------------------------- */
1076 void KX_GameObject::Relink(GEN_Map<GEN_HashedPtr, void*> *map_parameter)        
1077 {
1078         /* intentionally empty ? */
1079 }
1080