blender/source/gameengine/Ketsji/KX_RadarSensor.cpp

318 lines
8.7 KiB
C++
Raw Normal View History

2002-10-12 11:37:38 +00:00
/**
* $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_RadarSensor.h"
#include "KX_GameObject.h"
#include "KX_PyMath.h"
#include "PHY_IPhysicsController.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
2002-10-12 11:37:38 +00:00
/**
* RadarSensor constructor. Creates a near-sensor derived class, with a cone collision shape.
*/
KX_RadarSensor::KX_RadarSensor(SCA_EventManager* eventmgr,
KX_GameObject* gameobj,
PHY_IPhysicsController* physCtrl,
2002-10-12 11:37:38 +00:00
double coneradius,
double coneheight,
int axis,
double margin,
double resetmargin,
bool bFindMaterial,
const STR_String& touchedpropname,
class KX_Scene* kxscene,
PyTypeObject* T)
: KX_NearSensor(
eventmgr,
gameobj,
//DT_NewCone(coneradius,coneheight),
2002-10-12 11:37:38 +00:00
margin,
resetmargin,
bFindMaterial,
touchedpropname,
kxscene,
physCtrl,
2002-10-12 11:37:38 +00:00
T),
m_coneradius(coneradius),
m_coneheight(coneheight),
2002-10-12 11:37:38 +00:00
m_axis(axis)
{
m_client_info->m_type = KX_ClientObjectInfo::RADAR;
//m_client_info->m_clientobject = gameobj;
//m_client_info->m_auxilary_info = NULL;
//sumoObj->setClientObject(&m_client_info);
2002-10-12 11:37:38 +00:00
}
KX_RadarSensor::~KX_RadarSensor()
{
}
CValue* KX_RadarSensor::GetReplica()
{
KX_RadarSensor* replica = new KX_RadarSensor(*this);
replica->m_colliders = new CListValue();
BGE patch: DUPLIGROUP option supported in BGE. Blender duplicates groups in the 3D view at the location of objects having the DUPLIGROUP option set. This feature is now supported in the BGE: the groups will be instantiated as in the 3D view when the scene is converted. This is useful to populate a scene with multiple enemies without having to actually duplicate the objects in the blend file. Notes: * The BGE applies the same criteria to instantiate the group as Blender to display them: if you see the group in the 3D view, it will be instantiated in the BGE. * Groups are instantiated as if the object having the DUPLIGROUP option (usually an empty) executed an AddObject actuator on the top objects of the group (objects without parent). * As a result, only intra-group parent relationship is supported: the BGE will not instantiate objects that have parents outside the group. * Intra-group logic bricks connections are preserved between the duplicated objects, even between the top objects of the group. * For best result, the state engine of the objects in the group should be self-contained: logic bricks should only have intra-group connections. Use messages to communicate with state engines outside the group. * Nested groups are supported: if one or more objects in the group have the DUPLIGROUP option set, the corresponding groups will be instantiated at the corresponding position and orientation. * Nested groups are instantiated as separate groups, not as one big group. * Linked groups are supported as well as groups containing objects from the active layers. * There is a difference in the way Blender displays the groups in the 3D view and how BGE instantiates them: Blender does not take into account the parent relationship in the group and displays the objects as if they were all children of the object having the DUPLIGROUP option. That's correct for the top objects of the group but not for the children. Hence the orientation of the children objects may be different in the BGE. * An AddGroup actuator will be added in a future release.
2008-07-15 20:05:23 +00:00
replica->Init();
// this will copy properties and so on...
CValue::AddDataToReplica(replica);
replica->m_client_info = new KX_ClientObjectInfo(m_client_info->m_gameobject, KX_ClientObjectInfo::RADAR);
if (replica->m_physCtrl)
{
replica->m_physCtrl = replica->m_physCtrl->GetReplica();
if (replica->m_physCtrl)
{
replica->m_physCtrl->setNewClientInfo(replica->m_client_info);
}
}
//todo: make sure replication works fine!
//>m_sumoObj = new SM_Object(DT_NewCone(m_coneradius, m_coneheight),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();
return replica;
}
2002-10-12 11:37:38 +00:00
/**
* Transforms the collision object. A cone is not correctly centered
* for usage. */
void KX_RadarSensor::SynchronizeTransform()
{
// Getting the parent location was commented out. Why?
MT_Transform trans;
trans.setOrigin(((KX_GameObject*)GetParent())->NodeGetWorldPosition());
trans.setBasis(((KX_GameObject*)GetParent())->NodeGetWorldOrientation());
// What is the default orientation? pointing in the -y direction?
// is the geometry correctly converted?
// a collision cone is oriented
// center the cone correctly
// depends on the radar 'axis'
switch (m_axis)
{
case 0: // +X Axis
2002-10-12 11:37:38 +00:00
{
MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));
trans.rotate(rotquatje);
trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
break;
};
case 1: // +Y Axis
2002-10-12 11:37:38 +00:00
{
MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
trans.rotate(rotquatje);
trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
break;
};
case 2: // +Z Axis
2002-10-12 11:37:38 +00:00
{
MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-90));
trans.rotate(rotquatje);
trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
break;
};
case 3: // -X Axis
{
MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(-90));
trans.rotate(rotquatje);
trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
break;
};
case 4: // -Y Axis
{
//MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(-180));
//trans.rotate(rotquatje);
trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
break;
};
case 5: // -Z Axis
{
MT_Quaternion rotquatje(MT_Vector3(1,0,0),MT_radians(90));
trans.rotate(rotquatje);
trans.translate(MT_Vector3 (0, -m_coneheight/2.0 ,0));
break;
};
2002-10-12 11:37:38 +00:00
default:
{
}
}
//Using a temp variable to translate MT_Point3 to float[3].
//float[3] works better for the Python interface.
MT_Point3 temp = trans.getOrigin();
m_cone_origin[0] = temp[0];
m_cone_origin[1] = temp[1];
m_cone_origin[2] = temp[2];
temp = trans(MT_Point3(0, -m_coneheight/2.0 ,0));
m_cone_target[0] = temp[0];
m_cone_target[1] = temp[1];
m_cone_target[2] = temp[2];
2002-10-12 11:37:38 +00:00
if (m_physCtrl)
{
MT_Quaternion orn = trans.getRotation();
MT_Point3 pos = trans.getOrigin();
m_physCtrl->setPosition(pos[0],pos[1],pos[2]);
m_physCtrl->setOrientation(orn[0],orn[1],orn[2],orn[3]);
m_physCtrl->calcXform();
}
2002-10-12 11:37:38 +00:00
}
/* ------------------------------------------------------------------------- */
/* Python Functions */
2002-10-12 11:37:38 +00:00
/* ------------------------------------------------------------------------- */
//Deprecated ----->
/* getConeOrigin */
const char KX_RadarSensor::GetConeOrigin_doc[] =
"getConeOrigin()\n"
"\tReturns the origin of the cone with which to test. The origin\n"
"\tis in the middle of the cone.";
PyObject* KX_RadarSensor::PyGetConeOrigin(PyObject* self) {
ShowDeprecationWarning("getConeOrigin()", "the coneOrigin property");
PyObject *retVal = PyList_New(3);
PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_origin[0]));
PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_origin[1]));
PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_origin[2]));
return retVal;
}
/* getConeOrigin */
const char KX_RadarSensor::GetConeTarget_doc[] =
"getConeTarget()\n"
"\tReturns the center of the bottom face of the cone with which to test.\n";
PyObject* KX_RadarSensor::PyGetConeTarget(PyObject* self) {
ShowDeprecationWarning("getConeTarget()", "the coneTarget property");
PyObject *retVal = PyList_New(3);
PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_cone_target[0]));
PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_cone_target[1]));
PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_cone_target[2]));
return retVal;
}
/* getConeHeight */
const char KX_RadarSensor::GetConeHeight_doc[] =
"getConeHeight()\n"
"\tReturns the height of the cone with which to test.\n";
PyObject* KX_RadarSensor::PyGetConeHeight(PyObject* self) {
ShowDeprecationWarning("getConeHeight()", "the distance property");
return PyFloat_FromDouble(m_coneheight);
}
//<----- Deprecated
/* ------------------------------------------------------------------------- */
/* Python Integration Hooks */
/* ------------------------------------------------------------------------- */
2002-10-12 11:37:38 +00:00
PyTypeObject KX_RadarSensor::Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"KX_RadarSensor",
sizeof(KX_RadarSensor),
0,
PyDestructor,
0,
__getattr,
__setattr,
0, //&MyPyCompare,
__repr,
0, //&cvalue_as_number,
0,
0,
0,
0
};
PyParentObject KX_RadarSensor::Parents[] = {
&KX_RadarSensor::Type,
&KX_NearSensor::Type,
&KX_TouchSensor::Type,
&SCA_ISensor::Type,
&SCA_ILogicBrick::Type,
&CValue::Type,
NULL
};
PyMethodDef KX_RadarSensor::Methods[] = {
//Deprecated ----->
2002-10-12 11:37:38 +00:00
{"getConeOrigin", (PyCFunction) KX_RadarSensor::sPyGetConeOrigin,
METH_VARARGS, (PY_METHODCHAR)GetConeOrigin_doc},
2002-10-12 11:37:38 +00:00
{"getConeTarget", (PyCFunction) KX_RadarSensor::sPyGetConeTarget,
METH_VARARGS, (PY_METHODCHAR)GetConeTarget_doc},
2002-10-12 11:37:38 +00:00
{"getConeHeight", (PyCFunction) KX_RadarSensor::sPyGetConeHeight,
METH_VARARGS, (PY_METHODCHAR)GetConeHeight_doc},
//<-----
{NULL} //Sentinel
2002-10-12 11:37:38 +00:00
};
PyAttributeDef KX_RadarSensor::Attributes[] = {
KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneOrigin", KX_RadarSensor, m_cone_origin, 3),
KX_PYATTRIBUTE_FLOAT_ARRAY_RO("coneTarget", KX_RadarSensor, m_cone_target, 3),
KX_PYATTRIBUTE_FLOAT_RW("angle", 0, 360, KX_RadarSensor, m_coneradius),
KX_PYATTRIBUTE_INT_RW("axis", 0, 5, true, KX_RadarSensor, m_axis),
{NULL} //Sentinel
};
PyObject* KX_RadarSensor::_getattr(const char *attr)
{
PyObject* object = _getattr_self(Attributes, this, attr);
if (object != NULL)
return object;
2002-10-12 11:37:38 +00:00
_getattr_up(KX_NearSensor);
2002-10-12 11:37:38 +00:00
}
int KX_RadarSensor::_setattr(const char *attr, PyObject* value)
{
int ret = _setattr_self(Attributes, this, attr, value);
if (ret >= 0)
return ret;
2002-10-12 11:37:38 +00:00
return KX_NearSensor::_setattr(attr, value);
2002-10-12 11:37:38 +00:00
}