Use cylinder inertia tensor, instead of box, by default.

See Issue 427

Expose some internal data for constraints,
Thanks to Francois Sugny, see Issue 420
This commit is contained in:
erwin.coumans
2010-11-17 23:55:39 +00:00
parent bc3b920995
commit c49927b4ed
8 changed files with 133 additions and 10 deletions

View File

@@ -47,7 +47,64 @@ void btCylinderShape::getAabb(const btTransform& t,btVector3& aabbMin,btVector3&
void btCylinderShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
{
//approximation of box shape, todo: implement cylinder shape inertia before people notice ;-)
//Until Bullet 2.77 a box approximation was used, so uncomment this if you need backwards compatibility
//#define USE_BOX_INERTIA_APPROXIMATION 1
#ifndef USE_BOX_INERTIA_APPROXIMATION
/*
cylinder is defined as following:
*
* - principle axis aligned along y by default, radius in x, z-value not used
* - for btCylinderShapeX: principle axis aligned along x, radius in y direction, z-value not used
* - for btCylinderShapeZ: principle axis aligned along z, radius in x direction, y-value not used
*
*/
btScalar radius2; // square of cylinder radius
btScalar height2; // square of cylinder height
btVector3 halfExtents = getHalfExtentsWithMargin(); // get cylinder dimension
btScalar div12 = mass / 12.f;
btScalar div4 = mass / 4.f;
btScalar div2 = mass / 2.f;
int idxRadius, idxHeight;
switch (m_upAxis) // get indices of radius and height of cylinder
{
case 0: // cylinder is aligned along x
idxRadius = 1;
idxHeight = 0;
break;
case 2: // cylinder is aligned along z
idxRadius = 0;
idxHeight = 2;
break;
default: // cylinder is aligned along y
idxRadius = 0;
idxHeight = 1;
}
// calculate squares
radius2 = halfExtents[idxRadius] * halfExtents[idxRadius];
height2 = btScalar(4.) * halfExtents[idxHeight] * halfExtents[idxHeight];
// calculate tensor terms
btScalar t1 = div12 * height2 + div4 * radius2;
btScalar t2 = div2 * radius2;
switch (m_upAxis) // set diagonal elements of inertia tensor
{
case 0: // cylinder is aligned along x
inertia.setValue(t2,t1,t1);
break;
case 2: // cylinder is aligned along z
inertia.setValue(t1,t1,t2);
break;
default: // cylinder is aligned along y
inertia.setValue(t1,t2,t1);
}
#else //USE_BOX_INERTIA_APPROXIMATION
//approximation of box shape
btVector3 halfExtents = getHalfExtentsWithMargin();
btScalar lx=btScalar(2.)*(halfExtents.x());
@@ -57,7 +114,7 @@ void btCylinderShape::calculateLocalInertia(btScalar mass,btVector3& inertia) co
inertia.setValue(mass/(btScalar(12.0)) * (ly*ly + lz*lz),
mass/(btScalar(12.0)) * (lx*lx + lz*lz),
mass/(btScalar(12.0)) * (lx*lx + ly*ly));
#endif //USE_BOX_INERTIA_APPROXIMATION
}