WPF:使用旋转方块进行碰撞检测
感谢 这个的答案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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案
通过使用我发布的方法作为上一个问题的解决 方案和一个名为
IntersectsWith
的 WPF 方法(来自Rect
),我能够解决旋转矩形碰撞检测的问题,如下所示:Solution
By using the method I posted as a solution to this previous question and a WPF method called
IntersectsWith
(fromRect
), I was able to solve this issue of rotated rectangles collision detection like so:我会检查每条线是否发生碰撞(所以你最多进行 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.