views:

1867

answers:

2

With reference to this programming game I am currently building.

Thanks to the answers from this post, I am now able to find the x-y coordinates of all the points of the rectangles (even when rotated), and Collision-Detection with Walls is almost working perfectly now.

Now I need to implement collision detection with the bots themselves (cause obviously, there will be more than one bot in the Arena).

Square-Square Collision Detection (Non-rotated) is not valid in this case because the bots will be turned at an angle (just like I described here).

So what is the best way to implement this form of Rotated Rectangles Collision Detection in WPF?

I guess there must be some math involved, but usually it turns out that there are functions in WPF that "calculate" these maths for you (just like in this case)

A: 

I would check each line for collision (so you'd have max. 4*4 line collision checks, if two lines collide, the bots do, too, and you can stop), although I'm sure there are better/faster ways to do this. If the rectangles can have different sizes, you should also check if the smaller is inside the other.

The performance could be slightly increased if you first check the rotated x/y-min/max-value of the rectangles (or you can even calculate two circles around the bots and check these, which is even faster) so you don't have to check the lines if they are far away from each other.

schnaader
+5  A: 

Solution

By using the method I posted as a solution to this previous question and a WPF method called IntersectsWith (from Rect), I was able to solve this issue of rotated rectangles collision detection like so:

public Rect GetBounds(FrameworkElement of, FrameworkElement from)
{
        // Might throw an exception if of and from are not in the same visual tree
        GeneralTransform transform = of.TransformToVisual(from);

        return transform.TransformBounds(new Rect(0, 0, of.ActualWidth, of.ActualHeight));
}

Vehicle IsBotCollided(IEnumerable<Vehicle> blist)
{
    //currentBounds is of type Rect, which contains the 4 points of the rectangle (even when rotated)
    var currentBounds = GetBounds(BotBody, BattleArena);

    //Then I check if the current bounds intersect with each of the other Bots` bounds that are also in the Arena
    foreach (Vehicle vehicle in blist)
    {
        if(GetBounds(vehicle.BotBody, BattleArena).IntersectsWith(currentBounds))
        {
            return vehicle;
        }
    }
    return null;
}
Andreas Grech
It seems that GetBounds doesn't return a Rect structure, but a RectangleF structure (http://msdn.microsoft.com/de-de/library/system.drawing.rectanglef.aspx) that contains all four rectangle points and also has an IntersectsWith method. Nice to have, nice to know!
schnaader
English link: http://msdn.microsoft.com/en-us/library/system.drawing.rectanglef.aspx
Andreas Grech
Feel free to mark your own answer as the solution! :)
Andy Mikula
This answer helped me a lot! Thank you :)
Jobi Joy
.InterSects with is very slow
TimothyP