예제 #1
0
        protected bool CheckCircleToBoxCollision(CollisionCircle2D self, CollisionBox2D other)
        {
            // clamp(value, min, max) - limits value to the range min..max
            var boxSides = other.BoxSides;

            if (other.BoxSides.NeedUpdate)
            {
                boxSides.UpdateSides(new Vector2(other.Position.x, other.Position.z), other.Size);
            }

            //var DeltaX = self.Position.x - Mathf.Max(RectX, Min(CircleX, RectX + RectWidth));
            //var DeltaY = self.Position.z - Mathf.Max(RectY, Min(CircleY, RectY + RectHeight));
            //return (DeltaX * DeltaX + DeltaY * DeltaY) < (CircleRadius * CircleRadius);

            //Find the closest point to the circle within the rectangle
            float closestX = Mathf.Clamp(self.Position.x, boxSides.Left, boxSides.Right);
            float closestY = Mathf.Clamp(self.Position.z, boxSides.Top, boxSides.Bot);

            // Calculate the distance between the circle's center and this closest point
            float distanceX = self.Position.x - closestX;
            float distanceY = self.Position.z - closestY;

            // If the distance is less than the circle's radius, an intersection occurs
            float distanceSquared = (distanceX * distanceX) + (distanceY * distanceY);

            return(distanceSquared < (self.Radius * self.Radius));
        }
예제 #2
0
        protected bool CheckBoxToBoxCollision(CollisionBox2D self, CollisionBox2D other)
        {
            var otherSides = other.BoxSides;
            var selfSides  = self.BoxSides;

            if (self.BoxSides.NeedUpdate)
            {
                selfSides.UpdateSides(new Vector2(self.Position.x, self.Position.z), self.Size);
            }

            if (other.BoxSides.NeedUpdate)
            {
                otherSides.UpdateSides(new Vector2(other.Position.x, other.Position.z), other.Size);
            }


            return(!(otherSides.Left > selfSides.Right ||
                     otherSides.Right < selfSides.Left ||
                     otherSides.Top < selfSides.Bot ||
                     otherSides.Bot > selfSides.Top));

            return(false);
        }
예제 #3
0
 protected bool CheckCircleToBoxCollision(CollisionBox2D self, CollisionCircle2D other)
 {
     return(CheckCircleToBoxCollision(other, self));
 }