BGE patch #18051: add localInertia attribute to GameObject.
authorBenoit Bolsee <benoit.bolsee@online.be>
Wed, 8 Apr 2009 16:25:00 +0000 (16:25 +0000)
committerBenoit Bolsee <benoit.bolsee@online.be>
Wed, 8 Apr 2009 16:25:00 +0000 (16:25 +0000)
source/gameengine/Ketsji/KX_BulletPhysicsController.cpp
source/gameengine/Ketsji/KX_BulletPhysicsController.h
source/gameengine/Ketsji/KX_GameObject.cpp
source/gameengine/Ketsji/KX_GameObject.h
source/gameengine/Ketsji/KX_IPhysicsController.h
source/gameengine/Ketsji/KX_SumoPhysicsController.cpp
source/gameengine/Ketsji/KX_SumoPhysicsController.h
source/gameengine/PyDoc/KX_GameObject.py

index 435b2b5db19f0641a398ce91f470ba92697bb12e..c621f11994abd5fadb77878e1a1585dbbe0108e0 100644 (file)
@@ -162,6 +162,20 @@ MT_Scalar  KX_BulletPhysicsController::GetMass()
 
 }
 
+MT_Vector3 KX_BulletPhysicsController::GetLocalInertia()
+{
+    MT_Vector3 inertia(0.f, 0.f, 0.f);
+    btVector3 inv_inertia;
+    if (GetRigidBody()) {
+        inv_inertia = GetRigidBody()->getInvInertiaDiagLocal();
+               if (!btFuzzyZero(inv_inertia.getX()) && 
+                       !btFuzzyZero(inv_inertia.getY()) && 
+                       !btFuzzyZero(inv_inertia.getZ()))
+                       inertia = MT_Vector3(1.f/inv_inertia.getX(), 1.f/inv_inertia.getY(), 1.f/inv_inertia.getZ());
+    }
+    return inertia;
+}
+
 MT_Scalar KX_BulletPhysicsController::GetRadius()
 {
        return MT_Scalar(CcdPhysicsController::GetRadius());
index 44fbde7054e7b8f8390062ce14effa6647763da2..9821b3fd253d6cf45208c34219805f7df3a489f0 100644 (file)
@@ -42,6 +42,7 @@ public:
        virtual void setScaling(const MT_Vector3& scaling);
        virtual MT_Scalar       GetMass();
        virtual void    SetMass(MT_Scalar newmass);
+       virtual MT_Vector3      GetLocalInertia();
        virtual MT_Vector3      getReactionForce();
        virtual void    setRigidBody(bool rigid);
        virtual void    AddCompoundChild(KX_IPhysicsController* child);
index 339a955702aa4440c530a25c212ceb7475ab3392..ea53ffea48fbf1e3ab209426cc99b5a7f33a93a4 100644 (file)
@@ -772,6 +772,16 @@ MT_Scalar KX_GameObject::GetMass()
        return 0.0;
 }
 
