From 59d5a3dc1ebea1aa8bc6b78eef06d1d17d9fd30e Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Thu, 19 Dec 2013 10:15:32 +0100 Subject: [PATCH] Created function for friction --- .../Implementation/PhysicsAPI_Impl.cpp | 31 +++---------------- Code/GamePhysics/PhysicsFormula-Impl.h | 28 +++++++++++++++++ Code/GamePhysics/PhysicsFormula.h | 7 +++++ 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index 9ede391d..9d30a7e4 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -7,6 +7,7 @@ using namespace ::Oyster::Physics; using namespace ::Oyster::Math; using namespace ::Oyster::Collision3D; using namespace ::Utility::DynamicMemory; +using namespace ::Utility::Value; API_Impl API_instance; @@ -43,32 +44,10 @@ namespace protoState.GetMass(), protoG_Magnitude );; Float4 sumJ = normal*impulse; - // FRICTION - // Relative momentum after normal impulse - Float4 relativeMomentum = deuterState.GetLinearMomentum() - protoState.GetLinearMomentum(); - - Float4 tanFriction = relativeMomentum - relativeMomentum.Dot(normal)*normal; - tanFriction.Normalize(); - - Float magnitudeFriction = -relativeMomentum.Dot(tanFriction); - magnitudeFriction = magnitudeFriction/( 1/protoState.GetMass() + 1/deuterState.GetMass() ); - - float mu = 0.5f; - - Float4 frictionImpulse; - if( abs(magnitudeFriction) < impulse*mu ) - { - frictionImpulse = magnitudeFriction*tanFriction; - } - else - { - Float dynamicFriction = 0.5f; - frictionImpulse = -impulse*tanFriction*dynamicFriction; - } - - // Apply - sumJ -= ( 1 / protoState.GetMass() )*frictionImpulse; - // FRICTION END + sumJ -= Formula::CollisionResponse::Friction( impulse, normal, protoState.GetLinearMomentum(), + protoState.GetFrictionCoeff(), 0.2f, protoState.GetMass(), + deuterState.GetLinearMomentum(), deuterState.GetFrictionCoeff(), + 0.2f, deuterState.GetMass()); // calc from perspective of proto proto->GetNormalAt( worldPointOfContact, normal ); diff --git a/Code/GamePhysics/PhysicsFormula-Impl.h b/Code/GamePhysics/PhysicsFormula-Impl.h index c0d4ed64..4cb7b5ef 100644 --- a/Code/GamePhysics/PhysicsFormula-Impl.h +++ b/Code/GamePhysics/PhysicsFormula-Impl.h @@ -40,6 +40,34 @@ namespace Oyster { namespace Physics { namespace Formula { return (e + 1) * (mB*gA - mA*gB) / (mA + mB); } + + inline ::Oyster::Math::Float4 Friction( ::Oyster::Math::Float i, ::Oyster::Math::Float4 iN, ::Oyster::Math::Float4 momA, ::Oyster::Math::Float sFA, ::Oyster::Math::Float dFA, ::Oyster::Math::Float mA, ::Oyster::Math::Float4 momB, ::Oyster::Math::Float sFB, ::Oyster::Math::Float dFB, ::Oyster::Math::Float mB ) + { + // FRICTION + // Relative momentum after normal impulse + ::Oyster::Math::Float4 relativeMomentum = momB - momA; + + ::Oyster::Math::Float4 tanFriction = relativeMomentum - relativeMomentum.Dot( iN )*iN; + tanFriction.Normalize(); + + ::Oyster::Math::Float magnitudeFriction = -relativeMomentum.Dot( tanFriction ); + magnitudeFriction = magnitudeFriction*mA*mB/( mA + mB ); + + ::Oyster::Math::Float mu = 0.5f*( sFA + sFB ); + + ::Oyster::Math::Float4 frictionImpulse; + if( abs(magnitudeFriction) < i*mu ) + { + frictionImpulse = magnitudeFriction*tanFriction; + } + else + { + ::Oyster::Math::Float dynamicFriction = 0.5f*( dFA + dFB ); + frictionImpulse = -i*tanFriction*dynamicFriction; + } + + return ( 1 / mA )*frictionImpulse; + } } } } } diff --git a/Code/GamePhysics/PhysicsFormula.h b/Code/GamePhysics/PhysicsFormula.h index c15d8078..a8fea658 100644 --- a/Code/GamePhysics/PhysicsFormula.h +++ b/Code/GamePhysics/PhysicsFormula.h @@ -20,6 +20,13 @@ namespace Oyster { namespace Physics { namespace Formula ::Oyster::Math::Float Impulse( ::Oyster::Math::Float coeffOfRestitution, ::Oyster::Math::Float massA, ::Oyster::Math::Float momentumA, ::Oyster::Math::Float massB, ::Oyster::Math::Float momentumB ); + + ::Oyster::Math::Float4 Friction( ::Oyster::Math::Float impulse, ::Oyster::Math::Float4 impulseNormal, + ::Oyster::Math::Float4 momentumA, ::Oyster::Math::Float staticFrictionA, + ::Oyster::Math::Float dynamicFrictionA, ::Oyster::Math::Float massA, + ::Oyster::Math::Float4 momentumB, ::Oyster::Math::Float staticFrictionB, + ::Oyster::Math::Float dynamicFrictionB, ::Oyster::Math::Float massB ); + } } } }