From 56dec6cf5df6919a31148789d1bfd71bbaf1a353 Mon Sep 17 00:00:00 2001 From: Robin Engman Date: Thu, 19 Dec 2013 08:34:16 +0100 Subject: [PATCH] Added "alpha" friction Alpha means that the friction is not tested and most likely needs tweaking. --- .../Implementation/PhysicsAPI_Impl.cpp | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp index b439ce03..9ede391d 100644 --- a/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp +++ b/Code/GamePhysics/Implementation/PhysicsAPI_Impl.cpp @@ -38,12 +38,37 @@ namespace deuterG_Magnitude = deuterG.Dot( normal ); // bounce - Float4 sumJ = normal * Formula::CollisionResponse::Impulse( deuterState.GetRestitutionCoeff(), + Float impulse = Formula::CollisionResponse::Impulse( deuterState.GetRestitutionCoeff(), deuterState.GetMass(), deuterG_Magnitude, - protoState.GetMass(), protoG_Magnitude ); + protoState.GetMass(), protoG_Magnitude );; + Float4 sumJ = normal*impulse; + + // FRICTION + // Relative momentum after normal impulse + Float4 relativeMomentum = deuterState.GetLinearMomentum() - protoState.GetLinearMomentum(); - // @todo TODO: friction - // sumJ -= ; + 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 // calc from perspective of proto proto->GetNormalAt( worldPointOfContact, normal ); @@ -54,8 +79,10 @@ namespace sumJ += normal * Formula::CollisionResponse::Impulse( protoState.GetRestitutionCoeff(), protoState.GetMass(), protoG_Magnitude, deuterState.GetMass(), deuterG_Magnitude ); - // @todo TODO: friction - // sumJ += ; + // FRICTION + // Apply + //sumJ += ( 1 / deuterState.GetMass() )*frictionImpulse; + // FRICTION END protoState.ApplyImpulse( sumJ, worldPointOfContact, normal ); proto->SetState( protoState );