Determining all collision directions for player

In my platformer I’m trying to figure out the best way of getting the collision directions of the player, a rectangular collision object; left/up/right/down. I want to use those flags to determine which state the player should be in to allow wall jumps and wall slides. Trickier than I thought!

So far I’ve tried these approaches:

  1. Using the normal from contact_point_response. This doesn’t work when the player is next to a wall, because the collision stops when the player stops running. Adding a force to press the player into the wall kind of works, but can lead to unexpected behaviour, such as jumping straight up when standing next to a short wall lands you on the ledge above, which feels weird.

  2. Adding a collision trigger which is slightly bigger than the player. This kind of works, but is not ideal because the player only needs to be close for it to trigger, so may trigger at slightly the wrong time. I also noticed that the trigger lag behind the player collision object for some reason.

Any other ideas on how to achieve this more cleanly? I’m going to try to use raycasts next.

I’ve used multiple collision detectors before for this kind of thing.

1 Like

That’s interesting. How were you able to get the triggers to keep up with the player collision object? It seems to always lag behind for me. I nest it inside the player game object, just like the main collision object.

I’d have to do another test to check that again as last time I did it was some time ago and I’m not sure where the project is. I’m working on something else right now. Could you try to make a small version which lags and post it too to get feedback on?

Thanks for the help! I’ve made a minimal project which demonstrates the issue: TestTriggerLag.zip (10.8 KB)

It’s simply a dynamic body (the player) with a slightly bigger trigger in the same game object. As you can see (hopefully!) the trigger lags a bit behind.

Following up on this; setting Box2D’s gravity to 0 and manually adding gravity each frame seems to be the way to do it. The collisions now always come through, including the ones where you’re standing next to a wall. This is presumably because the dynamic body never becomes inactive/sleeping.

This will also help in other ways. For example I’m adding a “jump back to wall” state, for which I want to cancel the gravity temporarily. Now I can, by simply not apply gravity when in that state. This is quite a neat workaround to use instead of body:setGravityScale(), which is not yet bound by Defold.

The lagging trigger, posted above, is still an issue though. Any idea whether it is a Defold bug, or if I’m misunderstanding something?