Commit patch #8799: Realtime SetParent function in the BGE

This patch consists in new KX_GameObject::SetParent() and KX_GameObject::RemoveParent() functions to create and destroy parent relation during game. These functions are accessible through python and through a new actuator KX_ParentActuator. Function documentation in PyDoc.
The object keeps its orientation, position and scale when it is parented but will further rotate, move and scale with its parent from that point on. When the parent relation is broken, the object keeps the orientation, position and scale it had at that time.
The function has no effect if any of the X/Y/Z scale of the object or its new parent are below Epsilon.
This commit is contained in:
Benoit Bolsee 2008-04-06 18:30:52 +00:00
parent 711306c2ba
commit e7384c9dd2
13 changed files with 474 additions and 5 deletions

@ -453,6 +453,9 @@
<File
RelativePath="..\..\..\source\gameengine\Ketsji\KX_ObjectActuator.cpp">
</File>
<File
RelativePath="..\..\..\source\gameengine\Ketsji\KX_ParentActuator.cpp">
</File>
<File
RelativePath="..\..\..\source\gameengine\Ketsji\KX_SCA_AddObjectActuator.cpp">
</File>
@ -668,6 +671,9 @@
<File
RelativePath="..\..\..\source\gameengine\Ketsji\KX_ObjectActuator.h">
</File>
<File
RelativePath="..\..\..\source\gameengine\Ketsji\KX_ParentActuator.h">
</File>
<File
RelativePath="..\..\..\source\gameengine\Ketsji\KX_SCA_AddObjectActuator.h">
</File>

@ -464,6 +464,9 @@ void init_actuator(bActuator *act)
case ACT_2DFILTER:
act->data = MEM_callocN(sizeof( bTwoDFilterActuator ), "2d filter act");
break;
case ACT_PARENT:
act->data = MEM_callocN(sizeof( bParentActuator ), "parent act");
break;
default:
; /* this is very severe... I cannot make any memory for this */
/* logic brick... */

@ -2982,6 +2982,10 @@ static void lib_link_object(FileData *fd, Main *main)
bTwoDFilterActuator *_2dfa = act->data;
_2dfa->text= newlibadr(fd, ob->id.lib, _2dfa->text);
}
else if(act->type==ACT_PARENT) {
bParentActuator *parenta = act->data;
parenta->ob = newlibadr(fd, ob->id.lib, parenta->ob);
}
act= act->next;
}

@ -738,6 +738,9 @@ static void write_actuators(WriteData *wd, ListBase *lb)
case ACT_2DFILTER:
writestruct(wd, DATA, "bTwoDFilterActuator", 1, act->data);
break;
case ACT_PARENT:
writestruct(wd, DATA, "bParentActuator", 1, act->data);
break;
default:
; /* error: don't know how to write this file */
}

