计算矩形与路径线的碰撞
我一直在寻找示例和教程,但找不到任何具体内容。
我正在制作 2D XNA C# 游戏,我想在玩家到达一条或多条路径线时检测其最终位置,这样它就不会穿过它们。
玩家由一个碰撞矩形组成,路径线都是线段。所以基本上我有玩家的碰撞矩形和下一个玩家的位置碰撞矩形。如果下一个玩家的位置与路径线碰撞,我想找到该玩家可以承受的最大位移。
该图像或多或少显示了我想要做的事情:
我想找到红色矩形的位置。
有谁有任何算法、解决方案或任何可以帮助我的链接吗?甚至可以是一个样本。
I've been looking for samples and tutorials, but I can't find anything specific.
I am making a 2D XNA C# Game and I want to detect the final position of the player when it reaches one or more path lines, so it won't cross them.
The player is made of a collision rectangle, the path lines are all segments. So basically I have the player's collision rectangle and the next player's position collision rectangle. If the next player's position collides to path lines, I want to find the maximum displacement the player can suffer.
The image shows more or less what I want to do:
I want to find the position of the red rectangle.
Does anyone have any algorithm, solution or any link that could help me? Could be even a sample.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您已经有办法检查特定碰撞框是否包含碰撞,我建议在玩家当前位置和碰撞框之间进行某种二分搜索:在碰撞和先前已知的非碰撞之间选择一个点-碰撞盒。使用这个新的碰撞盒再次进行测试。如果不是碰撞,则在该点和已知碰撞框之间选取一个点,否则选取向后一半的点。重复此操作,直到找到一个精度达到您满意水平的非碰撞框(例如,大约 1 到 2 像素)。只需进行少量测试,您就应该能够找到这样的点。
Assuming you already have the means to check whether a specific collision box contains a collision or not, I would recommend doing a sort of binary search between the player's current position and the collision box: pick a point halfway between your collision and the previous known non-collision box. Test again with this new collision box. If it's not a collision, pick a point halfway between this point and the known collision box, otherwise pick a point halfway backwards. Repeat until you have found a non-colliding box at a level of accuracy you are satisfied with (say, on the order of 1 to 2 pixels). With only a handful of tests, you should be able to find such a point.