BGE: user control to compound shape and setParent.
authorBenoit Bolsee <benoit.bolsee@online.be>
Thu, 21 May 2009 13:32:15 +0000 (13:32 +0000)
committerBenoit Bolsee <benoit.bolsee@online.be>
Thu, 21 May 2009 13:32:15 +0000 (13:32 +0000)
Compound shape control
======================
1) GUI control
It is now possible to control which child shape is added to
a parent compound shape in the Physics buttons. The "Compound"
shape button becomes "Add to parent" on child objects and
determines whether the child shape is to be added to the top
parent compound shape when the game is stated.

Notes: * "Compound" is only available to top parent objects
         (objects without parent).
       * Nesting of compound shape is not possible: a child
         object with "Add to parent" button set will be added
         to the top parent compound shape, regardless of its
         position in the parent-child hierarchy and even if its
         immediate parent doesn't have the "Add to parent" button set.

2) runtime control
It is now possible to control the compound shape at runtime:
The SetParent actuator has a new "Compound" button that indicates
whether the object shape should be added to the compound shape
of the parent object, provided the parent has a compound shape
of course. If not, the object retain it's individual state
while parented.
Similarly, the KX_GameObject.setParent() python function has
a new compound parameter.

Notes: * When an object is dynamically added to a compound
         shape, it looses temporarily all its physics capability
         to the benefit of the parent: it cannot register collisions
         and the characteristics of its shape are lost (ghost, sensor,
         dynamic, etc.).
       * Nested compound shape is not supported: if the object
         being parented is already a compound shape, it is not
         added to the compound parent (as if the Compound option
         was not set in the actuator or the setParent function).
       * To ensure compatibility with old blend files, the Blender
         subversion is changed to 2.48.5 and the old blend files
         are automatically converted to match the old behavior:
         all children of a Compound object will have the "Add to
         parent" button set automatically.

Child ghost control
===================
It is now possible to control if an object should becomes ghost
or solid when parented. This is only applicable if the object
is not added to the parent compound shape (see above).
A new "Ghost" button is available on the SetParent actuator to
that effect. Similarly the KX_GameObject.setParent() python function
has a new compound parameter.

Notes: * This option is not applicable to sensor objects: they stay
         ghost all the time.
       * Make sure the child object does not enter in collision with
         the parent shape when the Ghost option if off and the parent is
         dynamic: the collision creates a reaction force but the parent
         cannot escape the child, so the force builds up and produces
         eratic movements.
       * The collision capability of an ordinary object (dynamic or static)
         is limited when it is parented: it becomes automatically static
         and can only detect dynamic and sensor objects.
       * A sensor object retain its full collision capability when parented:
         it can detect static and dynamic object.

Python control
==============
KX_GameObject.setParent(parent,compound,ghost):
Sets this object's parent.
Control the shape status with the optional compound and ghost parameters:
compound=1: the object shape should be added to the parent compound shape (default)
compound=0: the object should keep its individual shape.
In that case you can control if it should be ghost or not:
ghost=1 if the object should be made ghost while parented (default)
ghost=0 if the object should be solid while parented
Note: if the object type is sensor, it stays ghost regardless of ghost parameter

parent: KX_GameObject reference or string (object name w/o OB prefix)

12 files changed:
source/blender/blenkernel/BKE_blender.h
source/blender/blenloader/intern/readfile.c
source/blender/makesdna/DNA_actuator_types.h
source/blender/src/buttons_logic.c
source/gameengine/Converter/BL_BlenderDataConversion.cpp
source/gameengine/Converter/KX_ConvertActuators.cpp
source/gameengine/Ketsji/KX_ConvertPhysicsObjects.cpp
source/gameengine/Ketsji/KX_GameObject.cpp
source/gameengine/Ketsji/KX_GameObject.h
source/gameengine/Ketsji/KX_ParentActuator.cpp
source/gameengine/Ketsji/KX_ParentActuator.h
source/gameengine/PyDoc/GameTypes.py

index d49a5425b61f0049a05498fbce19721870ef0a60..aa2366661f8f85cfb5d9d5ddb8119cea379f7e4d 100644 (file)
@@ -41,7 +41,7 @@ struct ListBase;
 struct MemFile;
 
 #define BLENDER_VERSION                        248