@ -205,6 +205,12 @@ typedef struct bTwoDFilterActuator{
struct Text *text;
}bTwoDFilterActuator;
typedef struct bParentActuator {
char pad[4];
int type;
struct Object *ob;
} bParentActuator;
typedef struct bActuator {
struct bActuator *next, *prev, *mynew;
short type;
@ -274,6 +280,7 @@ typedef struct FreeCamera {
#define ACT_GAME 17
#define ACT_VISIBILITY 18
#define ACT_2DFILTER 19
#define ACT_PARENT 20
/* actuator flag */
#define ACT_SHOW 1
@ -423,6 +430,10 @@ typedef struct FreeCamera {
#define ACT_2DFILTER_INVERT 11
#define ACT_2DFILTER_CUSTOMFILTER 12
#define ACT_2DFILTER_NUMBER_OF_FILTERS 13
/* parentactuator->type */
#define ACT_PARENT_SET 0
#define ACT_PARENT_REMOVE 1
#endif

@ -714,6 +714,8 @@ static char *actuator_name(int type)
return "Visibility";
case ACT_2DFILTER:
return "2D Filter";
case ACT_PARENT:
return "Parent";
}
return "unknown";
}
@ -729,13 +731,13 @@ static char *actuator_pup(Object *owner)
return "Actuators %t|Action %x15|Motion %x0|Constraint %x9|Ipo %x1"
"|Camera %x3|Sound %x5|Property %x6|Edit Object %x10"
"|Scene %x11|Random %x13|Message %x14|CD %x16|Game %x17"
"|Visibility %x18|2D Filter %x19";
"|Visibility %x18|2D Filter %x19|Parent %x20";
break;
default:
return "Actuators %t|Motion %x0|Constraint %x9|Ipo %x1"
"|Camera %x3|Sound %x5|Property %x6|Edit Object %x10"
"|Scene %x11|Random %x13|Message %x14|CD %x16|Game %x17"
"|Visibility %x18|2D Filter %x19";
"|Visibility %x18|2D Filter %x19|Parent %x20";
}
}
@ -1474,6 +1476,7 @@ static short draw_actuatorbuttons(bActuator *act, uiBlock *block, short xco, sho
bGameActuator *gma = NULL;
bVisibilityActuator *visAct = NULL;
bTwoDFilterActuator *tdfa = NULL;
bParentActuator *parAct = NULL;
float *fp;
short ysize = 0, wval;
@ -2222,6 +2225,29 @@ static short draw_actuatorbuttons(bActuator *act, uiBlock *block, short xco, sho
yco -= ysize;
break;
case ACT_PARENT:
parAct = act->data;
if(parAct->type==ACT_PARENT_SET) {
ysize= 48;
glRects(xco, yco-ysize, xco+width, yco);
uiEmboss((float)xco, (float)yco-ysize, (float)xco+width, (float)yco, 1);
uiDefIDPoinBut(block, test_obpoin_but, ID_OB, 1, "OB:", xco+40, yco-44, (width-80), 19, &(parAct->ob), "Set this object as parent");
}
else if(parAct->type==ACT_PARENT_REMOVE) {
ysize= 28;
glRects(xco, yco-ysize, xco+width, yco);
uiEmboss((float)xco, (float)yco-ysize, (float)xco+width, (float)yco, 1);
}
str= "Parent %t|Set Parent %x0|Remove Parent %x1";
uiDefButI(block, MENU, B_REDR, str, xco+40, yco-24, (width-80), 19, &parAct->type, 0.0, 0.0, 0, 0, "");
yco-= ysize;
break;
default:
ysize= 4;

@ -49,7 +49,6 @@
#include "SCA_RandomActuator.h"
#include "SCA_2DFilterActuator.h"
// Ketsji specific logicbricks
#include "KX_SceneActuator.h"
#include "KX_IpoActuator.h"
@ -64,6 +63,7 @@
#include "KX_SCA_AddObjectActuator.h"
#include "KX_SCA_EndObjectActuator.h"
#include "KX_SCA_ReplaceMeshActuator.h"
#include "KX_ParentActuator.h"
#include "KX_Scene.h"
#include "KX_KetsjiEngine.h"
@ -917,6 +917,32 @@ void BL_ConvertActuators(char* maggiename,
}
break;
case ACT_PARENT:
{
bParentActuator *parAct = (bParentActuator *) bact->data;
int mode = KX_ParentActuator::KX_PARENT_NODEF;
KX_GameObject *tmpgob;
switch(parAct->type)
{
case ACT_PARENT_SET:
mode = KX_ParentActuator::KX_PARENT_SET;
tmpgob = converter->FindGameObject(parAct->ob);
break;
case ACT_PARENT_REMOVE:
mode = KX_ParentActuator::KX_PARENT_REMOVE;
tmpgob = NULL;
break;
}
KX_ParentActuator *tmpparact
= new KX_ParentActuator(gameobj,
mode,
tmpgob);
baseact = tmpparact;
break;
}
default:
; /* generate some error */
}

@ -62,7 +62,7 @@ typedef unsigned long uint_ptr;
#include "KX_ClientObjectInfo.h"
#include "RAS_BucketManager.h"
#include "KX_RayCast.h"
#include "KX_PythonInit.h"
#include "KX_PyMath.h"
// This file defines relationships between parents and children
@ -88,7 +88,7 @@ KX_GameObject::KX_GameObject(
m_ignore_activity_culling = false;
m_pClient_info = new KX_ClientObjectInfo(this, KX_ClientObjectInfo::ACTOR);
m_pSGNode = new SG_Node(this,sgReplicationInfo,callbacks);
// define the relationship between this node and it's parent.
KX_NormalParentRelation * parent_relation =
@ -204,7 +204,62 @@ KX_GameObject* KX_GameObject::GetParent()
}
void KX_GameObject::SetParent(KX_Scene *scene, KX_GameObject* obj)
{
if (obj && GetSGNode()->GetSGParent() != obj->GetSGNode())
{
// Make sure the objects have some scale
MT_Vector3 scale1 = NodeGetWorldScaling();
MT_Vector3 scale2 = obj->NodeGetWorldScaling();
if (fabs(scale2[0]) < FLT_EPSILON ||
fabs(scale2[1]) < FLT_EPSILON ||
fabs(scale2[2]) < FLT_EPSILON ||
fabs(scale1[0]) < FLT_EPSILON ||
fabs(scale1[1]) < FLT_EPSILON ||
fabs(scale1[2]) < FLT_EPSILON) { return; }
// Remove us from our old parent and set our new parent
RemoveParent(scene);
obj->GetSGNode()->AddChild(GetSGNode());
// Set us to our new scale, position, and orientation
scale1[0] = scale1[0]/scale2[0];
scale1[1] = scale1[1]/scale2[1];
scale1[2] = scale1[2]/scale2[2];
MT_Matrix3x3 invori = obj->NodeGetWorldOrientation().inverse();
MT_Vector3 newpos = invori*(NodeGetWorldPosition()-obj->NodeGetWorldPosition())*scale1;
NodeSetLocalScale(scale1);
NodeSetLocalPosition(MT_Point3(newpos[0],newpos[1],newpos[2]));
NodeSetLocalOrientation(NodeGetWorldOrientation()*invori);
NodeUpdateGS(0.f,true);
// object will now be a child, it must be removed from the parent list
CListValue* rootlist = scene->GetRootParentList();
if (rootlist->RemoveValue(this))
// the object was in parent list, decrement ref count as it's now removed
Release();
}
}
void KX_GameObject::RemoveParent(KX_Scene *scene)
{
if (GetSGNode()->GetSGParent())
{
// Set us to the right spot
GetSGNode()->SetLocalScale(GetSGNode()->GetWorldScaling());
GetSGNode()->SetLocalOrientation(GetSGNode()->GetWorldOrientation());
GetSGNode()->SetLocalPosition(GetSGNode()->GetWorldPosition());
// Remove us from our parent
GetSGNode()->DisconnectFromParent();
NodeUpdateGS(0.f,true);
// the object is now a root object, add it to the parentlist
CListValue* rootlist = scene->GetRootParentList();
if (!rootlist->SearchValue(this))
// object was not in root list, add it now and increment ref count
rootlist->Add(AddRef());
}
}
void KX_GameObject::ProcessReplica(KX_GameObject* replica)
{
@ -657,6 +712,8 @@ PyMethodDef KX_GameObject::Methods[] = {
{"enableRigidBody", (PyCFunction)KX_GameObject::sPyEnableRigidBody,METH_VARARGS},
{"disableRigidBody", (PyCFunction)KX_GameObject::sPyDisableRigidBody,METH_VARARGS},
{"getParent", (PyCFunction)KX_GameObject::sPyGetParent,METH_VARARGS},
{"setParent", (PyCFunction)KX_GameObject::sPySetParent,METH_VARARGS},
{"removeParent", (PyCFunction)KX_GameObject::sPyRemoveParent,METH_VARARGS},
{"getMesh", (PyCFunction)KX_GameObject::sPyGetMesh,METH_VARARGS},
{"getPhysicsId", (PyCFunction)KX_GameObject::sPyGetPhysicsId,METH_VARARGS},
KX_PYMETHODTABLE(KX_GameObject, getDistanceTo),
@ -797,6 +854,7 @@ int KX_GameObject::_setattr(const STR_String& attr, PyObject *value) // _setattr
if (PyMatTo(value, rot))
{
NodeSetLocalOrientation(rot);
NodeUpdateGS(0.f,true);
return 0;
}
return 1;
@ -809,6 +867,7 @@ int KX_GameObject::_setattr(const STR_String& attr, PyObject *value) // _setattr
{
rot.setRotation(qrot);
NodeSetLocalOrientation(rot);
NodeUpdateGS(0.f,true);
return 0;
}
return 1;
@ -821,6 +880,7 @@ int KX_GameObject::_setattr(const STR_String& attr, PyObject *value) // _setattr
{
rot.setEuler(erot);
NodeSetLocalOrientation(rot);
NodeUpdateGS(0.f,true);
return 0;
}
return 1;
@ -835,6 +895,7 @@ int KX_GameObject::_setattr(const STR_String& attr, PyObject *value) // _setattr
if (PyVecTo(value, pos))
{
NodeSetLocalPosition(pos);
NodeUpdateGS(0.f,true);
return 0;
}
return 1;
@ -846,6 +907,7 @@ int KX_GameObject::_setattr(const STR_String& attr, PyObject *value) // _setattr
if (PyVecTo(value, scale))
{
NodeSetLocalScale(scale);
NodeUpdateGS(0.f,true);
return 0;
}
return 1;
@ -861,6 +923,8 @@ int KX_GameObject::_setattr(const STR_String& attr, PyObject *value) // _setattr
}
}
/* Need to have parent settable here too */
return SCA_IObject::_setattr(attr, value);
}
@ -985,7 +1049,31 @@ PyObject* KX_GameObject::PyGetParent(PyObject* self,
Py_Return;
}
PyObject* KX_GameObject::PySetParent(PyObject* self,
PyObject* args,
PyObject* kwds)
{
PyObject* gameobj;
if (PyArg_ParseTuple(args, "O!", &KX_GameObject::Type, &gameobj))
{
// The object we want to set as parent
CValue *m_ob = (CValue*)gameobj;
KX_GameObject *obj = ((KX_GameObject*)m_ob);
KX_Scene *scene = PHY_GetActiveScene();
this->SetParent(scene, obj);
}
Py_Return;
}
PyObject* KX_GameObject::PyRemoveParent(PyObject* self,
PyObject* args,
PyObject* kwds)
{
KX_Scene *scene = PHY_GetActiveScene();
this->RemoveParent(scene);
Py_Return;
}
PyObject* KX_GameObject::PyGetMesh(PyObject* self,
PyObject* args,
@ -1116,6 +1204,7 @@ PyObject* KX_GameObject::PySetOrientation(PyObject* self,
if (PyObject_IsMT_Matrix(pylist, 3) && PyMatTo(pylist, matrix))
{
NodeSetLocalOrientation(matrix);
NodeUpdateGS(0.f,true);
Py_Return;
}
@ -1124,6 +1213,7 @@ PyObject* KX_GameObject::PySetOrientation(PyObject* self,
{
matrix.setRotation(quat);
NodeSetLocalOrientation(matrix);
NodeUpdateGS(0.f,true);
Py_Return;
}
}

@ -47,6 +47,7 @@
#include "MT_CmMatrix4x4.h"
#include "GEN_Map.h"
#include "GEN_HashedPtr.h"
#include "KX_Scene.h"
#define KX_FIXED_FRAME_PER_SEC 25.0f
#define KX_FIXED_SEC_PER_FRAME (1.0f / KX_FIXED_FRAME_PER_SEC)
@ -134,6 +135,15 @@ public:
GetParent(
);
/**
* Sets the parent of this object to a game object
*/
void SetParent(KX_Scene *scene, KX_GameObject *obj);
/**
* Removes the parent of this object to a game object
*/
void RemoveParent(KX_Scene *scene);
/**
* Construct a game object. This class also inherits the
@ -628,6 +638,8 @@ public:
KX_PYMETHOD(KX_GameObject,SetCollisionMargin);
KX_PYMETHOD(KX_GameObject,GetMesh);
KX_PYMETHOD(KX_GameObject,GetParent);
KX_PYMETHOD(KX_GameObject,SetParent);
KX_PYMETHOD(KX_GameObject,RemoveParent);
KX_PYMETHOD(KX_GameObject,GetPhysicsId);
KX_PYMETHOD_DOC(KX_GameObject,rayCastTo);
KX_PYMETHOD_DOC(KX_GameObject,getDistanceTo);

@ -0,0 +1,172 @@
/**
* Set or remove an objects parent
*
* $Id: SCA_ParentActuator.cpp 13932 2008-03-01 19:05:41Z ben2610 $
*
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
*
* 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. The Blender
* Foundation also sells licenses for use in proprietary software under
* the Blender License. See http://www.blender.org/BL/ for information
* about this.
*
* 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/BL DUAL LICENSE BLOCK *****
*/
#include "KX_ParentActuator.h"
#include "KX_GameObject.h"
#include "KX_PythonInit.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/* ------------------------------------------------------------------------- */
/* Native functions */
/* ------------------------------------------------------------------------- */
KX_ParentActuator::KX_ParentActuator(SCA_IObject *gameobj,
int mode,
CValue *ob,
PyTypeObject* T)
: SCA_IActuator(gameobj, T),
m_mode(mode),
m_ob(ob)
{
}
KX_ParentActuator::~KX_ParentActuator()
{
/* intentionally empty */
}
CValue* KX_ParentActuator::GetReplica()
{
KX_ParentActuator* replica = new KX_ParentActuator(*this);
// replication just copy the m_base pointer => common random generator
replica->ProcessReplica();
CValue::AddDataToReplica(replica);
return replica;
}
bool KX_ParentActuator::Update()
{
KX_GameObject *obj = (KX_GameObject*) GetParent();
KX_Scene *scene = PHY_GetActiveScene();
switch (m_mode) {
case KX_PARENT_SET:
obj->SetParent(scene, (KX_GameObject*)m_ob);
break;
case KX_PARENT_REMOVE:
obj->RemoveParent(scene);
break;
};
return false;
}
/* ------------------------------------------------------------------------- */
/* Python functions */
/* ------------------------------------------------------------------------- */
/* Integration hooks ------------------------------------------------------- */
PyTypeObject KX_ParentActuator::Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"KX_ParentActuator",
sizeof(KX_ParentActuator),
0,
PyDestructor,
0,
__getattr,
__setattr,
0, //&MyPyCompare,
__repr,
0, //&cvalue_as_number,
0,
0,
0,
0
};
PyParentObject KX_ParentActuator::Parents[] = {
&KX_ParentActuator::Type,
&SCA_IActuator::Type,
&SCA_ILogicBrick::Type,
&CValue::Type,
NULL
};
PyMethodDef KX_ParentActuator::Methods[] = {
{"setObject", (PyCFunction) KX_ParentActuator::sPySetObject, METH_VARARGS, SetObject_doc},
{"getObject", (PyCFunction) KX_ParentActuator::sPyGetObject, METH_VARARGS, GetObject_doc},
{NULL,NULL} //Sentinel
};
PyObject* KX_ParentActuator::_getattr(const STR_String& attr) {
_getattr_up(SCA_IActuator);
}
/* 1. setObject */
char KX_ParentActuator::SetObject_doc[] =
"setObject(object)\n"
"\tSet the object to set as parent.\n"
"\tCan be an object name or an object\n";
PyObject* KX_ParentActuator::PySetObject(PyObject* self, PyObject* args, PyObject* kwds) {
PyObject* gameobj;
if (PyArg_ParseTuple(args, "O!", &KX_GameObject::Type, &gameobj))
{
m_ob = (CValue*)gameobj;
Py_Return;
}
PyErr_Clear();
char* objectname;
if (PyArg_ParseTuple(args, "s", &objectname))
{
CValue *object = (CValue*)SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_String(objectname));
if(object)
{
m_ob = object;
Py_Return;
}
}
return NULL;
}
/* 2. getObject */
char KX_ParentActuator::GetObject_doc[] =
"getObject()\n"
"\tReturns the object that is set to.\n";
PyObject* KX_ParentActuator::PyGetObject(PyObject* self, PyObject* args, PyObject* kwds) {
return PyString_FromString(m_ob->GetName());
}
/* eof */

