Пример #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);
            }
        }
Пример #2
0
 private bool Contains(CircleRigidBody circle)
 {
     return(circle.Top < Top &&
            circle.Bottom > Bottom &&
            circle.Left > Left &&
            circle.Right < Right);
 }
Пример #3
0
 /// <summary>
 /// Check if a container is still within the same quad and if not move it
 /// to the appropriate quad.
 /// </summary>
 /// <param name="circle"></param>
 private void Rectify(CircleRigidBody circle)
 {
     if (!Contains(circle) && Parent != null)
     {
         Parent.Rectify(circle);
     }
     else
     {
         circle.MyQuad.Remove(circle);
         Insert(circle);
     }
 }
Пример #4
0
        public Quadtree(CircleRigidBody owner, float top, float bottom, float left, float right, Quadtree parent = null, int maximumContents = 5)
        {
            MaximumContents = maximumContents;
            Contents        = new List <CircleRigidBody>();
            ToRectify       = new List <CircleRigidBody>();

            Owner  = owner;
            Top    = top;
            Bottom = bottom;
            Left   = left;
            Right  = right;

            NearBoundry = FarthestCorner() >= Owner.Radius;
        }
Пример #5
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);
            }
        }
Пример #6
0
 public void Insert(CircleRigidBody circle)
 {
     if (QuadOne == null)
     {
         if (Contents.Count >= MaximumContents)
         {
             Split();
             Insert(circle);
         }
         else
         {
             Contents.Add(circle);
             circle.MyQuad = this;
         }
     }
     else if (QuadOne.Contains(circle))
     {
         QuadOne.Insert(circle);
     }
     else if (QuadTwo.Contains(circle))
     {
         QuadTwo.Insert(circle);
     }
     else if (QuadThree.Contains(circle))
     {
         QuadThree.Insert(circle);
     }
     else if (QuadFour.Contains(circle))
     {
         QuadFour.Insert(circle);
     }
     else
     {
         Contents.Add(circle);
         circle.MyQuad = this;
     }
 }
Пример #7
0
 private void Remove(CircleRigidBody circle)
 {
     Contents.Remove(circle);
 }