public static void Update(Contexts contexts) { var dt = contexts.Configuration().Get <TimeStep>().Value; var g = contexts.Configuration().Get <GlobalForce>().Value; var context = contexts.Get <Game>(); foreach (var entity in context.AllWith <Force, Mass, Velocity>()) { if (entity.Is <Destroyed>()) { continue; } var force = entity.Get <Force>().Value + g; var mass = entity.Get <Mass>().Value; var velocity = entity.Get <Velocity>().Value; var damping = entity.Has <Damping>() ? entity.Get <Damping>().Value : 0; velocity += dt * force / mass; velocity *= Fix.One / (Fix.One + dt * damping); if (entity.Has <MaxVelocity>()) { var maxVelocity = entity.Get <MaxVelocity>(); velocity = FixVec2.ClampMagnitude(velocity, maxVelocity.Value); } entity.Replace(new Velocity(velocity)); entity.Replace(new Force()); } }
private static SteeringVelocity Arrive(Entity <Game> owner, FixVec2 target) { var tolerance = owner.Get <ArrivalTolerance>(); var position = owner.Get <Position>().Value; target.Sub(ref position); var distance = target.Magnitude; if (distance <= tolerance.Distance) { return(SteeringVelocity.Zero); } var maxVelocity = owner.Get <MaxVelocity>().Value; var decelerationDistance = tolerance.DecelerationDistance; if (distance <= decelerationDistance) { maxVelocity *= distance / decelerationDistance; } var velocity = target / distance * maxVelocity; return(new SteeringVelocity(FixVec2.ClampMagnitude(velocity, maxVelocity))); }
public void Limit(Fix maxAcceleration, Fix maxAngularAcceleration) { FixVec2.ClampMagnitude(ref Linear, maxAcceleration); if (Angular > maxAngularAcceleration) { Angular = maxAngularAcceleration; } }