RFC: Semi-solid platforms

Hello.

I'm a new soul on these forums so let me first share a few words. I've been an amateur programmer for a long time and I've worked with many languages (mostly visual basic), however, I've recently settled with ANSI/ISO C/C++. I've always been fascinated by 2D platformer games and I've also taken interest in game development. I am looking to develop a game with some physics involved and I needed a physics engine for that. I could code one myself, but why not use existing solutions, eh? I heard about Box2D while browsing the GameDev forums and it appears to be a nifty little 2D engine, perfect for a 2D platformer game though I noticed it could use a few more features. So I took a pencil and brainstormed a number of my ideas down on paper that I'd like to share with you. I'll be listing some more ideas like this in the near future.

This is a RFC (request for comments) thread. Basically, the intention of this threads is to comment and discuss a number of features that may possibly get implemented into the engine in the future. I'll try to do my best to describe the idea(s) so feel free to comment. This particular thread is about semi-solid platforms and the possibility to get the feature implemented in Box2D (if it hasn't already - note that I've only checked out Box2D v2.0.1). If you've played any 2D platformer games then you are most likely familiar with the concept of semi-solid platforms (I'll use the term "SS" to refer to semi-solid). SS platforms are just like fully-solid platforms except that they don't collide from beneath, allowing you to jump through it from underneath and land ontop. Some games go a step further, allowing you to jump back down by holding eg. the down button and hitting jump at the same time. I found an existing thread here, but I'd like to take this concept further as I think it's a must-have for a 2D engine.

I've tried to come up with a decent concept... something that scales well with future development of Box2D eg. kinematic bodies (movers), but I got stuck in a few places. I'm guessing the proper place to perform code modifications are probably shapes, not bodies. Frankly, the most feasible place for semi-solid shapes are static bodies (probably movers too when they are implemented). How dynamic bodies containing SS shapes would perform is a topic to be discussed.

So let's attempt to define a semi-solid static shape:
  • Collides against objects from top only (rectangular shapes are easy, but what about more irregular shapes?)
  • Collision is disabled from beneath (probably sideways too)
  • Holding down some keys should disable the collision checking, allowing players to fall through. This would need some extra user coding and is probably outside the scope of Box2D.

From the top, the semi-solid platform appears just like a normal fully-solid platform (except maybe when a combination of keys is pressed to disable the collision against certain objects, allowing them to pass through). From the bottom, any collision checking against objects entering the shape is automatically disabled. There is also no collision while the body is inside a SS shape, allowing bodies to move freely through in any direction. Once an object fully emerges ontop of the platform and is no longer colliding with it, the collision kicks in and prevents it from entering the shape from that direction. This way a player can jump from underneath and if he doesn't fully reach the top of the platform, he falls back down and there is no collision... meaning you won't get stuck in the middle of the semi-solid shape, should you not reach the top. There is one problem here - the concave shape groups. What if the whole concave shape group is supposed to be a SS platform? Objects may pass through only half-way before changing direction back down due to gravity. This means they would get stuck somewhere in the middle.

This concept of upper and lower side of a shape is rather simple, but there's a problem. Box2D can change the world gravity at any time. So what happens when gravity suddenly shifts and now faces sideways? What happens, should the bodies disobey the world gravity and have their own local gravity implemented at some point in the future? How would semi-solid shapes behave now? How do we know which way is up and which is down and therefore which way to block bodies or let them pass through? Do we calculate it based on a gravity vector of the body? Does the collision with SS shapes even change to adapt to new gravity direction or should it remain unchanged?

Not a perfect solution, but my ides was to define a new gravity-independent direction vector (let's call it "DV") for each SS shape that specifies which way a shape should let objects pass though. Just like a diode which passes the electric current in one direction no matter how you rotate the circuit. If an object is traveling at a direction that is greater than DV - 90° and lesser than DV + 90°, it will pass through the SS shape (collision will be disabled). If the same object hits the semi-solid shape from any other direction (lesser than DV - 90° or greater than DV + 90°), then it collides with the shape.

Looking into the future, I tend to ask myself a lot of questions. Suppose we have now implemented movers (kinematic bodies) in Box2D. We have a rotating mover in our level. How do we treat the case where a player wants to jump ontop of such SS body from underneath? What happens to the DV - does it still point to the original direction or does it rotate along with the body? If we use the former approach, it solves our problem, but may introduce a new one eg. what if we want the vector to rotate along? The latter approach lets us rotate the DV along the body, but by doing so it screws up the platform. Maybe the solution should be to have DV modified to automatically point in the opposite direction of gravity at each timestep. Again, something to be toughly discussed.

There, this pretty much sums up my ideas about SS platforms. I have tons of other ideas down on paper and I'll be making a few more writeups such as this one on various topics as I have time. But for now... please comment and discuss. Thanks.

Regards,
Copper
I agree this is very desirable.
I made it work here : viewtopic.php?f=6&t=1636

Basically use the static edges in Box2D. But you'll need to do further tricks to make it work properly. I haven't solved it entirely yet.
Here's another thread where we discussed this: viewtopic.php?f=8&t=1748

I don't use the built in gravity. Instead my "World" objects have a base gravity function that just mimics the built in gravity, but sub-classes can override it and implement body specific gravity, category based gravity, non-linear gravity, etc. My base "World" class sets a "gravity" b2Vec variable on my actors so they can respond to the direction of gravity if needed. For example, the "player" body needs to know which way is up so it can jump.
The notion that something collided from the top but not from the bottom has many problems.

What if the object is slightly penetrated into the platform, do we push it up? If so, then when the object moves up through the floor, will the solver see a large penetration and then pop the object up in one frame? You could say the object is already moving up, so the solver should not apply a correction.

What if a large object lands on the character and pushes the character into the platform. At some point the character is penetrated in the platform and moving up. Do we stop applying position correction because the character is moving up? The character could be moving up very slowly and so a persistent overlap would be observed.

Much of the desired behavior points to the need for a finite state machine (FSM). IMO, such a FSM does not belong inside Box2D, because the particulars would likely need to be customized based on game design.

So this leads me to the big question: should a character be a rigid body at all?

In games I've worked on, most of the time the character is not a rigid body. The character is a represented by a geometric proxy that is manipulated between physics time-steps (not during). I use a specialized character solver that has it's own set of rules. I think Box2D should be extended to support character proxies by providing phantoms that can be shape-cast and allow the computation of contact manifolds. With this functionality, it would be much easier to plug in a user FSM to control the collision behavior, without mucking with the code that handles rigid body movement.
I am not yet too familiar with the internals of Box2D so I'm going to say a few words based on pure logic.

Erin Catto wrote:
What if the object is slightly penetrated into the platform, do we push it up? If so, then when the object moves up through the floor, will the solver see a large penetration and then pop the object up in one frame? You could say the object is already moving up, so the solver should not apply a correction.
Well the obvious question would be how that object ended up slightly penetrating the SS shape in the first place. I can think of three scenarios.

A) It came from beneath and is traveling upwards - this probably means the object is about to leave the SS body ontop and there shouldn't be any correction. Should the object not have enough energy to fully leave the SS shape then it should start falling back down without any collision occurring.

B) It came from above and somehow ended up piercing the SS shape. Here's the part where the object cannot pass through and the solver should stop it. I think the real question is how to detect this behavior. The object velocity vector (moving direction) could be a nice indicator tho I must admit this approach is just an idea I had, not an actual foundation. Checking whether the object was colliding with the shape in the previous gametick/iteration might be another indicator.

