Esempio n. 1
0
        private float CheckCollision(CircleRigidBody circle1, CircleRigidBody circle2)
        {
            if (circle1 == circle2)
            {
                return(0);
            }

            Vector2 relativePosition = circle1.LocalPosition - circle2.LocalPosition;
            float   distance         = relativePosition.magnitude;
            Vector2 direction        = relativePosition / distance;
            float   minDistance      = circle1.Radius + circle2.Radius + COLLISION_MARGIN;

            if (distance < minDistance)
            {
                float overlap             = minDistance - distance;
                float circle1Contribution = circle2.Mass / (circle1.Mass + circle2.Mass);
                float circle2Contribution = 1 - circle1Contribution;
                circle1.Push(direction * circle1Contribution * overlap);
                circle2.Push(-direction * circle2Contribution * overlap);
                ToRectify.Add(circle1);
                ToRectify.Add(circle2);
                return(overlap);
            }
            else
            {
                return(0);
            }
        }
Esempio n. 2
0
        private float CheckBoundry(CircleRigidBody circle)
        {
            float distance    = Owner.Radius - circle.LocalPosition.magnitude;
            float minDistance = circle.Radius + COLLISION_MARGIN;

            if (distance < minDistance)
            {
                float overlap      = minDistance - distance;
                float pushDistance = overlap;
                circle.Push(-circle.LocalPosition.normalized * pushDistance);
                ToRectify.Add(circle);
                return(Mathf.Abs(overlap));
            }
            else
            {
                return(0);
            }
        }