removed cached copies of damping and restitution

This commit is contained in:
Andrew Meadows 2015-04-24 14:50:37 -07:00
parent 7cd9023e23
commit 3ecf959b3e
7 changed files with 38 additions and 53 deletions

View file

@ -675,6 +675,17 @@ void EntityItem::setMass(float mass) {
}
}
const float DEFAULT_ENTITY_RESTITUTION = 0.5f;
const float DEFAULT_ENTITY_FRICTION = 0.5f;
float EntityItem::getRestitution() const {
return DEFAULT_ENTITY_RESTITUTION;
}
float EntityItem::getFriction() const {
return DEFAULT_ENTITY_FRICTION;
}
void EntityItem::simulate(const quint64& now) {
if (_lastSimulated == 0) {
_lastSimulated = now;
@ -1157,7 +1168,7 @@ void EntityItem::updateVelocity(const glm::vec3& value) {
void EntityItem::updateDamping(float value) {
if (fabsf(_damping - value) > MIN_DAMPING_DELTA) {
_damping = glm::clamp(value, 0.0f, 1.0f);
_dirtyFlags |= EntityItem::DIRTY_VELOCITY;
_dirtyFlags |= EntityItem::DIRTY_MATERIAL;
}
}
@ -1188,7 +1199,7 @@ void EntityItem::updateAngularVelocity(const glm::vec3& value) {
void EntityItem::updateAngularDamping(float value) {
if (fabsf(_angularDamping - value) > MIN_DAMPING_DELTA) {
_angularDamping = glm::clamp(value, 0.0f, 1.0f);
_dirtyFlags |= EntityItem::DIRTY_VELOCITY;
_dirtyFlags |= EntityItem::DIRTY_MATERIAL;
}
}

View file

@ -54,7 +54,8 @@ public:
DIRTY_MOTION_TYPE = 0x0010,
DIRTY_SHAPE = 0x0020,
DIRTY_LIFETIME = 0x0040,
DIRTY_UPDATEABLE = 0x0080
DIRTY_UPDATEABLE = 0x0080,
DIRTY_MATERIAL = 0x00100
};
DONT_ALLOW_INSTANTIATION // This class can not be instantiated directly
@ -188,6 +189,9 @@ public:
float getDamping() const { return _damping; }
void setDamping(float value) { _damping = value; }
float getRestitution() const;
float getFriction() const;
// lifetime related properties.
float getLifetime() const { return _lifetime; } /// get the lifetime in seconds for the entity
void setLifetime(float value) { _lifetime = value; } /// set the lifetime in seconds for the entity

View file

@ -143,15 +143,9 @@ void EntityMotionState::updateObjectEasy(uint32_t flags, uint32_t step) {
_sentStep = step;
}
// TODO: entity support for friction and restitution
//_restitution = _entity->getRestitution();
_body->setRestitution(_restitution);
//_friction = _entity->getFriction();
_body->setFriction(_friction);
_linearDamping = _entity->getDamping();
_angularDamping = _entity->getAngularDamping();
_body->setDamping(_linearDamping, _angularDamping);
if (flags & EntityItem::DIRTY_MATERIAL) {
updateMaterialProperties();
}
if (flags & EntityItem::DIRTY_MASS) {
float mass = _entity->computeMass();
@ -161,7 +155,13 @@ void EntityMotionState::updateObjectEasy(uint32_t flags, uint32_t step) {
_body->updateInertiaTensor();
}
_body->activate();
};
}
void EntityMotionState::updateMaterialProperties() {
_body->setRestitution(_entity->getRestitution());
_body->setFriction(_entity->getFriction());
_body->setDamping(fabsf(btMin(_entity->getDamping(), 1.0f)), fabsf(btMin(_entity->getAngularDamping(), 1.0f)));
}
void EntityMotionState::updateObjectVelocities() {
if (_body) {

View file

@ -51,6 +51,7 @@ public:
// these relay incoming values to the RigidBody
virtual void updateObjectEasy(uint32_t flags, uint32_t step);
virtual void updateMaterialProperties();
virtual void updateObjectVelocities();
virtual void computeShapeInfo(ShapeInfo& shapeInfo);

View file

@ -43,10 +43,6 @@ void ObjectMotionState::setSimulationStep(uint32_t step) {
}
ObjectMotionState::ObjectMotionState() :
_friction(DEFAULT_FRICTION),
_restitution(DEFAULT_RESTITUTION),
_linearDamping(0.0f),
_angularDamping(0.0f),
_motionType(MOTION_TYPE_STATIC),
_body(NULL),
_sentMoving(false),
@ -80,7 +76,7 @@ void ObjectMotionState::measureAcceleration() {
// Note: the integration equation for velocity uses damping: v1 = (v0 + a * dt) * (1 - D)^dt
// hence the equation for acceleration is: a = (v1 / (1 - D)^dt - v0) / dt
glm::vec3 velocity = bulletToGLM(_body->getLinearVelocity());
_measuredAcceleration = (velocity / powf(1.0f - _linearDamping, dt) - _lastVelocity) * invDt;
_measuredAcceleration = (velocity / powf(1.0f - _body->getLinearDamping(), dt) - _lastVelocity) * invDt;
_lastVelocity = velocity;
}
}
@ -90,22 +86,6 @@ void ObjectMotionState::resetMeasuredAcceleration() {
_lastVelocity = bulletToGLM(_body->getLinearVelocity());
}
void ObjectMotionState::setFriction(float friction) {
_friction = btMax(btMin(fabsf(friction), MAX_FRICTION), 0.0f);
}
void ObjectMotionState::setRestitution(float restitution) {
_restitution = btMax(btMin(fabsf(restitution), 1.0f), 0.0f);
}
void ObjectMotionState::setLinearDamping(float damping) {
_linearDamping = btMax(btMin(fabsf(damping), 1.0f), 0.0f);
}
void ObjectMotionState::setAngularDamping(float damping) {
_angularDamping = btMax(btMin(fabsf(damping), 1.0f), 0.0f);
}
void ObjectMotionState::setVelocity(const glm::vec3& velocity) const {
_body->setLinearVelocity(glmToBullet(velocity));
}
@ -181,7 +161,7 @@ bool ObjectMotionState::shouldSendUpdate(uint32_t simulationStep) {
// compute position error
if (glm::length2(_sentVelocity) > 0.0f) {
_sentVelocity += _sentAcceleration * dt;
_sentVelocity *= powf(1.0f - _linearDamping, dt);
_sentVelocity *= powf(1.0f - _body->getLinearDamping(), dt);
_sentPosition += dt * _sentVelocity;
}
@ -206,7 +186,7 @@ bool ObjectMotionState::shouldSendUpdate(uint32_t simulationStep) {
if (glm::length2(_sentAngularVelocity) > 0.0f) {
// compute rotation error
float attenuation = powf(1.0f - _angularDamping, dt);
float attenuation = powf(1.0f - _body->getAngularDamping(), dt);
_sentAngularVelocity *= attenuation;
// Bullet caps the effective rotation velocity inside its rotation integration step, therefore

View file

@ -36,7 +36,7 @@ enum MotionStateType {
// and re-added to the physics engine and "easy" which just updates the body properties.
const uint32_t HARD_DIRTY_PHYSICS_FLAGS = (uint32_t)(EntityItem::DIRTY_MOTION_TYPE | EntityItem::DIRTY_SHAPE);
const uint32_t EASY_DIRTY_PHYSICS_FLAGS = (uint32_t)(EntityItem::DIRTY_POSITION | EntityItem::DIRTY_VELOCITY |
EntityItem::DIRTY_MASS | EntityItem::DIRTY_COLLISION_GROUP);
EntityItem::DIRTY_MASS | EntityItem::DIRTY_COLLISION_GROUP | EntityItem::DIRTY_MATERIAL);
// These are the set of incoming flags that the PhysicsEngine needs to hear about:
const uint32_t DIRTY_PHYSICS_FLAGS = HARD_DIRTY_PHYSICS_FLAGS | EASY_DIRTY_PHYSICS_FLAGS;
@ -69,6 +69,7 @@ public:
// An EASY update does not require the object to be removed and then reinserted into the PhysicsEngine
virtual void updateObjectEasy(uint32_t flags, uint32_t frame) = 0;
virtual void updateMaterialProperties() = 0;
virtual void updateObjectVelocities() = 0;
MotionStateType getType() const { return _type; }
@ -77,11 +78,6 @@ public:
virtual void computeShapeInfo(ShapeInfo& info) = 0;
virtual float computeMass(const ShapeInfo& shapeInfo) const = 0;
void setFriction(float friction);
void setRestitution(float restitution);
void setLinearDamping(float damping);
void setAngularDamping(float damping);
void setVelocity(const glm::vec3& velocity) const;
void setAngularVelocity(const glm::vec3& velocity) const;
void setGravity(const glm::vec3& gravity) const;
@ -120,15 +116,9 @@ public:
protected:
void setRigidBody(btRigidBody* body);
MotionStateType _type = MOTION_STATE_TYPE_UNKNOWN;
MotionStateType _type = MOTION_STATE_TYPE_UNKNOWN; // type of MotionState
// TODO: move these materials properties outside of ObjectMotionState
float _friction;
float _restitution;
float _linearDamping;
float _angularDamping;
MotionType _motionType;
MotionType _motionType; // type of motion: KINEMATIC, DYNAMIC, or STATIC
btRigidBody* _body;

View file

@ -527,9 +527,8 @@ void PhysicsEngine::addObject(const ShapeInfo& shapeInfo, btCollisionShape* shap
}
}
body->setFlags(BT_DISABLE_WORLD_GRAVITY);
body->setRestitution(motionState->_restitution);
body->setFriction(motionState->_friction);
body->setDamping(motionState->_linearDamping, motionState->_angularDamping);
motionState->updateMaterialProperties();
_dynamicsWorld->addRigidBody(body);
motionState->resetMeasuredAcceleration();
}