Esempio n. 1
0
        public void ApplyPhysics(TimeSpan delta)
        {
            foreach (var C1 in Components)
            {
                if (!C1.HasPhysics)
                {
                    continue;
                }
                C1.Start();
                //C1.ApplyForce(new Vector2(0,9f));


                foreach (var C2 in Components)
                {
                    if (C1 == C2)
                    {
                        continue;
                    }

                    var force = C2.Component.BoundingRectangle.Center -
                                C1.Component.BoundingRectangle.Center;
                    var d = force.LengthSquared();
                    force = Vector2.Normalize(force);
                    var strength = 0.5f * C1.Mass * C2.Mass / d;
                    force *= strength;
                    C1.ApplyForce(force);
                }

                C1.ApplyFriction();
                C1.ApplyAcceleration(delta);

                var distance     = C1.ApplyVelocity(delta);
                var newPrimitive = C1.Component.BoundingRectangle.Translate(distance);
                if (Components.Any(c =>
                                   C1 != c && c.HasCollisions && c.Component.BoundingRectangle.CollidesWith(newPrimitive)))
                {
                    C1.Velocity *= -0.9f;
                }

                distance = C1.ApplyVelocity(delta);
                C1.Component.Translate(distance);
            }
        }