| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * $Id$ | 
					
						
							| 
									
										
										
										
											2008-04-16 22:40:48 +00:00
										 |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							| 
									
										
										
										
											2008-04-16 22:40:48 +00:00
										 |  |  |  * of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is: all of this file. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): none yet. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2008-04-16 22:40:48 +00:00
										 |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "KX_TouchEventManager.h"
 | 
					
						
							|  |  |  | #include "SCA_ISensor.h"
 | 
					
						
							|  |  |  | #include "KX_TouchSensor.h"
 | 
					
						
							|  |  |  | #include "KX_GameObject.h"
 | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | #include "PHY_IPhysicsEnvironment.h"
 | 
					
						
							|  |  |  | #include "PHY_IPhysicsController.h"
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-25 15:29:57 +00:00
										 |  |  | #ifdef HAVE_CONFIG_H
 | 
					
						
							|  |  |  | #include <config.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | KX_TouchEventManager::KX_TouchEventManager(class SCA_LogicManager* logicmgr, | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | 	PHY_IPhysicsEnvironment* physEnv) | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	: SCA_EventManager(TOUCH_EVENTMGR), | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | 	  m_logicmgr(logicmgr), | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | 	  m_physEnv(physEnv) | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | 	//notm_scene->addTouchCallback(STATIC_RESPONSE, KX_TouchEventManager::collisionResponse, this);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	//m_scene->addTouchCallback(OBJECT_RESPONSE, KX_TouchEventManager::collisionResponse, this);
 | 
					
						
							|  |  |  | 	//m_scene->addTouchCallback(SENSOR_RESPONSE, KX_TouchEventManager::collisionResponse, this);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	m_physEnv->addTouchCallback(PHY_OBJECT_RESPONSE, KX_TouchEventManager::newCollisionResponse, this); | 
					
						
							|  |  |  | 	m_physEnv->addTouchCallback(PHY_SENSOR_RESPONSE, KX_TouchEventManager::newCollisionResponse, this); | 
					
						
							| 
									
										
										
										
											2008-03-01 19:17:37 +00:00
										 |  |  | 	m_physEnv->addTouchCallback(PHY_BROADPH_RESPONSE, KX_TouchEventManager::newBroadphaseResponse, this); | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-06 04:58:10 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | bool	KX_TouchEventManager::NewHandleCollision(void* object1, void* object2, const PHY_CollData *coll_data) | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | 	PHY_IPhysicsController* obj1 = static_cast<PHY_IPhysicsController*>(object1); | 
					
						
							|  |  |  | 	PHY_IPhysicsController* obj2 = static_cast<PHY_IPhysicsController*>(object2); | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | 	m_newCollisions.insert(std::pair<PHY_IPhysicsController*, PHY_IPhysicsController*>(obj1, obj2)); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-11-06 04:58:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | bool	 KX_TouchEventManager::newCollisionResponse(void *client_data,  | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | 							void *object1, | 
					
						
							|  |  |  | 							void *object2, | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | 							const PHY_CollData *coll_data) | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	KX_TouchEventManager *touchmgr = (KX_TouchEventManager *) client_data; | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | 	touchmgr->NewHandleCollision(object1, object2, coll_data); | 
					
						
							|  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-01 19:17:37 +00:00
										 |  |  | bool	 KX_TouchEventManager::newBroadphaseResponse(void *client_data,  | 
					
						
							|  |  |  | 							void *object1, | 
					
						
							|  |  |  | 							void *object2, | 
					
						
							|  |  |  | 							const PHY_CollData *coll_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	PHY_IPhysicsController* ctrl = static_cast<PHY_IPhysicsController*>(object1); | 
					
						
							|  |  |  | 	KX_ClientObjectInfo* info = (ctrl) ? static_cast<KX_ClientObjectInfo*>(ctrl->getNewClientInfo()) : NULL; | 
					
						
							|  |  |  | 	// This call back should only be called for controllers of Near and Radar sensor
 | 
					
						
							|  |  |  | 	if (info && | 
					
						
							|  |  |  |         info->m_sensors.size() == 1 && | 
					
						
							|  |  |  | 		(info->m_type == KX_ClientObjectInfo::NEAR || | 
					
						
							|  |  |  | 		 info->m_type == KX_ClientObjectInfo::RADAR)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		// only one sensor for this type of object
 | 
					
						
							|  |  |  | 		KX_TouchSensor* touchsensor = static_cast<KX_TouchSensor*>(*info->m_sensors.begin()); | 
					
						
							|  |  |  | 		return touchsensor->BroadPhaseFilterCollision(object1,object2); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | void KX_TouchEventManager::RegisterSensor(SCA_ISensor* sensor) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	KX_TouchSensor* touchsensor = static_cast<KX_TouchSensor*>(sensor); | 
					
						
							|  |  |  | 	m_sensors.push_back(touchsensor); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | 	touchsensor->RegisterSumo(this); | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void KX_TouchEventManager::EndFrame() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	vector<SCA_ISensor*>::iterator it; | 
					
						
							|  |  |  | 	for ( it = m_sensors.begin(); | 
					
						
							|  |  |  | 	!(it==m_sensors.end());it++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		((KX_TouchSensor*)*it)->EndFrame(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-16 11:41:50 +00:00
										 |  |  | void KX_TouchEventManager::NextFrame() | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (m_sensors.size() > 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		vector<SCA_ISensor*>::iterator it; | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2004-11-06 04:58:10 +00:00
										 |  |  | 		for (it = m_sensors.begin();!(it==m_sensors.end());++it) | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | 			static_cast<KX_TouchSensor*>(*it)->SynchronizeTransform(); | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | 		for (std::set<NewCollision>::iterator cit = m_newCollisions.begin(); cit != m_newCollisions.end(); ++cit) | 
					
						
							| 
									
										
										
										
											2004-11-22 10:19:19 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | 			PHY_IPhysicsController* ctrl1 = (*cit).first; | 
					
						
							| 
									
										
										
										
											2005-06-04 16:22:50 +00:00
										 |  |  | //			PHY_IPhysicsController* ctrl2 = (*cit).second;
 | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | //			KX_GameObject* gameOb1 = ctrl1->getClientInfo();
 | 
					
						
							|  |  |  | //			KX_GameObject* gameOb1 = ctrl1->getClientInfo();
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			KX_ClientObjectInfo *client_info = static_cast<KX_ClientObjectInfo *>(ctrl1->getNewClientInfo()); | 
					
						
							| 
									
										
										
										
											2004-11-22 10:19:19 +00:00
										 |  |  | 			list<SCA_ISensor*>::iterator sit; | 
					
						
							| 
									
										
										
										
											2008-07-08 22:43:44 +00:00
										 |  |  | 			if (client_info) { | 
					
						
							|  |  |  | 				for ( sit = client_info->m_sensors.begin(); sit != client_info->m_sensors.end(); ++sit) { | 
					
						
							|  |  |  | 					static_cast<KX_TouchSensor*>(*sit)->NewHandleCollision((*cit).first, (*cit).second, NULL); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | 			client_info = static_cast<KX_ClientObjectInfo *>((*cit).second->getNewClientInfo()); | 
					
						
							| 
									
										
										
										
											2008-07-08 22:43:44 +00:00
										 |  |  | 			if (client_info) { | 
					
						
							|  |  |  | 				for ( sit = client_info->m_sensors.begin(); sit != client_info->m_sensors.end(); ++sit) { | 
					
						
							|  |  |  | 					static_cast<KX_TouchSensor*>(*sit)->NewHandleCollision((*cit).second, (*cit).first, NULL); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2004-11-22 10:19:19 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2004-11-06 04:58:10 +00:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											2005-03-25 10:33:39 +00:00
										 |  |  | 		m_newCollisions.clear(); | 
					
						
							| 
									
										
										
										
											2004-11-06 04:58:10 +00:00
										 |  |  | 			 | 
					
						
							|  |  |  | 		for (it = m_sensors.begin();!(it==m_sensors.end());++it) | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 			(*it)->Activate(m_logicmgr,NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void KX_TouchEventManager::RemoveSensor(class SCA_ISensor* sensor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::vector<SCA_ISensor*>::iterator i = | 
					
						
							|  |  |  | 	std::find(m_sensors.begin(), m_sensors.end(), sensor); | 
					
						
							|  |  |  | 	if (!(i == m_sensors.end())) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2004-03-22 22:02:18 +00:00
										 |  |  | 		std::swap(*i, m_sensors.back()); | 
					
						
							|  |  |  | 		m_sensors.pop_back(); | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2004-11-22 10:19:19 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	// remove the sensor forever :)
 | 
					
						
							|  |  |  | 	SCA_EventManager::RemoveSensor(sensor); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |