Browse Source

Added methods for getting and setting (overriding) inertia.

enn0x 14 years ago
parent
commit
66ace2c235

+ 5 - 3
panda/src/bullet/bulletGhostNode.cxx

@@ -77,9 +77,11 @@ transform_changed() {
 
 
     if (ts->has_scale()) {
     if (ts->has_scale()) {
       LVecBase3f scale = ts->get_scale();
       LVecBase3f scale = ts->get_scale();
-      for (int i=0; i<get_num_shapes(); i++) {
-        PT(BulletShape) shape = _shapes[i];
-        shape->set_local_scale(scale);
+      if (!scale.almost_equal(LVecBase3f(1.0f, 1.0f, 1.0f))) {
+        for (int i=0; i<get_num_shapes(); i++) {
+          PT(BulletShape) shape = _shapes[i];
+          shape->set_local_scale(scale);
+        }
       }
       }
     }
     }
   }
   }

+ 53 - 16
panda/src/bullet/bulletRigidBodyNode.cxx

@@ -97,13 +97,14 @@ shape_changed() {
 void BulletRigidBodyNode::
 void BulletRigidBodyNode::
 set_mass(float mass) {
 set_mass(float mass) {
 
 
-  btVector3 inertia(0.0f, 0.0f, 0.0f);
+  btScalar bt_mass = mass;
+  btVector3 bt_inertia(0.0, 0.0, 0.0);
 
 
-  if (mass > 0.0f) {
-    _rigid->getCollisionShape()->calculateLocalInertia(mass, inertia);
+  if (bt_mass > 0.0) {
+    _rigid->getCollisionShape()->calculateLocalInertia(bt_mass, bt_inertia);
   }
   }
 
 
-  _rigid->setMassProps(mass, inertia);
+  _rigid->setMassProps(bt_mass, bt_inertia);
   _rigid->updateInertiaTensor();
   _rigid->updateInertiaTensor();
 }
 }
 
 
@@ -115,14 +116,46 @@ set_mass(float mass) {
 float BulletRigidBodyNode::
 float BulletRigidBodyNode::
 get_mass() const {
 get_mass() const {
 
 
-  btScalar invMass = _rigid->getInvMass();
+  btScalar inv_mass = _rigid->getInvMass();
+  btScalar mass = inv_mass == btScalar(0.0) ? btScalar(0.0) : btScalar(1.0) / inv_mass;
 
 
-  if (invMass == 0.0f) {
-    return 0.0f;
-  }
-  else {
-    return 1.0f / invMass;
-  }
+  return mass;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: BulletRigidBodyNode::set_inertia
+//       Access: Published
+//  Description:
+////////////////////////////////////////////////////////////////////
+void BulletRigidBodyNode::
+set_inertia(const LVecBase3f &inertia) {
+
+  btVector3 inv_inertia(
+    inertia.get_x() == 0.0 ? btScalar(0.0) : btScalar(1.0 / inertia.get_x()),
+    inertia.get_y() == 0.0 ? btScalar(0.0) : btScalar(1.0 / inertia.get_y()),
+    inertia.get_z() == 0.0 ? btScalar(0.0) : btScalar(1.0 / inertia.get_z())
+    );
+
+  _rigid->setInvInertiaDiagLocal(inv_inertia);
+  _rigid->updateInertiaTensor();
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: BulletRigidBodyNode::get_inertia
+//       Access: Published
+//  Description:
+////////////////////////////////////////////////////////////////////
+LVector3f BulletRigidBodyNode::
+get_inertia() const {
+
+  btVector3 inv_inertia = _rigid->getInvInertiaDiagLocal();
+  LVector3f inertia(
+    inv_inertia.x() == btScalar(0.0) ? 0.0 : 1.0 / inv_inertia.x(),
+    inv_inertia.y() == btScalar(0.0) ? 0.0 : 1.0 / inv_inertia.y(),
+    inv_inertia.z() == btScalar(0.0) ? 0.0 : 1.0 / inv_inertia.z()
+    );
+
+  return inertia;
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -137,7 +170,7 @@ apply_force(const LVector3f &force, const LPoint3f &pos) {
   nassertv_always(!pos.is_nan());
   nassertv_always(!pos.is_nan());
 
 
   _rigid->applyForce(LVecBase3f_to_btVector3(force),
   _rigid->applyForce(LVecBase3f_to_btVector3(force),
-                    LVecBase3f_to_btVector3(pos));
+                     LVecBase3f_to_btVector3(pos));
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -191,7 +224,7 @@ apply_impulse(const LVector3f &impulse, const LPoint3f &pos) {
   nassertv_always(!pos.is_nan());
   nassertv_always(!pos.is_nan());
 
 
   _rigid->applyImpulse(LVecBase3f_to_btVector3(impulse),
   _rigid->applyImpulse(LVecBase3f_to_btVector3(impulse),
-                      LVecBase3f_to_btVector3(pos));
+                       LVecBase3f_to_btVector3(pos));
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -231,9 +264,13 @@ transform_changed() {
 
 
     if (ts->has_scale()) {
     if (ts->has_scale()) {
       LVecBase3f scale = ts->get_scale();
       LVecBase3f scale = ts->get_scale();
-      for (int i=0; i<get_num_shapes(); i++) {
-        PT(BulletShape) shape = _shapes[i];
-        shape->set_local_scale(scale);
+      if (!scale.almost_equal(LVecBase3f(1.0f, 1.0f, 1.0f))) {
+        for (int i=0; i<get_num_shapes(); i++) {
+          PT(BulletShape) shape = _shapes[i];
+          shape->set_local_scale(scale);
+        }
+
+        shape_changed();
       }
       }
     }
     }
 
 

+ 4 - 2
panda/src/bullet/bulletRigidBodyNode.h

@@ -36,9 +36,11 @@ PUBLISHED:
   BulletRigidBodyNode(const char *name="rigid");
   BulletRigidBodyNode(const char *name="rigid");
   INLINE ~BulletRigidBodyNode();
   INLINE ~BulletRigidBodyNode();
 
 
-  // Mass
-  float get_mass() const;
+  // Mass & inertia
   void set_mass(float mass);
   void set_mass(float mass);
+  float get_mass() const;
+  void set_inertia(const LVecBase3f &inertia);
+  LVector3f get_inertia() const;
 
 
   // Velocity
   // Velocity
   LVector3f get_linear_velocity() const;
   LVector3f get_linear_velocity() const;

+ 2 - 2
panda/src/bullet/bulletShape.cxx

@@ -52,7 +52,7 @@ set_margin(float margin) {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: BulletShape::get_local_scale
 //     Function: BulletShape::get_local_scale
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 LVecBase3f BulletShape::
 LVecBase3f BulletShape::
@@ -63,7 +63,7 @@ get_local_scale() const {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: BulletShape::set_local_scale
 //     Function: BulletShape::set_local_scale
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void BulletShape::
 void BulletShape::

+ 4 - 4
panda/src/bullet/bulletShape.h

@@ -20,7 +20,6 @@
 #include "bullet_includes.h"
 #include "bullet_includes.h"
 
 
 #include "typedReferenceCount.h"
 #include "typedReferenceCount.h"
-//#include "lvector3.h"
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //       Class : BulletShape
 //       Class : BulletShape
@@ -40,15 +39,16 @@ PUBLISHED:
   INLINE bool is_soft_body() const;
   INLINE bool is_soft_body() const;
 
 
   void set_margin(float margin);
   void set_margin(float margin);
-  void set_local_scale(const LVecBase3f &scaling);
-
   const char *get_name() const;
   const char *get_name() const;
+
   float get_margin() const;
   float get_margin() const;
-  LVecBase3f get_local_scale() const;
 
 
 public:
 public:
   virtual btCollisionShape *ptr() const = 0;
   virtual btCollisionShape *ptr() const = 0;
 
 
+  LVecBase3f get_local_scale() const;
+  void set_local_scale(const LVecBase3f &scale);
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 public:
 public:
   static TypeHandle get_class_type() {
   static TypeHandle get_class_type() {

+ 5 - 3
panda/src/bullet/bulletSoftBodyNode.cxx

@@ -202,9 +202,11 @@ transform_changed() {
 
 
     if (ts->has_scale()) {
     if (ts->has_scale()) {
       LVecBase3f scale = ts->get_scale();
       LVecBase3f scale = ts->get_scale();
-      for (int i=0; i<get_num_shapes(); i++) {
-        PT(BulletShape) shape = _shapes[i];
-        shape->set_local_scale(scale);
+      if (!scale.almost_equal(LVecBase3f(1.0f, 1.0f, 1.0f))) {
+        for (int i=0; i<get_num_shapes(); i++) {
+          PT(BulletShape) shape = _shapes[i];
+          shape->set_local_scale(scale);
+        }
       }
       }
     }
     }
   }
   }