2
votes

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:

Picture

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.

1
Is there any specific reason why are you using rectangles not circles?Piotr Auguscik
@piotr Auguscik Could be a precision issue.MGZero
Just asking, with circles such tasks are easier :)Piotr Auguscik
I need to use rectangles because my player is a character, a person. Using a circle wouldn't make sense at all.Fernando Wieliczko
You could make him a snowman! I can't believe XNA doesn't provide native collision detection for 2D sprites, that's shocking. Maybe this might have an example? create.msdn.com/en-US/education/catalog/tutorial/…rice

1 Answers

0
votes

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.