-#define BLENDER_SUBVERSION             4
+#define BLENDER_SUBVERSION             5
 
 #define BLENDER_MINVERSION             245
 #define BLENDER_MINSUBVERSION  15
index 0f233410f4e0ffb3c109d063d490341a6da5452b..25c8a928a3be605ead1d974ff0f6bc53c4cbabd4 100644 (file)
@@ -8106,6 +8106,22 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
        }
                
+       if (main->versionfile < 248 || (main->versionfile == 248 && main->subversionfile < 5)) {
+               Object *ob;
+               for(ob = main->object.first; ob; ob= ob->id.next) {
+                       if(ob->parent) {
+                               /* check if top parent has compound shape set and if yes, set this object
+                                  to compound shaper as well (was the behaviour before, now it's optional) */
+                               Object *parent= newlibadr(fd, lib, ob->parent);
+                               while (parent->parent != NULL) {
+                                       parent = newlibadr(fd, lib, parent->parent);
+                               }
+                               if (parent->gameflag & OB_CHILD)
+                                       ob->gameflag |= OB_CHILD;
+                       }
+               }
+       }
+
        if (main->versionfile < 249) {
                Scene *sce;
                for (sce= main->scene.first; sce; sce= sce->id.next)
index 81c02779cba900d1f775d21e35828c1d1cc655b5..f713b4a8acc2ce7efdf2a97e01a86f2aa0213acb 100644 (file)
@@ -215,7 +215,8 @@ typedef struct bTwoDFilterActuator{
 }bTwoDFilterActuator;
 
 typedef struct bParentActuator {
-       char pad[4];
+       char pad[2];
+       short flag;
        int type;
        struct Object *ob;
 } bParentActuator;
@@ -483,6 +484,11 @@ typedef struct FreeCamera {
 /* parentactuator->type */
 #define ACT_PARENT_SET      0
 #define ACT_PARENT_REMOVE   1
+
+/* parentactuator->flag */
+#define ACT_PARENT_COMPOUND    1
+#define ACT_PARENT_GHOST       2
+
 #endif
 
 
index 2f8933b593b771fddad6af3b315072c99e623b01..b7039673dfcb832d681cf8122e7b8a6699304a64 100644 (file)
@@ -2747,11 +2747,22 @@ static short draw_actuatorbuttons(Object *ob, bActuator *act, uiBlock *block, sh
 
                if(parAct->type==ACT_PARENT_SET) { 
                        
-                       ysize= 48; 
+                       ysize= 68; 
                        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"); 
+                       uiBlockBeginAlign(block);
+                       uiDefButBitI(block, TOGN, ACT_PARENT_COMPOUND, B_REDR,
+                                 "Compound",
+                                 xco + 40, yco - 64, (width - 80)/2, 19, &parAct->flag,
+                                 0.0, 0.0, 0, 0,
+                                 "Add this object shape to the parent shape (only if the parent shape is already compound)");
+                       uiDefButBitI(block, TOGN, ACT_PARENT_GHOST, B_REDR,
+                                 "Ghost",
+                                 xco + 40 + ((width - 80)/2), yco - 64, (width - 80)/2, 19, &parAct->flag,
+                                 0.0, 0.0, 0, 0,
+                                 "Make this object ghost while parented (only if not compound)");
+                       uiBlockEndAlign(block);
                }
                else if(parAct->type==ACT_PARENT_REMOVE) { 
                        
@@ -3480,9 +3491,14 @@ static void buttons_bullet(uiBlock *block, Object *ob)
                        }
                        if (ob->body_type!=OB_BODY_TYPE_SOFT)
                        {
-                               uiDefButBitI(block, TOG, OB_CHILD, B_REDR, "Compound", 240,105,110,19, 
-                                               &ob->gameflag, 0, 0, 0, 0, 
-                                               "Add Children");
+                               if (ob->parent)
+                                       uiDefButBitI(block, TOG, OB_CHILD, B_REDR, "Add to parent", 240,105,110,19, 
+                                                       &ob->gameflag, 0, 0, 0, 0, 
+                                                       "Add this shape to the parent compound shape");
+                               else
+                                       uiDefButBitI(block, TOG, OB_CHILD, B_REDR, "Compound", 240,105,110,19, 
+                                                       &ob->gameflag, 0, 0, 0, 0, 
+                                                       "Create a compound shape with the children's shape that are tagged for addition");
                        }
                }
                uiBlockEndAlign(block);
index bf965f677c6b8d8f651b29ff3636ec917fdf9a45..82edd4f218b39da85427a9b1ce8b5c7f6312c740 100644 (file)
@@ -1377,10 +1377,11 @@ void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
        }
 
        bool isCompoundChild = false;
+       bool hasCompoundChildren = !parent && (blenderobject->gameflag & OB_CHILD);
 
-       if (parent && (parent->gameflag & OB_DYNAMIC)) {
+       if (parent/* && (parent->gameflag & OB_DYNAMIC)*/) {
                
-               if ((parent->gameflag & OB_CHILD) != 0)
+               if ((parent->gameflag & OB_CHILD) != 0 && (blenderobject->gameflag & OB_CHILD))
                {
                        isCompoundChild = true;
                } 
@@ -1406,7 +1407,7 @@ void BL_CreatePhysicsObjectNew(KX_GameObject* gameobj,
        objprop.m_lockZRotaxis = (blenderobject->gameflag2 & OB_LOCK_RIGID_BODY_Z_ROT_AXIS) !=0;
 
        objprop.m_isCompoundChild = isCompoundChild;
-       objprop.m_hasCompoundChildren = (blenderobject->gameflag & OB_CHILD) != 0;
+       objprop.m_hasCompoundChildren = hasCompoundChildren;
        objprop.m_margin = blenderobject->margin;
        // ACTOR is now a separate feature
        objprop.m_isactor = (blenderobject->gameflag & OB_ACTOR)!=0;
index 98df34aa4a9c398b4d16cbeda4b506ca0bf9daf0..2b832996c459dcd2c52817d359f85b730a7cb88e 100644 (file)
@@ -1123,6 +1123,8 @@ void BL_ConvertActuators(char* maggiename,
                        {
                                bParentActuator *parAct = (bParentActuator *) bact->data;
                                int mode = KX_ParentActuator::KX_PARENT_NODEF;
+                               bool addToCompound = true;
+                               bool ghost = true;
                                KX_GameObject *tmpgob = NULL;
 
                                switch(parAct->type)
@@ -1130,6 +1132,8 @@ void BL_ConvertActuators(char* maggiename,
                                        case ACT_PARENT_SET:
                                                mode = KX_ParentActuator::KX_PARENT_SET;
                                                tmpgob = converter->FindGameObject(parAct->ob);
+                                               addToCompound = !(parAct->flag & ACT_PARENT_COMPOUND);
+                                               ghost = !(parAct->flag & ACT_PARENT_GHOST);
                                                break;
                                        case ACT_PARENT_REMOVE:
                                                mode = KX_ParentActuator::KX_PARENT_REMOVE;
@@ -1140,6 +1144,8 @@ void BL_ConvertActuators(char* maggiename,
                                KX_ParentActuator *tmpparact
                                        = new KX_ParentActuator(gameobj,
                                        mode,
+                                       addToCompound,
+                                       ghost,
                                        tmpgob);
                                baseact = tmpparact;
                                break;
index d81b6d5a6536654bbbcc54d1e71538179afc685f..406339586cc664767e5fb7cb84738ce4d9eca4b1 100644 (file)
@@ -949,39 +949,27 @@ void      KX_ConvertBulletObject( class   KX_GameObject* gameobj,
                        assert(colShape->isCompound());
                        btCompoundShape* compoundShape = (btCompoundShape*)colShape;
 
-                       // compute the local transform from parent, this may include a parent inverse node
+                       // compute the local transform from parent, this may include several node in the chain
                        SG_Node* gameNode = gameobj->GetSGNode();
-                       SG_Node* parentInverseNode = gameNode->GetSGParent();
-                       if (parentInverseNode && parentInverseNode->GetSGClientObject() != NULL)
-                               // this is not a parent inverse node, cancel it
-                               parentInverseNode = NULL;
-                       // now combine the parent inverse node and the game node
-                       MT_Point3 childPos = gameNode->GetLocalPosition();
-                       MT_Matrix3x3 childRot = gameNode->GetLocalOrientation();
-                       MT_Vector3 childScale = gameNode->GetLocalScale();
-                       if (parentInverseNode)
-                       {
-                               const MT_Point3& parentInversePos = parentInverseNode->GetLocalPosition();
-                               const MT_Matrix3x3& parentInverseRot = parentInverseNode->GetLocalOrientation();
-                               const MT_Vector3& parentInverseScale = parentInverseNode->GetLocalScale();
-                               childRot =  parentInverseRot * childRot;
-                               childScale = parentInverseScale * childScale;
-                               childPos = parentInversePos+parentInverseScale*(parentInverseRot*childPos);
-                       }
-
-                       shapeInfo->m_childScale.setValue(childScale.x(),childScale.y(),childScale.z());
+                       SG_Node* parentNode = objprop->m_dynamic_parent->GetSGNode();
+                       // relative transform
+                       MT_Vector3 parentScale = parentNode->GetWorldScaling();
+                       parentScale[0] = MT_Scalar(1.0)/parentScale[0];
+                       parentScale[1] = MT_Scalar(1.0)/parentScale[1];
+                       parentScale[2] = MT_Scalar(1.0)/parentScale[2];
+                       MT_Vector3 relativeScale = gameNode->GetWorldScaling() * parentScale;
+                       MT_Matrix3x3 parentInvRot = parentNode->GetWorldOrientation().transposed();
+                       MT_Vector3 relativePos = parentInvRot*((gameNode->GetWorldPosition()-parentNode->GetWorldPosition())*parentScale);
+                       MT_Matrix3x3 relativeRot = parentInvRot*gameNode->GetWorldOrientation();
+
+                       shapeInfo->m_childScale.setValue(relativeScale[0],relativeScale[1],relativeScale[2]);
                        bm->setLocalScaling(shapeInfo->m_childScale);
-                       
-                       shapeInfo->m_childTrans.setOrigin(btVector3(childPos.x(),childPos.y(),childPos.z()));
-                       float rotval[12];
-                       childRot.getValue(rotval);
-                       btMatrix3x3 newRot;
-                       newRot.setValue(rotval[0],rotval[1],rotval[2],rotval[4],rotval[5],rotval[6],rotval[8],rotval[9],rotval[10]);
-                       newRot = newRot.transpose();
-
-                       shapeInfo->m_childTrans.setBasis(newRot);
+                       shapeInfo->m_childTrans.getOrigin().setValue(relativePos[0],relativePos[1],relativePos[2]);
+                       float rot[12];
+                       relativeRot.getValue(rot);
+                       shapeInfo->m_childTrans.getBasis().setFromOpenGLSubMatrix(rot);
+
                        parentShapeInfo->AddShape(shapeInfo);   
-                       
                        compoundShape->addChildShape(shapeInfo->m_childTrans,bm);
                        //do some recalc?
                        //recalc inertia for rigidbody
index 63d508e62505d25b09fc99d9ee90493d37e1f946..0c191257f41d24bc8bb1b5d9e31273030a946d01 100644 (file)
@@ -224,7 +224,7 @@ KX_GameObject* KX_GameObject::GetParent()
        
 }
 
-void KX_GameObject::SetParent(KX_Scene *scene, KX_GameObject* obj)
+void KX_GameObject::SetParent(KX_Scene *scene, KX_GameObject* obj, bool addToCompound, bool ghost)
 {
        // check on valid node in case a python controller holds a reference to a deleted object
        if (obj && GetSGNode() && obj->GetSGNode() && GetSGNode()->GetSGParent() != obj->GetSGNode())
@@ -245,7 +245,7 @@ void KX_GameObject::SetParent(KX_Scene *scene, KX_GameObject* obj)
 
                if (m_pPhysicsController1) 
                {
-                       m_pPhysicsController1->SuspendDynamics(true);
+                       m_pPhysicsController1->SuspendDynamics(ghost);
                }
                // Set us to our new scale, position, and orientation
                scale2[0] = 1.0/scale2[0];
@@ -266,7 +266,7 @@ void KX_GameObject::SetParent(KX_Scene *scene, KX_GameObject* obj)
                        Release();
                // if the new parent is a compound object, add this object shape to the compound shape.
                // step 0: verify this object has physical controller
-               if (m_pPhysicsController1)
+               if (m_pPhysicsController1 && addToCompound)
                {
                        // step 1: find the top parent (not necessarily obj)
                        KX_GameObject* rootobj = (KX_GameObject*)obj->GetSGNode()->GetRootSGParent()->GetSGClientObject();
@@ -1160,7 +1160,7 @@ PyMethodDef KX_GameObject::Methods[] = {
        {"disableRigidBody", (PyCFunction)KX_GameObject::sPyDisableRigidBody,METH_NOARGS},
        {"applyImpulse", (PyCFunction) KX_GameObject::sPyApplyImpulse, METH_VARARGS},
        {"setCollisionMargin", (PyCFunction) KX_GameObject::sPySetCollisionMargin, METH_O},
-       {"setParent", (PyCFunction)KX_GameObject::sPySetParent,METH_O},
+       {"setParent", (PyCFunction)KX_GameObject::sPySetParent,METH_VARARGS},
        {"setVisible",(PyCFunction) KX_GameObject::sPySetVisible, METH_VARARGS},
        {"setOcclusion",(PyCFunction) KX_GameObject::sPySetOcclusion, METH_VARARGS},
        {"removeParent", (PyCFunction)KX_GameObject::sPyRemoveParent,METH_NOARGS},
@@ -2147,15 +2147,20 @@ PyObject* KX_GameObject::PyGetParent()
        Py_RETURN_NONE;
 }
 
-PyObject* KX_GameObject::PySetParent(PyObject* value)
+PyObject* KX_GameObject::PySetParent(PyObject* args)
 {
        KX_Scene *scene = KX_GetActiveScene();
+       PyObject* pyobj;
        KX_GameObject *obj;
+       int addToCompound=1, ghost=1;
        
-       if (!ConvertPythonToGameObject(value, &obj, false, "gameOb.setParent(value): KX_GameObject"))
+       if (!PyArg_ParseTuple(args,"O|ii:setParent", &pyobj, &addToCompound, &ghost)) {
+               return NULL; // Python sets a simple error
+       }
+       if (!ConvertPythonToGameObject(pyobj, &obj, true, "gameOb.setParent(obj): KX_GameObject"))
                return NULL;
-       
-       this->SetParent(scene, obj);
+       if (obj)
+               this->SetParent(scene, obj, addToCompound, ghost);
        Py_RETURN_NONE;
 }
 
index 0011b8407fd4cf948079fb535ff6d0807864dc46..c89b8f30779107d96c1416aaf35382dffae4152b 100644 (file)
@@ -171,7 +171,7 @@ public:
        /** 
         * Sets the parent of this object to a game object
         */                     
-       void SetParent(KX_Scene *scene, KX_GameObject *obj);
+       void SetParent(KX_Scene *scene, KX_GameObject *obj, bool addToCompound=true, bool ghost=true);
 
        /** 
         * Removes the parent of this object to a game object
@@ -858,7 +858,7 @@ public:
        KX_PYMETHOD_VARARGS(KX_GameObject,ApplyImpulse);
        KX_PYMETHOD_O(KX_GameObject,SetCollisionMargin);
        KX_PYMETHOD_NOARGS(KX_GameObject,GetParent);
-       KX_PYMETHOD_O(KX_GameObject,SetParent);
+       KX_PYMETHOD_VARARGS(KX_GameObject,SetParent);
        KX_PYMETHOD_NOARGS(KX_GameObject,RemoveParent);
        KX_PYMETHOD_NOARGS(KX_GameObject,GetChildren);  
        KX_PYMETHOD_NOARGS(KX_GameObject,GetChildrenRecursive);
index 38ad9aec33bdaa5f867751e318903b857460456b..cd2ed456c48ddae1c2d0a5d52b691253b360bd1b 100644 (file)
 
 KX_ParentActuator::KX_ParentActuator(SCA_IObject *gameobj, 
                                                                         int mode,
+                                                                        bool addToCompound,
+                                                                        bool ghost,
                                                                         SCA_IObject *ob,
                                                                         PyTypeObject* T)
        : SCA_IActuator(gameobj, T),
          m_mode(mode),
+         m_addToCompound(addToCompound),
+         m_ghost(ghost),
          m_ob(ob)
 {
        if (m_ob)
@@ -121,7 +125,7 @@ bool KX_ParentActuator::Update()
        switch (m_mode) {
                case KX_PARENT_SET:
                        if (m_ob)
-                               obj->SetParent(scene, (KX_GameObject*)m_ob);
+                               obj->SetParent(scene, (KX_GameObject*)m_ob, m_addToCompound, m_ghost);
                        break;
                case KX_PARENT_REMOVE:
                        obj->RemoveParent(scene);
@@ -179,6 +183,8 @@ PyMethodDef KX_ParentActuator::Methods[] = {
 PyAttributeDef KX_ParentActuator::Attributes[] = {
        KX_PYATTRIBUTE_RW_FUNCTION("object", KX_ParentActuator, pyattr_get_object, pyattr_set_object),
        KX_PYATTRIBUTE_INT_RW("mode", KX_PARENT_NODEF+1, KX_PARENT_MAX-1, true, KX_ParentActuator, m_mode),
+       KX_PYATTRIBUTE_BOOL_RW("compound", KX_ParentActuator, m_addToCompound),
+       KX_PYATTRIBUTE_BOOL_RW("ghost", KX_ParentActuator, m_ghost),
        { NULL }        //Sentinel
 };
 
index 6af0888e2bae99f7cf8ad5075b3cc5efd6a37349..148375e994cd08543587455a4e489cfeea1ff688 100644 (file)
@@ -46,6 +46,9 @@ class KX_ParentActuator : public SCA_IActuator
        /** Mode */
        int m_mode;
        
+       /** option */
+       bool    m_addToCompound;
+       bool    m_ghost;
        /** Object to set as parent */
        SCA_IObject *m_ob;
        
@@ -63,6 +66,8 @@ class KX_ParentActuator : public SCA_IActuator
  
        KX_ParentActuator(class SCA_IObject* gameobj,
                                                int mode,
+                                               bool addToCompound,
+                                               bool ghost,
                                                SCA_IObject *ob,
                                                PyTypeObject* T=&Type);
        virtual ~KX_ParentActuator();
index 47c27a5c3c9f76b0337ff2ae8bf0864f987b09bd..3f9b627247f7cdfe86dbb01e6ee032c4a3d82c4f 100644 (file)
@@ -1896,12 +1896,23 @@ class KX_GameObject(SCA_IObject):
                @rtype: L{KX_GameObject}
                @return: this object's parent object, or None if this object has no parent.
                """
-       def setParent(parent):
+       def setParent(parent,compound,ghost):
                """
-               Sets this object's parent.
+               Sets this object's parent. 
+               Control the shape status with the optional compound and ghost parameters:
+               compound=1: the object shape should be added to the parent compound shape (default)
+               compound=0: the object should keep its individual shape. 
+               In that case you can control if it should be ghost or not:
+               ghost=1 if the object should be made ghost while parented (default)
+               ghost=0 if the object should be solid while parented 
+               Note: if the object type is sensor, it stays ghost regardless of ghost parameter
                
                @type parent: L{KX_GameObject}
                @param parent: new parent object.
+               @type compound: int
+               @param compound: whether the shape should be added to the parent compound shape
+               @type ghost: int
+               @param ghost: whether the object should be ghost while parented
                """
        def removeParent():
                """
@@ -2937,6 +2948,13 @@ class KX_ParentActuator(SCA_IActuator):
        @type object: KX_GameObject or None
        @ivar mode: The mode of this actuator
        @type mode: int from 0 to 1 L{GameLogic.Parent Actuator}
+       @ivar compound: Whether the object shape should be added to the parent compound shape when parenting
+                       Effective only if the parent is already a compound shape
+       @type compound: bool
+       @ivar ghost: whether the object should be made ghost when parenting
+                    Effective only if the shape is not added to the parent compound shape
+       @type ghost: bool
+       
        """
        def setObject(object):
                """