Hit Testing a Rotated Rectangle
I'm making a game that needs to hit test a Rect object inside 4 points that basically forms rotated a rectangle. This means I can't use the Rect class's IntersectsWith function to hit test. So my question is: if I have 4 points that creates an imaginary rectangle of any orientation, how can I tell if another rectangle is inside the "fence" so to speak?
Thanks in Advance. :)
Re: Hit Testing a Rotated Rectangle
Are both rectangles rotated? Must one be wholly within the other or any area of intersection is enough?
Re: Hit Testing a Rotated Rectangle
This is the same football game I've been working on for years JMC. I'm at the point now where I'm trying to have players "see" in front of them. If a defender is in the way they turn to avoid him. So the big rectangle I'm describing is the runners field of vision. This field of vision rectangle is a few yards in front of the player and has to rotate with him, which is why the strcit up/down left/right Rect class won't work to hit test. The rectangles I'm testing for inside this Field of Vision rotated rect are the defenders. Thankfully, they are NOT rotated; they are simply 30X30 bitmaps.
For now I'll test to see if there's an intersection between the rectangles; the player's rect doesn't have to be entirely contained in the field of vision rect.
I hope that explains things. If not I can try again. :)
Thanks so much.
Re: Hit Testing a Rotated Rectangle
This is somewhat theoretical at this stage but I think that you should be able to use Regions. If you create a Region from a Rectangle and then Transform it, you should be able to make it represent your rotated rectangle. You can then create another Region from the other rectangle. You can then use the Intersect and IsEmpty methods to determine whether there exists a non-empty intersection between the two Regions. The documentation for the Region.Transform method has a code example that creates a Region from a Rectangle and then rotates it by 45 degrees. That might be a good place to start.
Re: Hit Testing a Rotated Rectangle
If jmc's method doesn't pan out, then you could alternatively do line segment intersection tests for the lines that make up the rectangles.
Re: Hit Testing a Rotated Rectangle
Do you have to use rectangles? Doesn't seem very appropriate for a field of vision. Using a circular arc for field of vision and a complete circle for the other player would be pretty easy. Just have to test radiuses and the angle.
Even if the field of vision is a rectangle, it'd be easier if the other player was a circle, then you only have to test if the center of the circle is within a slightly larger rectangle using line segment tests as Max suggested.
Re: Hit Testing a Rotated Rectangle
Can't you use polygon-point intersection? I've posted some code for that a few times before but I'm sure you can find it if you google something like "point in polygon". All it needs is the coordinates of the points that make up the polygon (4 points in your case) and the point you want to check for. It can return true or false depending on whether the point is inside the polygon or outside.
You would repeat this procedure 4 times, once for every corner of the 'player' rectangle. If you want the 'player' to be completely inside the polygon, then all four points must be inside the polygon. If, on the other hand, you are looking for intersection only, then you can return true if at least one point is inside the polygon.
I think this will be the easiest method and probably also the fastest.
Re: Hit Testing a Rotated Rectangle
Quote:
Originally Posted by
NickThissen
You would repeat this procedure 4 times, once for every corner of the 'player' rectangle. If you want the 'player' to be completely inside the polygon, then all
Hair split, but I think you'd have to do it 8 times. Again for each corner of the Field of View intersecting the player. Otherwise the player might significantly overlap a corner of the FoV rectangle, with his corners straddling the FoV's corner, but both outside the FoV.
Re: Hit Testing a Rotated Rectangle
Quote:
Originally Posted by
Kallog
Hair split, but I think you'd have to do it 8 times. Again for each corner of the Field of View intersecting the player. Otherwise the player might significantly overlap a corner of the FoV rectangle, with his corners straddling the FoV's corner, but both outside the FoV.
Ah yes, you're right. Still, it doesn't really matter. The algorithm I mean is just a loop through all points of the polygon, with a few comparisons each iteration. It will be so quick that you can safely do this loop for hundreds of players and their fields of view before noticing any delay at all.
Re: Hit Testing a Rotated Rectangle
Wow, what a lot of great responses! Thanks!
They are all excellent , but I like Kallog's idea of using an arc instead of a rectangle for the field of vision the best. I'm thinking I could do two checks: one for distance inside a circle all around the player using the circle's radius and a GetDistance function I have already created with good all pythagoream theorem. Once I get a list of the defenders in the ball carriers vicinity I can then check to see if each defenders angle from the ball carrier lies in the ball carriers field of vision arc. The better the player, the wider the arc and greater the radius.
I guess I'm just repeating what Kallog said in more detail, but it helps to write stuff out.
I'll give it a go today and report on my success. Thanks again everyone. :)