Tuesday, October 1, 2019

collision detection - How to differentiate landing on top of an object from falling down the side?


I am looking for a conceptual solution to my problem. It's a simple platformer-alike game where player can move horizontally during free-fall.


Consider those two cases:


In the first case, from game experience point of view, the player should land on top of the box; and in the other case he hit the left edge, hence the player should fall down.


However, from my code point of view ("real behaviour"), both those collision detection cases are identical. I am not sure how to separate them.


In both cases the vertical velocity is positive (falling down) and the user is moving with some fixed positive horizontal velocity. (moving right)


From a collision-standpoint the two cases are identical, I think. How can I tell whether I should put the player on top of it or let him fall?




Answer



There are three common ways games sort this out:




  1. Minimum Separation Vector


    Upon a collision, compute the shortest movement that pushes the bodies apart into a non-intersecting position.


    If I've crept just 0.1 units into the platform's left side, but my feet are 0.2 units below its top surface, then the shortest movement to resolve the penetration is to shift me 0.1 units directly left.


    If the penetration from each face is the same, you can pick one arbitrarily.





  2. Rewind to Moment of First Intersection


    Knowing the penetration depth of the object and its incident velocity, you can compute the moment in its travel this frame that it first contacted the obstacle, and back-track it along its velocity to the moment of contact. This will generally be a diagonal movement, rather than favouring the horizontal or vertical axis exclusively.


    That rewound position will sit either wholly to the left, or wholly above, or exactly corner-to-corner in the most extreme case.




  3. "Love the Player"


    For the particular case of the player controller, we might want to use player experience rather than geometry to guide us.


    As you say, these two situations look almost identical to the player - if in one case they fall and one they land, based on tiny fractional differences in the numbers, then the mechanic is liable to appear inconsistent, capricious, and frustrating to the player.


    Instead, we can choose to make a charitable assumption that honours what the player was trying to do: as long as they haven't sunk too far below the top of the platform (maybe use their falling speed times half a frame as a threshold), correct them to the top of the platform as though they landed on it, even if a perfectly continuous simulation would have said otherwise.


    This will only fudge the player's position by a half a frame's movement, tops, so it generally won't be very noticeable if you choose a suitable threshold, and a landing sprite/animation/dust cloud/sound effect can help sell the landing and mask the fudge factor.



    If you want to increase this tolerance range further, you can add a ledge grab state to trigger on near-misses like this and save the player from a fall when they were "close enough" for your game's target experience, even if raw physics disagrees.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...