Detect collision from a rotated rectangle?

Asked

Viewed 141 times

1

I’m using the following code to know if a point is inside a rectangle:

public bool Intersect(ÍcaroDantasCollisions.Rectangle rectangle)
{
    if(x >= rectangle.x && x <= rectangle.x + rectangle.width)
    {
        if(y >= rectangle.y && y <= rectangle.y + rectangle.height)
        {
            return true;
        }
    }
    return false;
}

Obs¹: The loose x and y in the code refer to the coordinates of the point'.

Obs²: In the rectangle class only the declaration of the variables x, y, width and height are present.

Now the question, let’s say that this rectangle is rotated by 45º, how do we know if the point is colliding with it? I should necessarily use polygons?

1 answer

-1

I found the solution:

First you measure the angle of rotation of the rectangle from the top left, then just rotate the point in a direction contrary to the direction of the rectangle also from its origin, then just make the check as if the rectangle was not rotated :D

  • 3

    It would be important for a real solution here. If it’s just to point out an external link, better post as comment. Another thing, the "rotation" of the point has to be in the opposite direction of the rectangle. Otherwise, your question does not mention the origin of the rotation, it would be good to add there this detail (and take advantage to remove the unnecessary ifs, since the return is bool). And one more thing: the post indicated is very bad as a solution. It is suggested to post real code in the answer, for better evaluation, and to help other community members with similar doubt.

  • Jeez! Really :O! The rotation has to be really in reverse, sorry and!

  • You don’t have to apologize, I just warned you to warn you about the problem. As for the rest, it’s just a suggestion for improvement to enhance the answer (and the question too).

  • I improved on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.