WPF: Rotated Square Collision Detection

Regarding this programming game that I am currently creating.

Thanks to the answers from this post , now I can find the xy coordinates of all the points of the rectangles (even when turning) and Collision- Detection with walls now works almost fine.

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

Square-Square collision detection (no rotation) is not valid in this case, because the bots will be rotated at an angle (the same as I described here ).

So what is the best way to implement this form of collision detection with rotating rectangles in WPF?

I suppose some kind of math should be involved, but usually it turns out that in WPF there are functions that “calculate” this math data for you (as in this case )

+3
source share
2 answers

Decision

Using the method that I posted as a solution to this previous question , and the WPF method called IntersectsWith(from Rect), I was able to solve this problem of colliding with rotating rectangles:

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;
}
+9
source

( 4 * 4, , , ), , / . , , .

, x/y-min/max- ( , ), , .

0

Source: https://habr.com/ru/post/1703995/


All Articles