Exemplo n.º 1
0
        /// <summary>
        /// Run dynamic intersection test against all colliders. Return the earliest time of impact, or <c>1.0f</c> if no collisions occured.
        /// </summary>
        /// <remarks>This function will finish immediately if the earliest time of impact reaches 0.</remarks>
        private static void CalculateActorEarliestTimeOfImpact(ActorRigidbody actor)
        {
            float time = 1f;

            foreach (var staticbody in AllStatics)
            {
                if (time == 0)
                {
                    break;
                }

                if (PhysicsMath.IntersectMoving(staticbody.MainCollider, actor.MainCollider, Vector2.Zero, actor.ProcessingData.CalcVel, out float first))
                {
                    time = Math.Min(time, first);
                }
            }

            foreach (var solid in AllSolids)
            {
                foreach (var solidCollider in solid.Colliders)
                {
                    if (time == 0)
                    {
                        break;
                    }

                    if (PhysicsMath.IntersectMoving(solidCollider, actor.MainCollider, Vector2.Zero, actor.ProcessingData.CalcVel, out float first))
                    {
                        time = Math.Min(time, first);
                    }
                }
            }

            actor.ProcessingData.TimeOfImpact = time;
        }
Exemplo n.º 2
0
        private static void CalculateSolidEarliestTimeOfImpact(SolidRigidbody solid)
        {
            Console.WriteLine("running on " + solid.Entity);

            float earliest = 1f;

            // Regional check against all statics
            foreach (var staticbody in AllStatics)
            {
                foreach (var collider in solid.Colliders)
                {
                    if (earliest == 0f)
                    {
                        break;
                    }

                    if (PhysicsMath.IntersectMoving(collider, staticbody.MainCollider, solid.ProcessingData.CalcVel, Vector2.Zero, out float first))
                    {
                        Console.WriteLine($"{solid.Entity} ({collider.LocalPosition}) hit {staticbody.Entity} at time {first}");
                        earliest = Math.Min(earliest, first);
                    }
                }
            }

            // Regional check against all solids
            foreach (var otherSolid in AllSolids)
            {
                if (earliest == 0f)
                {
                    break;
                }

                // Skip self
                if (ReferenceEquals(solid, otherSolid))
                {
                    continue;
                }

                if (PhysicsMath.IntersectMoving(solid.MainCollider, otherSolid.MainCollider, solid.ProcessingData.CalcVel, otherSolid.ProcessingData.CalcVel, out float first))
                {
                    Console.WriteLine($"{solid.Entity} hit {otherSolid.Entity} at time {first}");
                    earliest = Math.Min(earliest, first);
                }
            }

            solid.ProcessingData.TimeOfImpact = earliest;
        }