blender/source/gameengine/Ketsji/KX_NearSensor.cpp

330 lines
8.4 KiB
C++
Raw Normal View History

2002-10-12 11:37:38 +00:00
/**
* Sense if other objects are near
*
* $Id$
*
* ***** 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
* 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.
*
* ***** END GPL LICENSE BLOCK *****
2002-10-12 11:37:38 +00:00
*/
#include "KX_NearSensor.h"
#include "SCA_LogicManager.h"
#include "KX_GameObject.h"
#include "KX_TouchEventManager.h"
#include "KX_Scene.h" // needed to create a replica
#include "PHY_IPhysicsEnvironment.h"
#include "PHY_IPhysicsController.h"
2002-10-12 11:37:38 +00:00
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
KX_NearSensor::KX_NearSensor(SCA_EventManager* eventmgr,
KX_GameObject* gameobj,
double margin,
double resetmargin,
bool bFindMaterial,
const STR_String& touchedpropname,
class KX_Scene* scene,
PHY_IPhysicsController* ctrl,
PyTypeObject* T)
:KX_TouchSensor(eventmgr,
gameobj,
bFindMaterial,
touchedpropname,
/* scene, */
T),
m_Margin(margin),
m_ResetMargin(resetmargin)
2002-10-12 11:37:38 +00:00
{
gameobj->getClientInfo()->m_sensors.remove(this);
m_client_info = new KX_ClientObjectInfo(gameobj, KX_ClientObjectInfo::NEAR);
m_client_info->m_sensors.push_back(this);
//DT_ShapeHandle shape = (DT_ShapeHandle) vshape;
m_physCtrl = ctrl;
if (m_physCtrl)
{
m_physCtrl->SetMargin(m_Margin);
m_physCtrl->setNewClientInfo(m_client_info);
}
SynchronizeTransform();
2002-10-12 11:37:38 +00:00
}
void KX_NearSensor::SynchronizeTransform()
{
// The near and radar sensors are using a different physical object which is
// not linked to the parent object, must synchronize it.
if (m_physCtrl)
{
KX_GameObject* parent = ((KX_GameObject*)GetParent());
MT_Vector3 pos = parent->NodeGetWorldPosition();
MT_Quaternion orn = parent->NodeGetWorldOrientation().getRotation();
m_physCtrl->setPosition(pos.x(),pos.y(),pos.z());
m_physCtrl->setOrientation(orn.x(),orn.y(),orn.z(),orn.w());
m_physCtrl->calcXform();
}
}
void KX_NearSensor::RegisterSumo(KX_TouchEventManager *touchman)
{
if (m_physCtrl)
{
touchman->GetPhysicsEnvironment()->addSensor(m_physCtrl);
}
}
2002-10-12 11:37:38 +00:00
void KX_NearSensor::UnregisterSumo(KX_TouchEventManager* touchman)
{
if (m_physCtrl)
{
touchman->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
}
}
2002-10-12 11:37:38 +00:00
CValue* KX_NearSensor::GetReplica()
{
KX_NearSensor* replica = new KX_NearSensor(*this);
replica->m_colliders = new CListValue();
BGE logic update: new servo control motion actuator, new distance constraint actuator, new orientation constraint actuator, new actuator sensor. General ======= - Removal of Damp option in motion actuator (replaced by Servo control motion). - No PyDoc at present, will be added soon. Generalization of the Lvl option ================================ A sensor with the Lvl option selected will always produce an event at the start of the game or when entering a state or at object creation. The event will be positive or negative depending of the sensor condition. A negative pulse makes sense when used with a NAND controller: it will be converted into an actuator activation. Servo control motion ==================== A new variant of the motion actuator allows to control speed with force. The control if of type "PID" (Propotional, Integral, Derivate): the force is automatically adapted to achieve the target speed. All the parameters of the servo controller are configurable. The result is a great variety of motion style: anysotropic friction, flying, sliding, pseudo Dloc... This actuator should be used in preference to Dloc and LinV as it produces more fluid movements and avoids the collision problem with Dloc. LinV : target speed as (X,Y,Z) vector in local or world coordinates (mostly useful in local coordinates). Limit: the force can be limited along each axis (in the same coordinates of LinV). No limitation means that the force will grow as large as necessary to achieve the target speed along that axis. Set a max value to limit the accelaration along an axis (slow start) and set a min value (negative) to limit the brake force. P: Proportional coefficient of servo controller, don't set directly unless you know what you're doing. I: Integral coefficient of servo controller. Use low value (<0.1) for slow reaction (sliding), high values (>0.5) for hard control. The P coefficient will be automatically set to 60 times the I coefficient (a reasonable value). D: Derivate coefficient. Leave to 0 unless you know what you're doing. High values create instability. Notes: - This actuator works perfectly in zero friction environment: the PID controller will simulate friction by applying force as needed. - This actuator is compatible with simple Drot motion actuator but not with LinV and Dloc motion. - (0,0,0) is a valid target speed. - All parameters are accessible through Python. Distance constraint actuator ============================ A new variant of the constraint actuator allows to set the distance and orientation relative to a surface. The controller uses a ray to detect the surface (or any object) and adapt the distance and orientation parallel to the surface. Damp: Time constant (in nb of frames) of distance and orientation control. Dist: Select to enable distance control and set target distance. The object will be position at the given distance of surface along the ray direction. Direction: chose a local axis as the ray direction. Range: length of ray. Objecgt within this distance will be detected. N : Select to enable orientation control. The actuator will change the orientation and the location of the object so that it is parallel to the surface at the vertical of the point of contact of the ray. M/P : Select to enable material detection. Default is property detection. Property/Material: name of property/material that the target of ray must have to be detected. If not set, property/ material filter is disabled and any collisioning object within range will be detected. PER : Select to enable persistent operation. Normally the actuator disables itself automatically if the ray does not reach a valid target. time : Maximum activation time of actuator. 0 : unlimited. >0: number of frames before automatic deactivation. rotDamp: Time constant (in nb of frame) of orientation control. 0 : use Damp parameter. >0: use a different time constant for orientation. Notes: - If neither N nor Dist options are set, the actuator does not change the position and orientation of the object; it works as a ray sensor. - The ray has no "X-ray" capability: if the first object hit does not have the required property/material, it returns no hit and the actuator disables itself unless PER option is enabled. - This actuator changes the position and orientation but not the speed of the object. This has an important implication in a gravity environment: the gravity will cause the speed to increase although the object seems to stay still (it is repositioned at each frame). The gravity must be compensated in one way or another. the new servo control motion actuator is the simplest way: set the target speed along the ray axis to 0 and the servo control will automatically compensate the gravity. - This actuator changes the orientation of the object and will conflict with Drot motion unless it is placed BEFORE the Drot motion actuator (the order of actuator is important) - All parameters are accessible through Python. Orientation constraint ====================== A new variant of the constraint actuator allows to align an object axis along a global direction. Damp : Time constant (in nb of frames) of orientation control. X,Y,Z: Global coordinates of reference direction. time : Maximum activation time of actuator. 0 : unlimited. >0: number of frames before automatic deactivation. Notes: - (X,Y,Z) = (0,0,0) is not a valid direction - This actuator changes the orientation of the object and will conflict with Drot motion unless it is placed BEFORE the Drot motion actuator (the order of actuator is important). - This actuator doesn't change the location and speed. It is compatible with gravity. - All parameters are accessible through Python. Actuator sensor =============== This sensor detects the activation and deactivation of actuators of the same object. The sensor generates a positive pulse when the corresponding sensor is activated and a negative pulse when it is deactivated (the contrary if the Inv option is selected). This is mostly useful to chain actions and to detect the loss of contact of the distance motion actuator. Notes: - Actuators are disabled at the start of the game; if you want to detect the On-Off transition of an actuator after it has been activated at least once, unselect the Lvl and Inv options and use a NAND controller. - Some actuators deactivates themselves immediately after being activated. The sensor detects this situation as an On-Off transition. - The actuator name can be set through Python.
2008-07-04 08:14:50 +00:00
replica->Init();
2002-10-12 11:37:38 +00:00
// this will copy properties and so on...
CValue::AddDataToReplica(replica);
replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::NEAR);
if (replica->m_physCtrl)
{
replica->m_physCtrl = replica->m_physCtrl->GetReplica();
if (replica->m_physCtrl)
{
//static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->addSensor(replica->m_physCtrl);
replica->m_physCtrl->SetMargin(m_Margin);
replica->m_physCtrl->setNewClientInfo(replica->m_client_info);
}
}
//static_cast<KX_TouchEventManager*>(m_eventmgr)->RegisterSensor(this);
//todo: make sure replication works fine
//>m_sumoObj = new SM_Object(DT_NewSphere(0.0),NULL,NULL,NULL);
//replica->m_sumoObj->setMargin(m_Margin);
//replica->m_sumoObj->setClientObject(replica->m_client_info);
((KX_GameObject*)replica->GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
replica->SynchronizeTransform();
2002-10-12 11:37:38 +00:00
return replica;
}
void KX_NearSensor::ReParent(SCA_IObject* parent)
{
m_client_info->m_gameobject = static_cast<KX_GameObject*>(parent);
m_client_info->m_sensors.push_back(this);
2002-10-12 11:37:38 +00:00
/* KX_ClientObjectInfo *client_info = gameobj->getClientInfo();
client_info->m_gameobject = gameobj;
client_info->m_auxilary_info = NULL;
client_info->m_sensors.push_back(this);
SCA_ISensor::ReParent(parent);
*/
((KX_GameObject*)GetParent())->GetSGNode()->ComputeWorldTransforms(NULL);
SynchronizeTransform();
SCA_ISensor::ReParent(parent);
2002-10-12 11:37:38 +00:00
}
KX_NearSensor::~KX_NearSensor()
{
// for nearsensor, the sensor is the 'owner' of sumoobj
// for touchsensor, it's the parent
if (m_physCtrl)
{
//static_cast<KX_TouchEventManager*>(m_eventmgr)->GetPhysicsEnvironment()->removeSensor(m_physCtrl);
delete m_physCtrl;
m_physCtrl = NULL;
}
if (m_client_info)
delete m_client_info;
2002-10-12 11:37:38 +00:00
}
bool KX_NearSensor::Evaluate(CValue* event)
{
bool result = false;
// KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
2002-10-12 11:37:38 +00:00
if (m_bTriggered != m_bLastTriggered)
{
m_bLastTriggered = m_bTriggered;
if (m_bTriggered)
{
if (m_physCtrl)
2002-10-12 11:37:38 +00:00
{
m_physCtrl->SetMargin(m_ResetMargin);
2002-10-12 11:37:38 +00:00
}
} else
{
if (m_physCtrl)
2002-10-12 11:37:38 +00:00
{
m_physCtrl->SetMargin(m_Margin);
2002-10-12 11:37:38 +00:00
}
}
result = true;
}
return result;
}
// this function is called at broad phase stage to check if the two controller
// need to interact at all. It is used for Near/Radar sensor that don't need to
// check collision with object not included in filter
bool KX_NearSensor::BroadPhaseFilterCollision(void*obj1,void*obj2)
{
KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
// need the mapping from PHY_IPhysicsController to gameobjects now
assert(obj1==m_physCtrl && obj2);
KX_ClientObjectInfo* client_info = static_cast<KX_ClientObjectInfo*>((static_cast<PHY_IPhysicsController*>(obj2))->getNewClientInfo());
2002-10-12 11:37:38 +00:00
KX_GameObject* gameobj = ( client_info ?
client_info->m_gameobject :
NULL);
if (gameobj && (gameobj != parent))
{
// only take valid colliders
if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
{
if ((m_touchedpropname.Length() == 0) ||
(gameobj->GetProperty(m_touchedpropname)))
{
return true;
}
}
}
return false;
}
2002-10-12 11:37:38 +00:00
bool KX_NearSensor::NewHandleCollision(void* obj1,void* obj2,const PHY_CollData * coll_data)
2002-10-12 11:37:38 +00:00
{
// KX_TouchEventManager* toucheventmgr = static_cast<KX_TouchEventManager*>(m_eventmgr);
KX_GameObject* parent = static_cast<KX_GameObject*>(GetParent());
// need the mapping from PHY_IPhysicsController to gameobjects now
2002-10-12 11:37:38 +00:00
KX_ClientObjectInfo* client_info =static_cast<KX_ClientObjectInfo*> (obj1 == m_physCtrl?
((PHY_IPhysicsController*)obj2)->getNewClientInfo() :
((PHY_IPhysicsController*)obj1)->getNewClientInfo());
2002-10-12 11:37:38 +00:00
KX_GameObject* gameobj = ( client_info ?
client_info->m_gameobject :
2002-10-12 11:37:38 +00:00
NULL);
// these checks are done already in BroadPhaseFilterCollision()
if (gameobj /*&& (gameobj != parent)*/)
2002-10-12 11:37:38 +00:00
{
if (!m_colliders->SearchValue(gameobj))
m_colliders->Add(gameobj->AddRef());
// only take valid colliders
// These checks are done already in BroadPhaseFilterCollision()
//if (client_info->m_type == KX_ClientObjectInfo::ACTOR)
//{
// if ((m_touchedpropname.Length() == 0) ||
// (gameobj->GetProperty(m_touchedpropname)))
// {
2002-10-12 11:37:38 +00:00
m_bTriggered = true;
m_hitObject = gameobj;
// }
//}
2002-10-12 11:37:38 +00:00
}
return DT_CONTINUE;
2002-10-12 11:37:38 +00:00
}
// python embedding
PyTypeObject KX_NearSensor::Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"KX_NearSensor",
sizeof(KX_NearSensor),
0,
PyDestructor,
0,
__getattr,
__setattr,
0, //&MyPyCompare,
__repr,
0, //&cvalue_as_number,
0,
0,
0,
0
};
PyParentObject KX_NearSensor::Parents[] = {
&KX_NearSensor::Type,
&KX_TouchSensor::Type,
&SCA_ISensor::Type,
&SCA_ILogicBrick::Type,
&CValue::Type,
NULL
};
PyMethodDef KX_NearSensor::Methods[] = {
{"setProperty",
(PyCFunction) KX_NearSensor::sPySetProperty, METH_VARARGS, SetProperty_doc},
{"getProperty",
(PyCFunction) KX_NearSensor::sPyGetProperty, METH_VARARGS, GetProperty_doc},
{"getHitObject",
(PyCFunction) KX_NearSensor::sPyGetHitObject, METH_VARARGS, GetHitObject_doc},
{"getHitObjectList",
(PyCFunction) KX_NearSensor::sPyGetHitObjectList, METH_VARARGS, GetHitObjectList_doc},
{NULL,NULL} //Sentinel
};
PyObject*
KX_NearSensor::_getattr(const STR_String& attr)
2002-10-12 11:37:38 +00:00
{
_getattr_up(KX_TouchSensor);
}