From e39eb5fb4b91dd9380e698535d37b8a6ebcc3044 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Tue, 4 Feb 2014 11:48:50 +0100 Subject: [PATCH] RigidBody update adjusted Spherical and Simple --- .../Implementation/SimpleRigidBody.cpp | 52 ++++++++----------- .../Implementation/SphericalRigidBody.cpp | 51 ++++++++---------- 2 files changed, 46 insertions(+), 57 deletions(-) diff --git a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp index 9c66e736..034cedea 100644 --- a/Code/GamePhysics/Implementation/SimpleRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SimpleRigidBody.cpp @@ -325,41 +325,30 @@ void * SimpleRigidBody::GetCustomTag() const // return this->rigid.GetLinearVelocity(); //} - UpdateState SimpleRigidBody::Update( Float timeStepLength ) { - //if( this->isForwarded ) - //{ - // this->rigid.Move( this->deltaPos.xyz, this->deltaAxis.xyz ); - // this->deltaPos = Float4::null; - // this->deltaAxis = Float4::null; - // this->isForwarded = false; - //} - + if( this->collisionRebound.timeOfContact < 1.0f ) { // Rebound if needed - if( this->collisionRebound.timeOfContact < 1.0f ) - { - this->rigid.centerPos = Lerp( this->collisionRebound.previousSpatial.center, this->rigid.centerPos, this->collisionRebound.timeOfContact ); - this->rigid.SetRotation( Lerp(this->collisionRebound.previousSpatial.axis, this->rigid.axis, this->collisionRebound.timeOfContact) ); - this->rigid.boundingReach = Lerp( this->collisionRebound.previousSpatial.reach, this->rigid.boundingReach, this->collisionRebound.timeOfContact ); - this->collisionRebound.timeOfContact = 1.0f; - } - - // Update rebound data - this->collisionRebound.previousSpatial.center = this->rigid.centerPos; - this->collisionRebound.previousSpatial.axis = this->rigid.axis; - this->collisionRebound.previousSpatial.reach = this->rigid.boundingReach; + this->rigid.centerPos = Lerp( this->collisionRebound.previousSpatial.center, this->rigid.centerPos, this->collisionRebound.timeOfContact ); + this->rigid.SetRotation( Lerp(this->collisionRebound.previousSpatial.axis, this->rigid.axis, this->collisionRebound.timeOfContact) ); + this->rigid.boundingReach = Lerp( this->collisionRebound.previousSpatial.reach, this->rigid.boundingReach, this->collisionRebound.timeOfContact ); + this->collisionRebound.timeOfContact = 1.0f; } - this->rigid.Update_LeapFrog( timeStepLength ); + // Maintain rotation resolution by keeping axis within [0, 2pi] (trigonometric methods gets faster too) + Float4 temp; + ::std::modf( this->rigid.axis * (0.5f / pi), temp.xyz ); + this->rigid.axis -= ((2.0f * pi) * temp).xyz; - { // Maintain rotation resolution by keeping axis within [0, 2pi] (trigonometric methods gets faster too) - Float3 n; - ::std::modf( this->rigid.axis * (0.5f / pi), n ); - this->rigid.axis -= (2.0f * pi) * n; - } + // Update rebound data + this->collisionRebound.previousSpatial.center = this->rigid.centerPos; + this->collisionRebound.previousSpatial.axis = this->rigid.axis; + this->collisionRebound.previousSpatial.reach = this->rigid.boundingReach; - { // Check if this is close enough to be set resting + // Check if this is close enough to be set resting + temp = Float4( this->rigid.impulse_Linear, 0.0f ) + Float4( this->rigid.impulse_Angular, 0.0f ); + if( temp.Dot(temp) <= (Constant::epsilon * Constant::epsilon) ) + { unsigned char resting = 0; if( this->rigid.momentum_Linear.Dot(this->rigid.momentum_Linear) <= (Constant::epsilon * Constant::epsilon) ) { @@ -371,9 +360,14 @@ UpdateState SimpleRigidBody::Update( Float timeStepLength ) this->rigid.momentum_Angular = Float3::null; ++resting; } - if( resting == 2 ) return UpdateState_resting; + if( resting == 2 ) + { + this->rigid.impulse_Linear = this->rigid.impulse_Angular = Float3::null; + return UpdateState_resting; + } } + this->rigid.Update_LeapFrog( timeStepLength ); return UpdateState_altered; } diff --git a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp index 448dbb4f..6b4cd4ed 100644 --- a/Code/GamePhysics/Implementation/SphericalRigidBody.cpp +++ b/Code/GamePhysics/Implementation/SphericalRigidBody.cpp @@ -249,38 +249,28 @@ void * SphericalRigidBody::GetCustomTag() const UpdateState SphericalRigidBody::Update( Float timeStepLength ) { - //if( this->isForwarded ) - //{ - // this->rigid.Move( this->deltaPos.xyz, this->deltaAxis.xyz ); - // this->deltaPos = Float4::null; - // this->deltaAxis = Float4::null; - // this->isForwarded = false; - //} - + if( this->collisionRebound.timeOfContact < 1.0f ) { // Rebound if needed - if( this->collisionRebound.timeOfContact < 1.0f ) - { - this->rigid.centerPos = Lerp( this->collisionRebound.previousSpatial.center, this->rigid.centerPos, this->collisionRebound.timeOfContact ); - this->rigid.SetRotation( Lerp(this->collisionRebound.previousSpatial.axis, this->rigid.axis, this->collisionRebound.timeOfContact) ); - this->rigid.boundingReach = Lerp( this->collisionRebound.previousSpatial.reach, this->rigid.boundingReach, this->collisionRebound.timeOfContact ); - this->collisionRebound.timeOfContact = 1.0f; - } - - // Update rebound data - this->collisionRebound.previousSpatial.center = this->rigid.centerPos; - this->collisionRebound.previousSpatial.axis = this->rigid.axis; - this->collisionRebound.previousSpatial.reach = this->rigid.boundingReach; + this->rigid.centerPos = Lerp( this->collisionRebound.previousSpatial.center, this->rigid.centerPos, this->collisionRebound.timeOfContact ); + this->rigid.SetRotation( Lerp(this->collisionRebound.previousSpatial.axis, this->rigid.axis, this->collisionRebound.timeOfContact) ); + this->rigid.boundingReach = Lerp( this->collisionRebound.previousSpatial.reach, this->rigid.boundingReach, this->collisionRebound.timeOfContact ); + this->collisionRebound.timeOfContact = 1.0f; } - this->rigid.Update_LeapFrog( timeStepLength ); + // Maintain rotation resolution by keeping axis within [0, 2pi] (trigonometric methods gets faster too) + Float4 temp; + ::std::modf( this->rigid.axis * (0.5f / pi), temp.xyz ); + this->rigid.axis -= ((2.0f * pi) * temp).xyz; - { // Maintain rotation resolution by keeping axis within [0, 2pi] (trigonometric methods gets faster too) - Float3 n; - ::std::modf( this->rigid.axis * (0.5f / pi), n ); - this->rigid.axis -= (2.0f * pi) * n; - } + // Update rebound data + this->collisionRebound.previousSpatial.center = this->rigid.centerPos; + this->collisionRebound.previousSpatial.axis = this->rigid.axis; + this->collisionRebound.previousSpatial.reach = this->rigid.boundingReach; - { // Check if this is close enough to be set resting + // Check if this is close enough to be set resting + temp = Float4( this->rigid.impulse_Linear, 0.0f ) + Float4( this->rigid.impulse_Angular, 0.0f ); + if( temp.Dot(temp) <= (Constant::epsilon * Constant::epsilon) ) + { unsigned char resting = 0; if( this->rigid.momentum_Linear.Dot(this->rigid.momentum_Linear) <= (Constant::epsilon * Constant::epsilon) ) { @@ -292,9 +282,14 @@ UpdateState SphericalRigidBody::Update( Float timeStepLength ) this->rigid.momentum_Angular = Float3::null; ++resting; } - if( resting == 2 ) return UpdateState_resting; + if( resting == 2 ) + { + this->rigid.impulse_Linear = this->rigid.impulse_Angular = Float3::null; + return UpdateState_resting; + } } + this->rigid.Update_LeapFrog( timeStepLength ); return UpdateState_altered; }