And*_*ech 3 math wpf collision-detection rotation
参考我目前正在构建的这个编程游戏.
感谢这篇文章的答案,我现在能够找到矩形的所有点的xy坐标(即使在旋转时),并且与墙壁的碰撞检测现在几乎完美地工作.
现在我需要与机器人本身实现碰撞检测(显然,在竞技场中将有不止一个机器人).
方形角碰撞检测(非旋转)在这种情况下无效,因为机器人将以一定角度转动(就像我在这里描述的那样).
那么在WPF中实现这种形式的旋转矩形碰撞检测的最佳方法是什么?
我想必须有一些数学,但通常会发现WPF中有函数可以为你"计算"这些数学(就像在这种情况下)
通过使用我发布的方法作为前一个问题的解决方案和一个名为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;
}
Run Code Online (Sandbox Code Playgroud)