Ejemplo n.º 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));
        }
Ejemplo n.º 2
0
 protected bool CheckCircleToBoxCollision(CollisionBox2D self, CollisionCircle2D other)
 {
     return(CheckCircleToBoxCollision(other, self));
 }
Ejemplo n.º 3
0
 protected bool CheckCircleToCircleCollision(CollisionCircle2D self, CollisionCircle2D other)
 {
     return(Mathf.Pow(other.Position.x - self.Position.x, 2) +
            Mathf.Pow(self.Position.z - other.Position.z, 2) <=
            Mathf.Pow(other.Radius + self.Radius, 2));
 }