WPF:使用旋转方块进行碰撞检测

发布于 2024-07-14 06:29:28 字数 744 浏览 7 评论 0原文

参考这个编程游戏< /a> 我目前正在构建。

感谢 这个的答案post,我现在能够找到矩形所有点的 xy 坐标(即使旋转时),并且墙壁碰撞检测现在几乎可以完美工作。

现在我需要对机器人本身实施碰撞检测(因为显然,竞技场中会有多个机器人)。

方方碰撞检测(非旋转)在这种情况下无效,因为机器人将以一定角度转动(就像我所描述的此处)。

那么在 WPF 中实现这种形式的旋转矩形碰撞检测的最佳方法是什么?

我想一定涉及到一些数学知识,但通常情况下,WPF 中有一些函数可以“计算”这些为您提供数学知识(就像本例一样)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

行至春深 2024-07-21 06:29:28

解决方案

通过使用我发布的方法作为上一个问题的解决 方案和一个名为 IntersectsWith 的 WPF 方法(来自 Rect),我能够解决旋转矩形碰撞检测的问题,如下所示:

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;
}

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;
}
沐歌 2024-07-21 06:29:28

我会检查每条线是否发生碰撞(所以你最多进行 4*4 线碰撞检查,如果两条线发生碰撞,机器人也会发生碰撞,你可以停下来),尽管我确信有更好/更快的方法去做这个。 如果矩形可以有不同的大小,您还应该检查较小的矩形是否在另一个矩形的内部。

如果您首先检查矩形的旋转 x/y 最小值/最大值(或者您甚至可以计算机器人周围的两个圆圈并检查它们,这甚至更快),那么性能可能会略有提高,因此您不必检查线路是否彼此远离。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文