41 #if !XBOX && !WINDOWS_PHONE 44 internal sealed
class FrictionLogic : PhysicsLogic
46 private TopDownPhysicsGame parent;
48 public FrictionLogic( TopDownPhysicsGame parent, Lifespan lifetime )
54 private bool Overlap( Body b, Body surface )
56 if ( !b.Shape.CanGetIntersection || !surface.Shape.CanGetIntersection )
59 Vector2D offset = b.State.Position.Linear - surface.State.Position.Linear;
61 var rect = surface.Rectangle;
62 double surfaceWidth = Math.Abs( rect.Max.X - rect.Min.X );
63 double surfaceHeight = Math.Abs( rect.Max.Y - rect.Min.Y );
64 double targetRadius = Math.Sqrt( Math.Pow( surfaceWidth, 2 ) + Math.Pow( surfaceHeight, 2 ) );
66 for (
int i = 0; i < b.Shape.Vertexes.Length; i++ )
68 Vector2D vec = b.Shape.Vertexes[i] + offset + Vector2D.FromLengthAndAngle( 10, b.Shape.Vertexes[i].Angle );
70 if ( surface.Shape.TryGetIntersection( vec, out ii ) )
81 protected internal override void RunLogic( TimeStep step )
83 double surfaceFriction = parent.KineticFriction;
84 double gravityNormal = parent.Gravity;
86 foreach ( Body e
in Bodies )
88 if ( e.IgnoresPhysicsLogics )
continue;
89 if ( !( e.Tag is PhysicsObject ) )
continue;
91 double frictionCoefficient = 0;
92 if ( Game.Instance is TopDownPhysicsGame )
93 frictionCoefficient = ( (TopDownPhysicsGame)Game.Instance ).KineticFriction;
95 foreach ( var surface
in parent.Surfaces )
97 if ( surface.Body == e )
100 if ( Overlap( e, surface.Body ) )
103 frictionCoefficient = Math.Sqrt( surface.KineticFriction * e.Coefficients.DynamicFriction );
112 frictionCoefficient = Math.Sqrt( surfaceFriction * e.Coefficients.DynamicFriction );
116 float friction = (float)( frictionCoefficient * gravityNormal * e.Mass.MassInv * step.Dt );
117 if ( e.State.Velocity.Linear.Magnitude <= friction )
118 e.State.Velocity.Linear = Vector2D.Zero;
120 e.State.Velocity.Linear.Magnitude -= friction;