BGE patch 18065: gameobj mass writeable + setmass actuator. This patch allows to...
authorBenoit Bolsee <benoit.bolsee@online.be>
Wed, 14 Jan 2009 22:33:39 +0000 (22:33 +0000)
committerBenoit Bolsee <benoit.bolsee@online.be>
Wed, 14 Jan 2009 22:33:39 +0000 (22:33 +0000)
12 files changed:
source/blender/makesdna/DNA_actuator_types.h
source/blender/src/buttons_logic.c
source/gameengine/Converter/KX_ConvertActuators.cpp
source/gameengine/Ketsji/KX_BulletPhysicsController.cpp
source/gameengine/Ketsji/KX_BulletPhysicsController.h
source/gameengine/Ketsji/KX_GameObject.cpp
source/gameengine/Ketsji/KX_IPhysicsController.h
source/gameengine/Ketsji/KX_SCA_DynamicActuator.cpp
source/gameengine/Ketsji/KX_SCA_DynamicActuator.h
source/gameengine/Ketsji/KX_SumoPhysicsController.cpp
source/gameengine/Ketsji/KX_SumoPhysicsController.h
source/gameengine/Physics/Bullet/CcdPhysicsController.cpp

index 43dada97d0e996258cbbb40047d48c2827d09734..48432b8c6e226a8d14032dbd169a304eb48e1ee7 100644 (file)
@@ -82,7 +82,7 @@ typedef struct bEditObjectActuator {
        char name[32];
        float linVelocity[3]; /* initial lin. velocity on creation */
        float angVelocity[3]; /* initial ang. velocity on creation */
-       float pad;
+       float mass;
        short localflag; /* flag for the lin & ang. vel: apply locally   */
        short dyn_operation;
 } bEditObjectActuator;
index 8aca111f41ff24f65656a451d8bfd7cd83b23221..494dc05d8f95dca3b18b2b36f2ebde7ab1f313bf 100644 (file)
@@ -2136,12 +2136,17 @@ static short draw_actuatorbuttons(Object *ob, bActuator *act, uiBlock *block, sh
                        uiDefButS(block, TOG, 0, "3D",  xco+width-50, yco-44, 40, 19, &eoa->flag, 0.0, 0.0, 0, 0, "Enable 3D tracking");
                }
                else if(eoa->type==ACT_EDOB_DYNAMICS) {
-                       ysize= 48;
+                       ysize= 69;
                        glRects(xco, yco-ysize, xco+width, yco);
                        uiEmboss((float)xco, (float)yco-ysize, (float)xco+width, (float)yco, 1);
                        
-                       str= "Dynamic Operation %t|Restore Dynamics %x0|Suspend Dynamics %x1|Enable Rigid Body %x2|Disable Rigid Body %x3";
+                       str= "Dynamic Operation %t|Restore Dynamics %x0|Suspend Dynamics %x1|Enable Rigid Body %x2|Disable Rigid Body %x3|Set Mass %x4";
                        uiDefButS(block, MENU, B_REDR, str,             xco+40, yco-44, (width-80), 19,  &(eoa->dyn_operation), 0.0, 0.0, 0, 0, "");
+                       if(eoa->dyn_operation==4) {
+                               uiDefButF(block, NUM, 0, "",            xco+40, yco-63, width-80, 19,
+                                        &eoa->mass, 0.0, 10000.0, 10, 0,
+                                        "Mass for object.");
+                       }
                }
                str= "Edit Object %t|Add Object %x0|End Object %x1|Replace Mesh %x2|Track to %x3|Dynamics %x4";
                uiDefButS(block, MENU, B_REDR, str,             xco+40, yco-24, (width-80), 19, &eoa->type, 0.0, 0.0, 0, 0, "");
