开发者

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.

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:

Computing collisions of a rectangle with path lines

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). With only a handful of tests, you should be able to find such a point.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