Add the Material class to store the material properties of a rigid body

This commit is contained in:
Daniel Chappuis 2013-07-10 00:17:14 +02:00
parent a3ca3598d5
commit e081661d8c
10 changed files with 231 additions and 76 deletions

View File

@ -84,7 +84,7 @@ void Mesh::calculateNormals() {
// Normalize the normal at each vertex
for (uint i=0; i<getNbVertices(); i++) {
assert(mNormals[i].length() > 0);
std::cout << "vertex n : " << i << std::endl;
mNormals[i] = mNormals[i].normalize();
}
}

View File

@ -59,7 +59,7 @@ Scene::Scene(GlutViewer* viewer) : mViewer(viewer), mLight0(0),
float radius = 2.0f;
// Create all the cubes of the scene
for (int i=0; i<NB_CUBES; i++) {
for (int i=0; i<NB_SPHERES; i++) {
// Position of the cubes
float angle = i * 30.0f;
@ -70,11 +70,11 @@ Scene::Scene(GlutViewer* viewer) : mViewer(viewer), mLight0(0),
// Create a cube and a corresponding rigid in the dynamics world
Box* cube = new Box(BOX_SIZE, position , CUBE_MASS, mDynamicsWorld);
// The box is a moving rigid body
cube->getRigidBody()->setIsMotionEnabled(true);
// Set the bouncing factor of the box
cube->getRigidBody()->setRestitution(0.4);
// Change the material properties of the rigid body
rp3d::Material& material = cube->getRigidBody()->getMaterial();
material.setBounciness(0.4);
// Add the box the list of box in the scene
mBoxes.push_back(cube);
@ -87,8 +87,9 @@ Scene::Scene(GlutViewer* viewer) : mViewer(viewer), mLight0(0),
// The floor must be a non-moving rigid body
mFloor->getRigidBody()->setIsMotionEnabled(false);
// Set the bouncing factor of the floor
mFloor->getRigidBody()->setRestitution(0.3);
// Change the material properties of the floor rigid body
rp3d::Material& material = mFloor->getRigidBody()->getMaterial();
material.setBounciness(0.3);
// Start the simulation
startSimulation();

View File

@ -32,7 +32,7 @@
#include "Box.h"
// Constants
const int NB_CUBES = 20; // Number of boxes in the scene
const int NB_SPHERES = 20; // Number of boxes in the scene
const openglframework::Vector3 BOX_SIZE(2, 2, 2); // Box dimensions in meters
const openglframework::Vector3 FLOOR_SIZE(20, 0.5f, 20); // Floor dimensions in meters
const float CUBE_MASS = 1.0f; // Box mass in kilograms

View File

@ -209,8 +209,9 @@ void Scene::createBallAndSocketJoints() {
if (i == 0) mBallAndSocketJointChainBoxes[i]->getRigidBody()->setIsMotionEnabled(false);
else mBallAndSocketJointChainBoxes[i]->getRigidBody()->setIsMotionEnabled(true);
// Set the bouncing factor of the box
mBallAndSocketJointChainBoxes[i]->getRigidBody()->setRestitution(0.4);
// Change the material properties of the rigid body
rp3d::Material& material = mBallAndSocketJointChainBoxes[i]->getRigidBody()->getMaterial();
material.setBounciness(0.4);
positionBox.y -= boxDimension.y + 0.5;
}
@ -248,8 +249,9 @@ void Scene::createSliderJoint() {
// The fist box cannot move
mSliderJointBottomBox->getRigidBody()->setIsMotionEnabled(false);
// Set the bouncing factor of the box
mSliderJointBottomBox->getRigidBody()->setRestitution(0.4);
// Change the material properties of the rigid body
rp3d::Material& material1 = mSliderJointBottomBox->getRigidBody()->getMaterial();
material1.setBounciness(0.4);
// --------------- Create the second box --------------- //
@ -263,8 +265,9 @@ void Scene::createSliderJoint() {
// The second box is allowed to move
mSliderJointTopBox->getRigidBody()->setIsMotionEnabled(true);
// Set the bouncing factor of the box
mSliderJointTopBox->getRigidBody()->setRestitution(0.4);
// Change the material properties of the rigid body
rp3d::Material& material2 = mSliderJointTopBox->getRigidBody()->getMaterial();
material2.setBounciness(0.4);
// --------------- Create the joint --------------- //
@ -301,8 +304,9 @@ void Scene::createPropellerHingeJoint() {
// The fist box cannot move
mPropellerBox->getRigidBody()->setIsMotionEnabled(true);
// Set the bouncing factor of the box
mPropellerBox->getRigidBody()->setRestitution(0.4);
// Change the material properties of the rigid body
rp3d::Material& material = mPropellerBox->getRigidBody()->getMaterial();
material.setBounciness(0.4);
// --------------- Create the Hinge joint --------------- //
@ -338,8 +342,9 @@ void Scene::createFixedJoints() {
// The fist box cannot move
mFixedJointBox1->getRigidBody()->setIsMotionEnabled(true);
// Set the bouncing factor of the box
mFixedJointBox1->getRigidBody()->setRestitution(0.4);
// Change the material properties of the rigid body
rp3d::Material& material1 = mFixedJointBox1->getRigidBody()->getMaterial();
material1.setBounciness(0.4);
// --------------- Create the second box --------------- //
@ -352,8 +357,9 @@ void Scene::createFixedJoints() {
// The second box is allowed to move
mFixedJointBox2->getRigidBody()->setIsMotionEnabled(true);
// Set the bouncing factor of the box
mFixedJointBox2->getRigidBody()->setRestitution(0.4);
// Change the material properties of the rigid body
rp3d::Material& material2 = mFixedJointBox2->getRigidBody()->getMaterial();
material2.setBounciness(0.4);
// --------------- Create the first fixed joint --------------- //
@ -390,6 +396,7 @@ void Scene::createFloor() {
// The floor must be a non-moving rigid body
mFloor->getRigidBody()->setIsMotionEnabled(false);
// Set the bouncing factor of the floor
mFloor->getRigidBody()->setRestitution(0.3);
// Change the material properties of the rigid body
rp3d::Material& material = mFloor->getRigidBody()->getMaterial();
material.setBounciness(0.3);
}

View File

@ -35,10 +35,7 @@ using namespace reactphysics3d;
CollisionShape *collisionShape, bodyindex id)
: CollisionBody(transform, collisionShape, id), mInertiaTensorLocal(inertiaTensorLocal),
mMass(mass), mInertiaTensorLocalInverse(inertiaTensorLocal.getInverse()),
mMassInverse(decimal(1.0) / mass), mFrictionCoefficient(DEFAULT_FRICTION_COEFFICIENT),
mIsGravityEnabled(true) {
mRestitution = decimal(1.0);
mMassInverse(decimal(1.0) / mass), mIsGravityEnabled(true) {
assert(collisionShape);
}

View File

@ -29,6 +29,7 @@
// Libraries
#include <cassert>
#include "CollisionBody.h"
#include "../engine/Material.h"
#include "../mathematics/mathematics.h"
/// Namespace reactphysics3d
@ -73,15 +74,12 @@ class RigidBody : public CollisionBody {
/// Inverse of the mass of the body
decimal mMassInverse;
/// Coefficient of restitution (between 0 and 1) where 1 is for a very bouncy body
decimal mRestitution;
/// Friction coefficient
decimal mFrictionCoefficient;
/// True if the gravity needs to be applied to this rigid body
bool mIsGravityEnabled;
/// Material properties of the rigid body
Material mMaterial;
// -------------------- Methods -------------------- //
/// Private copy-constructor
@ -151,24 +149,18 @@ class RigidBody : public CollisionBody {
/// Return the inverse of the inertia tensor in world coordinates.
Matrix3x3 getInertiaTensorInverseWorld() const;
/// Get the restitution coefficient
decimal getRestitution() const;
/// Set the restitution coefficient
void setRestitution(decimal restitution);
/// Get the friction coefficient
decimal getFrictionCoefficient() const;
/// Set the friction coefficient
void setFrictionCoefficient(decimal frictionCoefficient);
/// Return true if the gravity needs to be applied to this rigid body
bool isGravityEnabled() const;
/// Set the variable to know if the gravity is applied to this rigid body
void enableGravity(bool isEnabled);
/// Return a reference to the material properties of the rigid body
Material& getMaterial();
/// Set a new material for this rigid body
void setMaterial(const Material& material);
};
// Method that return the mass of the body
@ -276,27 +268,6 @@ inline void RigidBody::setLinearVelocity(const Vector3& linearVelocity) {
}
}
// Get the restitution coeffficient of the rigid body
inline decimal RigidBody::getRestitution() const {
return mRestitution;
}
// Set the restitution coefficient
inline void RigidBody::setRestitution(decimal restitution) {
assert(restitution >= 0.0 && restitution <= 1.0);
mRestitution = restitution;
}
// Get the friction coefficient
inline decimal RigidBody::getFrictionCoefficient() const {
return mFrictionCoefficient;
}
// Set the friction coefficient
inline void RigidBody::setFrictionCoefficient(decimal frictionCoefficient) {
mFrictionCoefficient = frictionCoefficient;
}
// Return true if the gravity needs to be applied to this rigid body
inline bool RigidBody::isGravityEnabled() const {
return mIsGravityEnabled;
@ -307,6 +278,16 @@ inline void RigidBody::enableGravity(bool isEnabled) {
mIsGravityEnabled = isEnabled;
}
// Return a reference to the material properties of the rigid body
inline Material& RigidBody::getMaterial() {
return mMaterial;
}
// Set a new material for this rigid body
inline void RigidBody::setMaterial(const Material& material) {
mMaterial = material;
}
}
#endif

View File

@ -89,6 +89,9 @@ const decimal DEFAULT_TIMESTEP = decimal(1.0 / 60.0);
/// Default friction coefficient for a rigid body
const decimal DEFAULT_FRICTION_COEFFICIENT = decimal(0.3);
/// Default bounciness factor for a rigid body
const decimal DEFAULT_BOUNCINESS = decimal(0.5);
/// True if the deactivation (sleeping) of inactive bodies is enabled
const bool DEACTIVATION_ENABLED = true;

View File

@ -367,12 +367,12 @@ class ContactSolver {
const ContactManifoldSolver& manifold);
/// Compute the collision restitution factor from the restitution factor of each body
decimal computeMixedRestitutionFactor(const RigidBody* body1,
const RigidBody* body2) const;
decimal computeMixedRestitutionFactor(RigidBody *body1,
RigidBody *body2) const;
/// Compute the mixed friction coefficient from the friction coefficient of each body
decimal computeMixedFrictionCoefficient(const RigidBody* body1,
const RigidBody* body2)const;
decimal computeMixedFrictionCoefficient(RigidBody* body1,
RigidBody* body2) const;
/// Compute the two unit orthogonal vectors "t1" and "t2" that span the tangential friction
/// plane for a contact point. The two vectors have to be
@ -489,20 +489,21 @@ inline void ContactSolver::setIsSolveFrictionAtContactManifoldCenterActive(bool
}
// Compute the collision restitution factor from the restitution factor of each body
inline decimal ContactSolver::computeMixedRestitutionFactor(const RigidBody* body1,
const RigidBody* body2) const {
decimal restitution1 = body1->getRestitution();
decimal restitution2 = body2->getRestitution();
inline decimal ContactSolver::computeMixedRestitutionFactor(RigidBody* body1,
RigidBody* body2) const {
decimal restitution1 = body1->getMaterial().getBounciness();
decimal restitution2 = body2->getMaterial().getBounciness();
// Return the largest restitution factor
return (restitution1 > restitution2) ? restitution1 : restitution2;
}
// Compute the mixed friction coefficient from the friction coefficient of each body
inline decimal ContactSolver::computeMixedFrictionCoefficient(const RigidBody* body1,
const RigidBody* body2) const {
inline decimal ContactSolver::computeMixedFrictionCoefficient(RigidBody *body1,
RigidBody *body2) const {
// Use the geometric mean to compute the mixed friction coefficient
return sqrt(body1->getFrictionCoefficient() * body2->getFrictionCoefficient());
return sqrt(body1->getMaterial().getFrictionCoefficient() *
body2->getMaterial().getFrictionCoefficient());
}
// Compute a penetration constraint impulse

46
src/engine/Material.cpp Normal file
View File

@ -0,0 +1,46 @@
/********************************************************************************
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
* Copyright (c) 2010-2013 Daniel Chappuis *
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
// Libraries
#include "Material.h"
using namespace reactphysics3d;
// Constructor
Material::Material()
: mFrictionCoefficient(DEFAULT_FRICTION_COEFFICIENT), mBounciness(DEFAULT_BOUNCINESS) {
}
// Copy-constructor
Material::Material(const Material& material)
: mFrictionCoefficient(material.mFrictionCoefficient), mBounciness(material.mBounciness) {
}
// Destructor
Material::~Material() {
}

119
src/engine/Material.h Normal file
View File

@ -0,0 +1,119 @@
/********************************************************************************
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
* Copyright (c) 2010-2013 Daniel Chappuis *
*********************************************************************************
* *
* This software is provided 'as-is', without any express or implied warranty. *
* In no event will the authors be held liable for any damages arising from the *
* use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not claim *
* that you wrote the original software. If you use this software in a *
* product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source distribution. *
* *
********************************************************************************/
#ifndef REACTPHYSICS3D_MATERIAL_H
#define REACTPHYSICS3D_MATERIAL_H
// Libraries
#include <cassert>
#include "../configuration.h"
namespace reactphysics3d {
// Class Material
/**
* This class contains the material properties of a rigid body that will be use for
* the dynamics simulation like the friction coefficient or the bounciness of the rigid
* body.
*/
class Material {
private :
// -------------------- Attributes -------------------- //
/// Friction coefficient (positive value)
decimal mFrictionCoefficient;
/// Bounciness during collisions (between 0 and 1) where 1 is for a very bouncy body
decimal mBounciness;
public :
// -------------------- Methods -------------------- //
/// Constructor
Material();
/// Copy-constructor
Material(const Material& material);
/// Destructor
~Material();
/// Return the bounciness
decimal getBounciness() const;
/// Set the bounciness
void setBounciness(decimal bounciness);
/// Return the friction coefficient
decimal getFrictionCoefficient() const;
/// Set the friction coefficient
void setFrictionCoefficient(decimal frictionCoefficient);
/// Overloaded assignment operator
Material& operator=(const Material& material);
};
// Return the bounciness
inline decimal Material::getBounciness() const {
return mBounciness;
}
// Set the bounciness
inline void Material::setBounciness(decimal bounciness) {
assert(bounciness >= decimal(0.0) && bounciness <= decimal(1.0));
mBounciness = bounciness;
}
// Return the friction coefficient
inline decimal Material::getFrictionCoefficient() const {
return mFrictionCoefficient;
}
// Set the friction coefficient
inline void Material::setFrictionCoefficient(decimal frictionCoefficient) {
assert(frictionCoefficient >= decimal(0.0));
mFrictionCoefficient = frictionCoefficient;
}
// Overloaded assignment operator
inline Material& Material::operator=(const Material& material) {
// Check for self-assignment
if (this != &material) {
mFrictionCoefficient = material.mFrictionCoefficient;
mBounciness = material.mBounciness;
}
// Return this material
return *this;
}
}
#endif