2

I am making a game in C# and XNA and I am using three Points to represent the hitbox of an enemy object. A Rectangle variable is used to represent the player's hitbox. I am using three Points for the enemy rather than a rectangle as the enemy is shaped like a triangle.

//Example coordinates
Point pointOne = new Point(0, 50);
Point pointTwo = new Point(50, 50);
Point pointThree = new Point(25, 0);
Rectangle rect = new Rectangle(0, 0, 10, 10);

I need a way to determine if the Rectangle overlaps the area between all of the Points, as shown in the following pictures. Would anybody be able to show me a method or some kind of code that could help me accomplish this?

enter image description hereenter image description hereenter image description here

Ben
  • 1,299
  • 3
  • 17
  • 37

1 Answers1

2

Here is an alternative implementation:

class Triangle
{
    Vector2 topPoint, rightPoint, leftPoint;


    public Triangle(Vector2 p1, Vector2 p2, Vector2 p3)
    {
        topPoint = p1;
        rightPoint = p2;
        leftPoint = p3;
    }



    public bool IsRectIntersecting(List<Vector2> corners)//corners are the corners of the polygon being tested
    {


        if (AreAnyOfTheCornesInsideTriangleLineSegment(topPoint, rightPoint, corners) && 
            AreAnyOfTheCornesInsideTriangleLineSegment(rightPoint, leftPoint, corners) && 
            AreAnyOfTheCornesInsideTriangleLineSegment(leftPoint, topPoint, corners))
        {
            return true;
        }

        return false;
    }

    private bool AreAnyOfTheCornesInsideTriangleLineSegment(Vector2 pointA, Vector2 pointB, List<Vector2> corners)
    {
        Vector2 lineSegment = pointA - pointB;
        Vector3 lineSegment3D = new Vector3(lineSegment, 0);
        Vector3 normal3D = Vector3.Cross(lineSegment3D, Vector3.UnitZ);
        Vector2 normal = new Vector2(normal3D.X, normal3D.Y);

        foreach (Vector2 corner in corners)
        {
            if (Vector2.Dot(normal, corner - pointB) < 0)
            {
                return true;
            }
        }

        return false;
    }
}
Steve H
  • 5,479
  • 4
  • 20
  • 26
  • Nice solution. A lot less verbose than mine. One difference I noticed is that your solution does not consider tangent intersection as intersection (if only rectangle's and triangle's corners touch, they are considered non-overlapping). Just pointing out since sometimes there are different requirements for edge cases. – Jaanus Varus Aug 21 '15 at 14:08
  • 1
    For a rect corner resting on a triangle's edge (or a rect corner co-located with a triangle corner), the Dot condition can be altered to this: `if (Vector2.Dot(normal, corner - pointB) <= 0)` – Steve H Aug 21 '15 at 14:15
  • But for a triangle corner resting on a rect edge, however, we would have to run the triangle corners through the same drill using the rect's line segments just like we did the rect's corners to the triangles line segments... good point. If the OP requires that criteria, I'd be willing to add that functionallity. – Steve H Aug 21 '15 at 14:25
  • This is exactly what I needed. Thanks. :) – Ben Aug 22 '15 at 15:50