svn merge -r 15409:15529 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender-staging.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->Init();
106         // this will copy properties and so on...
107         CValue::AddDataToReplica(replica);
108         
109         replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::NEAR);
110         
111         if (replica->m_physCtrl)
112         {
113                 replica->m_physCtrl = replica->m_physCtrl->GetReplica();
114                 if (replica->m_physCtrl)
115                 {
116                         //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->addSensor(replica->m_physCtrl);
117                         replica->m_physCtrl->SetMargin(m_Margin);
118                         replica->m_physCtrl->setNewClientInfo(replica->m_client_info);
119                 }
120                 
121         }
122         //static_cast<KX_TouchEventManager*>(m_eventmgr)->RegisterSensor(this);
123         //todo: make sure replication works fine
124         //>m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
125         //replica->m_sumoObj->setMargin(m_Margin);
126         //replica->m_sumoObj->setClientObject(replica->m_client_info);
127         
128         ((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
129         replica->SynchronizeTransform();
130         
131         return replica;
132 }
133
134
135
136 void KX_NearSensor::ReParent(SCA_IObject* parent)
137 {
138
139         SCA_ISensor::ReParent(parent);
140         
141         m_client_info->m_gameobject = static_cast<KX_GameObject*>(parent); 
142         m_client_info->m_sensors.push_back(this);
143         
144
145 /*      KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
146         client_info->m_gameobject = gameobj;
147         client_info->m_auxilary_info = NULL;
148         
149         client_info->m_sensors.push_back(this);
150         SCA_ISensor::ReParent(parent);
151 */
152         ((KX_GameObject*)GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
153         SynchronizeTransform();
154 }
155
156
157
158 KX_NearSensor::~KX_NearSensor()
159 {
160         // for nearsensor, the sensor is the 'owner' of sumoobj
161         // for touchsensor, it's the parent
162         if (m_physCtrl)
163         {
164                 //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
165                 delete m_physCtrl;
166                 m_physCtrl = NULL;
167         }
168         
169                 
170         if (m_client_info)
171                 delete m_client_info;
172 }
173
174
175 bool KX_NearSensor::Evaluate(CValue* event)
176 {
177         bool result = false;
178 //      KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
179
180         if (m_bTriggered != m_bLastTriggered)
181         {
182                 m_bLastTriggered = m_bTriggered;
183                 if (m_bTriggered)
184                 {
185                         if (m_physCtrl)
186                         {
187                                 m_physCtrl->SetMargin(m_ResetMargin);
188                         }
189                 } else
190                 {
191                         if (m_physCtrl)
192                         {
193                                 m_physCtrl->SetMargin(m_Margin);
194                         }
195
196                 }
197                 result = true;
198         }
199
200         return result;
201 }
202
203 // this function is called at broad phase stage to check if the two controller
204 // need to interact at all. It is used for Near/Radar sensor that don't need to
205 // check collision with object not included in filter
206 bool    KX_NearSensor::BroadPhaseFilterCollision(void*obj1,void*obj2)
207 {
208         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
209         
210         // need the mapping from PHY_IPhysicsController to gameobjects now
211         assert(obj1==m_physCtrl && obj2);
212         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>((static_cast<PHY_IPhysicsController*>(obj2))->getNewClientInfo());
213
214         KX_GameObject* gameobj = ( client_info ? 
215                         client_info->m_gameobject :
216                         NULL);
217         
218         if (gameobj && (gameobj != parent))
219         {
220                 // only take valid colliders
221                 if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
222                 {
223                         if ((m_touchedpropname.Length() == 0) || 
224                                 (gameobj->GetProperty(m_touchedpropname)))
225                         {
226                                 return true;
227                         }
228                 }
229         }
230
231         return false;
232 }
233
234 bool    KX_NearSensor::NewHandleCollision(void* obj1,void* obj2,const PHY_CollData * coll_data)
235 {
236 //      KX_TouchEventManager* toucheventmgr = static_cast<KX_TouchEventManager*>(m_eventmgr);
237         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
238         
239         // need the mapping from PHY_IPhysicsController to gameobjects now
240         
241         KX_ClientObjectInfo* client_info =static_cast<KX_ClientObjectInfo*> (obj1 == m_physCtrl? 
242                                         ((PHY_IPhysicsController*)obj2)->getNewClientInfo() : 
243                                         ((PHY_IPhysicsController*)obj1)->getNewClientInfo());
244
245         KX_GameObject* gameobj = ( client_info ? 
246                         client_info->m_gameobject :
247                         NULL);
248         
249         // these checks are done already in BroadPhaseFilterCollision()
250         if (gameobj /*&& (gameobj != parent)*/)
251         {
252                 if (!m_colliders->SearchValue(gameobj))
253                         m_colliders->Add(gameobj->AddRef());
254                 // only take valid colliders
255                 // These checks are done already in BroadPhaseFilterCollision()
256                 //if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
257                 //{
258                 //      if ((m_touchedpropname.Length() == 0) || 
259                 //              (gameobj->GetProperty(m_touchedpropname)))
260                 //      {
261                                 m_bTriggered = true;
262                                 m_hitObject = gameobj;
263                 //      }
264                 //}
265         }
266         
267         return DT_CONTINUE;
268 }
269
270
271
272 // python embedding
273 PyTypeObject KX_NearSensor::Type = {
274         PyObject_HEAD_INIT(&PyType_Type)
275         0,
276         "KX_NearSensor",
277         sizeof(KX_NearSensor),
278         0,
279         PyDestructor,
280         0,
281         __getattr,
282         __setattr,
283         0, //&MyPyCompare,
284         __repr,
285         0, //&cvalue_as_number,
286         0,
287         0,
288         0,
289         0
290 };
291
292
293
294 PyParentObject KX_NearSensor::Parents[] = {
295         &KX_NearSensor::Type,
296         &KX_TouchSensor::Type,
297         &SCA_ISensor::Type,
298         &SCA_ILogicBrick::Type,
299         &CValue::Type,
300         NULL
301 };
302
303
304
305 PyMethodDef KX_NearSensor::Methods[] = {
306         {"setProperty", 
307          (PyCFunction) KX_NearSensor::sPySetProperty,      METH_VARARGS, SetProperty_doc},
308         {"getProperty", 
309          (PyCFunction) KX_NearSensor::sPyGetProperty,      METH_VARARGS, GetProperty_doc},
310         {"getHitObject", 
311          (PyCFunction) KX_NearSensor::sPyGetHitObject,     METH_VARARGS, GetHitObject_doc},
312         {"getHitObjectList", 
313          (PyCFunction) KX_NearSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
314         {NULL,NULL} //Sentinel
315 };
316
317
318 PyObject*
319 KX_NearSensor::_getattr(const STR_String& attr)
320 {
321   _getattr_up(KX_TouchSensor);
322 }
323