index 4f152acc918ee83ef862bf777f6d87d48356722b..3a6122e6608b2c433f9e0a337e006cf2301bb897 100644 (file)
@@ -661,7 +661,8 @@ void BL_ConvertActuators(char* maggiename,
                                        {
                                                KX_SCA_DynamicActuator* tmpdynact 
                                                        = new KX_SCA_DynamicActuator(gameobj, 
-                                                               editobact->dyn_operation
+                                                               editobact->dyn_operation,
+                                                               editobact->mass
                                                                );
                                                        baseact = tmpdynact;
                                        }
index a67f4a54b3fec0baebf87ef0a21b40aa197241ae..534c48661b7c25f9626c0a5700cf880b02a999c4 100644 (file)
@@ -303,6 +303,25 @@ void    KX_BulletPhysicsController::RemoveCompoundChild(KX_IPhysicsController* c
        GetPhysicsEnvironment()->enableCcdPhysicsController(childCtrl);
 }
 
+void KX_BulletPhysicsController::SetMass(MT_Scalar newmass)
+{
+       btRigidBody *body = GetRigidBody();
+       if (body && body->getActivationState() != DISABLE_SIMULATION && 
+               newmass>MT_EPSILON && GetMass()>MT_EPSILON)
+       {
+               btVector3 grav = body->getGravity();
+               btVector3 accel = grav / GetMass();
+               
+               btBroadphaseProxy* handle = body->getBroadphaseHandle();
+               GetPhysicsEnvironment()->updateCcdPhysicsController(this, 
+                       newmass,
+                       body->getCollisionFlags(),
+                       handle->m_collisionFilterGroup, 
+                       handle->m_collisionFilterMask);
+               body->setGravity(accel);
+       }
+}
+
 void   KX_BulletPhysicsController::SuspendDynamics(bool ghost)
 {
        btRigidBody *body = GetRigidBody();
@@ -333,7 +352,7 @@ void        KX_BulletPhysicsController::RestoreDynamics()
                        m_savedCollisionFlags,
                        m_savedCollisionFilterGroup,
                        m_savedCollisionFilterMask);
-               GetRigidBody()->forceActivationState(m_savedActivationState);
+               body->forceActivationState(m_savedActivationState);
        }
 }
 
index a50af2699bfb596b1b755efd22a3e86c4331e88f..44fbde7054e7b8f8390062ce14effa6647763da2 100644 (file)
@@ -41,6 +41,7 @@ public:
        virtual void setPosition(const MT_Point3& pos);
        virtual void setScaling(const MT_Vector3& scaling);
        virtual MT_Scalar       GetMass();
+       virtual void    SetMass(MT_Scalar newmass);
        virtual MT_Vector3      getReactionForce();
        virtual void    setRigidBody(bool rigid);
        virtual void    AddCompoundChild(KX_IPhysicsController* child);
index 706b80a1fab9afc10f0d1d9551634037db333eb8..42316c6c87333eaa10b173baa5df79a7d8bb31ce 100644 (file)
@@ -1129,7 +1129,7 @@ PyObject* KX_GameObject::_getattr(const STR_String& attr)
        if (m_pPhysicsController1)
        {
                if (attr == "mass")
-                       return PyFloat_FromDouble(GetPhysicsController()->GetMass());
+                       return PyFloat_FromDouble(m_pPhysicsController1->GetMass());
        }
 
        if (attr == "parent")
@@ -1171,10 +1171,6 @@ PyObject* KX_GameObject::_getattr(const STR_String& attr)
 
 int KX_GameObject::_setattr(const STR_String& attr, PyObject *value)   // _setattr method
 {
-       if (attr == "mass") {
-               PyErr_SetString(PyExc_AttributeError, "attribute \"mass\" is read only");
-               return 1;
-       }
        
        if (attr == "parent") {
                PyErr_SetString(PyExc_AttributeError, "attribute \"mass\" is read only\nUse setParent()");
@@ -1203,6 +1199,11 @@ int KX_GameObject::_setattr(const STR_String& attr, PyObject *value)     // _setattr
                                return 0;
                        }               
                }
+               if (attr == "mass") {
+                       if (m_pPhysicsController1)
+                               m_pPhysicsController1->SetMass(val);
+                       return 0;
+               }
        }
        
        if (PySequence_Check(value))
