From 66ace2c235abeaa6489bbdf4a566121a46453fee Mon Sep 17 00:00:00 2001 From: enn0x Date: Tue, 6 Sep 2011 22:16:58 +0000 Subject: [PATCH] Added methods for getting and setting (overriding) inertia. --- panda/src/bullet/bulletGhostNode.cxx | 8 +-- panda/src/bullet/bulletRigidBodyNode.cxx | 69 ++++++++++++++++++------ panda/src/bullet/bulletRigidBodyNode.h | 6 ++- panda/src/bullet/bulletShape.cxx | 4 +- panda/src/bullet/bulletShape.h | 8 +-- panda/src/bullet/bulletSoftBodyNode.cxx | 8 +-- 6 files changed, 73 insertions(+), 30 deletions(-) diff --git a/panda/src/bullet/bulletGhostNode.cxx b/panda/src/bullet/bulletGhostNode.cxx index 79ae5f3ea4..8b3f147944 100644 --- a/panda/src/bullet/bulletGhostNode.cxx +++ b/panda/src/bullet/bulletGhostNode.cxx @@ -77,9 +77,11 @@ transform_changed() { if (ts->has_scale()) { LVecBase3f scale = ts->get_scale(); - for (int i=0; iset_local_scale(scale); + if (!scale.almost_equal(LVecBase3f(1.0f, 1.0f, 1.0f))) { + for (int i=0; iset_local_scale(scale); + } } } } diff --git a/panda/src/bullet/bulletRigidBodyNode.cxx b/panda/src/bullet/bulletRigidBodyNode.cxx index 21731de2cb..3e7e76f03a 100644 --- a/panda/src/bullet/bulletRigidBodyNode.cxx +++ b/panda/src/bullet/bulletRigidBodyNode.cxx @@ -97,13 +97,14 @@ shape_changed() { void BulletRigidBodyNode:: 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(); } @@ -115,14 +116,46 @@ set_mass(float mass) { float BulletRigidBodyNode:: 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()); _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()); _rigid->applyImpulse(LVecBase3f_to_btVector3(impulse), - LVecBase3f_to_btVector3(pos)); + LVecBase3f_to_btVector3(pos)); } //////////////////////////////////////////////////////////////////// @@ -231,9 +264,13 @@ transform_changed() { if (ts->has_scale()) { LVecBase3f scale = ts->get_scale(); - for (int i=0; iset_local_scale(scale); + if (!scale.almost_equal(LVecBase3f(1.0f, 1.0f, 1.0f))) { + for (int i=0; iset_local_scale(scale); + } + + shape_changed(); } } diff --git a/panda/src/bullet/bulletRigidBodyNode.h b/panda/src/bullet/bulletRigidBodyNode.h index 7e765cf0c5..72a037d095 100644 --- a/panda/src/bullet/bulletRigidBodyNode.h +++ b/panda/src/bullet/bulletRigidBodyNode.h @@ -36,9 +36,11 @@ PUBLISHED: BulletRigidBodyNode(const char *name="rigid"); INLINE ~BulletRigidBodyNode(); - // Mass - float get_mass() const; + // Mass & inertia void set_mass(float mass); + float get_mass() const; + void set_inertia(const LVecBase3f &inertia); + LVector3f get_inertia() const; // Velocity LVector3f get_linear_velocity() const; diff --git a/panda/src/bullet/bulletShape.cxx b/panda/src/bullet/bulletShape.cxx index 775ba34ad6..3736d37998 100644 --- a/panda/src/bullet/bulletShape.cxx +++ b/panda/src/bullet/bulletShape.cxx @@ -52,7 +52,7 @@ set_margin(float margin) { //////////////////////////////////////////////////////////////////// // Function: BulletShape::get_local_scale -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// LVecBase3f BulletShape:: @@ -63,7 +63,7 @@ get_local_scale() const { //////////////////////////////////////////////////////////////////// // Function: BulletShape::set_local_scale -// Access: Published +// Access: Public // Description: //////////////////////////////////////////////////////////////////// void BulletShape:: diff --git a/panda/src/bullet/bulletShape.h b/panda/src/bullet/bulletShape.h index 8c7fdc059e..1727759bc3 100644 --- a/panda/src/bullet/bulletShape.h +++ b/panda/src/bullet/bulletShape.h @@ -20,7 +20,6 @@ #include "bullet_includes.h" #include "typedReferenceCount.h" -//#include "lvector3.h" //////////////////////////////////////////////////////////////////// // Class : BulletShape @@ -40,15 +39,16 @@ PUBLISHED: INLINE bool is_soft_body() const; void set_margin(float margin); - void set_local_scale(const LVecBase3f &scaling); - const char *get_name() const; + float get_margin() const; - LVecBase3f get_local_scale() const; public: virtual btCollisionShape *ptr() const = 0; + LVecBase3f get_local_scale() const; + void set_local_scale(const LVecBase3f &scale); + //////////////////////////////////////////////////////////////////// public: static TypeHandle get_class_type() { diff --git a/panda/src/bullet/bulletSoftBodyNode.cxx b/panda/src/bullet/bulletSoftBodyNode.cxx index b22554c5b3..1cb839e657 100644 --- a/panda/src/bullet/bulletSoftBodyNode.cxx +++ b/panda/src/bullet/bulletSoftBodyNode.cxx @@ -202,9 +202,11 @@ transform_changed() { if (ts->has_scale()) { LVecBase3f scale = ts->get_scale(); - for (int i=0; iset_local_scale(scale); + if (!scale.almost_equal(LVecBase3f(1.0f, 1.0f, 1.0f))) { + for (int i=0; iset_local_scale(scale); + } } } }