Manipulate contact data - fooling box2d

Hello!

There are quite a few opinions and approaches on how to create retro-style jump'n'run dynamics. I now want to focus on the issue of a slight slope that is supposed to not affect your character's movement speed (i.e. walking up should not slow you down).
I found some solutions to that, that do not really satisfy me (basically, use abnormal friction and gravity).
I am also well aware of the fact that box2d is not intended to feature this unphysical behaviour out of the box.
The question then is, of course, whether to use box2d at all and if so, how to trick it.

Long story short: I want to pose a simple question.
Is there a way to manipulate the properties of a contact between two fixtures so that box2d solves the "wrong" problem?
With respect to my slope issue I would then simply catch the PreSolve event and tell box2d that there is in fact no edge with increasing height, but instead there is a flat edge to collide with (at the height where to overlap takes place).
The result would then still need a little shift in the height coordinate.


I would consider such a solution an acceptable way to trick box2d.


Any help and remarks appreciated.
I think there are two main ways to do this. One is to set the friction to zero and give the player body your own friction by means of applying forces to it. This is relatively easy since you can use the info from the contacts with the ground to make decisions about friction, and all the rest of the physics works as normal. However I suspect this is just the kind of thing you are saying was not satisfactory.

You could also detach the player body from the ground completely and use sensors or ray casts to 'feel' the ground below and react appropriately. While it does give you very fine control over exactly how the character should move, it's a lot more work and many hours tweaking because a lot of the regular behavior now depends on you, and you can forget about PreSolve. I feel you are heading more in this direction 😉
Example: http://www.youtube.com/watch?v=LijiW5HOUZM

Hmm.... I suppose you could also add a fake 'surface velocity' to sloped surfaces to effectively make them behave like a conveyor belt helping the player to move up the slope quicker.
http://www.iforce2d.net/b2dtut/conveyor-belts
http://www.youtube.com/watch?v=yGU1E6wQFqE
To be honest, I did not want to start another "my retro physics game" thread.

However your answer was helpful and is highly appreciated!


The reason I think my solution (if possible) would be nice is that I would have all abnormal physical behavior (one-sided platforms and this issue) taken care of in one place, the ContactListener.
Therefore I want to draw attention to my question. Possible, not possible? Advantages, downsides?
I'm not sure what you mean by the first comment there, the video links were supposed to help illustrate what I was talking about since I have spent a lot of time seeking the same goal you're talking about.

But to be fair, I did completely ignore your suggestion didn't I... 😀
Now that I think about it, that seems like a pretty good idea, at least for going up the slope. I may be wrong but I think going down the slope would become a bumpy ride as the body would never be allowed to travel tangentially along the surface of the slope - unless of course you detected that case and turned off your 'fooling' mode.

As I think about it a little more, it seems to me that you could get the same behavior by having a static body with a flat upper surface that follows the player around, ie. use SetTransform to place it at the appropriate height under the character before calling world->Step each time... this may be a little easier than altering contact info in PreSolve.

I don't know the details of Box2D's friction calculations, but you might find that the body still slows a little going up a slope even with this 'fooling' because it is being pushed upward with a greater impulse than if it were moving across genuinely flat ground, which is equivalent to the body being heavier.

I'm not aware of anyone having tried this idea yet, so the advantages and downsides will be for you to tell us 🙂
By my remark, I did not mean to comment on showcasing some videos, not at all. I just wanted to say that I did not want to start another "my platformer" thread because there are quite a few of those. Maybe a collective thread or a note in the FAQ would be appropriate.


I toyed around with manipulating the manifold of such a contact, but I don't think it is the way to go. I'll paste a Testbed code that manipulates the contact normal. The result is indeed a bumpy ride for moving downwards and also bad for moving upwards. In the latter case you move through the fixture, as if it was really flat. I would have expected box2d to update the contact in every step so that the position gets adjusted.

I like your "elevator" idea, that could be a good solution. However it is only easy for colliding with circles or non-rotating bodies.
It would require more than just changing the normal direction. You would also need to change the magnitude of the impulse to be applied, and if the body is not fixed-rotation you would also need to change the location of the points in the manifold too. In other words you're pretty much making the entire contact yourself, that's why I started thinking that using a separate fixture to follow the character would be easier.

Here is that idea applied to the scene in your test. Any contact between the player and the slope is completely disabled, and a third fixture is placed under the player body when it is touching the slope. I added a second player on the top platform so you can see they slow down at the same rate.
Attachment:
NeglectableSlope.h [5.1 KiB]
Downloaded 13 times

It seems to work well enough to prove the idea, but there are a bunch of rough edges that would need to be polished off:
- height for 'elevator' fixture must be calculated from slope. Easy for circle on shallow slope but polygons not so easy.
- at the ends of the slope the player body can stutter up and down as the 'elevator' appears and disappears
- friction for the elevator fixture needs to be changed if slopes have different frictions
- need to detect case where player touches platform from underneath and ignore
- elevator placement is one step behind player movement, so will need to be wider for fast-moving cases
None of these are show stoppers, but the first two could be quite a PITA for anything more than very basic scenes.

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