Add a method to compute the inertia tensor from the collision shape of the rigid body

git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@438 92aac97c-a6ce-11dd-a772-7fcde58d38e6
This commit is contained in:
chappuis.daniel 2011-08-23 21:53:01 +00:00
parent 40ec21b398
commit 6c6b6c6c86
8 changed files with 53 additions and 17 deletions

View File

@ -45,6 +45,17 @@ BoxShape::~BoxShape() {
} }
// Return the local inertia tensor of the shape
void BoxShape::computeLocalInertiaTensor(Matrix3x3& tensor, double mass) const {
double factor = (1.0 / 3.0) * mass;
double xSquare = extent.getX() * extent.getX();
double ySquare = extent.getY() * extent.getY();
double zSquare = extent.getZ() * extent.getZ();
tensor.setAllValues(factor * (ySquare + zSquare), 0.0, 0.0,
0.0, factor * (xSquare + zSquare), 0.0,
0.0, 0.0, factor * (xSquare + ySquare));
}
#ifdef VISUAL_DEBUG #ifdef VISUAL_DEBUG
// Draw the Box (only for testing purpose) // Draw the Box (only for testing purpose)
void BoxShape::draw() const { void BoxShape::draw() const {

View File

@ -46,16 +46,17 @@ namespace reactphysics3d {
*/ */
class BoxShape : public Shape { class BoxShape : public Shape {
private : private :
Vector3 extent; // Extent sizes of the box Vector3 extent; // Extent sizes of the box in the x, y and z direction
public : public :
BoxShape(const Vector3& extent); // Constructor BoxShape(const Vector3& extent); // Constructor
virtual ~BoxShape(); // Destructor virtual ~BoxShape(); // Destructor
const Vector3& getExtent() const; // Return the extents of the box const Vector3& getExtent() const; // Return the extents of the box
void setExtent(const Vector3& extent); // Set the extents of the box void setExtent(const Vector3& extent); // Set the extents of the box
virtual Vector3 getLocalExtents(double margin=0.0) const; // Return the local extents in x,y and z direction virtual Vector3 getLocalExtents(double margin=0.0) const; // Return the local extents in x,y and z direction
virtual Vector3 getLocalSupportPoint(const Vector3& direction, double margin=0.0) const; // Return a local support point in a given direction virtual Vector3 getLocalSupportPoint(const Vector3& direction, double margin=0.0) const; // Return a local support point in a given direction
virtual void computeLocalInertiaTensor(Matrix3x3& tensor, double mass) const; // Return the local inertia tensor of the shape
#ifdef VISUAL_DEBUG #ifdef VISUAL_DEBUG
virtual void draw() const; // Draw the Box (only for testing purpose) virtual void draw() const; // Draw the Box (only for testing purpose)

View File

@ -75,7 +75,7 @@ inline Vector3 ConeShape::getLocalSupportPoint(const Vector3& direction, double
if (v.lengthSquare() > MACHINE_EPSILON * MACHINE_EPSILON) { if (v.lengthSquare() > MACHINE_EPSILON * MACHINE_EPSILON) {
unitVec = v.getUnit(); unitVec = v.getUnit();
} }
supportPoint = supportPoint + unitVec * margin; supportPoint += unitVec * margin;
} }
return supportPoint; return supportPoint;

View File

@ -56,8 +56,9 @@ class ConeShape : public Shape {
void setRadius(double radius); // Set the radius void setRadius(double radius); // Set the radius
double getHeight() const; // Return the height double getHeight() const; // Return the height
void setHeight(double height); // Set the height void setHeight(double height); // Set the height
virtual Vector3 getLocalSupportPoint(const Vector3& direction, double margin=0.0) const; // Return a support point in a given direction virtual Vector3 getLocalSupportPoint(const Vector3& direction, double margin=0.0) const; // Return a support point in a given direction
virtual Vector3 getLocalExtents(double margin=0.0) const; // Return the local extents in x,y and z direction virtual Vector3 getLocalExtents(double margin=0.0) const; // Return the local extents in x,y and z direction
virtual void computeLocalInertiaTensor(Matrix3x3& tensor, double mass) const; // Return the local inertia tensor of the shape
#ifdef VISUAL_DEBUG #ifdef VISUAL_DEBUG
@ -96,6 +97,13 @@ inline Vector3 ConeShape::getLocalExtents(double margin) const {
return Vector3(radius + margin, halfHeight + margin, radius + margin); return Vector3(radius + margin, halfHeight + margin, radius + margin);
} }
// Return the local inertia tensor of the shape
inline void ConeShape::computeLocalInertiaTensor(Matrix3x3& tensor, double mass) const {
double rSquare = radius * radius;
double diagXZ = 0.15 * mass * (rSquare + halfHeight);
tensor.setAllValues(diagXZ, 0.0, 0.0, 0.0, 0.3 * mass * rSquare, 0.0, 0.0, 0.0, diagXZ);
}
}; // End of the ReactPhysics3D namespace }; // End of the ReactPhysics3D namespace
#endif #endif

View File

@ -53,7 +53,7 @@ Vector3 CylinderShape::getLocalSupportPoint(const Vector3& direction, double mar
if (lengthW != 0.0) { if (lengthW != 0.0) {
if (uDotv < 0.0) supportPoint.setY(-halfHeight); if (uDotv < 0.0) supportPoint.setY(-halfHeight);
else supportPoint.setY(halfHeight); else supportPoint.setY(halfHeight);
supportPoint = supportPoint + (radius / lengthW) * w; supportPoint += (radius / lengthW) * w;
} }
else { else {
if (uDotv < 0.0) supportPoint.setY(-halfHeight); if (uDotv < 0.0) supportPoint.setY(-halfHeight);
@ -66,7 +66,7 @@ Vector3 CylinderShape::getLocalSupportPoint(const Vector3& direction, double mar
if (direction.lengthSquare() > MACHINE_EPSILON * MACHINE_EPSILON) { if (direction.lengthSquare() > MACHINE_EPSILON * MACHINE_EPSILON) {
unitVec = direction.getUnit(); unitVec = direction.getUnit();
} }
supportPoint = supportPoint + unitVec * margin; supportPoint += unitVec * margin;
} }
return supportPoint; return supportPoint;

View File

@ -56,9 +56,9 @@ class CylinderShape : public Shape {
void setRadius(double radius); // Set the radius void setRadius(double radius); // Set the radius
double getHeight() const; // Return the height double getHeight() const; // Return the height
void setHeight(double height); // Set the height void setHeight(double height); // Set the height
virtual Vector3 getLocalSupportPoint(const Vector3& direction, double margin=0.0) const; // Return a support point in a given direction virtual Vector3 getLocalSupportPoint(const Vector3& direction, double margin=0.0) const; // Return a support point in a given direction
virtual Vector3 getLocalExtents(double margin=0.0) const; // Return the local extents in x,y and z direction virtual Vector3 getLocalExtents(double margin=0.0) const; // Return the local extents in x,y and z direction
virtual void computeLocalInertiaTensor(Matrix3x3& tensor, double mass) const; // Return the local inertia tensor of the shape
#ifdef VISUAL_DEBUG #ifdef VISUAL_DEBUG
virtual void draw() const; // Draw the sphere (only for testing purpose) virtual void draw() const; // Draw the sphere (only for testing purpose)
@ -90,6 +90,13 @@ inline Vector3 CylinderShape::getLocalExtents(double margin) const {
return Vector3(radius + margin, halfHeight + margin, radius + margin); return Vector3(radius + margin, halfHeight + margin, radius + margin);
} }
// Return the local inertia tensor of the cylinder
inline void CylinderShape::computeLocalInertiaTensor(Matrix3x3& tensor, double mass) const {
double height = 2.0 * halfHeight;
double diag = (1.0 / 12.0) * mass * (3 * radius * radius + height * height);
tensor.setAllValues(diag, 0.0, 0.0, 0.0, 0.5 * mass * radius * radius, 0.0, 0.0, 0.0, diag);
}
}; // End of the ReactPhysics3D namespace }; // End of the ReactPhysics3D namespace
#endif #endif

View File

@ -29,6 +29,7 @@
// Libraries // Libraries
#include <cassert> #include <cassert>
#include "../mathematics/Vector3.h" #include "../mathematics/Vector3.h"
#include "../mathematics/Matrix3x3.h"
// ReactPhysics3D namespace // ReactPhysics3D namespace
namespace reactphysics3d { namespace reactphysics3d {
@ -52,8 +53,9 @@ class Shape {
Body* getBodyPointer() const; // Return the body pointer Body* getBodyPointer() const; // Return the body pointer
void setBodyPointer(Body* bodyPointer); // Set the body pointer void setBodyPointer(Body* bodyPointer); // Set the body pointer
virtual Vector3 getLocalSupportPoint(const Vector3& direction, double margin=0.0) const=0; // Return a local support point in a given direction virtual Vector3 getLocalSupportPoint(const Vector3& direction, double margin=0.0) const=0; // Return a local support point in a given direction
virtual Vector3 getLocalExtents(double margin=0.0) const=0; // Return the local extents in x,y and z direction virtual Vector3 getLocalExtents(double margin=0.0) const=0; // Return the local extents in x,y and z direction
virtual void computeLocalInertiaTensor(Matrix3x3& tensor, double mass) const=0; // Return the local inertia tensor of the shape
}; };
// Return the body pointer // Return the body pointer

View File

@ -48,8 +48,9 @@ class SphereShape : public Shape {
double getRadius() const; // Return the radius of the sphere double getRadius() const; // Return the radius of the sphere
void setRadius(double radius); // Set the radius of the sphere void setRadius(double radius); // Set the radius of the sphere
virtual Vector3 getLocalSupportPoint(const Vector3& direction, double margin=0.0) const; // Return a local support point in a given direction virtual Vector3 getLocalSupportPoint(const Vector3& direction, double margin=0.0) const; // Return a local support point in a given direction
virtual Vector3 getLocalExtents(double margin=0.0) const; // Return the local extents in x,y and z direction virtual Vector3 getLocalExtents(double margin=0.0) const; // Return the local extents in x,y and z direction
virtual void computeLocalInertiaTensor(Matrix3x3& tensor, double mass) const; // Return the local inertia tensor of the shape
#ifdef VISUAL_DEBUG #ifdef VISUAL_DEBUG
virtual void draw() const; // Draw the sphere (only for testing purpose) virtual void draw() const; // Draw the sphere (only for testing purpose)
@ -88,6 +89,12 @@ inline Vector3 SphereShape::getLocalExtents(double margin) const {
return Vector3(radius + margin, radius + margin, radius + margin); return Vector3(radius + margin, radius + margin, radius + margin);
} }
// Return the local inertia tensor of the sphere
inline void SphereShape::computeLocalInertiaTensor(Matrix3x3& tensor, double mass) const {
double diag = 0.4 * mass * radius * radius;
tensor.setAllValues(diag, 0.0, 0.0, 0.0, diag, 0.0, 0.0, 0.0, diag);
}
}; // End of the ReactPhysics3D namespace }; // End of the ReactPhysics3D namespace
#endif #endif