C) It was already there since the first gametick. Same as case A - no collision.

Erin Catto wrote:
What if a large object lands on the character and pushes the character into the platform. At some point the character is penetrated in the platform and moving up. Do we stop applying position correction because the character is moving up? The character could be moving up very slowly and so a persistent overlap would be observed.
Hmm... Hell, just make the character explode with lots of blood spitting out 😉 You'd probably need a way to detect from what side a body has entered the SS shape so if it enters from the forbidden side then you can execute extra code that prevents stuff from happening or makes some other stuff happen. Again, would point B in my previous answer be of any use?

Erin Catto wrote:
Much of the desired behavior points to the need for a finite state machine (FSM). IMO, such a FSM does not belong inside Box2D, because the particulars would likely need to be customized based on game design.

So this leads me to the big question: should a character be a rigid body at all?

In games I've worked on, most of the time the character is not a rigid body. The character is a represented by a geometric proxy that is manipulated between physics time-steps (not during). I use a specialized character solver that has it's own set of rules. I think Box2D should be extended to support character proxies by providing phantoms that can be shape-cast and allow the computation of contact manifolds. With this functionality, it would be much easier to plug in a user FSM to control the collision behavior, without mucking with the code that handles rigid body movement.
I've seen the SS platforms being done in a lot of games that use pixel terrain mask, but I've never seen them done with a vector terrain mask. If you're talking about modifying/changing collision code for a character (as in a player character) then how would you handle a case where you wanted some other dynamic body (like a bullet) to act the same way eg. allowing you to shoot through a wall from one side (bullets can pass through from beneath), but not the other (bullets can't pass through from above)?

Christ, I never thought 2D game design was this complex 😉
Ah, stating things a FSM makes so much sense. In that other thread where we are working on improving the contact system, we've essentially implemented that, albeit with only two states, non/sensor.
You make this sound like the hack that it is, Erin.

Perhaps b2Contact/b2ContactPoint or something should also have userData - so that users can implement that sort of logic without having to recreate the contact list structure, which is quite complicated.
what would be great is a collision filtering callback
what if the contactlistener had another function called before Add that could return false which cancelled the collision
then you could set your contact filtering callback to return false if the player's collision vector is 'upward' or whatever critera suits your game
You can essentially do that, with the patch.

Original thread: https://box2d.org/forum/viewtopic.php?t=2760