@ -0,0 +1,86 @@
/**
* Set or remove an objects parent
*
*
* $Id: KX_ParentActuator.h 3271 2004-10-16 11:41:50Z kester $
*
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
*
* 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. The Blender
* Foundation also sells licenses for use in proprietary software under
* the Blender License. See http://www.blender.org/BL/ for information
* about this.
*
* 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/BL DUAL LICENSE BLOCK *****
*/
#ifndef __KX_PARENTACTUATOR
#define __KX_PARENTACTUATOR
#include "SCA_IActuator.h"
#include "SCA_LogicManager.h"
class KX_ParentActuator : public SCA_IActuator
{
Py_Header;
/** Mode */
int m_mode;
/** Object to set as parent */
CValue *m_ob;
public:
enum KX_PARENTACT_MODE
{
KX_PARENT_NODEF = 0,
KX_PARENT_SET,
KX_PARENT_REMOVE,
};
KX_ParentActuator(class SCA_IObject* gameobj,
int mode,
CValue *ob,
PyTypeObject* T=&Type);
virtual ~KX_ParentActuator();
virtual bool Update();
virtual CValue* GetReplica();
/* --------------------------------------------------------------------- */
/* Python interface ---------------------------------------------------- */
/* --------------------------------------------------------------------- */
virtual PyObject* _getattr(const STR_String& attr);
/* 1. setObject */
KX_PYMETHOD_DOC(KX_ParentActuator,SetObject);
/* 2. getObject */
KX_PYMETHOD_DOC(KX_ParentActuator,GetObject);
}; /* end of class KX_ParentActuator : public SCA_PropertyActuator */
#endif

@ -135,6 +135,14 @@ class KX_GameObject:
@rtype: L{KX_GameObject}
@return: this object's parent object, or None if this object has no parent.
"""
def setParent(parent):
"""
Sets this object's parent.
"""
def removeParent():
"""
Removes this objects parent.
"""
def getMesh(mesh):
"""
Gets the mesh object for this object.

@ -0,0 +1,22 @@
# $Id: KX_ParentActuator.py 2615 2004-06-02 12:43:27Z kester $
# Documentation for KX_ParentActuator
from SCA_IActuator import *
class KX_ParentActuator(SCA_IActuator):
"""
The parent actuator can set or remove an objects parent object.
"""
def setObject(object):
"""
Sets the object to set as parent.
Object can be either a L{KX_GameObject} or the name of the object.
@type object: L{KX_GameObject} or string
"""
def getObject():
"""
Returns the name of the object to change to.
@rtype: string
"""