void btBox2dShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const { //btScalar margin = btScalar(0.); btVector3 halfExtents = getHalfExtentsWithMargin(); btScalar lx = btScalar(2.) * (halfExtents.x()); btScalar ly = btScalar(2.) * (halfExtents.y()); btScalar lz = btScalar(2.) * (halfExtents.z()); 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)); }
void btCylinderShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const { //approximation of box shape, todo: implement cylinder shape inertia before people notice ;-) btVector3 halfExtents = getHalfExtentsWithMargin(); btScalar lx=btScalar(2.)*(halfExtents.x()); btScalar ly=btScalar(2.)*(halfExtents.y()); btScalar lz=btScalar(2.)*(halfExtents.z()); 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)); }
void btCylinderShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const { //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()); btScalar ly=btScalar(2.)*(halfExtents.y()); btScalar lz=btScalar(2.)*(halfExtents.z()); 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 }