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 43dada9..48432b8 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 8aca111..494dc05 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 4f152ac..3a6122e 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 a67f4a5..534c486 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 a50af26..44fbde7 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 706b80a..42316c6 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 fed7173..13501f1 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 d44ab47..176ccf1 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 b47c3a5..de2fab6 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 c842ca1..7631ee0 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 a684b63..46c8ba6 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 fafce5c..d09ad58 100644 (file)
@@ -74,10 +74,10 @@ CcdPhysicsController::CcdPhysicsController (const CcdConstructionInfo& ci)
        
 
 ///???
-#ifdef WIN32
+/*#ifdef WIN32
        if (GetRigidBody() && !GetRigidBody()->isStaticObject())
                GetRigidBody()->setLinearVelocity(startVel);
-#endif
+#endif*/
 
 }