svn merge -r 16060:16222 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / gameengine / Ketsji / KX_GameObject.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Game object wrapper
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #if defined(_WIN64)
36 typedef unsigned __int64 uint_ptr;
37 #else
38 typedef unsigned long uint_ptr;
39 #endif
40
41 #ifdef WIN32
42 // This warning tells us about truncation of __long__ stl-generated names.
43 // It can occasionally cause DevStudio to have internal compiler warnings.
44 #pragma warning( disable : 4786 )     
45 #endif
46
47
48 #define KX_INERTIA_INFINITE 10000
49 #include "RAS_IPolygonMaterial.h"
50 #include "KX_BlenderMaterial.h"
51 #include "KX_GameObject.h"
52 #include "RAS_MeshObject.h"
53 #include "KX_MeshProxy.h"
54 #include <stdio.h> // printf
55 #include "SG_Controller.h"
56 #include "KX_IPhysicsController.h"
57 #include "SG_Node.h"
58 #include "SG_Controller.h"
59 #include "KX_ClientObjectInfo.h"
60 #include "RAS_BucketManager.h"
61 #include "KX_RayCast.h"
62 #include "KX_PythonInit.h"
63 #include "KX_PyMath.h"
64 #include "SCA_IActuator.h"
65 #include "SCA_ISensor.h"
66
67 // This file defines relationships between parents and children
68 // in the game engine.
69
70 #include "KX_SG_NodeRelationships.h"
71
72 KX_GameObject::KX_GameObject(
73         void* sgReplicationInfo,
74         SG_Callbacks callbacks,
75         PyTypeObject* T
76 ) : 
77         SCA_IObject(T),
78         m_bDyna(false),
79         m_layer(0),
80         m_pBlenderObject(NULL),
81         m_bSuspendDynamics(false),
82         m_bUseObjectColor(false),
83         m_bIsNegativeScaling(false),
84         m_bVisible(true),
85         m_pPhysicsController1(NULL),
86         m_pPhysicsEnvironment(NULL),
87         m_pHitObject(NULL),
88         m_isDeformable(false)
89 {
90         m_ignore_activity_culling = false;
91         m_pClient_info = new KX_ClientObjectInfo(this, KX_ClientObjectInfo::ACTOR);
92         m_pSGNode = new SG_Node(this,sgReplicationInfo,callbacks);
93
94         // define the relationship between this node and it's parent.
95         
96         KX_NormalParentRelation * parent_relation = 
97                 KX_NormalParentRelation::New();
98         m_pSGNode->SetParentRelation(parent_relation);
99 };
100
101
102 KX_GameObject::~KX_GameObject()
103 {
104         // is this delete somewhere ?
105         //if (m_sumoObj)
106         //      delete m_sumoObj;
107         delete m_pClient_info;
108         //if (m_pSGNode)
109         //      delete m_pSGNode;
110         if (m_pSGNode)
111         {
112                 // must go through controllers and make sure they will not use us anymore
113                 // This is important for KX_BulletPhysicsControllers that unregister themselves
114                 // from the object when they are deleted.
115                 SGControllerList::iterator contit;
116                 SGControllerList& controllers = m_pSGNode->GetSGControllerList();
117                 for (contit = controllers.begin();contit!=controllers.end();++contit)
118                 {
119                         (*contit)->ClearObject();
120                 }
121                 m_pSGNode->SetSGClientObject(NULL);
122         }
123 }
124
125
126
127 CValue* KX_GameObject:: Calc(VALUE_OPERATOR op, CValue *val) 
128 {
129         return NULL;
130 }
131
132
133
134 CValue* KX_GameObject::CalcFinal(VALUE_DATA_TYPE dtype, VALUE_OPERATOR op, CValue *val)
135 {
136         return NULL;
137 }
138
139
140
141 const STR_String & KX_GameObject::GetText()
142 {
143         return m_text;
144 }
145
146
147
148 float KX_GameObject::GetNumber()
149 {
150         return 0;
151 }
152
153
154
155 STR_String KX_GameObject::GetName()
156 {
157         return m_name;
158 }
159
160
161
162 void KX_GameObject::SetName(STR_String name)
163 {
164         m_name = name;
165
166 };                                                              // Set the name of the value
167
168
169
170 void KX_GameObject::ReplicaSetName(STR_String name)
171 {
172 }
173
174
175
176
177
178
179 KX_IPhysicsController* KX_GameObject::GetPhysicsController()
180 {
181         return m_pPhysicsController1;
182 }
183
184
185
186
187
188 KX_GameObject* KX_GameObject::GetParent()
189 {
190         KX_GameObject* result = NULL;
191         SG_Node* node = m_pSGNode;
192         
193         while (node && !result)
194         {
195                 node = node->GetSGParent();
196                 if (node)
197                         result = (KX_GameObject*)node->GetSGClientObject();
198         }
199         
200         if (result)
201                 result->AddRef();
202
203         return result;
204         
205 }
206
207 void KX_GameObject::SetParent(KX_Scene *scene, KX_GameObject* obj)
208 {
209         if (obj && GetSGNode()->GetSGParent() != obj->GetSGNode())
210         {
211                 // Make sure the objects have some scale
212                 MT_Vector3 scale1 = NodeGetWorldScaling();
213                 MT_Vector3 scale2 = obj->NodeGetWorldScaling();
214                 if (fabs(scale2[0]) < FLT_EPSILON || 
215                         fabs(scale2[1]) < FLT_EPSILON || 
216                         fabs(scale2[2]) < FLT_EPSILON || 
217                         fabs(scale1[0]) < FLT_EPSILON || 
218                         fabs(scale1[1]) < FLT_EPSILON || 
219                         fabs(scale1[2]) < FLT_EPSILON) { return; }
220
221                 // Remove us from our old parent and set our new parent
222                 RemoveParent(scene);
223                 obj->GetSGNode()->AddChild(GetSGNode());
224
225                 if (m_pPhysicsController1) 
226                 {
227                         m_pPhysicsController1->SuspendDynamics(true);
228                 }
229                 // Set us to our new scale, position, and orientation
230                 scale1[0] = scale1[0]/scale2[0];
231                 scale1[1] = scale1[1]/scale2[1];
232                 scale1[2] = scale1[2]/scale2[2];
233                 MT_Matrix3x3 invori = obj->NodeGetWorldOrientation().inverse();
234                 MT_Vector3 newpos = invori*(NodeGetWorldPosition()-obj->NodeGetWorldPosition())*scale1;
235
236                 NodeSetLocalScale(scale1);
237                 NodeSetLocalPosition(MT_Point3(newpos[0],newpos[1],newpos[2]));
238                 NodeSetLocalOrientation(invori*NodeGetWorldOrientation());
239                 NodeUpdateGS(0.f,true);
240                 // object will now be a child, it must be removed from the parent list
241                 CListValue* rootlist = scene->GetRootParentList();
242                 if (rootlist->RemoveValue(this))
243                         // the object was in parent list, decrement ref count as it's now removed
244                         Release();
245         }
246 }
247
248 void KX_GameObject::RemoveParent(KX_Scene *scene)
249 {
250         if (GetSGNode()->GetSGParent())
251         {
252                 // Set us to the right spot 
253                 GetSGNode()->SetLocalScale(GetSGNode()->GetWorldScaling());
254                 GetSGNode()->SetLocalOrientation(GetSGNode()->GetWorldOrientation());
255                 GetSGNode()->SetLocalPosition(GetSGNode()->GetWorldPosition());
256
257                 // Remove us from our parent
258                 GetSGNode()->DisconnectFromParent();
259                 NodeUpdateGS(0.f,true);
260                 // the object is now a root object, add it to the parentlist
261                 CListValue* rootlist = scene->GetRootParentList();
262                 if (!rootlist->SearchValue(this))
263                         // object was not in root list, add it now and increment ref count
264                         rootlist->Add(AddRef());
265                 if (m_pPhysicsController1) 
266                 {
267                         m_pPhysicsController1->RestoreDynamics();
268                 }
269         }
270 }
271
272 void KX_GameObject::ProcessReplica(KX_GameObject* replica)
273 {
274         replica->m_pPhysicsController1 = NULL;
275         replica->m_pSGNode = NULL;
276         replica->m_pClient_info = new KX_ClientObjectInfo(*m_pClient_info);
277         replica->m_pClient_info->m_gameobject = replica;
278         replica->m_state = 0;
279 }
280
281
282
283 CValue* KX_GameObject::GetReplica()
284 {
285         KX_GameObject* replica = new KX_GameObject(*this);
286         
287         // this will copy properties and so on...
288         CValue::AddDataToReplica(replica);
289         ProcessReplica(replica);
290         
291         return replica;
292 }
293
294
295
296 void KX_GameObject::ApplyForce(const MT_Vector3& force,bool local)
297 {
298         if (m_pPhysicsController1)
299                 m_pPhysicsController1->ApplyForce(force,local);
300 }
301
302
303
304 void KX_GameObject::ApplyTorque(const MT_Vector3& torque,bool local)
305 {
306         if (m_pPhysicsController1)
307                 m_pPhysicsController1->ApplyTorque(torque,local);
308 }
309
310
311
312 void KX_GameObject::ApplyMovement(const MT_Vector3& dloc,bool local)
313 {
314         if (m_pPhysicsController1) // (IsDynamic())
315         {
316                 m_pPhysicsController1->RelativeTranslate(dloc,local);
317         }
318         GetSGNode()->RelativeTranslate(dloc,GetSGNode()->GetSGParent(),local);
319 }
320
321
322
323 void KX_GameObject::ApplyRotation(const MT_Vector3& drot,bool local)
324 {
325         MT_Matrix3x3 rotmat(drot);
326
327         GetSGNode()->RelativeRotate(rotmat,local);
328
329         if (m_pPhysicsController1) { // (IsDynamic())
330                 m_pPhysicsController1->RelativeRotate(rotmat,local); 
331         }
332 }
333
334
335
336 /**
337 GetOpenGL Matrix, returns an OpenGL 'compatible' matrix
338 */
339 double* KX_GameObject::GetOpenGLMatrix()
340 {
341         // todo: optimize and only update if necessary
342         double* fl = m_OpenGL_4x4Matrix.getPointer();
343         MT_Transform trans;
344         
345         trans.setOrigin(GetSGNode()->GetWorldPosition());
346         trans.setBasis(GetSGNode()->GetWorldOrientation());
347         
348         MT_Vector3 scaling = GetSGNode()->GetWorldScaling();
349         m_bIsNegativeScaling = ((scaling[0] < 0.0) ^ (scaling[1] < 0.0) ^ (scaling[2] < 0.0)) ? true : false;
350         trans.scale(scaling[0], scaling[1], scaling[2]);
351         trans.getValue(fl);
352
353         return fl;
354 }
355
356
357
358 void KX_GameObject::Bucketize()
359 {
360         double* fl = GetOpenGLMatrix();
361
362         for (size_t i=0;i<m_meshes.size();i++)
363                 m_meshes[i]->Bucketize(fl, this, m_bUseObjectColor, m_objectColor);
364 }
365
366
367
368 void KX_GameObject::RemoveMeshes()
369 {
370         double* fl = GetOpenGLMatrix();
371
372         for (size_t i=0;i<m_meshes.size();i++)
373                 m_meshes[i]->RemoveFromBuckets(fl, this);
374
375         //note: meshes can be shared, and are deleted by KX_BlenderSceneConverter
376
377         m_meshes.clear();
378 }
379
380
381
382 void KX_GameObject::UpdateNonDynas()
383 {
384         if (m_pPhysicsController1)
385         {
386                 m_pPhysicsController1->SetSumoTransform(true);
387         }
388 }
389
390
391
392 void KX_GameObject::UpdateTransform()
393 {
394         if (m_pPhysicsController1)
395                 m_pPhysicsController1->SetSumoTransform(false);
396 }
397
398 void KX_GameObject::UpdateTransformFunc(SG_IObject* node, void* gameobj, void* scene)
399 {
400         ((KX_GameObject*)gameobj)->UpdateTransform();
401 }
402
403
404 void KX_GameObject::SetDebugColor(unsigned int bgra)
405 {
406         for (size_t i=0;i<m_meshes.size();i++)
407                 m_meshes[i]->DebugColor(bgra);  
408 }
409
410
411
412 void KX_GameObject::ResetDebugColor()
413 {
414         SetDebugColor(0xff000000);
415 }
416
417 void KX_GameObject::InitIPO(bool ipo_as_force,
418                                                         bool ipo_add,
419                                                         bool ipo_local)
420 {
421         SGControllerList::iterator it = GetSGNode()->GetSGControllerList().begin();
422
423         while (it != GetSGNode()->GetSGControllerList().end()) {
424                 (*it)->SetOption(SG_Controller::SG_CONTR_IPO_RESET, true);
425                 (*it)->SetOption(SG_Controller::SG_CONTR_IPO_IPO_AS_FORCE, ipo_as_force);
426                 (*it)->SetOption(SG_Controller::SG_CONTR_IPO_IPO_ADD, ipo_add);
427                 (*it)->SetOption(SG_Controller::SG_CONTR_IPO_LOCAL, ipo_local);
428                 it++;
429         }
430
431
432 void KX_GameObject::UpdateIPO(float curframetime,
433                                                           bool recurse) 
434 {
435         // just the 'normal' update procedure.
436         GetSGNode()->SetSimulatedTime(curframetime,recurse);
437         GetSGNode()->UpdateWorldData(curframetime);
438         UpdateTransform();
439 }
440
441 // IPO update
442 void 
443 KX_GameObject::UpdateMaterialData(
444                 dword matname_hash,
445                 MT_Vector4 rgba,
446                 MT_Vector3 specrgb,
447                 MT_Scalar hard,
448                 MT_Scalar spec,
449                 MT_Scalar ref,
450                 MT_Scalar emit,
451                 MT_Scalar alpha
452
453         )
454 {
455         int mesh = 0;
456
457         if (((unsigned int)mesh < m_meshes.size()) && mesh >= 0) {
458                 RAS_MaterialBucket::Set::iterator mit = m_meshes[mesh]->GetFirstMaterial();
459                 for(; mit != m_meshes[mesh]->GetLastMaterial(); ++mit)
460                 {
461                         RAS_IPolyMaterial* poly = (*mit)->GetPolyMaterial();
462                         if(poly->GetFlag() & RAS_BLENDERMAT)
463                         {
464                                 KX_BlenderMaterial *m =  static_cast<KX_BlenderMaterial*>(poly);
465                                 
466                                 if (matname_hash == NULL)
467                                 {
468                                         m->UpdateIPO(rgba, specrgb,hard,spec,ref,emit, alpha);
469                                         // if mesh has only one material attached to it then use original hack with no need to edit vertices (better performance)
470                                         if(!(poly->GetFlag() & RAS_BLENDERGLSL))
471                                                 SetObjectColor(rgba);
472                                 }
473                                 else
474                                 {
475                                         if (matname_hash == poly->GetMaterialNameHash())
476                                         {
477                                                 m->UpdateIPO(rgba, specrgb,hard,spec,ref,emit, alpha);
478                                                 m_meshes[mesh]->SetVertexColor(poly,rgba);
479                                                 
480                                                 // no break here, because one blender material can be split into several game engine materials
481                                                 // (e.g. one uvsphere material is split into one material at poles with ras_mode TRIANGLE and one material for the body
482                                                 // if here was a break then would miss some vertices if material was split
483                                         }
484                                 }
485                         }
486                 }
487         }
488 }
489 bool
490 KX_GameObject::GetVisible(
491         void
492         )
493 {
494         return m_bVisible;
495 }
496
497 void
498 KX_GameObject::SetVisible(
499         bool v
500         )
501 {
502         m_bVisible = v;
503 }
504
505 void
506 KX_GameObject::SetLayer(
507         int l
508         )
509 {
510         m_layer = l;
511 }
512
513 int
514 KX_GameObject::GetLayer(
515         void
516         )
517 {
518         return m_layer;
519 }
520
521 // used by Python, and the actuatorshould _not_ be misused by the
522 // scene!
523 void 
524 KX_GameObject::MarkVisible(
525         bool visible
526         )
527 {
528         /* If explicit visibility settings are used, this is
529          * determined on this level. Maybe change this to mesh level
530          * later on? */
531         
532         double* fl = GetOpenGLMatrixPtr()->getPointer();
533         for (size_t i=0;i<m_meshes.size();i++)
534         {
535                 m_meshes[i]->MarkVisible(fl,this,visible,m_bUseObjectColor,m_objectColor);
536         }
537 }
538
539
540 // Always use the flag?
541 void 
542 KX_GameObject::MarkVisible(
543         void
544         )
545 {
546         double* fl = GetOpenGLMatrixPtr()->getPointer();
547         for (size_t i=0;i<m_meshes.size();i++)
548         {
549                 m_meshes[i]->MarkVisible(fl,
550                                          this,
551                                          m_bVisible,
552                                          m_bUseObjectColor,
553                                          m_objectColor
554                         );
555         }
556 }
557
558
559 void KX_GameObject::addLinearVelocity(const MT_Vector3& lin_vel,bool local)
560 {
561         if (m_pPhysicsController1)
562                 m_pPhysicsController1->SetLinearVelocity(lin_vel + m_pPhysicsController1->GetLinearVelocity(),local);
563 }
564
565
566
567 void KX_GameObject::setLinearVelocity(const MT_Vector3& lin_vel,bool local)
568 {
569         if (m_pPhysicsController1)
570                 m_pPhysicsController1->SetLinearVelocity(lin_vel,local);
571 }
572
573
574
575 void KX_GameObject::setAngularVelocity(const MT_Vector3& ang_vel,bool local)
576 {
577         if (m_pPhysicsController1)
578                 m_pPhysicsController1->SetAngularVelocity(ang_vel,local);
579 }
580
581 void KX_GameObject::ResolveCombinedVelocities(
582         const MT_Vector3 & lin_vel,
583         const MT_Vector3 & ang_vel,
584         bool lin_vel_local,
585         bool ang_vel_local
586 ){
587         if (m_pPhysicsController1)
588         {
589
590                 MT_Vector3 lv = lin_vel_local ? NodeGetWorldOrientation() * lin_vel : lin_vel;
591                 MT_Vector3 av = ang_vel_local ? NodeGetWorldOrientation() * ang_vel : ang_vel;
592                 m_pPhysicsController1->resolveCombinedVelocities(
593                         lv.x(),lv.y(),lv.z(),av.x(),av.y(),av.z());
594         }
595 }
596
597
598 void KX_GameObject::SetObjectColor(const MT_Vector4& rgbavec)
599 {
600         m_bUseObjectColor = true;
601         m_objectColor = rgbavec;
602 }
603
604 void KX_GameObject::AlignAxisToVect(const MT_Vector3& dir, int axis, float fac)
605 {
606         MT_Matrix3x3 orimat;
607         MT_Vector3 vect,ori,z,x,y;
608         MT_Scalar len;
609
610         vect = dir;
611         len = vect.length();
612         if (MT_fuzzyZero(len))
613         {
614                 cout << "alignAxisToVect() Error: Null vector!\n";
615                 return;
616         }
617         
618         if (fac<=0.0) {
619                 return;
620         }
621         
622         // normalize
623         vect /= len;
624         orimat = GetSGNode()->GetWorldOrientation();
625         switch (axis)
626         {       
627                 case 0: //x axis
628                         ori = MT_Vector3(orimat[0][2], orimat[1][2], orimat[2][2]); //pivot axis
629                         if (MT_abs(vect.dot(ori)) > 1.0-3.0*MT_EPSILON) //is the vector paralell to the pivot?
630                                 ori = MT_Vector3(orimat[0][1], orimat[1][1], orimat[2][1]); //change the pivot!
631                         if (fac == 1.0) {
632                                 x = vect;
633                         } else {
634                                 x = (vect * fac) + ((orimat * MT_Vector3(1.0, 0.0, 0.0)) * (1-fac));
635                                 len = x.length();
636                                 if (MT_fuzzyZero(len)) x = vect;
637                                 else x /= len;
638                         }
639                         y = ori.cross(x);
640                         z = x.cross(y);
641                         break;
642                 case 1: //y axis
643                         ori = MT_Vector3(orimat[0][0], orimat[1][0], orimat[2][0]);
644                         if (MT_abs(vect.dot(ori)) > 1.0-3.0*MT_EPSILON)
645                                 ori = MT_Vector3(orimat[0][2], orimat[1][2], orimat[2][2]);
646                         if (fac == 1.0) {
647                                 y = vect;
648                         } else {
649                                 y = (vect * fac) + ((orimat * MT_Vector3(0.0, 1.0, 0.0)) * (1-fac));
650                                 len = y.length();
651                                 if (MT_fuzzyZero(len)) y = vect;
652                                 else y /= len;
653                         }
654                         z = ori.cross(y);
655                         x = y.cross(z);
656                         break;
657                 case 2: //z axis
658                         ori = MT_Vector3(orimat[0][1], orimat[1][1], orimat[2][1]);
659                         if (MT_abs(vect.dot(ori)) > 1.0-3.0*MT_EPSILON)
660                                 ori = MT_Vector3(orimat[0][0], orimat[1][0], orimat[2][0]);
661                         if (fac == 1.0) {
662                                 z = vect;
663                         } else {
664                                 z = (vect * fac) + ((orimat * MT_Vector3(0.0, 0.0, 1.0)) * (1-fac));
665                                 len = z.length();
666                                 if (MT_fuzzyZero(len)) z = vect;
667                                 else z /= len;
668                         }
669                         x = ori.cross(z);
670                         y = z.cross(x);
671                         break;
672                 default: //wrong input?
673                         cout << "alignAxisToVect(): Wrong axis '" << axis <<"'\n";
674                         return;
675         }
676         x.normalize(); //normalize the vectors
677         y.normalize();
678         z.normalize();
679         orimat = MT_Matrix3x3(  x[0],y[0],z[0],
680                                                         x[1],y[1],z[1],
681                                                         x[2],y[2],z[2]);
682         if (GetSGNode()->GetSGParent() != NULL)
683         {
684                 // the object is a child, adapt its local orientation so that 
685                 // the global orientation is aligned as we want.
686                 MT_Matrix3x3 invori = GetSGNode()->GetSGParent()->GetWorldOrientation().inverse();
687                 NodeSetLocalOrientation(invori*orimat);
688         }
689         else
690                 NodeSetLocalOrientation(orimat);
691 }
692
693 MT_Scalar KX_GameObject::GetMass()
694 {
695         if (m_pPhysicsController1)
696         {
697                 return m_pPhysicsController1->GetMass();
698         }
699         return 0.0;
700 }
701
702 MT_Vector3 KX_GameObject::GetLinearVelocity(bool local)
703 {
704         MT_Vector3 velocity(0.0,0.0,0.0), locvel;
705         MT_Matrix3x3 ori;
706         if (m_pPhysicsController1)
707         {
708                 velocity = m_pPhysicsController1->GetLinearVelocity();
709                 
710                 if (local)
711                 {
712                         ori = GetSGNode()->GetWorldOrientation();
713                         
714                         locvel = velocity * ori;
715                         return locvel;
716                 }
717         }
718         return velocity;        
719 }
720
721 MT_Vector3 KX_GameObject::GetAngularVelocity(bool local)
722 {
723         MT_Vector3 velocity(0.0,0.0,0.0), locvel;
724         MT_Matrix3x3 ori;
725         if (m_pPhysicsController1)
726         {
727                 velocity = m_pPhysicsController1->GetAngularVelocity();
728                 
729                 if (local)
730                 {
731                         ori = GetSGNode()->GetWorldOrientation();
732                         
733                         locvel = velocity * ori;
734                         return locvel;
735                 }
736         }
737         return velocity;        
738 }
739
740
741
742 // scenegraph node stuff
743
744 void KX_GameObject::NodeSetLocalPosition(const MT_Point3& trans)
745 {
746         if (m_pPhysicsController1 && (!GetSGNode() || !GetSGNode()->GetSGParent()))
747         {
748                 // don't update physic controller if the object is a child:
749                 // 1) the transformation will not be right
750                 // 2) in this case, the physic controller is necessarily a static object
751                 //    that is updated from the normal kinematic synchronization
752                 m_pPhysicsController1->setPosition(trans);
753         }
754
755         if (GetSGNode())
756                 GetSGNode()->SetLocalPosition(trans);
757 }
758
759
760
761 void KX_GameObject::NodeSetLocalOrientation(const MT_Matrix3x3& rot)
762 {
763         if (m_pPhysicsController1 && (!GetSGNode() || !GetSGNode()->GetSGParent()))
764         {
765                 // see note above
766                 m_pPhysicsController1->setOrientation(rot);
767         }
768         if (GetSGNode())
769                 GetSGNode()->SetLocalOrientation(rot);
770 }
771
772
773
774 void KX_GameObject::NodeSetLocalScale(const MT_Vector3& scale)
775 {
776         if (m_pPhysicsController1 && (!GetSGNode() || !GetSGNode()->GetSGParent()))
777         {
778                 // see note above
779                 m_pPhysicsController1->setScaling(scale);
780         }
781         
782         if (GetSGNode())
783                 GetSGNode()->SetLocalScale(scale);
784 }
785
786
787
788 void KX_GameObject::NodeSetRelativeScale(const MT_Vector3& scale)
789 {
790         if (GetSGNode())
791                 GetSGNode()->RelativeScale(scale);
792 }
793
794 void KX_GameObject::NodeSetWorldPosition(const MT_Point3& trans)
795 {
796         SG_Node* parent = m_pSGNode->GetSGParent();
797         if (parent != NULL)
798         {
799                 // Make sure the objects have some scale
800                 MT_Vector3 scale = parent->GetWorldScaling();
801                 if (fabs(scale[0]) < FLT_EPSILON || 
802                         fabs(scale[1]) < FLT_EPSILON || 
803                         fabs(scale[2]) < FLT_EPSILON)
804                 { 
805                         return; 
806                 }
807                 scale[0] = 1.0/scale[0];
808                 scale[1] = 1.0/scale[1];
809                 scale[2] = 1.0/scale[2];
810                 MT_Matrix3x3 invori = parent->GetWorldOrientation().inverse();
811                 MT_Vector3 newpos = invori*(trans-parent->GetWorldPosition())*scale;
812                 NodeSetLocalPosition(MT_Point3(newpos[0],newpos[1],newpos[2]));
813         }
814         else 
815         {
816                 NodeSetLocalPosition(trans);
817         }
818 }
819
820
821 void KX_GameObject::NodeUpdateGS(double time,bool bInitiator)
822 {
823         if (GetSGNode())
824                 GetSGNode()->UpdateWorldData(time);
825 }
826
827
828
829 const MT_Matrix3x3& KX_GameObject::NodeGetWorldOrientation() const
830 {
831         return GetSGNode()->GetWorldOrientation();
832 }
833
834
835
836 const MT_Vector3& KX_GameObject::NodeGetWorldScaling() const
837 {
838         return GetSGNode()->GetWorldScaling();
839 }
840
841
842
843 const MT_Point3& KX_GameObject::NodeGetWorldPosition() const
844 {
845         return GetSGNode()->GetWorldPosition();
846 }
847
848 /* Suspend/ resume: for the dynamic behaviour, there is a simple
849  * method. For the residual motion, there is not. I wonder what the
850  * correct solution is for Sumo. Remove from the motion-update tree?
851  *
852  * So far, only switch the physics and logic.
853  * */
854
855 void KX_GameObject::Resume(void)
856 {
857         if (m_suspended) {
858                 SCA_IObject::Resume();
859                 GetPhysicsController()->RestoreDynamics();
860
861                 m_suspended = false;
862         }
863 }
864
865 void KX_GameObject::Suspend()
866 {
867         if ((!m_ignore_activity_culling) 
868                 && (!m_suspended))  {
869                 SCA_IObject::Suspend();
870                 GetPhysicsController()->SuspendDynamics();
871                 m_suspended = true;
872         }
873 }
874
875
876
877
878 /* ------- python stuff ---------------------------------------------------*/
879
880
881
882
883 PyMethodDef KX_GameObject::Methods[] = {
884         {"getPosition", (PyCFunction) KX_GameObject::sPyGetPosition, METH_NOARGS},
885         {"setPosition", (PyCFunction) KX_GameObject::sPySetPosition, METH_O},
886         {"getLinearVelocity", (PyCFunction) KX_GameObject::sPyGetLinearVelocity, METH_VARARGS},
887         {"setLinearVelocity", (PyCFunction) KX_GameObject::sPySetLinearVelocity, METH_VARARGS},
888         {"getVelocity", (PyCFunction) KX_GameObject::sPyGetVelocity, METH_VARARGS},
889         {"getMass", (PyCFunction) KX_GameObject::sPyGetMass, METH_NOARGS},
890         {"getReactionForce", (PyCFunction) KX_GameObject::sPyGetReactionForce, METH_NOARGS},
891         {"getOrientation", (PyCFunction) KX_GameObject::sPyGetOrientation, METH_NOARGS},
892         {"setOrientation", (PyCFunction) KX_GameObject::sPySetOrientation, METH_O},
893         {"getVisible",(PyCFunction) KX_GameObject::sPyGetVisible, METH_NOARGS},
894         {"setVisible",(PyCFunction) KX_GameObject::sPySetVisible, METH_O},
895         {"getState",(PyCFunction) KX_GameObject::sPyGetState, METH_NOARGS},
896         {"setState",(PyCFunction) KX_GameObject::sPySetState, METH_O},
897         {"alignAxisToVect",(PyCFunction) KX_GameObject::sPyAlignAxisToVect, METH_VARARGS},
898         {"getAxisVect",(PyCFunction) KX_GameObject::sPyGetAxisVect, METH_O},
899         {"suspendDynamics", (PyCFunction)KX_GameObject::sPySuspendDynamics,METH_NOARGS},
900         {"restoreDynamics", (PyCFunction)KX_GameObject::sPyRestoreDynamics,METH_NOARGS},
901         {"enableRigidBody", (PyCFunction)KX_GameObject::sPyEnableRigidBody,METH_NOARGS},
902         {"disableRigidBody", (PyCFunction)KX_GameObject::sPyDisableRigidBody,METH_NOARGS},
903         {"applyImpulse", (PyCFunction) KX_GameObject::sPyApplyImpulse, METH_VARARGS},
904         {"setCollisionMargin", (PyCFunction) KX_GameObject::sPySetCollisionMargin, METH_O},
905         {"getParent", (PyCFunction)KX_GameObject::sPyGetParent,METH_NOARGS},
906         {"setParent", (PyCFunction)KX_GameObject::sPySetParent,METH_O},
907         {"removeParent", (PyCFunction)KX_GameObject::sPyRemoveParent,METH_NOARGS},
908         {"getChildren", (PyCFunction)KX_GameObject::sPyGetChildren,METH_NOARGS},
909         {"getChildrenRecursive", (PyCFunction)KX_GameObject::sPyGetChildrenRecursive,METH_NOARGS},
910         {"getMesh", (PyCFunction)KX_GameObject::sPyGetMesh,METH_VARARGS},
911         {"getPhysicsId", (PyCFunction)KX_GameObject::sPyGetPhysicsId,METH_NOARGS},
912         {"getPropertyNames", (PyCFunction)KX_GameObject::sPyGetPropertyNames,METH_NOARGS},
913         {"endObject",(PyCFunction) KX_GameObject::sPyEndObject, METH_NOARGS},
914         KX_PYMETHODTABLE(KX_GameObject, rayCastTo),
915         KX_PYMETHODTABLE(KX_GameObject, rayCast),
916         KX_PYMETHODTABLE(KX_GameObject, getDistanceTo),
917         KX_PYMETHODTABLE(KX_GameObject, getVectTo),
918         {NULL,NULL} //Sentinel
919 };
920
921
922
923 /*
924 bool KX_GameObject::ConvertPythonVectorArgs(PyObject* args,
925                                                                                         MT_Vector3& pos,
926                                                                                         MT_Vector3& pos2)
927 {
928         PyObject* pylist;
929         PyObject* pylist2;
930         bool error = (PyArg_ParseTuple(args,"OO",&pylist,&pylist2)) != 0;
931
932         pos = ConvertPythonPylist(pylist);
933         pos2 = ConvertPythonPylist(pylist2);
934                 
935         return error;
936 }
937 */
938
939 PyObject* KX_GameObject::PyEndObject(PyObject* self)
940 {
941
942         KX_Scene *scene = PHY_GetActiveScene();
943         scene->DelayedRemoveObject(this);
944         
945         Py_RETURN_NONE;
946
947 }
948
949
950 PyObject* KX_GameObject::PyGetPosition(PyObject* self)
951 {
952         return PyObjectFrom(NodeGetWorldPosition());
953 }
954
955
956
957 PyTypeObject KX_GameObject::Type = {
958         PyObject_HEAD_INIT(&PyType_Type)
959                 0,
960                 "KX_GameObject",
961                 sizeof(KX_GameObject),
962                 0,
963                 PyDestructor,
964                 0,
965                 __getattr,
966                 __setattr,
967                 0, //&MyPyCompare,
968                 __repr,
969                 0, //&cvalue_as_number,
970                 0,
971                 0,
972                 0,
973                 0
974 };
975
976
977
978 PyParentObject KX_GameObject::Parents[] = {
979         &KX_GameObject::Type,
980                 &SCA_IObject::Type,
981                 &CValue::Type,
982                 NULL
983 };
984
985
986
987
988 PyObject* KX_GameObject::_getattr(const STR_String& attr)
989 {
990         if (m_pPhysicsController1)
991         {
992                 if (attr == "mass")
993                         return PyFloat_FromDouble(GetPhysicsController()->GetMass());
994         }
995
996         if (attr == "parent")
997         {       
998                 KX_GameObject* parent = GetParent();
999                 if (parent)
1000                 {
1001                         parent->AddRef();
1002                         return parent;
1003                 }
1004                 Py_RETURN_NONE;
1005         }
1006
1007         if (attr == "visible")
1008                 return PyInt_FromLong(m_bVisible);
1009         
1010         if (attr == "position")
1011                 return PyObjectFrom(NodeGetWorldPosition());
1012         
1013         if (attr == "orientation")
1014                 return PyObjectFrom(NodeGetWorldOrientation());
1015         
1016         if (attr == "scaling")
1017                 return PyObjectFrom(NodeGetWorldScaling());
1018                 
1019         if (attr == "name")
1020                 return PyString_FromString(m_name.ReadPtr());
1021         if (attr == "timeOffset") {
1022                 if (m_pSGNode->GetSGParent()->IsSlowParent()) {
1023                         return PyFloat_FromDouble(static_cast<KX_SlowParentRelation *>(m_pSGNode->GetSGParent()->GetParentRelation())->GetTimeOffset());
1024                 } else {
1025                         return PyFloat_FromDouble(0.0);
1026                 }
1027         }
1028         
1029         
1030         _getattr_up(SCA_IObject);
1031 }
1032
1033 int KX_GameObject::_setattr(const STR_String& attr, PyObject *value)    // _setattr method
1034 {
1035         if (attr == "mass")
1036                 return 1;
1037         
1038         if (attr == "parent")
1039                 return 1;
1040                 
1041         if (PyInt_Check(value))
1042         {
1043                 int val = PyInt_AsLong(value);
1044                 if (attr == "visible")
1045                 {
1046                         SetVisible(val != 0);
1047                         return 0;
1048                 }
1049         }
1050
1051         if (PyFloat_Check(value))
1052         {
1053                 MT_Scalar val = PyFloat_AsDouble(value);
1054                 if (attr == "timeOffset") {
1055                         if (m_pSGNode->GetSGParent() && m_pSGNode->GetSGParent()->IsSlowParent()) {
1056                                 static_cast<KX_SlowParentRelation *>(m_pSGNode->GetSGParent()->GetParentRelation())->SetTimeOffset(val);
1057                                 return 0;
1058                         } else {
1059                                 return 0;
1060                         }               
1061                 }
1062         }
1063         
1064         if (PySequence_Check(value))
1065         {
1066                 if (attr == "orientation")
1067                 {
1068                         MT_Matrix3x3 rot;
1069                         if (PyObject_IsMT_Matrix(value, 3))
1070                         {
1071                                 if (PyMatTo(value, rot))
1072                                 {
1073                                         NodeSetLocalOrientation(rot);
1074                                         NodeUpdateGS(0.f,true);
1075                                         return 0;
1076                                 }
1077                                 return 1;
1078                         }
1079                         
1080                         if (PySequence_Size(value) == 4)
1081                         {
1082                                 MT_Quaternion qrot;
1083                                 if (PyVecTo(value, qrot))
1084                                 {
1085                                         rot.setRotation(qrot);
1086                                         NodeSetLocalOrientation(rot);
1087                                         NodeUpdateGS(0.f,true);
1088                                         return 0;
1089                                 }
1090                                 return 1;
1091                         }
1092                         
1093                         if (PySequence_Size(value) == 3)
1094                         {
1095                                 MT_Vector3 erot;
1096                                 if (PyVecTo(value, erot))
1097                                 {
1098                                         rot.setEuler(erot);
1099                                         NodeSetLocalOrientation(rot);
1100                                         NodeUpdateGS(0.f,true);
1101                                         return 0;
1102                                 }
1103                                 return 1;
1104                         }
1105                         
1106                         return 1;
1107                 }
1108                 
1109                 if (attr == "position")
1110                 {
1111                         MT_Point3 pos;
1112                         if (PyVecTo(value, pos))
1113                         {
1114                                 NodeSetLocalPosition(pos);
1115                                 NodeUpdateGS(0.f,true);
1116                                 return 0;
1117                         }
1118                         return 1;
1119                 }
1120                 
1121                 if (attr == "scaling")
1122                 {
1123                         MT_Vector3 scale;
1124                         if (PyVecTo(value, scale))
1125                         {
1126                                 NodeSetLocalScale(scale);
1127                                 NodeUpdateGS(0.f,true);
1128                                 return 0;
1129                         }
1130                         return 1;
1131                 }
1132         }
1133         
1134         if (PyString_Check(value))
1135         {
1136                 if (attr == "name")
1137                 {
1138                         m_name = PyString_AsString(value);
1139                         return 0;
1140                 }
1141         }
1142         
1143         /* Need to have parent settable here too */
1144         
1145         return SCA_IObject::_setattr(attr, value);
1146 }
1147
1148
1149 PyObject* KX_GameObject::PyGetLinearVelocity(PyObject* self, 
1150                                                                                          PyObject* args, 
1151                                                                                          PyObject* kwds)
1152 {
1153         // only can get the velocity if we have a physics object connected to us...
1154         int local = 0;
1155         if (PyArg_ParseTuple(args,"|i",&local))
1156         {
1157                 return PyObjectFrom(GetLinearVelocity((local!=0)));
1158         }
1159         else
1160         {
1161                 return NULL;
1162         }
1163 }
1164
1165 PyObject* KX_GameObject::PySetLinearVelocity(PyObject* self, 
1166                                                                                          PyObject* args, 
1167                                                                                          PyObject* kwds)
1168 {
1169         int local = 0;
1170         PyObject* pyvect;
1171         
1172         if (PyArg_ParseTuple(args,"O|i",&pyvect,&local)) {
1173                 MT_Vector3 velocity;
1174                 if (PyVecTo(pyvect, velocity)) {
1175                         setLinearVelocity(velocity, (local!=0));
1176                         Py_RETURN_NONE;
1177                 }
1178         }
1179         return NULL;
1180 }
1181
1182 PyObject* KX_GameObject::PySetVisible(PyObject* self, PyObject* value)
1183 {
1184         int visible = PyInt_AsLong(value);
1185         
1186         if (visible==-1 && PyErr_Occurred()) {
1187                 PyErr_SetString(PyExc_TypeError, "expected 0 or 1");
1188                 return NULL;
1189         }
1190         
1191         MarkVisible(visible!=0);
1192         m_bVisible = (visible!=0);
1193         Py_RETURN_NONE;
1194         
1195 }
1196
1197 PyObject* KX_GameObject::PyGetVisible(PyObject* self)
1198 {
1199         return PyInt_FromLong(m_bVisible);      
1200 }
1201
1202 PyObject* KX_GameObject::PyGetState(PyObject* self)
1203 {
1204         int state = 0;
1205         state |= GetState();
1206         return PyInt_FromLong(state);
1207 }
1208
1209 PyObject* KX_GameObject::PySetState(PyObject* self, PyObject* value)
1210 {
1211         int state_i = PyInt_AsLong(value);
1212         unsigned int state = 0;
1213         
1214         if (state_i == -1 && PyErr_Occurred()) {
1215                 PyErr_SetString(PyExc_TypeError, "expected an int bit field");
1216                 return NULL;
1217         }
1218         
1219         state |= state_i;
1220         if ((state & ((1<<30)-1)) == 0) {
1221                 PyErr_SetString(PyExc_AttributeError, "The state bitfield was not between 0 and 30 (1<<0 and 1<<29)");
1222                 return NULL;
1223         }
1224         SetState(state);
1225         
1226         Py_RETURN_NONE;
1227 }
1228
1229
1230
1231 PyObject* KX_GameObject::PyGetVelocity(PyObject* self, 
1232                                                                            PyObject* args, 
1233                                                                            PyObject* kwds)
1234 {
1235         // only can get the velocity if we have a physics object connected to us...
1236         MT_Vector3 velocity(0.0,0.0,0.0);
1237         MT_Point3 point(0.0,0.0,0.0);
1238         
1239         
1240         PyObject* pypos = NULL;
1241         if (PyArg_ParseTuple(args, "|O", &pypos))
1242         {
1243                 if (pypos)
1244                         PyVecTo(pypos, point);
1245         }
1246         else {
1247                 return NULL;
1248         }
1249         
1250         if (m_pPhysicsController1)
1251         {
1252                 velocity = m_pPhysicsController1->GetVelocity(point);
1253         }
1254         
1255         return PyObjectFrom(velocity);
1256 }
1257
1258
1259
1260 PyObject* KX_GameObject::PyGetMass(PyObject* self)
1261 {
1262         return PyFloat_FromDouble(GetPhysicsController()->GetMass());
1263 }
1264
1265
1266
1267 PyObject* KX_GameObject::PyGetReactionForce(PyObject* self)
1268 {
1269         // only can get the velocity if we have a physics object connected to us...
1270         return PyObjectFrom(GetPhysicsController()->getReactionForce());
1271 }
1272
1273
1274
1275 PyObject* KX_GameObject::PyEnableRigidBody(PyObject* self)
1276 {
1277         GetPhysicsController()->setRigidBody(true);
1278
1279         Py_RETURN_NONE;
1280 }
1281
1282
1283
1284 PyObject* KX_GameObject::PyDisableRigidBody(PyObject* self)
1285 {
1286         GetPhysicsController()->setRigidBody(false);
1287
1288         Py_RETURN_NONE;
1289 }
1290
1291
1292
1293 PyObject* KX_GameObject::PyGetParent(PyObject* self)
1294 {
1295         KX_GameObject* parent = this->GetParent();
1296         if (parent)
1297         {
1298                 parent->AddRef();
1299                 return parent;
1300         }
1301         Py_RETURN_NONE;
1302 }
1303
1304 PyObject* KX_GameObject::PySetParent(PyObject* self, PyObject* value)
1305 {
1306         if (!PyObject_TypeCheck(value, &KX_GameObject::Type)) {
1307                 PyErr_SetString(PyExc_TypeError, "expected a KX_GameObject type");
1308                 return NULL;
1309         }
1310         
1311         // The object we want to set as parent
1312         CValue *m_ob = (CValue*)value;
1313         KX_GameObject *obj = ((KX_GameObject*)m_ob);
1314         KX_Scene *scene = PHY_GetActiveScene();
1315         
1316         this->SetParent(scene, obj);
1317                 
1318         Py_RETURN_NONE;
1319 }
1320
1321 PyObject* KX_GameObject::PyRemoveParent(PyObject* self)
1322 {
1323         KX_Scene *scene = PHY_GetActiveScene();
1324         this->RemoveParent(scene);
1325         Py_RETURN_NONE;
1326 }
1327
1328
1329 static void walk_children(SG_Node* node, CListValue* list, bool recursive)
1330 {
1331         NodeList& children = node->GetSGChildren();
1332
1333         for (NodeList::iterator childit = children.begin();!(childit==children.end());++childit)
1334         {
1335                 SG_Node* childnode = (*childit);
1336                 CValue* childobj = (CValue*)childnode->GetSGClientObject();
1337                 if (childobj != NULL) // This is a GameObject
1338                 {
1339                         // add to the list
1340                         list->Add(childobj->AddRef());
1341                 }
1342                 
1343                 // if the childobj is NULL then this may be an inverse parent link
1344                 // so a non recursive search should still look down this node.
1345                 if (recursive || childobj==NULL) {
1346                         walk_children(childnode, list, recursive);
1347                 }
1348         }
1349 }
1350
1351 PyObject* KX_GameObject::PyGetChildren(PyObject* self)
1352 {
1353         CListValue* list = new CListValue();
1354         walk_children(m_pSGNode, list, 0);
1355         return list;
1356 }
1357
1358 PyObject* KX_GameObject::PyGetChildrenRecursive(PyObject* self)
1359 {
1360         CListValue* list = new CListValue();
1361         walk_children(m_pSGNode, list, 1);
1362         return list;
1363 }
1364
1365 PyObject* KX_GameObject::PyGetMesh(PyObject* self, 
1366                                                                    PyObject* args, 
1367                                                                    PyObject* kwds)
1368 {
1369         int mesh = 0;
1370
1371         if (!PyArg_ParseTuple(args, "|i", &mesh))
1372                 return NULL; // python sets a simple error
1373         
1374         if (((unsigned int)mesh < m_meshes.size()) && mesh >= 0)
1375         {
1376                 KX_MeshProxy* meshproxy = new KX_MeshProxy(m_meshes[mesh]);
1377                 return meshproxy;
1378         }
1379         
1380         Py_RETURN_NONE;
1381 }
1382
1383
1384
1385
1386
1387 PyObject* KX_GameObject::PySetCollisionMargin(PyObject* self, PyObject* value)
1388 {
1389         float collisionMargin = PyFloat_AsDouble(value);
1390         
1391         if (collisionMargin==-1 && PyErr_Occurred()) {
1392                 PyErr_SetString(PyExc_TypeError, "expected a float");
1393                 return NULL;
1394         }
1395         
1396         if (m_pPhysicsController1)
1397         {
1398                 m_pPhysicsController1->setMargin(collisionMargin);
1399                 Py_RETURN_NONE;
1400         }
1401         PyErr_SetString(PyExc_RuntimeError, "This object has no physics controller");
1402         return NULL;
1403 }
1404
1405
1406
1407 PyObject* KX_GameObject::PyApplyImpulse(PyObject* self, 
1408                                                                                 PyObject* args, 
1409                                                                                 PyObject* kwds)
1410 {
1411         PyObject* pyattach;
1412         PyObject* pyimpulse;
1413         
1414         if (!m_pPhysicsController1)     {
1415                 PyErr_SetString(PyExc_RuntimeError, "This object has no physics controller");
1416                 return NULL;
1417         }
1418         
1419         if (PyArg_ParseTuple(args, "OO", &pyattach, &pyimpulse))
1420         {
1421                 MT_Point3  attach;
1422                 MT_Vector3 impulse;
1423                 if (PyVecTo(pyattach, attach) && PyVecTo(pyimpulse, impulse))
1424                 {
1425                         m_pPhysicsController1->applyImpulse(attach, impulse);
1426                         Py_RETURN_NONE;
1427                 }
1428
1429         }
1430         
1431         return NULL;
1432 }
1433
1434
1435
1436 PyObject* KX_GameObject::PySuspendDynamics(PyObject* self)
1437 {
1438         SuspendDynamics();
1439         Py_RETURN_NONE;
1440 }
1441
1442
1443
1444 PyObject* KX_GameObject::PyRestoreDynamics(PyObject* self)
1445 {
1446         RestoreDynamics();
1447         Py_RETURN_NONE;
1448 }
1449
1450
1451
1452 PyObject* KX_GameObject::PyGetOrientation(PyObject* self) //keywords
1453 {
1454         return PyObjectFrom(NodeGetWorldOrientation());
1455 }
1456
1457
1458
1459 PyObject* KX_GameObject::PySetOrientation(PyObject* self, PyObject* value)
1460 {
1461         MT_Matrix3x3 matrix;
1462         if (PyObject_IsMT_Matrix(value, 3) && PyMatTo(value, matrix))
1463         {
1464                 NodeSetLocalOrientation(matrix);
1465                 NodeUpdateGS(0.f,true);
1466                 Py_RETURN_NONE;
1467         }
1468
1469         MT_Quaternion quat;
1470         if (PyVecTo(value, quat))
1471         {
1472                 matrix.setRotation(quat);
1473                 NodeSetLocalOrientation(matrix);
1474                 NodeUpdateGS(0.f,true);
1475                 Py_RETURN_NONE;
1476         }
1477         return NULL;
1478 }
1479
1480 PyObject* KX_GameObject::PyAlignAxisToVect(PyObject* self, 
1481                                                                                   PyObject* args, 
1482                                                                                   PyObject* kwds)
1483 {
1484         PyObject* pyvect;
1485         int axis = 2; //z axis is the default
1486         float fac = 1.0;
1487         
1488         if (PyArg_ParseTuple(args,"O|if",&pyvect,&axis, &fac))
1489         {
1490                 MT_Vector3 vect;
1491                 if (PyVecTo(pyvect, vect))
1492                 {
1493                         if (fac<=0.0) Py_RETURN_NONE; // Nothing to do.
1494                         if (fac> 1.0) fac= 1.0;
1495                         
1496                         AlignAxisToVect(vect,axis,fac);
1497                         NodeUpdateGS(0.f,true);
1498                         Py_RETURN_NONE;
1499                 }
1500         }
1501         return NULL;
1502 }
1503
1504 PyObject* KX_GameObject::PyGetAxisVect(PyObject* self, PyObject* value)
1505 {
1506         MT_Vector3 vect;
1507         if (PyVecTo(value, vect))
1508         {
1509                 return PyObjectFrom(NodeGetWorldOrientation() * vect);
1510         }
1511         return NULL;
1512 }
1513
1514 PyObject* KX_GameObject::PySetPosition(PyObject* self, PyObject* value)
1515 {
1516         MT_Point3 pos;
1517         if (PyVecTo(value, pos))
1518         {
1519                 NodeSetLocalPosition(pos);
1520                 NodeUpdateGS(0.f,true);
1521                 Py_RETURN_NONE;
1522         }
1523
1524         return NULL;
1525 }
1526
1527 PyObject* KX_GameObject::PyGetPhysicsId(PyObject* self)
1528 {
1529         KX_IPhysicsController* ctrl = GetPhysicsController();
1530         uint_ptr physid=0;
1531         if (ctrl)
1532         {
1533                 physid= (uint_ptr)ctrl->GetUserData();
1534         }
1535         return PyInt_FromLong((long)physid);
1536 }
1537
1538 PyObject* KX_GameObject::PyGetPropertyNames(PyObject* self)
1539 {
1540         return ConvertKeysToPython();
1541 }
1542
1543 KX_PYMETHODDEF_DOC(KX_GameObject, getDistanceTo,
1544 "getDistanceTo(other): get distance to another point/KX_GameObject")
1545 {
1546         MT_Point3 b;
1547         if (PyVecArgTo(args, b))
1548         {
1549                 return PyFloat_FromDouble(NodeGetWorldPosition().distance(b));
1550         }
1551         PyErr_Clear();
1552         
1553         PyObject *pyother;
1554         KX_GameObject *other;
1555         if (PyArg_ParseTuple(args, "O", &pyother) && ConvertPythonToGameObject(pyother, &other, false))
1556         {
1557                 return PyFloat_FromDouble(NodeGetWorldPosition().distance(other->NodeGetWorldPosition()));
1558         }
1559         
1560         return NULL;
1561 }
1562
1563 KX_PYMETHODDEF_DOC(KX_GameObject, getVectTo,
1564 "getVectTo(other): get vector and the distance to another point/KX_GameObject\n"
1565 "Returns a 3-tuple with (distance,worldVector,localVector)\n")
1566 {
1567         MT_Point3 toPoint, fromPoint;
1568         MT_Vector3 toDir, locToDir;
1569         MT_Scalar distance;
1570
1571         PyObject *returnValue;
1572         PyObject *pyother;
1573
1574         if (!PyVecArgTo(args, toPoint))
1575         {
1576                 PyErr_Clear();
1577                 
1578                 KX_GameObject *other;
1579                 if (PyArg_ParseTuple(args, "O", &pyother) && ConvertPythonToGameObject(pyother, &other, false))
1580                 {
1581                         toPoint = other->NodeGetWorldPosition();
1582                 } else
1583                 {
1584                         PyErr_SetString(PyExc_TypeError, "Expected a 3D Vector or GameObject type");
1585                         return NULL;
1586                 }
1587         }
1588
1589         fromPoint = NodeGetWorldPosition();
1590         toDir = toPoint-fromPoint;
1591         distance = toDir.length();
1592
1593         if (MT_fuzzyZero(distance))
1594         {
1595                 //cout << "getVectTo() Error: Null vector!\n";
1596                 locToDir = toDir = MT_Vector3(0.0,0.0,0.0);
1597                 distance = 0.0;
1598         } else {
1599                 toDir.normalize();
1600                 locToDir = toDir * NodeGetWorldOrientation();
1601         }
1602         
1603         returnValue = PyTuple_New(3);
1604         if (returnValue) { // very unlikely to fail, python sets a memory error here.
1605                 PyTuple_SET_ITEM(returnValue, 0, PyFloat_FromDouble(distance));
1606                 PyTuple_SET_ITEM(returnValue, 1, PyObjectFrom(toDir));
1607                 PyTuple_SET_ITEM(returnValue, 2, PyObjectFrom(locToDir));
1608         }
1609         return returnValue;
1610 }
1611
1612 bool KX_GameObject::RayHit(KX_ClientObjectInfo* client, MT_Point3& hit_point, MT_Vector3& hit_normal, void * const data)
1613 {
1614
1615         KX_GameObject* hitKXObj = client->m_gameobject;
1616         
1617         if (client->m_type > KX_ClientObjectInfo::ACTOR)
1618         {
1619                 // false hit
1620                 return false;
1621         }
1622
1623         if (m_testPropName.Length() == 0 || hitKXObj->GetProperty(m_testPropName) != NULL)
1624         {
1625                 m_pHitObject = hitKXObj;
1626                 return true;
1627         }
1628
1629         return false;
1630         
1631 }
1632
1633 KX_PYMETHODDEF_DOC(KX_GameObject, rayCastTo,
1634 "rayCastTo(other,dist,prop): look towards another point/KX_GameObject and return first object hit within dist that matches prop\n"
1635 " prop = property name that object must have; can be omitted => detect any object\n"
1636 " dist = max distance to look (can be negative => look behind); 0 or omitted => detect up to other\n"
1637 " other = 3-tuple or object reference")
1638 {
1639         MT_Point3 toPoint;
1640         PyObject* pyarg;
1641         float dist = 0.0f;
1642         char *propName = NULL;
1643
1644         if (!PyArg_ParseTuple(args,"O|fs", &pyarg, &dist, &propName)) {
1645                 return NULL; // python sets simple error
1646         }
1647
1648         if (!PyVecTo(pyarg, toPoint))
1649         {
1650                 KX_GameObject *other;
1651                 PyErr_Clear();
1652                 
1653                 if (ConvertPythonToGameObject(pyarg, &other, false))
1654                 {
1655                         toPoint = other->NodeGetWorldPosition();
1656                 } else
1657                 {
1658                         PyErr_SetString(PyExc_TypeError, "the first argument to rayCastTo must be a vector or a KX_GameObject");
1659                         return NULL;
1660                 }
1661         }
1662         MT_Point3 fromPoint = NodeGetWorldPosition();
1663         if (dist != 0.0f)
1664         {
1665                 MT_Vector3 toDir = toPoint-fromPoint;
1666                 toDir.normalize();
1667                 toPoint = fromPoint + (dist) * toDir;
1668         }
1669
1670         MT_Point3 resultPoint;
1671         MT_Vector3 resultNormal;
1672         PHY_IPhysicsEnvironment* pe = GetPhysicsEnvironment();
1673         KX_IPhysicsController *spc = GetPhysicsController();
1674         KX_GameObject *parent = GetParent();
1675         if (!spc && parent)
1676                 spc = parent->GetPhysicsController();
1677         if (parent)
1678                 parent->Release();
1679         
1680         m_pHitObject = NULL;
1681         if (propName)
1682                 m_testPropName = propName;
1683         else
1684                 m_testPropName.SetLength(0);
1685         KX_RayCast::RayTest(spc, pe, fromPoint, toPoint, resultPoint, resultNormal, KX_RayCast::Callback<KX_GameObject>(this));
1686
1687     if (m_pHitObject)
1688         {
1689                 m_pHitObject->AddRef();
1690                 return m_pHitObject;
1691         }
1692         Py_RETURN_NONE;
1693 }
1694
1695 KX_PYMETHODDEF_DOC(KX_GameObject, rayCast,
1696                                    "rayCast(to,from,dist,prop): cast a ray and return tuple (object,hit,normal) of contact point with object within dist that matches prop or (None,None,None) tuple if no hit\n"
1697 " prop = property name that object must have; can be omitted => detect any object\n"
1698 " dist = max distance to look (can be negative => look behind); 0 or omitted => detect up to to\n"
1699 " from = 3-tuple or object reference for origin of ray (if object, use center of object)\n"
1700 "        Can be None or omitted => start from self object center\n"
1701 " to = 3-tuple or object reference for destination of ray (if object, use center of object)\n"
1702 "Note: the object on which you call this method matters: the ray will ignore it if it goes through it\n")
1703 {
1704         MT_Point3 toPoint;
1705         MT_Point3 fromPoint;
1706         PyObject* pyto;
1707         PyObject* pyfrom = NULL;
1708         float dist = 0.0f;
1709         char *propName = NULL;
1710         KX_GameObject *other;
1711
1712         if (!PyArg_ParseTuple(args,"O|Ofs", &pyto, &pyfrom, &dist, &propName)) {
1713                 return NULL; // Python sets a simple error
1714         }
1715
1716         if (!PyVecTo(pyto, toPoint))
1717         {
1718                 PyErr_Clear();
1719                 
1720                 if (ConvertPythonToGameObject(pyto, &other, false))
1721                 {
1722                         toPoint = other->NodeGetWorldPosition();
1723                 } else
1724                 {
1725                         PyErr_SetString(PyExc_TypeError, "the first argument to rayCast must be a vector or a KX_GameObject");
1726                         return NULL;
1727                 }
1728         }
1729         if (!pyfrom || pyfrom == Py_None)
1730         {
1731                 fromPoint = NodeGetWorldPosition();
1732         }
1733         else if (!PyVecTo(pyfrom, fromPoint))
1734         {
1735                 PyErr_Clear();
1736                 
1737                 if (ConvertPythonToGameObject(pyfrom, &other, false))
1738                 {
1739                         fromPoint = other->NodeGetWorldPosition();
1740                 } else
1741                 {
1742                         PyErr_SetString(PyExc_TypeError, "the second optional argument to rayCast must be a vector or a KX_GameObject");
1743                         return NULL;
1744                 }
1745         }
1746         
1747         if (dist != 0.0f) {
1748                 MT_Vector3 toDir = toPoint-fromPoint;
1749                 if (MT_fuzzyZero(toDir.length2())) {
1750                         return Py_BuildValue("OOO", Py_None, Py_None, Py_None);
1751                 }
1752                 toDir.normalize();
1753                 toPoint = fromPoint + (dist) * toDir;
1754         } else if (MT_fuzzyZero((toPoint-fromPoint).length2())) {
1755                 return Py_BuildValue("OOO", Py_None, Py_None, Py_None);
1756         }
1757         
1758         MT_Point3 resultPoint;
1759         MT_Vector3 resultNormal;
1760         PHY_IPhysicsEnvironment* pe = GetPhysicsEnvironment();
1761         KX_IPhysicsController *spc = GetPhysicsController();
1762         KX_GameObject *parent = GetParent();
1763         if (!spc && parent)
1764                 spc = parent->GetPhysicsController();
1765         if (parent)
1766                 parent->Release();
1767         
1768         m_pHitObject = NULL;
1769         if (propName)
1770                 m_testPropName = propName;
1771         else
1772                 m_testPropName.SetLength(0);
1773         KX_RayCast::RayTest(spc, pe, fromPoint, toPoint, resultPoint, resultNormal, KX_RayCast::Callback<KX_GameObject>(this));
1774
1775     if (m_pHitObject)
1776         {
1777                 PyObject* returnValue = PyTuple_New(3);
1778                 if (returnValue) { // unlikely this would ever fail, if it does python sets an error
1779                         PyTuple_SET_ITEM(returnValue, 0, m_pHitObject->AddRef());
1780                         PyTuple_SET_ITEM(returnValue, 1, PyObjectFrom(resultPoint));
1781                         PyTuple_SET_ITEM(returnValue, 2, PyObjectFrom(resultNormal));
1782                 }
1783                 return returnValue;
1784         }
1785         return Py_BuildValue("OOO", Py_None, Py_None, Py_None);
1786         //Py_RETURN_NONE;
1787 }
1788
1789 /* --------------------------------------------------------------------- 
1790  * Some stuff taken from the header
1791  * --------------------------------------------------------------------- */
1792 void KX_GameObject::Relink(GEN_Map<GEN_HashedPtr, void*> *map_parameter)        
1793 {
1794         // we will relink the sensors and actuators that use object references
1795         // if the object is part of the replicated hierarchy, use the new
1796         // object reference instead
1797         SCA_SensorList& sensorlist = GetSensors();
1798         SCA_SensorList::iterator sit;
1799         for (sit=sensorlist.begin(); sit != sensorlist.end(); sit++)
1800         {
1801                 (*sit)->Relink(map_parameter);
1802         }
1803         SCA_ActuatorList& actuatorlist = GetActuators();
1804         SCA_ActuatorList::iterator ait;
1805         for (ait=actuatorlist.begin(); ait != actuatorlist.end(); ait++)
1806         {
1807                 (*ait)->Relink(map_parameter);
1808         }
1809 }
1810
1811 bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py_none_ok)
1812 {
1813         if (value==NULL) {
1814                 PyErr_SetString(PyExc_TypeError, "Error in ConvertPythonToGameObject, python pointer NULL, should never happen");
1815                 *object = NULL;
1816                 return false;
1817         }
1818                 
1819         if (value==Py_None) {
1820                 *object = NULL;
1821                 
1822                 if (py_none_ok) {
1823                         return true;
1824                 } else {
1825                         PyErr_SetString(PyExc_TypeError, "Expected KX_GameObject or a string for a name of a KX_GameObject, None is invalid");
1826                         return false;
1827                 }
1828                 return (py_none_ok ? true : false);
1829         }
1830         
1831         if (PyString_Check(value)) {
1832                 *object = (KX_GameObject *)SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_String( PyString_AsString(value) ));
1833                 
1834                 if (*object) {
1835                         return true;
1836                 } else {
1837                         PyErr_SetString(PyExc_ValueError, "Requested name did not match any KX_GameObject");
1838                         return false;
1839                 }
1840         }
1841         
1842         if (PyObject_TypeCheck(value, &KX_GameObject::Type)) {
1843                 *object = static_cast<KX_GameObject*>(value);
1844                 return true;
1845         }
1846         
1847         *object = NULL;
1848         
1849         if (py_none_ok) {
1850                 PyErr_SetString(PyExc_TypeError, "Expect a KX_GameObject, a string or None");
1851         } else {
1852                 PyErr_SetString(PyExc_TypeError, "Expect a KX_GameObject or a string");
1853         }
1854         
1855         return false;
1856 }