예제 #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;
        }
예제 #2
0
        /// <summary>
        /// Collides the given actor against all other rigidbodies and calculate the allowed velocity during this frame.
        /// </summary>
        /// <remarks>This function will immediately return if the calculated velocity becomes zero.</remarks>
        private static void CalculateActorVelocity(ActorRigidbody actor)
        {
            actor.ProcessingData.CalcVel = actor.Velocity;

            if (actor.Velocity == Vector2.Zero)
            {
                return;
            }


            foreach (var staticbody in AllStatics)
            {
                if (actor.ProcessingData.CalcVel == Vector2.Zero)
                {
                    return;
                }

                if (PhysicsMath.IsOverlapping(actor.MainCollider, staticbody.MainCollider))
                {
                    if (!PhysicsMath.IsSlidingCorner(actor.MainCollider, staticbody.MainCollider))
                    {
                        actor.ProcessingData.CalcVel = VelocityMath.IntoPlane(actor.ProcessingData.CalcVel, PhysicsMath.GetNormal(actor.MainCollider, staticbody.MainCollider));
                    }
                }
            }

            foreach (var solid in AllSolids)
            {
                foreach (var solidCollider in solid.Colliders)
                {
                    if (actor.ProcessingData.CalcVel == Vector2.Zero)
                    {
                        return;
                    }

                    if (PhysicsMath.IsOverlapping(actor.MainCollider, solidCollider))
                    {
                        if (!PhysicsMath.IsSlidingCorner(actor.MainCollider, solidCollider))
                        {
                            actor.ProcessingData.CalcVel = VelocityMath.IntoPlane(actor.ProcessingData.CalcVel, PhysicsMath.GetNormal(actor.MainCollider, solidCollider));
                        }
                    }
                }
            }
        }