简体   繁体   English

计算矩形与路径线的碰撞

[英]Computing collisions of a rectangle with path lines

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. 我正在制作2D XNA C#游戏,我想检测玩家到达一条或多条路径时的最终位置,因此它不会越过它们。

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. 甚至可能是一个样本。

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). 重复进行操作,直到找到满足您要求的准确度(例如1到2像素)的非碰撞框。 With only a handful of tests, you should be able to find such a point. 仅需进行少量测试,您就可以发现这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM