Friction filtering (modify tangent impulse?)

Hi all,

I want to be able to alter friction based on the type of my bodies and am not sure how to achieve this.

I have several types of dynamic body (defined by a field in my UserData object within the box2d body). These fixtures all have a default "global" friction setting, but I want to dynamically alter the friction upon collision based on my types.

For example:

I have a concept of a "player", and in my world there are "barrels" (just circles). The barrels need friction otherwise they don't roll, but if my player collides with a barrel, I don't want any friction. I can't disable friction for the player because then he would slide about everywhere. With both bodies having friction, when the payer collides with the barrel he sort of "climbs" up on top, rather than just pushing it along which is the effect I am looking for.

Box2d is doing the correct thing, and my player (which is just a square BTW) is exhibiting expected behavior given the friction forces involved, but I need to override this behavior in this particular case.

The documentation talks about: "Tangent Impulse(s)" as:
Quote:
The tangent force is generated at a contact point to simulate friction. For convenience, this is stored as an impulse.


So.. I can get access to this array from the b2ContactImpulse in the PostSolve event of my contact listener, but is it safe to tinker with these values?

Also, given the PostSolve event is triggered for a single contact, why is the tangentImpulses field an array? Obviously there is more than one value, but not sure what they correspond to.

If I detect a collision between a "player" and a "barrel" am I ok to just zero-out all values in the tangentImpulses array during the PostSolve event?

Any ideas appreciated.

Cheers,

Jason.
Hey SnowCrash,


I think we're both dealing with the same kind of issue here. My topic is here http://www.box2d.org/forum/viewtopic.php?f=3&t=5042. Let's keep each other up to date.


See later mate,

Antoon
Yep.. looks like it.

If I find anything I'll let you know.
You can probably alter b2ContactSolver to achieve this.

Change this code:
Code:
float32 friction = b2MixFriction(fixtureA->GetFriction(), fixtureB->GetFriction());


To something like:
Code:
extern float32 MyMixFriction(b2Fixture* fixtureA, b2Fixture* fixtureB);
float32 friction = MyMixFriction(fixtureA, fixtureB);
Thanks Erin, I'll take a look.

What would be really nice is if there was a way to inject these sorts of calculations, so for example we would setup the b2World as normal, then inject any specific behavior modifications via external components, similar to the b2ContactListener but in a way that actually altered the behavior of the engine, without changing the core source code.

I have already made some changes to the source in order to satisfy the limitations of the Android platform I am working with, and it's always a bit fiddly doing a merge. It would be great if I could just blindly update from the trunk and have my injected code external to the core engine code.

Have you ever considered adding something like this? It's possible that there just aren't enough cases where this makes sense to warrant the extra work, and it's really only a minor thing. I come from more of a Java background and the whole dependency injection approach is the current bees knees, so maybe I'm just talking rubbish.

Thanks again.
snowcrash wrote:
What would be really nice is if there was a way to inject these sorts of calculations, so for example we would setup the b2World as normal, then inject any specific behavior modifications via external components, similar to the b2ContactListener but in a way that actually altered the behavior of the engine, without changing the core source code.


This sounds unrealistic to me. Box2D is two things: bookkeeping and calculations. The engine would become swiss cheese if I allow every calculation to be modified.

It is much easier for me to provide specific features. Feel free to request features here: http://code.google.com/p/box2d/issues/list. Please try to be specific and provide an example API and usage.
Box2D does go pretty far the other way, though. Pretty much everything in b2Settings.h could be set at runtime.
BorisTheBrave wrote:
Box2D does go pretty far the other way, though. Pretty much everything in b2Settings.h could be set at runtime.


Looking at b2Settings.h I can't see much that should be set at run-time. For example, changing the polygon radius on the fly doesn't seem useful or desirable. Most of these values are really and truly constants.

You will find most physics engines hide these constants never to be seen. Are you now blaming Box2D for being up front about the magic numbers?

Perhaps b2Settings is not a good place to put the friction and restitution mixing functions. I'm open to fixing that.
Hmm, i didn't really mean changed during the running of the world, I just injected at runtime rather than statically compiled. And yeah, the mixing functions are the biggest culprits. The fixed size arrays would also be good to attack (if there are any left?).
It should be easy to provide more generic mixing functions that just take the two fixtures as arguments. It should also not be too difficult to support variable size polygon arrays. For the latter, I'd like a bit more compelling use case before I change it. As always, issues should be filed because I quickly forget about forum discussions.

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