Current list of collision system posts:
Collision System Part 2D: Entering tunnels while fallingHere's another additional post that slots into the collision system series. I've been working on some general polish and bugfixes, and one thing I wanted to solve was that it was inconsistent whether Leilani could enter a small tunnel that she was falling past. This gif shows the problem situation:
Players should expect to be able to enter the tunnel and not fall past it.
Let's have another look in slow motion:
And now a breakdown of what's actually happening.
1. Here's the starting situation. Leilani is against the wall with diagonally downwards velocity.
2. As explained in an
earlier post in this series, movement is handled one axis at a time, alternating between the X and Y axis. Next Leilani's X movement is handled, pushing her into the wall. Most of the way down the wall she will just collide with the wall normally. Once she reaches this point though, the soft collision will kick in (also explained in an
earlier post) since only the topmost of the three collision checks detects the wall.
3. The result of the soft collision is that Leilani is nudged downwards to just below the tunnel's ceiling. (You can see this in the slow-motion gif above where Leilani snaps downwards slightly).
4. The next step is to handle Y axis movement, which moves Leilani down past the tunnel entrance.
5. Then X axis movement is handled again, but Leilani just collides against the wall. She's no longer able to go into the tunnel.
(Note that soft collision doesn't kick in at this point - why? Well in the past I decided to only enable soft collision against floors like this if Leilani is rolling, but not at other times. Soft collision against floors allows Leilani to roll seamlessly over single-tile gaps in the floor, but I don't want her to be able to simply walk over these kinds of gaps.)
So the core problem is that Leilani moves past the tunnel entrance in a single Y axis movement step, missing her chance to enter the tunnel.
This soft collision against the ceiling is intended to
help Leilani to get smoothly into gaps like this. Rather than bonking against the wall above the tunnel, she should slip down below the ceiling and into the tunnel without losing any momentum.
But the soft collision is actually exacerbating the problem by moving Leilani downwards, which makes her more likely to miss the tunnel opening during the following Y axis movement step.
Turning off the soft ceiling collision may slightly improve the chances of Leilani getting into the tunnel but still doesn't make it 100% reliable.
The fixThe way the soft collision works is a bit flawed - you can see in diagrams 2 and 3 above that the soft collision happens during an X axis movement, but it actually nudges Leilani downwards and doesn't even move her sideways at all. Ideally it would still move Leilani sideways into the tunnel as expected, as well as nudging her downwards to avoid the ceiling.
I wanted to fix this bug without messing too much with the general workings of the collision system, as it's getting really easy to introduce unintentional bugs at this point! So I didn't want to do a fundamental change like this.
So instead I did a very situation-specific hack!
4 (fixed). Continuing on from diagram 3 above after handling the soft collision with the ceiling. Now we do a collision check a short distance (0.1 blocks) in front of Leilani, starting from just below the ceiling Leilani soft-collided with, and stretching down just over 1 block. If this collision finds a floor almost exactly 1 block below the ceiling, then we decide there's a tunnel that Leilani should enter.
5 (fixed). Next we simply reduce Leilani's Y velocity to a very small value. Then when we do a Y axis movement step Leilani will hardly move at all.
6 (fixed). Finally the next X axis movement can move Leilani into the tunnel.
The fix is applied specifically for Leilani and not for enemies to further reduce any unintended side effects.
If the framerate was very low could Leilani still skip past the tunnel during the Y axis movement? In theory yes, in practise probably not, because I apply a maximum frame length of 0.1 seconds even if the game is running much slower. There are also worse issues to deal with if the game is running that badly.
So the fix is reliable because I expect the soft collision against the tunnel ceiling to always trigger when Leilani is falling down past it, which will then recognise the tunnel entrance and apply the hack to her Y velocity.
ResultsLeilani now successfully enters the tunnel! In this gif I'm holding the Right input to keep pushing Leilani against the wall and run into the tunnel.
In the next example I let go of the direction inputs altogether after jumping. The game has a feature that Leilani maintains a very small amount of X velocity even after colliding with a wall, so she'll still be trying to move to the right and will still just manage to enter the tunnel.
Finally this fix also works for big Leilani when crouching, and she can then start rolling once she enters the tunnel.