From e41d16cc0312cf964d47c528522a81ba4d9b1234 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 20 Nov 2013 11:36:17 +0100 Subject: [PATCH 1/8] Task assignment in RigidBody.cpp --- Code/OysterPhysics3D/RigidBody.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Code/OysterPhysics3D/RigidBody.cpp b/Code/OysterPhysics3D/RigidBody.cpp index 319d4da4..32395d8d 100644 --- a/Code/OysterPhysics3D/RigidBody.cpp +++ b/Code/OysterPhysics3D/RigidBody.cpp @@ -259,6 +259,10 @@ Float3 RigidBody::GetTangentialImpulseForceAt_World( const Float3 &worldPos ) co return this->GetTangentialImpulseForceAt_Local( (this->GetView() * Float4(worldPos, 1.0f)).xyz ); // should not be any disform thus result.w = 1.0f } +// Dan +////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Robin + Float3 RigidBody::GetTangentialLinearMomentumAt_Local( const Float3 &localPos ) const { // by Dan Andersson return Formula::TangentialLinearMomentum( this->angularMomentum, localPos ); From 2149ca8ab5ab23bd4cedffb81b1e9f09f0bc7357 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 20 Nov 2013 11:44:54 +0100 Subject: [PATCH 2/8] Dans fix part of RigidBody.cpp DONE --- Code/OysterPhysics3D/RigidBody.cpp | 62 +++++++++++------------------- 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/Code/OysterPhysics3D/RigidBody.cpp b/Code/OysterPhysics3D/RigidBody.cpp index 32395d8d..7af77118 100644 --- a/Code/OysterPhysics3D/RigidBody.cpp +++ b/Code/OysterPhysics3D/RigidBody.cpp @@ -80,68 +80,56 @@ void RigidBody::Update_LeapFrog( Float deltaTime ) this->impulseTorqueSum = Float3::null; } -void RigidBody::ApplyImpulseForce( const Float3 &f ) +void RigidBody::ApplyImpulseForce( const Float3 &worldF ) { // by Dan Andersson - this->impulseForceSum += f; + this->impulseForceSum += worldF; } -void RigidBody::ApplyImpulseForceAt_Local( const Float3 &localForce, const Float3 &localOffset ) +void RigidBody::ApplyImpulseForceAt( const Float3 &worldF, const Float3 &worldPos ) { // by Dan Andersson - if( localOffset != Float3::null ) + Float3 worldOffset = worldPos - this->box.center; + if( worldOffset != Float3::null ) { - this->impulseForceSum += VectorProjection( localForce, localOffset ); - this->impulseTorqueSum += Formula::ImpulseTorque( localForce, localOffset ); + this->impulseForceSum += VectorProjection( worldF, worldOffset ); + this->impulseTorqueSum += Formula::ImpulseTorque( worldF, worldOffset ); } else { - this->impulseForceSum += localForce; + this->impulseForceSum += worldF; } } -void RigidBody::ApplyImpulseForceAt_World( const Float3 &worldForce, const Float3 &worldPos ) +void RigidBody::ApplyLinearImpulseAcceleration( const Float3 &worldA ) { // by Dan Andersson - Float4x4 view = this->GetView(); - this->ApplyImpulseForceAt_Local( (view * Float4(worldForce, 0.0f)).xyz, - (view * Float4(worldPos, 1.0f)).xyz ); // should not be any disform thus result.w = 1.0f + this->impulseForceSum += Formula::ImpulseForce( this->mass, worldA ); } -void RigidBody::ApplyLinearImpulseAcceleration( const Float3 &a ) +void RigidBody::ApplyLinearImpulseAccelerationAt( const Float3 &worldA, const Float3 &worldPos ) { // by Dan Andersson - this->impulseForceSum += Formula::ImpulseForce( this->mass, a ); -} - -void RigidBody::ApplyLinearImpulseAccelerationAt_Local( const Float3 &localImpulseLinearAcc, const Float3 &localOffset ) -{ // by Dan Andersson - if( localOffset != Float3::null ) + Float3 worldOffset = worldPos - this->box.center; + if( worldOffset != Float3::null ) { - this->impulseForceSum += Formula::ImpulseForce( this->mass, VectorProjection(localImpulseLinearAcc, localOffset) ); + this->impulseForceSum += Formula::ImpulseForce( this->mass, VectorProjection(worldA, worldOffset) ); // tanAcc = angularAcc x localPosition // angularAcc = localPosition x tanAcc = localPosition x linearAcc // T = I * angularAcc - this->impulseTorqueSum += Formula::ImpulseTorque( this->momentOfInertiaTensor, Formula::AngularImpulseAcceleration(localImpulseLinearAcc, localOffset) ); + this->impulseTorqueSum += Formula::ImpulseTorque( this->momentOfInertiaTensor, Formula::AngularImpulseAcceleration(worldA, worldOffset) ); } else { - this->impulseForceSum += Formula::ImpulseForce( this->mass, localImpulseLinearAcc ); + this->impulseForceSum += Formula::ImpulseForce( this->mass, worldA ); } } -void RigidBody::ApplyLinearImpulseAccelerationAt_World( const Float3 &worldImpulseLinearAcc, const Float3 &worldPos ) +void RigidBody::ApplyImpulseTorque( const Float3 &worldT ) { // by Dan Andersson - Float4x4 view = this->GetView(); - this->ApplyLinearImpulseAccelerationAt_Local( (view * Float4(worldImpulseLinearAcc, 0.0f)).xyz, - (view * Float4(worldPos, 1.0f)).xyz ); // should not be any disform thus result.w = 1.0f + this->impulseTorqueSum += worldT; } -void RigidBody::ApplyImpulseTorque( const Float3 &t ) +void RigidBody::ApplyAngularImpulseAcceleration( const Float3 &worldA ) { // by Dan Andersson - this->impulseTorqueSum += t; -} - -void RigidBody::ApplyAngularImpulseAcceleration( const Float3 &a ) -{ // by Dan Andersson - this->impulseTorqueSum += Formula::ImpulseTorque( this->momentOfInertiaTensor, a ); + this->impulseTorqueSum += Formula::ImpulseTorque( this->momentOfInertiaTensor, worldA ); } Float4x4 & RigidBody::AccessOrientation() @@ -249,14 +237,10 @@ Float3 RigidBody::GetLinearVelocity() const return Formula::LinearVelocity( this->mass, this->linearMomentum ); } -Float3 RigidBody::GetTangentialImpulseForceAt_Local( const Float3 &localPos ) const +Float3 RigidBody::GetTangentialImpulseForceAt( const Float3 &worldPos ) const { // by Dan Andersson - return Formula::TangentialImpulseForce( this->impulseTorqueSum, localPos ); -} - -Float3 RigidBody::GetTangentialImpulseForceAt_World( const Float3 &worldPos ) const -{ // by Dan Andersson - return this->GetTangentialImpulseForceAt_Local( (this->GetView() * Float4(worldPos, 1.0f)).xyz ); // should not be any disform thus result.w = 1.0f + Float3 worldOffset = worldPos - this->box.center; + return Formula::TangentialImpulseForce( this->impulseTorqueSum, worldOffset ); } // Dan From 80301ae02ebd2af921bee1c57ba479b7a9bf4ec3 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Wed, 20 Nov 2013 11:48:39 +0100 Subject: [PATCH 3/8] Robin's fix part of RigidBody.cpp DONE --- Code/OysterPhysics3D/RigidBody.cpp | 115 ++++++----------------------- 1 file changed, 22 insertions(+), 93 deletions(-) diff --git a/Code/OysterPhysics3D/RigidBody.cpp b/Code/OysterPhysics3D/RigidBody.cpp index 7af77118..f6d0695d 100644 --- a/Code/OysterPhysics3D/RigidBody.cpp +++ b/Code/OysterPhysics3D/RigidBody.cpp @@ -243,58 +243,28 @@ Float3 RigidBody::GetTangentialImpulseForceAt( const Float3 &worldPos ) const return Formula::TangentialImpulseForce( this->impulseTorqueSum, worldOffset ); } -// Dan -////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// Robin - -Float3 RigidBody::GetTangentialLinearMomentumAt_Local( const Float3 &localPos ) const +Float3 RigidBody::GetTangentialLinearMomentumAt( const Float3 &worldPos ) const { // by Dan Andersson - return Formula::TangentialLinearMomentum( this->angularMomentum, localPos ); + return Formula::TangentialLinearMomentum( this->angularMomentum, worldPos ); } -Float3 RigidBody::GetTangentialLinearMomentumAt_World( const Float3 &worldPos ) const -{ // by Dan Andersson - return this->GetTangentialLinearMomentumAt_Local( (this->GetView() * Float4(worldPos, 1.0f)).xyz ); // should not be any disform thus result.w = 1.0f -} - -Float3 RigidBody::GetTangentialImpulseAccelerationAt_Local( const Float3 &localPos ) const -{ // by Dan Andersson - return Formula::TangentialImpulseAcceleration( this->momentOfInertiaTensor.GetInverse(), this->impulseTorqueSum, localPos ); -} - -Float3 RigidBody::GetTangentialImpulseAccelerationAt_World( const Float3 &worldPos ) const +Float3 RigidBody::GetTangentialImpulseAccelerationAt( const Float3 &worldPos ) const { // by Dan Andersson return this->GetTangentialImpulseAccelerationAt_Local( (this->GetView() * Float4(worldPos, 1.0f)).xyz ); // should not be any disform thus result.w = 1.0f } -Float3 RigidBody::GetTangentialLinearVelocityAt_Local( const Float3 &localPos ) const -{ // by Dan Andersson - return Formula::TangentialLinearVelocity( this->momentOfInertiaTensor.GetInverse(), this->angularMomentum, localPos ); -} - -Float3 RigidBody::GetTangentialLinearVelocityAt_World( const Float3 &worldPos ) const +Float3 RigidBody::GetTangentialLinearVelocityAt( const Float3 &worldPos ) const { // by Dan Andersson return this->GetTangentialLinearVelocityAt_Local( (this->GetView() * Float4(worldPos, 1.0f)).xyz ); // should not be any disform thus result.w = 1.0f } -Float3 RigidBody::GetImpulseForceAt_Local( const Float3 &localPos ) const -{ // by Dan Andersson - return this->impulseForceSum + Formula::TangentialImpulseForce( this->impulseForceSum, localPos ); -} - -Float3 RigidBody::GetImpulseForceAt_World( const Float3 &worldPos ) const +Float3 RigidBody::GetImpulseForceAt( const Float3 &worldPos ) const { // by Dan Andersson Float4 localForce = Float4( this->GetImpulseForceAt_Local((this->GetView() * Float4(worldPos, 1.0f)).xyz), 0.0f ); // should not be any disform thus result.w = 1.0f return (this->box.orientation * localForce).xyz; // should not be any disform thus result.w = 0.0f } -Float3 RigidBody::GetLinearMomentumAt_Local( const Float3 &localPos ) const -{ // by Dan Andersson - // Reminder! Momentum is a world value. - return Float3::null; // TODO: -} - -Float3 RigidBody::GetLinearMomentumAt_World( const Float3 &worldPos ) const +Float3 RigidBody::GetLinearMomentumAt( const Float3 &worldPos ) const { // by Dan Andersson // Reminder! Momentum is a world value. Float4 localMomentum = Float4( this->GetLinearMomentumAt_Local((this->GetView() * Float4(worldPos, 1.0f)).xyz), 0.0f ); // should not be any disform thus result.w = 1.0f @@ -304,35 +274,21 @@ Float3 RigidBody::GetLinearMomentumAt_World( const Float3 &worldPos ) const return this->linearMomentum + Formula::TangentialLinearMomentum( this->angularMomentum, worldPos ); } -Float3 RigidBody::GetImpulseAccelerationAt_Local( const Float3 &localPos ) const -{ // by Dan Andersson - // Reminder! Acceleration is a world value. - Float4 worldAccel = Float4( this->GetImpulseAccelerationAt_Local((this->box.orientation * Float4(localPos, 1.0f)).xyz), 0.0f ); // should not be any disform thus result.w = 1.0f - return (this->GetView() * worldAccel).xyz; // should not be any disform thus result.w = 0.0f -} - -Float3 RigidBody::GetImpulseAccelerationAt_World( const Float3 &worldPos ) const +Float3 RigidBody::GetImpulseAccelerationAt( const Float3 &worldPos ) const { // by Dan Andersson // Reminder! Acceleration is a world value. return Formula::LinearImpulseAcceleration( this->mass, this->impulseForceSum ) + Formula::TangentialImpulseAcceleration( this->momentOfInertiaTensor.GetInverse(), this->impulseTorqueSum, worldPos ); } -Float3 RigidBody::GetLinearVelocityAt_Local( const Float3 &localPos ) const -{ // by Dan Andersson - // Reminder! Velocity is a world value. - Float4 worldV = Float4( this->GetLinearVelocityAt_Local((this->box.orientation * Float4(localPos, 1.0f)).xyz), 0.0f ); // should not be any disform thus result.w = 1.0f - return (this->GetView() * worldV).xyz; // should not be any disform thus result.w = 0.0f -} - -Float3 RigidBody::GetLinearVelocityAt_World( const Float3 &worldPos ) const +Float3 RigidBody::GetLinearVelocityAt( const Float3 &worldPos ) const { // by Dan Andersson // Reminder! Velocity is a world value. return Formula::LinearVelocity( this->mass, this->linearMomentum ) + Formula::TangentialLinearVelocity( this->momentOfInertiaTensor.GetInverse(), this->angularMomentum, worldPos ); } -void RigidBody::SetMomentOfInertia( const Float4x4 &i ) +void RigidBody::SetMomentOfInertia( const Float4x4 &localI ) { // by Dan Andersson if( i.GetDeterminant() != 0.0f ) // insanitycheck! momentOfInertiaTensor must be invertable { @@ -368,98 +324,71 @@ void RigidBody::SetSize( const Float3 &widthHeight ) this->box.boundingOffset = 0.5f * widthHeight; } -void RigidBody::SetCenter( const Float3 &p ) +void RigidBody::SetCenter( const Float3 &worldPos ) { // by Dan Andersson this->box.center = p; } -void RigidBody::SetImpulseTorque( const Float3 &t ) +void RigidBody::SetImpulseTorque( const Float3 &worldT ) { // by Dan Andersson this->impulseTorqueSum = t; } -void RigidBody::SetAngularMomentum( const Float3 &h ) +void RigidBody::SetAngularMomentum( const Float3 &worldH ) { // by Dan Andersson this->angularMomentum = h; } -void RigidBody::SetAngularImpulseAcceleration( const Float3 &a ) +void RigidBody::SetAngularImpulseAcceleration( const Float3 &worldA ) { // by Dan Andersson this->impulseTorqueSum = Formula::ImpulseTorque( this->momentOfInertiaTensor, a ); } -void RigidBody::SetAngularVelocity( const Float3 &w ) +void RigidBody::SetAngularVelocity( const Float3 &worldW ) { // by Dan Andersson this->angularMomentum = Formula::AngularMomentum( this->momentOfInertiaTensor, w ); } -void RigidBody::SetImpulseForce( const Float3 &f ) +void RigidBody::SetImpulseForce( const Float3 &worldF ) { // by Dan Andersson this->impulseForceSum = f; } -void RigidBody::SetLinearMomentum( const Float3 &g ) +void RigidBody::SetLinearMomentum( const Float3 &worldG ) { // by Dan Andersson this->linearMomentum = g; } -void RigidBody::SetLinearImpulseAcceleration( const Float3 &a ) +void RigidBody::SetLinearImpulseAcceleration( const Float3 &worldA ) { // by Dan Andersson this->impulseForceSum = Formula::ImpulseForce( this->mass, a ); } -void RigidBody::SetLinearVelocity( const Float3 &v ) +void RigidBody::SetLinearVelocity( const Float3 &worldV ) { // by Dan Andersson this->linearMomentum = Formula::LinearMomentum( this->mass, v ); } -void RigidBody::SetImpulseForceAt_Local( const Float3 &localForce, const Float3 &localPos ) -{ // by Dan Andersson - // Reminder! Impulse force and torque is world values. - Float3 worldForce = ( this->box.orientation * Float4(localForce, 0.0f) ).xyz, - worldPos = ( this->box.orientation * Float4(localPos, 1.0f) ).xyz; - this->SetImpulseForceAt_World( worldForce, worldPos ); - -} - -void RigidBody::SetImpulseForceAt_World( const Float3 &worldForce, const Float3 &worldPos ) +void RigidBody::SetImpulseForceAt( const Float3 &worldF, const Float3 &worldPos ) { // by Dan Andersson // Reminder! Impulse force and torque is world values. this->impulseForceSum = VectorProjection( worldForce, worldPos ); this->impulseTorqueSum = Formula::ImpulseTorque( worldForce, worldPos ); } -void RigidBody::SetLinearMomentumAt_Local( const Float3 &localG, const Float3 &localPos ) -{ // by Dan Andersson - // Reminder! Linear and angular momentum is world values. - Float3 worldG = ( this->box.orientation * Float4(localG, 0.0f) ).xyz, - worldPos = ( this->box.orientation * Float4(localPos, 1.0f) ).xyz; - this->SetLinearMomentumAt_World( worldG, worldPos ); -} - -void RigidBody::SetLinearMomentumAt_World( const Float3 &worldG, const Float3 &worldPos ) +void RigidBody::SetLinearMomentumAt( const Float3 &worldG, const Float3 &worldPos ) { // by Dan Andersson // Reminder! Linear and angular momentum is world values. this->linearMomentum = VectorProjection( worldG, worldPos ); this->angularMomentum = Formula::AngularMomentum( worldG, worldPos ); } -void RigidBody::SetImpulseAccelerationAt_Local( const Float3 &a, const Float3 &pos ) -{ // by Dan Andersson - -} - -void RigidBody::SetImpulseAccelerationAt_World( const Float3 &a, const Float3 &pos ) +void RigidBody::SetImpulseAccelerationAt( const Float3 &worldA, const Float3 &pos ) { // by } -void RigidBody::SetLinearVelocityAt_Local( const Float3 &v, const Float3 &pos ) -{ // by - -} - -void RigidBody::SetLinearVelocityAt_World( const Float3 &v, const Float3 &pos ) +void RigidBody::SetLinearVelocityAt( const Float3 &worldV, const Float3 &pos ) { // by } \ No newline at end of file From 687fb6835a39ad9c40b715731c85b64c8bdcdbe3 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 20 Nov 2013 11:58:27 +0100 Subject: [PATCH 4/8] TangentialImpulseAcceleration fixed selfexplanatory documentation --- Code/OysterPhysics3D/OysterPhysics3D.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Code/OysterPhysics3D/OysterPhysics3D.h b/Code/OysterPhysics3D/OysterPhysics3D.h index a5d58c0b..ad049907 100644 --- a/Code/OysterPhysics3D/OysterPhysics3D.h +++ b/Code/OysterPhysics3D/OysterPhysics3D.h @@ -112,12 +112,12 @@ namespace Oyster { namespace Physics3D } /****************************************************************** - * Returns the local impulse acceleration at localPos, of a mass in angular acceleration. + * Returns the world impulse acceleration at ( worldOffset = worldPos - body's center of gravity ), of a mass in angular acceleration. * @todo TODO: improve doc ******************************************************************/ - inline ::Oyster::Math::Float3 TangentialImpulseAcceleration( const ::Oyster::Math::Float4x4 &momentOfInertiaInversed, const ::Oyster::Math::Float3 &impulseTorque, const ::Oyster::Math::Float3 &localOffset ) + inline ::Oyster::Math::Float3 TangentialImpulseAcceleration( const ::Oyster::Math::Float4x4 &worldMomentOfInertiaInversed, const ::Oyster::Math::Float3 &worldImpulseTorque, const ::Oyster::Math::Float3 &worldOffset ) { - return AngularImpulseAcceleration( momentOfInertiaInversed, impulseTorque ).Cross( localOffset ); + return AngularImpulseAcceleration( worldMomentOfInertiaInversed, worldImpulseTorque ).Cross( worldOffset ); } /****************************************************************** From d90074d9097f203c3f37e01df72d95ea312b675d Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Wed, 20 Nov 2013 17:40:12 +0100 Subject: [PATCH 5/8] Minor changes Some argument renaming and changed a few RigidBody functions, in respect to the change in the Box struct --- Code/OysterPhysics3D/OysterPhysics3D.h | 38 +++++++++++++------------- Code/OysterPhysics3D/RigidBody.cpp | 29 ++++++++------------ 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/Code/OysterPhysics3D/OysterPhysics3D.h b/Code/OysterPhysics3D/OysterPhysics3D.h index ad049907..86e29307 100644 --- a/Code/OysterPhysics3D/OysterPhysics3D.h +++ b/Code/OysterPhysics3D/OysterPhysics3D.h @@ -49,7 +49,7 @@ namespace Oyster { namespace Physics3D } /****************************************************************** - * Returns the local angular momentum of a mass in rotation. + * Returns the world angular momentum of a mass in rotation. * @todo TODO: improve doc ******************************************************************/ inline ::Oyster::Math::Float3 AngularMomentum( const ::Oyster::Math::Float4x4 &momentOfInertia, const ::Oyster::Math::Float3 &angularVelocity ) @@ -58,39 +58,39 @@ namespace Oyster { namespace Physics3D } /****************************************************************** - * Returns the local angular momentum of a mass in rotation. + * Returns the world angular momentum of a mass in rotation. * @todo TODO: improve doc ******************************************************************/ - inline ::Oyster::Math::Float3 AngularMomentum( const ::Oyster::Math::Float3 linearMomentum, const ::Oyster::Math::Float3 &offset ) + inline ::Oyster::Math::Float3 AngularMomentum( const ::Oyster::Math::Float3 linearMomentum, const ::Oyster::Math::Float3 &worldOffset ) { return offset.Cross( linearMomentum ); } /****************************************************************** - * Returns the local tangential momentum at localPos, of a mass in rotation. + * Returns the world tangential momentum at worldPos, of a mass in rotation. * @todo TODO: improve doc ******************************************************************/ - inline ::Oyster::Math::Float3 TangentialLinearMomentum( const ::Oyster::Math::Float3 &angularMomentum, const ::Oyster::Math::Float3 &localOffset ) + inline ::Oyster::Math::Float3 TangentialLinearMomentum( const ::Oyster::Math::Float3 &angularMomentum, const ::Oyster::Math::Float3 &worldOffset ) { - return angularMomentum.Cross( localOffset ); + return angularMomentum.Cross( worldOffset ); } /****************************************************************** - * Returns the local tangential momentum at localPos, of a mass in rotation. + * Returns the world tangential momentum at worldPos, of a mass in rotation. * @todo TODO: improve doc ******************************************************************/ - inline ::Oyster::Math::Float3 TangentialLinearMomentum( const ::Oyster::Math::Float4x4 &momentOfInertia, const ::Oyster::Math::Float3 &angularVelocity, const ::Oyster::Math::Float3 &localOffset ) + inline ::Oyster::Math::Float3 TangentialLinearMomentum( const ::Oyster::Math::Float4x4 &momentOfInertia, const ::Oyster::Math::Float3 &angularVelocity, const ::Oyster::Math::Float3 &worldOffset ) { - return TangentialLinearMomentum( AngularMomentum(momentOfInertia, angularVelocity), localOffset ); + return TangentialLinearMomentum( AngularMomentum(momentOfInertia, angularVelocity), worldOffset ); } /****************************************************************** - * Returns the local impulse force at localPos, of a mass in angular acceleration. + * Returns the world impulse force at worldPos, of a mass in angular acceleration. * @todo TODO: improve doc ******************************************************************/ - inline ::Oyster::Math::Float3 TangentialImpulseForce( const ::Oyster::Math::Float3 &impulseTorque, const ::Oyster::Math::Float3 &localOffset ) + inline ::Oyster::Math::Float3 TangentialImpulseForce( const ::Oyster::Math::Float3 &impulseTorque, const ::Oyster::Math::Float3 &worldOffset ) { - return impulseTorque.Cross( localOffset ); + return impulseTorque.Cross( worldOffset ); } /****************************************************************** @@ -106,7 +106,7 @@ namespace Oyster { namespace Physics3D * * @todo TODO: improve doc ******************************************************************/ - inline ::Oyster::Math::Float3 AngularImpulseAcceleration( const ::Oyster::Math::Float3 &linearImpulseAcceleration, const ::Oyster::Math::Float3 &offset ) + inline ::Oyster::Math::Float3 AngularImpulseAcceleration( const ::Oyster::Math::Float3 &linearImpulseAcceleration, const ::Oyster::Math::Float3 &worldOffset ) { return offset.Cross( linearImpulseAcceleration ); } @@ -121,7 +121,7 @@ namespace Oyster { namespace Physics3D } /****************************************************************** - * Returns the local angular velocity of a mass in rotation. + * Returns the world angular velocity of a mass in rotation. * @todo TODO: improve doc ******************************************************************/ inline ::Oyster::Math::Float3 AngularVelocity( const ::Oyster::Math::Float4x4 &momentOfInertiaInversed, const ::Oyster::Math::Float3 &angularMomentum ) @@ -130,19 +130,19 @@ namespace Oyster { namespace Physics3D } /****************************************************************** - * Returns the local tangential velocity at localPos, of a mass in rotation. + * Returns the world tangential velocity at worldPos, of a mass in rotation. * @todo TODO: improve doc ******************************************************************/ - inline ::Oyster::Math::Float3 TangentialLinearVelocity( const ::Oyster::Math::Float3 &angularVelocity, const ::Oyster::Math::Float3 &offset ) + inline ::Oyster::Math::Float3 TangentialLinearVelocity( const ::Oyster::Math::Float3 &angularVelocity, const ::Oyster::Math::Float3 &worldOffset ) { return angularVelocity.Cross( offset ); } /****************************************************************** - * Returns the local tangential velocity at localPos, of a mass in rotation. + * Returns the world tangential velocity at worldPos, of a mass in rotation. * @todo TODO: improve doc ******************************************************************/ - inline ::Oyster::Math::Float3 TangentialLinearVelocity( const ::Oyster::Math::Float4x4 &momentOfInertiaInversed, const ::Oyster::Math::Float3 &angularMomentum, const ::Oyster::Math::Float3 &offset ) + inline ::Oyster::Math::Float3 TangentialLinearVelocity( const ::Oyster::Math::Float4x4 &momentOfInertiaInversed, const ::Oyster::Math::Float3 &angularMomentum, const ::Oyster::Math::Float3 &worldOffset ) { return TangentialLinearVelocity( AngularVelocity(momentOfInertiaInversed, angularMomentum), offset ); } @@ -169,7 +169,7 @@ namespace Oyster { namespace Physics3D * * @todo TODO: improve doc ******************************************************************/ - inline ::Oyster::Math::Float3 ImpulseTorque( const ::Oyster::Math::Float3 & impulseForce, const ::Oyster::Math::Float3 &offset ) + inline ::Oyster::Math::Float3 ImpulseTorque( const ::Oyster::Math::Float3 & impulseForce, const ::Oyster::Math::Float3 &worldOffset ) { return offset.Cross( impulseForce ); } diff --git a/Code/OysterPhysics3D/RigidBody.cpp b/Code/OysterPhysics3D/RigidBody.cpp index 5f36ce15..6ed9da25 100644 --- a/Code/OysterPhysics3D/RigidBody.cpp +++ b/Code/OysterPhysics3D/RigidBody.cpp @@ -131,16 +131,6 @@ void RigidBody::ApplyAngularImpulseAcceleration( const Float3 &worldA ) this->impulseTorqueSum += Formula::ImpulseTorque( this->momentOfInertiaTensor, worldA ); } -Float4x4 & RigidBody::AccessOrientation() -{ // by Dan Andersson - return this->box.orientation; -} - -const Float4x4 & RigidBody::AccessOrientation() const -{ // by Dan Andersson - return this->box.orientation; -} - Float3 & RigidBody::AccessBoundingReach() { // by Dan Andersson return this->box.boundingOffset; @@ -171,14 +161,14 @@ const Float & RigidBody::GetMass() const return this->mass; } -const Float4x4 & RigidBody::GetOrientation() const +const Float4x4 RigidBody::GetOrientation() const { // by Dan Andersson - return this->box.orientation; + return OrientationMatrix( this->box.rotation, this->box.center ); } Float4x4 RigidBody::GetView() const { // by Dan Andersson - return InverseOrientationMatrix( this->box.orientation ); + return InverseOrientationMatrix( this->GetOrientation() ); } const Float3 & RigidBody::GetBoundingReach() const @@ -249,18 +239,23 @@ Float3 RigidBody::GetTangentialLinearMomentumAt( const Float3 &worldPos ) const Float3 RigidBody::GetTangentialImpulseAccelerationAt( const Float3 &worldPos ) const { // by Dan Andersson - return this->GetTangentialImpulseAccelerationAt_Local( (this->GetView() * Float4(worldPos, 1.0f)).xyz ); // should not be any disform thus result.w = 1.0f + Float4x4 invWorldMomentOfInertia = TransformMatrix( this->box.rotation, this->momentOfInertiaTensor ).GetInverse(); + Float3 worldOffset = worldPos - this->box.center; + + return Formula::TangentialImpulseAcceleration( invWorldMomentOfInertia, this->impulseTorqueSum, worldOffset ); } Float3 RigidBody::GetTangentialLinearVelocityAt( const Float3 &worldPos ) const { // by Dan Andersson - return this->GetTangentialLinearVelocityAt_Local( (this->GetView() * Float4(worldPos, 1.0f)).xyz ); // should not be any disform thus result.w = 1.0f + Float4x4 invWorldMomentOfInertia = TransformMatrix( this->box.rotation, this->momentOfInertiaTensor ).GetInverse(); + Float3 worldOffset = worldPos - this->box.center; + + return Formula::TangentialLinearVelocity( invWorldMomentOfInertia, this->angularMomentum, worldOffset ); } Float3 RigidBody::GetImpulseForceAt( const Float3 &worldPos ) const { // by Dan Andersson - Float4 localForce = Float4( this->GetImpulseForceAt_Local((this->GetView() * Float4(worldPos, 1.0f)).xyz), 0.0f ); // should not be any disform thus result.w = 1.0f - return (this->box.orientation * localForce).xyz; // should not be any disform thus result.w = 0.0f + return Float3::null; //! @todo TODO: surface normal needed as well. Same goes for those below. } Float3 RigidBody::GetLinearMomentumAt( const Float3 &worldPos ) const From f51d2312dd34804a13533933c24c4a4fbb818eb9 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Thu, 21 Nov 2013 11:39:11 +0100 Subject: [PATCH 6/8] RigidBody DONE Needs only testing for confirmations --- Code/OysterMath/LinearMath.h | 4 + Code/OysterMath/OysterMath.cpp | 5 + Code/OysterMath/OysterMath.h | 3 + Code/OysterPhysics3D/OysterPhysics3D.h | 17 ++- Code/OysterPhysics3D/RigidBody.cpp | 141 +++++++++---------------- Code/OysterPhysics3D/RigidBody.h | 10 +- 6 files changed, 79 insertions(+), 101 deletions(-) diff --git a/Code/OysterMath/LinearMath.h b/Code/OysterMath/LinearMath.h index c09caf32..4c954cd6 100644 --- a/Code/OysterMath/LinearMath.h +++ b/Code/OysterMath/LinearMath.h @@ -455,6 +455,10 @@ namespace LinearAlgebra3D template inline ::LinearAlgebra::Vector3 VectorProjection( const ::LinearAlgebra::Vector3 &vector, const ::LinearAlgebra::Vector3 &axis ) { return axis * ( vector.Dot(axis) / axis.Dot(axis) ); } + + template + inline ::LinearAlgebra::Vector3 NormalProjection( const ::LinearAlgebra::Vector3 &vector, const ::LinearAlgebra::Vector3 &normalizedAxis ) + { return axis * ( vector.Dot(axis) ); } } #include "Utilities.h" diff --git a/Code/OysterMath/OysterMath.cpp b/Code/OysterMath/OysterMath.cpp index fb2486fc..ab318e92 100644 --- a/Code/OysterMath/OysterMath.cpp +++ b/Code/OysterMath/OysterMath.cpp @@ -160,4 +160,9 @@ namespace Oyster { namespace Math3D { return ::LinearAlgebra3D::VectorProjection( vector, axis ); } + + Float3 NormalProjection( const Float3 &vector, const Float3 &normalizedAxis ) + { + return ::LinearAlgebra3D::NormalProjection( vector, normalizedAxis ); + } } } \ No newline at end of file diff --git a/Code/OysterMath/OysterMath.h b/Code/OysterMath/OysterMath.h index fd116b04..4a1eeefe 100644 --- a/Code/OysterMath/OysterMath.h +++ b/Code/OysterMath/OysterMath.h @@ -244,6 +244,9 @@ namespace Oyster { namespace Math3D /// Oyster's native math library specialized /// returns the component vector of vector that is parallell with axis Float3 VectorProjection( const Float3 &vector, const Float3 &axis ); + /// returns the component vector of vector that is parallell with axis. Faster than VectorProjection. + Float3 NormalProjection( const Float3 &vector, const Float3 &normalizedAxis ); + /// Helper inline function that sets and then returns targetMem = projection * view inline Float4x4 & ViewProjectionMatrix( const Float4x4 &view, const Float4x4 &projection, Float4x4 &targetMem = Float4x4() ) { return targetMem = projection * view; } diff --git a/Code/OysterPhysics3D/OysterPhysics3D.h b/Code/OysterPhysics3D/OysterPhysics3D.h index 86e29307..2eb9db9a 100644 --- a/Code/OysterPhysics3D/OysterPhysics3D.h +++ b/Code/OysterPhysics3D/OysterPhysics3D.h @@ -108,7 +108,7 @@ namespace Oyster { namespace Physics3D ******************************************************************/ inline ::Oyster::Math::Float3 AngularImpulseAcceleration( const ::Oyster::Math::Float3 &linearImpulseAcceleration, const ::Oyster::Math::Float3 &worldOffset ) { - return offset.Cross( linearImpulseAcceleration ); + return worldOffset.Cross( linearImpulseAcceleration ); } /****************************************************************** @@ -129,6 +129,15 @@ namespace Oyster { namespace Physics3D return ( momentOfInertiaInversed * ::Oyster::Math::Float4( angularMomentum, 0.0f ) ).xyz; } + /****************************************************************** + * Returns the world angular velocity of a mass in rotation. + * @todo TODO: improve doc + ******************************************************************/ + inline ::Oyster::Math::Float3 AngularVelocity( const ::Oyster::Math::Float3 &linearVelocity, const ::Oyster::Math::Float3 &worldOffset ) + { + return worldOffset.Cross( linearVelocity ); + } + /****************************************************************** * Returns the world tangential velocity at worldPos, of a mass in rotation. * @todo TODO: improve doc @@ -183,6 +192,12 @@ namespace Oyster { namespace Physics3D return ( momentOfInertia * ::Oyster::Math::Float4(angularImpulseAcceleration, 0.0f) ).xyz; } + inline ::Oyster::Math::Float3 Impulse( ) + { + //! @todo TODO: implement calculation for impulse. Hijack Mattias Eriksson + return ::Oyster::Math::Float3::null; + } + namespace MomentOfInertia { /// Library of Formulas to calculate moment of inerta for simple shapes /** @todo TODO: add MomentOfInertia tensor formulas */ diff --git a/Code/OysterPhysics3D/RigidBody.cpp b/Code/OysterPhysics3D/RigidBody.cpp index 6ed9da25..c65b040f 100644 --- a/Code/OysterPhysics3D/RigidBody.cpp +++ b/Code/OysterPhysics3D/RigidBody.cpp @@ -40,22 +40,20 @@ void RigidBody::Update_LeapFrog( Float deltaTime ) { // by Dan Andersson: Euler leap frog update when Runga Kutta is not needed // Important! The member data is all world data except the Inertia tensor. Thus a new InertiaTensor needs to be created to be compatible with the rest of the world data. - Float4x4 wMomentOfInertiaTensor = TransformMatrix( this->box.rotation, this->momentOfInertiaTensor ); + Float4x4 wMomentOfInertiaTensor = TransformMatrix( this->box.rotation, this->momentOfInertiaTensor ); // RI // updating the linear - // dv = dt * a = dt * F / m - // ds = dt * avg_v - Float3 deltaLinearVelocity = this->impulseForceSum; - deltaLinearVelocity *= (deltaTime / this->mass); - Float3 deltaPos = deltaTime * ::Utility::Value::AverageWithDelta( Formula::LinearVelocity(this->mass, this->linearMomentum), deltaLinearVelocity ); + // dG = F * dt + // ds = dt * Formula::LinearVelocity( m, avg_G ) = dt * avg_G / m = (dt / m) * avg_G + Float3 linearImpulse = this->impulseForceSum * deltaTime; // aka deltaLinearMomentum + Float3 deltaPos = ( deltaTime / this->mass ) * ::Utility::Value::AverageWithDelta( this->linearMomentum, linearImpulse ); // updating the angular - // dw = dt * a = dt * ( I^-1 * T ) - // rotation = dt * avg_w - Float4x4 inversedMomentOfInertiaTensor = wMomentOfInertiaTensor.GetInverse(); - Float3 deltaAngularVelocity = Formula::AngularImpulseAcceleration( inversedMomentOfInertiaTensor, this->impulseTorqueSum ); // I^-1 * T - deltaAngularVelocity *= deltaTime; - Float3 rotationAxis = ::Utility::Value::AverageWithDelta( Formula::AngularVelocity(inversedMomentOfInertiaTensor,this->angularMomentum), deltaAngularVelocity ); + // dH = T * dt + // dO = dt * Formula::AngularVelocity( (RI)^-1, avg_H ) = dt * (RI)^-1 * avg_H + Float3 angularImpulse = this->impulseTorqueSum * deltaTime; // aka deltaAngularMomentum + Float3 rotationAxis = Formula::AngularVelocity( wMomentOfInertiaTensor.GetInverse(), + ::Utility::Value::AverageWithDelta(this->angularMomentum, angularImpulse) ); Float deltaRadian = rotationAxis.Dot( rotationAxis ); if( deltaRadian != 0.0f ) @@ -72,10 +70,10 @@ void RigidBody::Update_LeapFrog( Float deltaTime ) this->box.center += deltaPos; } - // update movements and clear impulses - this->linearMomentum += Formula::LinearMomentum( this->mass, deltaLinearVelocity ); + // update movements and clear impulseForceSum and impulseTorqueSum + this->linearMomentum += linearImpulse; this->impulseForceSum = Float3::null; - this->angularMomentum += Formula::AngularMomentum( wMomentOfInertiaTensor, deltaAngularVelocity ); + this->angularMomentum += angularImpulse; this->impulseTorqueSum = Float3::null; } @@ -226,67 +224,21 @@ Float3 RigidBody::GetLinearVelocity() const return Formula::LinearVelocity( this->mass, this->linearMomentum ); } -Float3 RigidBody::GetTangentialImpulseForceAt( const Float3 &worldPos ) const -{ // by Dan Andersson +void RigidBody::GetMomentumAt( const Float3 &worldPos, const Float3 &surfaceNormal, Float3 &normalMomentum, Float3 &tangentialMomentum ) const +{ Float3 worldOffset = worldPos - this->box.center; - return Formula::TangentialImpulseForce( this->impulseTorqueSum, worldOffset ); -} + Float3 momentum = Formula::TangentialLinearMomentum( this->angularMomentum, worldOffset ); + momentum += this->linearMomentum; -Float3 RigidBody::GetTangentialLinearMomentumAt( const Float3 &worldPos ) const -{ // by Dan Andersson - return Formula::TangentialLinearMomentum( this->angularMomentum, worldPos ); -} - -Float3 RigidBody::GetTangentialImpulseAccelerationAt( const Float3 &worldPos ) const -{ // by Dan Andersson - Float4x4 invWorldMomentOfInertia = TransformMatrix( this->box.rotation, this->momentOfInertiaTensor ).GetInverse(); - Float3 worldOffset = worldPos - this->box.center; - - return Formula::TangentialImpulseAcceleration( invWorldMomentOfInertia, this->impulseTorqueSum, worldOffset ); -} - -Float3 RigidBody::GetTangentialLinearVelocityAt( const Float3 &worldPos ) const -{ // by Dan Andersson - Float4x4 invWorldMomentOfInertia = TransformMatrix( this->box.rotation, this->momentOfInertiaTensor ).GetInverse(); - Float3 worldOffset = worldPos - this->box.center; - - return Formula::TangentialLinearVelocity( invWorldMomentOfInertia, this->angularMomentum, worldOffset ); -} - -Float3 RigidBody::GetImpulseForceAt( const Float3 &worldPos ) const -{ // by Dan Andersson - return Float3::null; //! @todo TODO: surface normal needed as well. Same goes for those below. -} - -Float3 RigidBody::GetLinearMomentumAt( const Float3 &worldPos ) const -{ // by Dan Andersson - // Reminder! Momentum is a world value. - Float4 localMomentum = Float4( this->GetLinearMomentumAt_Local((this->GetView() * Float4(worldPos, 1.0f)).xyz), 0.0f ); // should not be any disform thus result.w = 1.0f - return (this->box.orientation * localMomentum).xyz; // should not be any disform thus result.w = 0.0f - - // TODO: angularMomentum is a local value!! - return this->linearMomentum + Formula::TangentialLinearMomentum( this->angularMomentum, worldPos ); -} - -Float3 RigidBody::GetImpulseAccelerationAt( const Float3 &worldPos ) const -{ // by Dan Andersson - // Reminder! Acceleration is a world value. - return Formula::LinearImpulseAcceleration( this->mass, this->impulseForceSum ) - + Formula::TangentialImpulseAcceleration( this->momentOfInertiaTensor.GetInverse(), this->impulseTorqueSum, worldPos ); -} - -Float3 RigidBody::GetLinearVelocityAt( const Float3 &worldPos ) const -{ // by Dan Andersson - // Reminder! Velocity is a world value. - return Formula::LinearVelocity( this->mass, this->linearMomentum ) - + Formula::TangentialLinearVelocity( this->momentOfInertiaTensor.GetInverse(), this->angularMomentum, worldPos ); + normalMomentum = NormalProjection( momentum, surfaceNormal ); + tangentialMomentum = momentum - normalMomentum; } void RigidBody::SetMomentOfInertia( const Float4x4 &localI ) { // by Dan Andersson - if( i.GetDeterminant() != 0.0f ) // insanitycheck! momentOfInertiaTensor must be invertable + if( localI.GetDeterminant() != 0.0f ) // insanitycheck! momentOfInertiaTensor must be invertable { - this->momentOfInertiaTensor = i; + this->momentOfInertiaTensor = localI; } } @@ -310,7 +262,8 @@ void RigidBody::SetMass_KeepMomentum( const Float &m ) void RigidBody::SetOrientation( const Float4x4 &o ) { // by Dan Andersson - this->box.orientation = o; + ExtractRotationMatrix( o, this->box.rotation ); + this->box.center = o.v[3].xyz; } void RigidBody::SetSize( const Float3 &widthHeight ) @@ -325,64 +278,70 @@ void RigidBody::SetCenter( const Float3 &worldPos ) void RigidBody::SetImpulseTorque( const Float3 &worldT ) { // by Dan Andersson - this->impulseTorqueSum = t; + this->impulseTorqueSum = worldT; } void RigidBody::SetAngularMomentum( const Float3 &worldH ) { // by Dan Andersson - this->angularMomentum = h; + this->angularMomentum = worldH; } void RigidBody::SetAngularImpulseAcceleration( const Float3 &worldA ) { // by Dan Andersson - this->impulseTorqueSum = Formula::ImpulseTorque( this->momentOfInertiaTensor, a ); + this->impulseTorqueSum = Formula::ImpulseTorque( this->momentOfInertiaTensor, worldA ); } void RigidBody::SetAngularVelocity( const Float3 &worldW ) { // by Dan Andersson - this->angularMomentum = Formula::AngularMomentum( this->momentOfInertiaTensor, w ); + this->angularMomentum = Formula::AngularMomentum( this->momentOfInertiaTensor, worldW ); } void RigidBody::SetImpulseForce( const Float3 &worldF ) { // by Dan Andersson - this->impulseForceSum = f; + this->impulseForceSum = worldF; } void RigidBody::SetLinearMomentum( const Float3 &worldG ) { // by Dan Andersson - this->linearMomentum = g; + this->linearMomentum = worldG; } void RigidBody::SetLinearImpulseAcceleration( const Float3 &worldA ) { // by Dan Andersson - this->impulseForceSum = Formula::ImpulseForce( this->mass, a ); + this->impulseForceSum = Formula::ImpulseForce( this->mass, worldA ); } void RigidBody::SetLinearVelocity( const Float3 &worldV ) { // by Dan Andersson - this->linearMomentum = Formula::LinearMomentum( this->mass, v ); + this->linearMomentum = Formula::LinearMomentum( this->mass, worldV ); } -void RigidBody::SetImpulseForceAt( const Float3 &worldF, const Float3 &worldPos ) +void RigidBody::SetImpulseForceAt( const Float3 &worldForce, const Float3 &worldPos ) { // by Dan Andersson - // Reminder! Impulse force and torque is world values. - this->impulseForceSum = VectorProjection( worldForce, worldPos ); - this->impulseTorqueSum = Formula::ImpulseTorque( worldForce, worldPos ); + Float3 worldOffset = worldPos - this->box.center; + this->impulseForceSum = VectorProjection( worldForce, worldOffset ); + this->impulseTorqueSum = Formula::ImpulseTorque( worldForce, worldOffset ); } void RigidBody::SetLinearMomentumAt( const Float3 &worldG, const Float3 &worldPos ) { // by Dan Andersson - // Reminder! Linear and angular momentum is world values. - this->linearMomentum = VectorProjection( worldG, worldPos ); - this->angularMomentum = Formula::AngularMomentum( worldG, worldPos ); + Float3 worldOffset = worldPos - this->box.center; + this->linearMomentum = VectorProjection( worldG, worldOffset ); + this->angularMomentum = Formula::AngularMomentum( worldG, worldOffset ); } -void RigidBody::SetImpulseAccelerationAt( const Float3 &worldA, const Float3 &pos ) -{ // by - +void RigidBody::SetImpulseAccelerationAt( const Float3 &worldA, const Float3 &worldPos ) +{ // by Dan Andersson + Float3 worldOffset = worldPos - this->box.center; + this->impulseForceSum = Formula::ImpulseForce( this->mass, VectorProjection(worldA, worldOffset) ); + this->impulseTorqueSum = Formula::ImpulseTorque( this->box.rotation * this->momentOfInertiaTensor, + Formula::AngularImpulseAcceleration(worldA, worldOffset) ); } -void RigidBody::SetLinearVelocityAt( const Float3 &worldV, const Float3 &pos ) -{ // by - +void RigidBody::SetLinearVelocityAt( const Float3 &worldV, const Float3 &worldPos ) +{ // by Dan Andersson + Float3 worldOffset = worldPos - this->box.center; + this->linearMomentum = Formula::LinearMomentum( this->mass, VectorProjection(worldV, worldOffset) ); + this->angularMomentum = Formula::AngularMomentum( this->box.rotation * this->momentOfInertiaTensor, + Formula::AngularVelocity(worldV, worldOffset) ); } \ No newline at end of file diff --git a/Code/OysterPhysics3D/RigidBody.h b/Code/OysterPhysics3D/RigidBody.h index 4b7f65c0..19ce6bb2 100644 --- a/Code/OysterPhysics3D/RigidBody.h +++ b/Code/OysterPhysics3D/RigidBody.h @@ -64,15 +64,7 @@ namespace Oyster { namespace Physics3D ::Oyster::Math::Float3 GetLinearImpulseAcceleration() const; ::Oyster::Math::Float3 GetLinearVelocity() const; - ::Oyster::Math::Float3 GetTangentialImpulseForceAt( const ::Oyster::Math::Float3 &worldPos ) const; - ::Oyster::Math::Float3 GetTangentialLinearMomentumAt( const ::Oyster::Math::Float3 &worldPos ) const; - ::Oyster::Math::Float3 GetTangentialImpulseAccelerationAt( const ::Oyster::Math::Float3 &worldPos ) const; - ::Oyster::Math::Float3 GetTangentialLinearVelocityAt( const ::Oyster::Math::Float3 &worldPos ) const; - - ::Oyster::Math::Float3 GetImpulseForceAt( const ::Oyster::Math::Float3 &worldPos ) const; - ::Oyster::Math::Float3 GetLinearMomentumAt( const ::Oyster::Math::Float3 &worldPos ) const; - ::Oyster::Math::Float3 GetImpulseAccelerationAt( const ::Oyster::Math::Float3 &worldPos ) const; - ::Oyster::Math::Float3 GetLinearVelocityAt( const ::Oyster::Math::Float3 &worldPos ) const; + void GetMomentumAt( const ::Oyster::Math::Float3 &worldPos, const ::Oyster::Math::Float3 &surfaceNormal, ::Oyster::Math::Float3 &normalMomentum, ::Oyster::Math::Float3 &tangentialMomentum ) const; // SET METHODS //////////////////////////////// From 938826b84e9ae02b4667a0ab31225927566ea8dc Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Thu, 21 Nov 2013 11:47:20 +0100 Subject: [PATCH 7/8] Forgot to test compile RigidBody Compile Errors is fixed --- Code/OysterPhysics3D/OysterPhysics3D.h | 8 ++++---- Code/OysterPhysics3D/RigidBody.cpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Code/OysterPhysics3D/OysterPhysics3D.h b/Code/OysterPhysics3D/OysterPhysics3D.h index 2eb9db9a..ca9948a8 100644 --- a/Code/OysterPhysics3D/OysterPhysics3D.h +++ b/Code/OysterPhysics3D/OysterPhysics3D.h @@ -63,7 +63,7 @@ namespace Oyster { namespace Physics3D ******************************************************************/ inline ::Oyster::Math::Float3 AngularMomentum( const ::Oyster::Math::Float3 linearMomentum, const ::Oyster::Math::Float3 &worldOffset ) { - return offset.Cross( linearMomentum ); + return worldOffset.Cross( linearMomentum ); } /****************************************************************** @@ -144,7 +144,7 @@ namespace Oyster { namespace Physics3D ******************************************************************/ inline ::Oyster::Math::Float3 TangentialLinearVelocity( const ::Oyster::Math::Float3 &angularVelocity, const ::Oyster::Math::Float3 &worldOffset ) { - return angularVelocity.Cross( offset ); + return angularVelocity.Cross( worldOffset ); } /****************************************************************** @@ -153,7 +153,7 @@ namespace Oyster { namespace Physics3D ******************************************************************/ inline ::Oyster::Math::Float3 TangentialLinearVelocity( const ::Oyster::Math::Float4x4 &momentOfInertiaInversed, const ::Oyster::Math::Float3 &angularMomentum, const ::Oyster::Math::Float3 &worldOffset ) { - return TangentialLinearVelocity( AngularVelocity(momentOfInertiaInversed, angularMomentum), offset ); + return TangentialLinearVelocity( AngularVelocity(momentOfInertiaInversed, angularMomentum), worldOffset ); } /****************************************************************** @@ -180,7 +180,7 @@ namespace Oyster { namespace Physics3D ******************************************************************/ inline ::Oyster::Math::Float3 ImpulseTorque( const ::Oyster::Math::Float3 & impulseForce, const ::Oyster::Math::Float3 &worldOffset ) { - return offset.Cross( impulseForce ); + return worldOffset.Cross( impulseForce ); } /****************************************************************** diff --git a/Code/OysterPhysics3D/RigidBody.cpp b/Code/OysterPhysics3D/RigidBody.cpp index c65b040f..fb663cf4 100644 --- a/Code/OysterPhysics3D/RigidBody.cpp +++ b/Code/OysterPhysics3D/RigidBody.cpp @@ -273,7 +273,7 @@ void RigidBody::SetSize( const Float3 &widthHeight ) void RigidBody::SetCenter( const Float3 &worldPos ) { // by Dan Andersson - this->box.center = p; + this->box.center = worldPos; } void RigidBody::SetImpulseTorque( const Float3 &worldT ) From dc9f4597e24c0b5344ce6e467d53feb9c3aef326 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Thu, 21 Nov 2013 15:19:32 +0100 Subject: [PATCH 8/8] Added OrientationMatrix build methods + other stuff --- Code/GamePhysics/GamePhysics.vcxproj.filters | 2 +- Code/GamePhysics/PhysicsAPI.h | 26 +++++++++++++++++--- Code/OysterMath/LinearMath.h | 19 +++++++++++++- Code/OysterMath/OysterMath.cpp | 10 ++++++++ Code/OysterMath/OysterMath.h | 6 +++++ Code/OysterPhysics3D/RigidBody.cpp | 2 +- 6 files changed, 58 insertions(+), 7 deletions(-) diff --git a/Code/GamePhysics/GamePhysics.vcxproj.filters b/Code/GamePhysics/GamePhysics.vcxproj.filters index 8c1b9f8b..07661556 100644 --- a/Code/GamePhysics/GamePhysics.vcxproj.filters +++ b/Code/GamePhysics/GamePhysics.vcxproj.filters @@ -30,7 +30,7 @@ - Header Files\Implementation + Source Files \ No newline at end of file diff --git a/Code/GamePhysics/PhysicsAPI.h b/Code/GamePhysics/PhysicsAPI.h index 5711b277..d9ae0a59 100644 --- a/Code/GamePhysics/PhysicsAPI.h +++ b/Code/GamePhysics/PhysicsAPI.h @@ -1,6 +1,7 @@ #ifndef PHYSICS_API_H #define PHYSICS_API_H +#include "OysterCollision3D.h" #include "OysterMath.h" #include "Utilities.h" @@ -12,6 +13,12 @@ namespace Oyster class IRigidBody; class IParticle; + enum UpdateState + { + resting, + altered + }; + namespace Constant { const float gravity_constant = (const float)6.67284e-11; // The _big_G_! ( N(m/kg)^2 ) Used in real gravityforcefields. @@ -32,17 +39,28 @@ namespace Oyster virtual void Update() = 0; - virtual void MoveToLimbo( unsigned int objRef ); - virtual void ReleaseFromLimbo( unsigned int objRef ); + virtual bool IsInLimbo( unsigned int objRef ) = 0; + virtual void MoveToLimbo( unsigned int objRef ) = 0; + virtual void ReleaseFromLimbo( unsigned int objRef ) = 0; - virtual unsigned int AddObject( ::Utility::DynamicMemory::UniquePointer handle ); - virtual void DestroyObject( unsigned int objRef ); + virtual unsigned int AddObject( ::Utility::DynamicMemory::UniquePointer handle ) = 0; + virtual ::Utility::DynamicMemory::UniquePointer ExtractObject( unsigned int objRef ) = 0; + virtual void DestroyObject( unsigned int objRef ) = 0; }; class IRigidBody { public: + virtual ~IRigidBody() {}; + virtual UpdateState Update( ::Oyster::Math::Float timeStepLength ) = 0; + + virtual bool IsSubscribingCollisions() const = 0; + virtual bool IsIntersecting( const IRigidBody &object, ::Oyster::Math::Float &deltaWhen, ::Oyster::Math::Float3 &worldPointOfContact ) = 0; + + virtual unsigned int GetReference() const = 0; + virtual ::Oyster::Collision3D::Sphere & GetBoundingSphere( ::Oyster::Collision3D::Sphere &targetMem = ::Oyster::Collision3D::Sphere() ) const = 0; + virtual ::Oyster::Math::Float3 & GetNormalAt( const ::Oyster::Math::Float3 &worldPos, ::Oyster::Math::Float3 &targetMem = ::Oyster::Math::Float3() ) const = 0; }; class IParticle diff --git a/Code/OysterMath/LinearMath.h b/Code/OysterMath/LinearMath.h index 4c954cd6..ba427bb6 100644 --- a/Code/OysterMath/LinearMath.h +++ b/Code/OysterMath/LinearMath.h @@ -386,6 +386,23 @@ namespace LinearAlgebra3D return targetMem; } + template + inline ::LinearAlgebra::Matrix4x4 OrientationMatrix_LookAtDirection( const ::LinearAlgebra::Vector3 &normalizedDirection, const ::LinearAlgebra::Vector3 &normalizedUpVector, const ::LinearAlgebra::Vector3 &worldPos ) + { // Righthanded system! Forward is considered to be along negative z axis. Up is considered along positive y axis. + ::LinearAlgebra::Vector3 right = normalizedDirection.Cross( normalizedUpVector ).GetNormalized(); + return ::LinearAlgebra::Matrix4x4( ::LinearAlgebra::Vector4( right, 0.0f ), + ::LinearAlgebra::Vector4( right.Cross( normalizedDirection ), 0.0f ), + ::LinearAlgebra::Vector4( -normalizedDirection, 0.0f ), + ::LinearAlgebra::Vector4( worldPos, 1.0f ) ); + } + + template + inline ::LinearAlgebra::Matrix4x4 OrientationMatrix_LookAtPos( const ::LinearAlgebra::Vector3 &worldLookAt, const ::LinearAlgebra::Vector3 &normalizedUpVector, const ::LinearAlgebra::Vector3 &worldPos ) + { // Righthanded system! Forward is considered to be along negative z axis. Up is considered along positive y axis. + ::LinearAlgebra::Vector3 direction = ( worldLookAt - worldPos ).GetNormalized(); + return OrientationMatrix_LookAtDirection( direction, normalizedUpVector, worldPos ); + } + template inline ::LinearAlgebra::Matrix4x4 & InverseOrientationMatrix( const ::LinearAlgebra::Matrix4x4 &orientationMatrix, ::LinearAlgebra::Matrix4x4 &targetMem = ::LinearAlgebra::Matrix4x4() ) { @@ -458,7 +475,7 @@ namespace LinearAlgebra3D template inline ::LinearAlgebra::Vector3 NormalProjection( const ::LinearAlgebra::Vector3 &vector, const ::LinearAlgebra::Vector3 &normalizedAxis ) - { return axis * ( vector.Dot(axis) ); } + { return normalizedAxis * ( vector.Dot(normalizedAxis) ); } } #include "Utilities.h" diff --git a/Code/OysterMath/OysterMath.cpp b/Code/OysterMath/OysterMath.cpp index ab318e92..35df5975 100644 --- a/Code/OysterMath/OysterMath.cpp +++ b/Code/OysterMath/OysterMath.cpp @@ -131,6 +131,16 @@ namespace Oyster { namespace Math3D return ::LinearAlgebra3D::OrientationMatrix( sumDeltaAngularAxis, sumTranslation, centerOfMass, targetMem ); } + Float4x4 & OrientationMatrix_LookAtDirection( const Float3 &normalizedDirection, const Float3 &normalizedUpVector, const Float3 &worldPos, Float4x4 &targetMem ) + { + return targetMem = ::LinearAlgebra3D::OrientationMatrix_LookAtDirection( normalizedDirection, normalizedUpVector, worldPos ); + } + + Float4x4 & OrientationMatrix_LookAtPos( const Float3 &worldLookAt, const Float3 &normalizedUpVector, const Float3 &worldPos, Float4x4 &targetMem ) + { + return targetMem = ::LinearAlgebra3D::OrientationMatrix_LookAtPos( worldLookAt, normalizedUpVector, worldPos ); + } + Float4x4 & InverseOrientationMatrix( const Float4x4 &orientationMatrix, Float4x4 &targetMem ) { return ::LinearAlgebra3D::InverseOrientationMatrix( orientationMatrix, targetMem ); diff --git a/Code/OysterMath/OysterMath.h b/Code/OysterMath/OysterMath.h index 4a1eeefe..125fd29d 100644 --- a/Code/OysterMath/OysterMath.h +++ b/Code/OysterMath/OysterMath.h @@ -207,6 +207,12 @@ namespace Oyster { namespace Math3D /// Oyster's native math library specialized *******************************************************************/ Float4x4 & OrientationMatrix( const Float3 &sumDeltaAngularAxis, const Float3 &sumTranslation, const Float3 ¢erOfMass, Float4x4 &targetMem = Float4x4() ); + //! @todo TODO: Add documentation and not tested + Float4x4 & OrientationMatrix_LookAtDirection( const Float3 &normalizedDirection, const Float3 &normalizedUpVector, const Float3 &worldPos, Float4x4 &targetMem = Float4x4() ); + + //! @todo TODO: Add documentation and not tested + Float4x4 & OrientationMatrix_LookAtPos( const Float3 &worldLookAt, const Float3 &normalizedUpVector, const Float3 &worldPos, Float4x4 &targetMem = Float4x4() ); + /// If orientationMatrix is assumed to be by all definitions a rigid orientation matrix aka rigid body matrix. Then this is a much faster inverse method. Float4x4 & InverseOrientationMatrix( const Float4x4 &orientationMatrix, Float4x4 &targetMem = Float4x4() ); diff --git a/Code/OysterPhysics3D/RigidBody.cpp b/Code/OysterPhysics3D/RigidBody.cpp index fb663cf4..214a1aee 100644 --- a/Code/OysterPhysics3D/RigidBody.cpp +++ b/Code/OysterPhysics3D/RigidBody.cpp @@ -70,7 +70,7 @@ void RigidBody::Update_LeapFrog( Float deltaTime ) this->box.center += deltaPos; } - // update movements and clear impulseForceSum and impulseTorqueSum + // update momentums and clear impulseForceSum and impulseTorqueSum this->linearMomentum += linearImpulse; this->impulseForceSum = Float3::null; this->angularMomentum += angularImpulse;