Raylib and collision response


It’s been more than a month without doing one of these. Anyway lets start

I’ve decided to ditch my game engine (or just the rendering and sound part of it), and just use Raylib. It’s way better than what I was doing and also it compiles without any problem in linux and windows.
So I scrap everything that I could use for the game into a new project.
And 20 days or so later I have everything drawn on the screen like in the older proyect and with some collisions.


Collisions (swept AABB)

For the collisions, I launch a ray that goes from the center of the player and collides with the possible colliders that happen in a given frame.

The bounds of each collider are expanded according to the width and height of the player’s collisions (Minkowski difference). With this setup, we only need to use the Raylib’s built-in ray collision check and we get the collision point where the player should go.

But when it comes to response, this falls flat. Because in a 2D platformer we need to conserve our velocity in the X axis when we are walking. This method will anchor the player to the ground.
To get a good response, I check for collisions 2 times. And instead of setting the velocity to 0 in each axis. I only set it when the normal of the collision in that axis is different to 0.
So When the player collides with the ground (Y axis). I put the velocity of Y to 0 leaving the movement in X unchanged.
This way we can react to walls and the ground as expected in a 2d platformer.

But what if there was a corner?. In this case we got a collision in two axis and we must determine what velocity axis we need to zero out. This corner could be on a wall, in which we know we must zero the X axis. But it could also be on a flat ground. Or even on the corner of a wall.

So, to implement this I make an assumption that the player can continue moving in the X axis. So, I zero out the Y axis and check for collisions again. If the next collision does not occur or occurs at a distance that is more than 0, we know we can continue in this direction. Otherwise, if there is a collision at a distance of 0, It means we cannot go in X direction. Therefore the X velocity needs to be zero.
And after this we can continue with the 2º collision check.

Though I didn’t check the performance, it’s probably bad because of all the calculations needed to raycast to an AABB. And moreso, doing it 20 times or so per frame. While

Anyway, here is a video of the collision response in action. Although there are no physics per se, so the movement is weird.