+MT_Vector3 KX_GameObject::GetLocalInertia()
+{
+       MT_Vector3 local_inertia(0.0,0.0,0.0);
+       if (m_pPhysicsController1)
+       {
+               local_inertia = m_pPhysicsController1->GetLocalInertia();
+       }
+       return local_inertia;
+}
+
 MT_Vector3 KX_GameObject::GetLinearVelocity(bool local)
 {
        MT_Vector3 velocity(0.0,0.0,0.0), locvel;
@@ -1050,6 +1060,7 @@ PyAttributeDef KX_GameObject::Attributes[] = {
        KX_PYATTRIBUTE_RW_FUNCTION("mass",              KX_GameObject, pyattr_get_mass,         pyattr_set_mass),
        KX_PYATTRIBUTE_RW_FUNCTION("visible",   KX_GameObject, pyattr_get_visible,      pyattr_set_visible),
        KX_PYATTRIBUTE_RW_FUNCTION("position",  KX_GameObject, pyattr_get_position,     pyattr_set_position),
+       KX_PYATTRIBUTE_RO_FUNCTION("localInertia",      KX_GameObject, pyattr_get_localInertia),
        KX_PYATTRIBUTE_RW_FUNCTION("orientation",KX_GameObject,pyattr_get_orientation,pyattr_set_orientation),
        KX_PYATTRIBUTE_RW_FUNCTION("scaling",   KX_GameObject, pyattr_get_scaling,      pyattr_set_scaling),
        KX_PYATTRIBUTE_RW_FUNCTION("timeOffset",KX_GameObject, pyattr_get_timeOffset,pyattr_set_timeOffset),
@@ -1305,6 +1316,15 @@ int KX_GameObject::pyattr_set_position(void *self_v, const KX_PYATTRIBUTE_DEF *a
        return 0;
 }
 
+PyObject* KX_GameObject::pyattr_get_localInertia(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_GameObject* self= static_cast<KX_GameObject*>(self_v);
+       if (self->GetPhysicsController())
+       {
+               return PyObjectFrom(self->GetPhysicsController()->GetLocalInertia());
+       }
+       Py_RETURN_NONE;
+}
 
 PyObject* KX_GameObject::pyattr_get_orientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
@@ -1722,8 +1742,6 @@ PyObject* KX_GameObject::PyGetMass(PyObject* self)
        return PyFloat_FromDouble((GetPhysicsController() != NULL) ? GetPhysicsController()->GetMass() : 0.0f);
 }
 
-
-
 PyObject* KX_GameObject::PyGetReactionForce(PyObject* self)
 {
        // only can get the velocity if we have a physics object connected to us...
index 9c7dda5e3947b7434c7e4a10ffc954463bb01f5e..08cc303147926235b3e7d57dc2d0e837f7528ad8 100644 (file)
@@ -281,6 +281,12 @@ public:
                MT_Scalar       
        GetMass();
 
+       /**
+        * Return the local inertia vector of the object
+        */
+               MT_Vector3
+       GetLocalInertia();
+
        /** 
         * Return the angular velocity of the game object.
         */
@@ -820,6 +826,8 @@ public:
        static int                      pyattr_set_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
        static PyObject*        pyattr_get_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static int                      pyattr_set_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
+       static PyObject*        pyattr_get_localInertia(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+       static int                      pyattr_set_localInertia(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
        static PyObject*        pyattr_get_orientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
        static int                      pyattr_set_orientation(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
        static PyObject*        pyattr_get_scaling(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
index 13501f1fbbdf972924641721e926ba7bf20162e4..b7603203241947139104cb9ca6bca26d0ef8499a 100644 (file)
@@ -79,6 +79,7 @@ public:
        virtual void setScaling(const MT_Vector3& scaling)=0;
        virtual MT_Scalar       GetMass()=0;
        virtual void    SetMass(MT_Scalar newmass)=0;
+       virtual MT_Vector3      GetLocalInertia()=0;
        virtual MT_Vector3      getReactionForce()=0;
        virtual void    setRigidBody(bool rigid)=0;
        virtual void    AddCompoundChild(KX_IPhysicsController* child) = 0;
index 7631ee05b0b83d053282b493662321ebc9c814c1..fc053f05e63693783da7a7b0e0c0d14b6eeac921 100644 (file)
@@ -209,6 +209,11 @@ void       KX_SumoPhysicsController::SetMass(MT_Scalar newmass)
 {
 }
 
+MT_Vector3  KX_SumoPhysicsController::GetLocalInertia()
+{
+    return MT_Vector3(0.f, 0.f, 0.f); // \todo
+}
+
 MT_Scalar      KX_SumoPhysicsController::GetRadius()
 {
        return SumoPhysicsController::GetRadius();
index 46c8ba6df45736f1f80689fd4c43d3b7be160e9c..8762612eca21ab62722ba93eb1447ceafaf8e10c 100644 (file)
@@ -88,6 +88,7 @@ public:
        virtual void setScaling(const MT_Vector3& scaling);
        virtual MT_Scalar       GetMass();
        virtual void            SetMass(MT_Scalar newmass);
+       virtual MT_Vector3      GetLocalInertia();
        virtual MT_Scalar       GetRadius();
        virtual MT_Vector3      getReactionForce();
        virtual void    setRigidBody(bool rigid);
index 97f6dab52bfaf83010787cb027adb3e188801bdc..42656503384f535c65831c778e900959eb3c147a 100644 (file)
@@ -16,8 +16,10 @@ class KX_GameObject: # (SCA_IObject)
        @ivar name: The object's name. (Read only)
                - note: Currently (Blender 2.49) the prefix "OB" is added to all objects name. This may change in blender 2.5.
        @type name: string.
-       @ivar mass: The object's mass (provided the object has a physics controller). Read only.
+       @ivar mass: The object's mass (provided the object has a physics controller).
        @type mass: float
+       @ivar localInertia: the object's inertia vector in local coordinates. Read only.
+       @type localInertia: list [ix, iy, iz]
        @ivar parent: The object's parent object. (Read only)
        @type parent: L{KX_GameObject} or None
        @ivar visible: visibility flag.