fix BGE bug #8094: Collision sensor on child object makes the object rotate or move...
[blender.git] / source / gameengine / Ketsji / KX_NearSensor.cpp
1 /**
2  * Sense if other objects are near
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include "KX_NearSensor.h"
33 #include "SCA_LogicManager.h"
34 #include "KX_GameObject.h"
35 #include "KX_TouchEventManager.h"
36 #include "KX_Scene.h" // needed to create a replica
37 #include "PHY_IPhysicsEnvironment.h"
38 #include "PHY_IPhysicsController.h"
39
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44 KX_NearSensor::KX_NearSensor(SCA_EventManager* eventmgr,
45                                                          KX_GameObject* gameobj,
46                                                          double margin,
47                                                          double resetmargin,
48                                                          bool bFindMaterial,
49                                                          const STR_String& touchedpropname,
50                                                          class KX_Scene* scene,
51                                                          PHY_IPhysicsController*        ctrl,
52                                                          PyTypeObject* T)
53                          :KX_TouchSensor(eventmgr,
54                                                          gameobj,
55                                                          bFindMaterial,
56                                                          touchedpropname,
57                                                          /* scene, */
58                                                          T),
59                          m_Margin(margin),
60                          m_ResetMargin(resetmargin)
61
62 {
63
64         gameobj->getClientInfo()->m_sensors.remove(this);
65         m_client_info = new KX_ClientObjectInfo(gameobj, KX_ClientObjectInfo::NEAR);
66         m_client_info->m_sensors.push_back(this);
67         
68         //DT_ShapeHandle shape = (DT_ShapeHandle) vshape;
69         m_physCtrl = ctrl;
70         if (m_physCtrl)
71         {
72                 m_physCtrl->SetMargin(m_Margin);
73                 m_physCtrl->setNewClientInfo(m_client_info);
74         }
75         SynchronizeTransform();
76 }
77
78 void KX_NearSensor::SynchronizeTransform()
79 {
80         // The near and radar sensors are using a different physical object which is 
81         // not linked to the parent object, must synchronize it.
82         if (m_physCtrl)
83         {
84                 KX_GameObject* parent = ((KX_GameObject*)GetParent());
85                 MT_Vector3 pos = parent->NodeGetWorldPosition();
86                 MT_Quaternion orn = parent->NodeGetWorldOrientation().getRotation();
87                 m_physCtrl->setPosition(pos.x(),pos.y(),pos.z());
88                 m_physCtrl->setOrientation(orn.x(),orn.y(),orn.z(),orn.w());
89                 m_physCtrl->calcXform();
90         }
91 }
92
93 void KX_NearSensor::RegisterSumo(KX_TouchEventManager *touchman)
94 {
95         if (m_physCtrl)
96         {
97                 touchman->GetPhysicsEnvironment()->addSensor(m_physCtrl);
98         }
99 }
100
101 CValue* KX_NearSensor::GetReplica()
102 {
103         KX_NearSensor* replica = new KX_NearSensor(*this);
104         replica->m_colliders = new CListValue();
105         replica->m_bCollision = false;
106         replica->m_bTriggered= false;
107         replica->m_hitObject = NULL;
108         replica->m_bLastTriggered = false;
109         // this will copy properties and so on...
110         CValue::AddDataToReplica(replica);
111         
112         replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::NEAR);
113         
114         if (replica->m_physCtrl)
115         {
116                 replica->m_physCtrl = replica->m_physCtrl->GetReplica();
117                 if (replica->m_physCtrl)
118                 {
119                         //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->addSensor(replica->m_physCtrl);
120                         replica->m_physCtrl->SetMargin(m_Margin);
121                         replica->m_physCtrl->setNewClientInfo(replica->m_client_info);
122                 }
123                 
124         }
125         //static_cast<KX_TouchEventManager*>(m_eventmgr)->RegisterSensor(this);
126         //todo: make sure replication works fine
127         //>m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
128         //replica->m_sumoObj->setMargin(m_Margin);
129         //replica->m_sumoObj->setClientObject(replica->m_client_info);
130         
131         ((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
132         replica->SynchronizeTransform();
133         
134         return replica;
135 }
136
137
138
139 void KX_NearSensor::ReParent(SCA_IObject* parent)
140 {
141
142         SCA_ISensor::ReParent(parent);
143         
144         m_client_info->m_gameobject = static_cast<KX_GameObject*>(parent); 
145         m_client_info->m_sensors.push_back(this);
146         
147
148 /*      KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
149         client_info->m_gameobject = gameobj;
150         client_info->m_auxilary_info = NULL;
151         
152         client_info->m_sensors.push_back(this);
153         SCA_ISensor::ReParent(parent);
154 */
155         ((KX_GameObject*)GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
156         SynchronizeTransform();
157 }
158
159
160
161 KX_NearSensor::~KX_NearSensor()
162 {
163         // for nearsensor, the sensor is the 'owner' of sumoobj
164         // for touchsensor, it's the parent
165         if (m_physCtrl)
166         {
167                 //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
168                 delete m_physCtrl;
169                 m_physCtrl = NULL;
170         }
171         
172                 
173         if (m_client_info)
174                 delete m_client_info;
175 }
176
177
178 bool KX_NearSensor::Evaluate(CValue* event)
179 {
180         bool result = false;
181 //      KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
182
183         if (m_bTriggered != m_bLastTriggered)
184         {
185                 m_bLastTriggered = m_bTriggered;
186                 if (m_bTriggered)
187                 {
188                         if (m_physCtrl)
189                         {
190                                 m_physCtrl->SetMargin(m_ResetMargin);
191                         }
192                 } else
193                 {
194                         if (m_physCtrl)
195                         {
196                                 m_physCtrl->SetMargin(m_Margin);
197                         }
198
199                 }
200                 result = true;
201         }
202
203         return result;
204 }
205
206 // this function is called at broad phase stage to check if the two controller
207 // need to interact at all. It is used for Near/Radar sensor that don't need to
208 // check collision with object not included in filter
209 bool    KX_NearSensor::BroadPhaseFilterCollision(void*obj1,void*obj2)
210 {
211         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
212         
213         // need the mapping from PHY_IPhysicsController to gameobjects now
214         assert(obj1==m_physCtrl && obj2);
215         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>((static_cast<PHY_IPhysicsController*>(obj2))->getNewClientInfo());
216
217         KX_GameObject* gameobj = ( client_info ? 
218                         client_info->m_gameobject :
219                         NULL);
220         
221         if (gameobj && (gameobj != parent))
222         {
223                 // only take valid colliders
224                 if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
225                 {
226                         if ((m_touchedpropname.Length() == 0) || 
227                                 (gameobj->GetProperty(m_touchedpropname)))
228                         {
229                                 return true;
230                         }
231                 }
232         }
233
234         return false;
235 }
236
237 bool    KX_NearSensor::NewHandleCollision(void* obj1,void* obj2,const PHY_CollData * coll_data)
238 {
239 //      KX_TouchEventManager* toucheventmgr = static_cast<KX_TouchEventManager*>(m_eventmgr);
240         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
241         
242         // need the mapping from PHY_IPhysicsController to gameobjects now
243         
244         KX_ClientObjectInfo* client_info =static_cast<KX_ClientObjectInfo*> (obj1 == m_physCtrl? 
245                                         ((PHY_IPhysicsController*)obj2)->getNewClientInfo() : 
246                                         ((PHY_IPhysicsController*)obj1)->getNewClientInfo());
247
248         KX_GameObject* gameobj = ( client_info ? 
249                         client_info->m_gameobject :
250                         NULL);
251         
252         // these checks are done already in BroadPhaseFilterCollision()
253         if (gameobj /*&& (gameobj != parent)*/)
254         {
255                 if (!m_colliders->SearchValue(gameobj))
256                         m_colliders->Add(gameobj->AddRef());
257                 // only take valid colliders
258                 // These checks are done already in BroadPhaseFilterCollision()
259                 //if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
260                 //{
261                 //      if ((m_touchedpropname.Length() == 0) || 
262                 //              (gameobj->GetProperty(m_touchedpropname)))
263                 //      {
264                                 m_bTriggered = true;
265                                 m_hitObject = gameobj;
266                 //      }
267                 //}
268         }
269         
270         return DT_CONTINUE;
271 }
272
273
274
275 // python embedding
276 PyTypeObject KX_NearSensor::Type = {
277         PyObject_HEAD_INIT(&PyType_Type)
278         0,
279         "KX_NearSensor",
280         sizeof(KX_NearSensor),
281         0,
282         PyDestructor,
283         0,
284         __getattr,
285         __setattr,
286         0, //&MyPyCompare,
287         __repr,
288         0, //&cvalue_as_number,
289         0,
290         0,
291         0,
292         0
293 };
294
295
296
297 PyParentObject KX_NearSensor::Parents[] = {
298         &KX_NearSensor::Type,
299         &KX_TouchSensor::Type,
300         &SCA_ISensor::Type,
301         &SCA_ILogicBrick::Type,
302         &CValue::Type,
303         NULL
304 };
305
306
307
308 PyMethodDef KX_NearSensor::Methods[] = {
309         {"setProperty", 
310          (PyCFunction) KX_NearSensor::sPySetProperty,      METH_VARARGS, SetProperty_doc},
311         {"getProperty", 
312          (PyCFunction) KX_NearSensor::sPyGetProperty,      METH_VARARGS, GetProperty_doc},
313         {"getHitObject", 
314          (PyCFunction) KX_NearSensor::sPyGetHitObject,     METH_VARARGS, GetHitObject_doc},
315         {"getHitObjectList", 
316          (PyCFunction) KX_NearSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
317         {NULL,NULL} //Sentinel
318 };
319
320
321 PyObject*
322 KX_NearSensor::_getattr(const STR_String& attr)
323 {
324   _getattr_up(KX_TouchSensor);
325 }
326