解决方案
通过使用我发布的作为上一个问题的解决方案的方法和称为IntersectsWith
(from Rect
) 的 WPF 方法,我能够解决旋转矩形碰撞检测的问题,如下所示:
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;
}