The sticky situation part 1
The part of the code I re-written the most is definitively the collisions handling. Collisions handling is the program that makes hitting a wall, walking on a platform, pushing a crate, jumping, dying, bouncing… working properly in a game.
Hopefully, most of those actions are working good in my game. The only thing that I can’t get rid of is the “sticky situation”. It’s a well known situation for those using Box2D. I didn’t find any “perfect solution” to this. Only workarounds that could be featured in “There I fixed it” website.
So, I want to share with you my efforts (and misery) to make the best hit box for the hero of the game. So people won’t do the same mistakes I did and, maybe, a Box2D prophet will read this and show me the light.
This is my first very naive approach to the hero hit-box. As you can see, the hero is a simple round body. The ground and wall collisions are handled by those thin rectangles, called “sensors”, that are placed on the actual wall and ground physics bodies.
The sensors are not part of the physics world. They’re just here to send informations when something touch them.
The ground sensor prevent the hero from jumping when in mid-air. If the hero touches the sensors it means that he’s “grounded” so he has the permission to jump. As soon as he’s not touching it anymore, the permission is revoked. Simple and effective.
So why is the wall sensors are for? Is a big physics box not enough? NO! When the hero is jumping and moving against a wall, all we get is the infamous “sticky situation”! Meaning that the hero stays sticked to the wall until you release the left or right direction. This is something that makes me crazy about Box2D. Why don’t he just slides along the wall onto the floor?
Well, this is because of how velocity is handled in Box2D. As long as the hero has a velocity pushing him in a direction, he will stay sticked to any vertical surfaces. So that’s why I used sensors on the walls as well. They simply cancel the hero’s velocity so he can fall gently.
This was good enough. Simple and effective. Until I started to make more levels.
Making sensors on every surfaces was a pain and was quadrupling the physics bodies in the scene. So I had an idea. Why not putting the sensors on the hero directly? A tutorial on iForce2D (an excellent website about using Box2D) was saying like “Yeah, this is the way to go”.
So I came up with this. The same round body but with the sensors all around. The head sensor is for when the hero is walking upside-down but it does exactly the same as the foot sensor.
I almost believed this was the final attempt. Until the hero got stuck on a cliff. Yeah, the exposed surfaces of the rounded body was sticking to the cliff… Gosh…
Well, let’s make a square body then! If it was that simple… So what we have here, it’s that the corner of the main body are stil exposed to collisions without the sensor colliding.
So why not making the sensors the size of the main body? Because the collisions, especially at high speed, are not that precise. Box2D somewhat simulates the collisions but not at every frames. So, the result is that sometimes, the foot sensor hits the wall so the player can wall jump. Even if it’s cool, it’s not what I want. Or worst, the wall sensor hits the ground so the hero can’t move anymore. Bummer!
But, hey, at least, he’s not stuck on cliff anymore!
What I learned here is that I will never ever use a physics engine for a platformer again. There are plenty of “fake” physics that are just enough for this kind of games. But now, I’m too far into the development to restart from scratch. And, honestly, it’s so much fun to play with Box2D. It is far more easy to make physics based puzzles for exemple. So, I will “stick” with it.
I have different hypothesis to fix this sticky situation (which is actually the only issue I have with Box2D). I will try to make different bodies for ground and walls so the sensors will detect which material they’re colliding with and thus, will prevent confusions.
Feel free to share your experiences! I’m very curious to know how you struggled and maybe succeeded with this issue.
And thanks for reading my bad english.
Read part 2.