BGE Scenegraph and View frustrum culling improvement.
[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 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43 KX_NearSensor::KX_NearSensor(SCA_EventManager* eventmgr,
44                                                          KX_GameObject* gameobj,
45                                                          float margin,
46                                                          float resetmargin,
47                                                          bool bFindMaterial,
48                                                          const STR_String& touchedpropname,
49                                                          class KX_Scene* scene,
50                                                          PHY_IPhysicsController*        ctrl,
51                                                          PyTypeObject* T)
52                          :KX_TouchSensor(eventmgr,
53                                                          gameobj,
54                                                          bFindMaterial,
55                                                          false,
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 void KX_NearSensor::UnregisterSumo(KX_TouchEventManager* touchman)
102 {
103         if (m_physCtrl)
104         {
105                 touchman->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
106         }
107 }
108
109 CValue* KX_NearSensor::GetReplica()
110 {
111         KX_NearSensor* replica = new KX_NearSensor(*this);
112         replica->m_colliders = new CListValue();
113         replica->Init();
114         // this will copy properties and so on...
115         CValue::AddDataToReplica(replica);
116         
117         replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::NEAR);
118         
119         if (replica->m_physCtrl)
120         {
121                 replica->m_physCtrl = replica->m_physCtrl->GetReplica();
122                 if (replica->m_physCtrl)
123                 {
124                         //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->addSensor(replica->m_physCtrl);
125                         replica->m_physCtrl->SetMargin(m_Margin);
126                         replica->m_physCtrl->setNewClientInfo(replica->m_client_info);
127                 }
128                 
129         }
130         //Wrong: the parent object could be a child, this code works only if it is a root parent.
131         //Anyway, at this stage, the parent object is already synchronized, nothing to do.
132         //bool parentUpdated = false;
133         //((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL, parentUpdated);
134         replica->SynchronizeTransform();
135         
136         return replica;
137 }
138
139
140
141 void KX_NearSensor::ReParent(SCA_IObject* parent)
142 {
143         m_client_info->m_gameobject = static_cast<KX_GameObject*>(parent); 
144         m_client_info->m_sensors.push_back(this);
145         
146
147 /*      KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
148         client_info->m_gameobject = gameobj;
149         client_info->m_auxilary_info = NULL;
150         
151         client_info->m_sensors.push_back(this);
152         SCA_ISensor::ReParent(parent);
153 */
154         //Not needed, was done in GetReplica() already
155         //bool parentUpdated = false;
156         //((KX_GameObject*)GetParent())->GetSGNode()->ComputeWorldTransforms(NULL,parentUpdated);
157         //SynchronizeTransform();
158         SCA_ISensor::ReParent(parent);
159 }
160
161
162
163 KX_NearSensor::~KX_NearSensor()
164 {
165         // for nearsensor, the sensor is the 'owner' of sumoobj
166         // for touchsensor, it's the parent
167         if (m_physCtrl)
168         {
169                 //static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
170                 delete m_physCtrl;
171                 m_physCtrl = NULL;
172         }
173         
174                 
175         if (m_client_info)
176                 delete m_client_info;
177 }
178
179
180 bool KX_NearSensor::Evaluate(CValue* event)
181 {
182         bool result = false;
183 //      KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
184
185         if (m_bTriggered != m_bLastTriggered)
186         {
187                 m_bLastTriggered = m_bTriggered;
188                 if (m_bTriggered)
189                 {
190                         if (m_physCtrl)
191                         {
192                                 m_physCtrl->SetRadius(m_ResetMargin);
193                         }
194                 } else
195                 {
196                         if (m_physCtrl)
197                         {
198                                 m_physCtrl->SetRadius(m_Margin);
199                         }
200
201                 }
202                 result = true;
203         }
204
205         return result;
206 }
207
208 // this function is called at broad phase stage to check if the two controller
209 // need to interact at all. It is used for Near/Radar sensor that don't need to
210 // check collision with object not included in filter
211 bool    KX_NearSensor::BroadPhaseFilterCollision(void*obj1,void*obj2)
212 {
213         KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
214         
215         // need the mapping from PHY_IPhysicsController to gameobjects now
216         assert(obj1==m_physCtrl && obj2);
217         KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>((static_cast<PHY_IPhysicsController*>(obj2))->getNewClientInfo());
218
219         KX_GameObject* gameobj = ( client_info ? 
220                         client_info->m_gameobject :
221                         NULL);
222         
223         if (gameobj && (gameobj != parent))
224         {
225                 // only take valid colliders
226                 if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
227                 {
228                         if ((m_touchedpropname.Length() == 0) || 
229                                 (gameobj->GetProperty(m_touchedpropname)))
230                         {
231                                 return true;
232                         }
233                 }
234         }
235
236         return false;
237 }
238
239 bool    KX_NearSensor::NewHandleCollision(void* obj1,void* obj2,const PHY_CollData * coll_data)
240 {
241 //      KX_TouchEventManager* toucheventmgr = static_cast<KX_TouchEventManager*>(m_eventmgr);
242 //      KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
243         
244         // need the mapping from PHY_IPhysicsController to gameobjects now
245         
246         KX_ClientObjectInfo* client_info =static_cast<KX_ClientObjectInfo*> (obj1 == m_physCtrl? 
247                                         ((PHY_IPhysicsController*)obj2)->getNewClientInfo() : 
248                                         ((PHY_IPhysicsController*)obj1)->getNewClientInfo());
249
250         KX_GameObject* gameobj = ( client_info ? 
251                         client_info->m_gameobject :
252                         NULL);
253         
254         // Add the same check as in SCA_ISensor::Activate(), 
255         // we don't want to record collision when the sensor is not active.
256         if (m_links && !m_suspended &&
257                 gameobj /* done in BroadPhaseFilterCollision() && (gameobj != parent)*/)
258         {
259                 if (!m_colliders->SearchValue(gameobj))
260                         m_colliders->Add(gameobj->AddRef());
261                 // only take valid colliders
262                 // These checks are done already in BroadPhaseFilterCollision()
263                 //if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
264                 //{
265                 //      if ((m_touchedpropname.Length() == 0) || 
266                 //              (gameobj->GetProperty(m_touchedpropname)))
267                 //      {
268                                 m_bTriggered = true;
269                                 m_hitObject = gameobj;
270                 //      }
271                 //}
272         }
273         
274         return false; // was DT_CONTINUE; but this was defined in Sumo as false
275 }
276
277
278 /* ------------------------------------------------------------------------- */
279 /* Python Functions                                                                                                                      */
280 /* ------------------------------------------------------------------------- */
281
282 //No methods
283
284 /* ------------------------------------------------------------------------- */
285 /* Python Integration Hooks                                                  */
286 /* ------------------------------------------------------------------------- */
287
288 PyTypeObject KX_NearSensor::Type = {
289         PyObject_HEAD_INIT(NULL)
290         0,
291         "KX_NearSensor",
292         sizeof(KX_NearSensor),
293         0,
294         PyDestructor,
295         0,
296         0,
297         0,
298         0,
299         py_base_repr,
300         0,0,0,0,0,0,
301         py_base_getattro,
302         py_base_setattro,
303         0,0,0,0,0,0,0,0,0,
304         Methods
305 };
306
307
308
309 PyParentObject KX_NearSensor::Parents[] = {
310         &KX_NearSensor::Type,
311         &KX_TouchSensor::Type,
312         &SCA_ISensor::Type,
313         &SCA_ILogicBrick::Type,
314         &CValue::Type,
315         NULL
316 };
317
318
319
320 PyMethodDef KX_NearSensor::Methods[] = {
321         //No methods
322         {NULL,NULL} //Sentinel
323 };
324
325 PyAttributeDef KX_NearSensor::Attributes[] = {
326         KX_PYATTRIBUTE_FLOAT_RW_CHECK("distance", 0, 100, KX_NearSensor, m_Margin, CheckResetDistance),
327         KX_PYATTRIBUTE_FLOAT_RW_CHECK("resetDistance", 0, 100, KX_NearSensor, m_ResetMargin, CheckResetDistance),
328         {NULL} //Sentinel
329 };
330
331
332 PyObject* KX_NearSensor::py_getattro(PyObject *attr)
333 {
334         py_getattro_up(KX_TouchSensor);
335 }
336
337 int KX_NearSensor::py_setattro(PyObject*attr, PyObject* value)
338 {
339         py_setattro_up(KX_TouchSensor);
340 }