index fed71735becd26526d6128b50b3f3f1f1a36ad53..13501f1fbbdf972924641721e926ba7bf20162e4 100644 (file)
@@ -78,6 +78,7 @@ public:
        virtual void setPosition(const MT_Point3& pos)=0;
        virtual void setScaling(const MT_Vector3& scaling)=0;
        virtual MT_Scalar       GetMass()=0;
+       virtual void    SetMass(MT_Scalar newmass)=0;
        virtual MT_Vector3      getReactionForce()=0;
        virtual void    setRigidBody(bool rigid)=0;
        virtual void    AddCompoundChild(KX_IPhysicsController* child) = 0;
index d44ab477749f07098f0ee99c4bd97cc970931eb9..176ccf1a84a4de529ff1872570cf82a366cd6432 100644 (file)
@@ -133,10 +133,12 @@ KX_PYMETHODDEF_DOC(KX_SCA_DynamicActuator, getOperation,
 
 KX_SCA_DynamicActuator::KX_SCA_DynamicActuator(SCA_IObject *gameobj,
                                                                                                           short dyn_operation,
+                                                                                                          float setmass,
                                                                                                           PyTypeObject* T) : 
 
        SCA_IActuator(gameobj, T),
-       m_dyn_operation(dyn_operation)
+       m_dyn_operation(dyn_operation),
+       m_setmass(setmass)
 {
 } /* End of constructor */
 
@@ -179,6 +181,9 @@ bool KX_SCA_DynamicActuator::Update()
                case 3:
                        controller->setRigidBody(false);
                        break;
+               case 4:
+                       controller->SetMass(m_setmass);
+                       break;
        }
 
        return false;
index b47c3a511d9518d5cb06ca387a18ef048fdd7508..de2fab68d15dba40d0406409e6da07628b3b7790 100644 (file)
@@ -45,10 +45,12 @@ class KX_SCA_DynamicActuator : public SCA_IActuator
 
        // dynamics operation to apply to the game object
        short m_dyn_operation;
+       float m_setmass;
  public:
        KX_SCA_DynamicActuator(
                SCA_IObject* gameobj, 
                short dyn_operation,
+               float setmass,
                PyTypeObject* T=&Type
        );
 
index c842ca1ee14b3a390fcb69903ead909318e6f396..7631ee05b0b83d053282b493662321ebc9c814c1 100644 (file)
@@ -205,6 +205,10 @@ MT_Scalar  KX_SumoPhysicsController::GetMass()
        return SumoPhysicsController::getMass();
 }
 
+void   KX_SumoPhysicsController::SetMass(MT_Scalar newmass)
+{
+}
+
 MT_Scalar      KX_SumoPhysicsController::GetRadius()
 {
        return SumoPhysicsController::GetRadius();
index a684b6378945bc0e4aef02b5c3e8167437495774..46c8ba6df45736f1f80689fd4c43d3b7be160e9c 100644 (file)
@@ -87,6 +87,7 @@ public:
        virtual void setPosition(const MT_Point3& pos);
        virtual void setScaling(const MT_Vector3& scaling);
        virtual MT_Scalar       GetMass();
+       virtual void            SetMass(MT_Scalar newmass);
        virtual MT_Scalar       GetRadius();
        virtual MT_Vector3      getReactionForce();
        virtual void    setRigidBody(bool rigid);
index fafce5cf5cc3d5b56543624c95fa6f78f5341090..d09ad58fe3bcff7f7c15df8d4ab6ecb3c52cfd1d 100644 (file)
@@ -74,10 +74,10 @@ CcdPhysicsController::CcdPhysicsController (const CcdConstructionInfo& ci)
        
 
 ///???
-#ifdef WIN32
+/*#ifdef WIN32
        if (GetRigidBody() && !GetRigidBody()->isStaticObject())
                GetRigidBody()->setLinearVelocity(startVel);
-#endif
+#endif*/
 
 }