I'm making a platformer, and what I would like to do is give the player the ability to jump through the bottom of certain platforms. However, there doesn't seem to be a good way of doing this with Box2D. The only solution I could think of would be to change the collision filtering information for the player's body when he jumps. Is there a better solution than this? If not, how would I go about changing the collision filtering information after the player's body and shape have been created?
Additionally, an attractive solution to solve the problem of the player sliding down slopes when he is supposed to stop would seem to be to put the player's body to sleep when he lets go of the controls. However, I couldn't seem to find a way to do this. If the API doesn't support this, would it be easy for me to add this functionality? I have an alternate solution if this isn't a convenient method of solving the problem.
Ideally what you want is a callback from the engine just before it gets to the solver. The callback will return you the current collision data and you would decide if the collision should be solid.
I would put this callback on the player shape. Most of the time the callback would be NULL, so there should be little performance hit for the common case.
You will then need some sort of finite state machine in your game to determine when there should be a collision response.
Give it a shot. If you make this work for 2.0, please send me a patch.
Thanks. I'll be adding this to 2.0, but I doubt I'll have time to finish it before 2.0 is released. I'll be sure to send you whatever I come up with.
I'm facing the same problem - a platformer simply seems to be more fun when you can jump on a platform from underneath.
Has anybody got a decent solution with the current release for this?
My first idea would be to implement a contact filter which filters out contacts between platforms and players moving upward. But then if gravity stops the player in the middle of a platform, things could become nasty and you would need some more logic to prevent cemented players...
Any ideas appreciated.
I don't know about the C++ version, but in the Flash version, the ContactFilter only gets called at the beginning, when two shapes' AABBs start to overlap, and the same result is used for the rest of the timesteps until the two AABBs stop overlapping. This is almost exactly what you want: When the character moves into a platform from below, the ContactFilter would evaluate to false, and the character would not collide with the platform even if the character starts to fall, unless the character completely cleared the top of the platform first, resetting the ContactFilter result. The only problem is that what we really care about is how long the shapes are actually intersecting, rather than how long their AABBs intersect. I don't think there is an easy way to fix that, though Erin's statement that we need a callback right before the solver is probably accurate. Box2D would need to be modified.
Well, if it is only the character you can probably afford to call b2World::Refilter each step.
Thanks for the quick answers.
The described behaviour sounds indeed like what I want and since I (currently) use a fixed-rotation shape for the character and only horizontal squares as platforms, the bounding volume issue shouldn't be a problem.
However if axis-aligned BBs are used diagonal platforms would cause problems.
The described mechanism should be applied to quite a few objects (not only the player) so I guess the refilter call could be critical.
btw: I use the Java port.
just tested it: works great. 😛
maybe one way collisions can be done changing shape's normals?