b2Distance.Distance() problem

Hi, I'm still exploring the possibilities of solving the problem stated in this topic: viewtopic.php?f=3&t=5066&start=0

I want to have a player body that collects and reacts to contacts collected from the world bodies, but itself does not change their movement. Like the mass difference between those bodies was infinite - big bodies being dynamic when interacting with one another, but static when interacting with player body. I want to process player contacts myself, solve them simply by manipulating player position and if they are unsolvable report that the player has been squished.

My current approach goes like this:
-disable contacts concerning player body via contact listener
-collect them by GetContactList()
Then, for each contact:
-get the normal vector from contact manifold
-get the distance between the center of players body and the other shape
-if the distance is lower than player's body radius, move the body according to normal vector.

(what do you think of this approach? If you can point me to a better solution I would be thankful. It seems a bit hacky to me :/)

I used the distance test from the C++ TestBed as a base. To get distance between player's body center and other body, I have a dummy b2CircleShape, that has radius = 0 and transform copied from PlayerOuter body (BTW, is there a simpler way of doing that? Without the dummy shape.).

(in code below X is a b2ContactEdge extracted by PlayerOuter.GetContactList())
Code:
var distIn: b2DistanceInput = new b2DistanceInput();

distIn.transformA = X.contact.GetFixtureA().GetBody().m_xf;
distIn.transformB = PlayerOuter.m_xf;

var proxyA: b2DistanceProxy = new b2DistanceProxy();
proxyA.Set(X.contact.GetFixtureA().GetShape());
var proxyB: b2DistanceProxy = new b2DistanceProxy();
proxyB.Set(X.contact.GetFixtureB().GetShape());
               
distIn.useRadii = true;
var distOut: b2DistanceOutput;
var cache: b2SimplexCache = new b2SimplexCache();
cache.count = 0;
               
b2Distance.Distance(distOut, cache, distIn);
               
trace(distOut.distance);

This reports a null object reference for b2Distance.Distance(distOut, cache, distIn).
Code:
at b2Simplex/ReadCache()[D:\Praca\Programowanie\Flash\Projekty\ProtoBox\src\Box2D\Collision\b2Simplex.as:80]
at Box2D.Collision::b2Distance$/Distance()[D:\Praca\Programowanie\Flash\Projekty\ProtoBox\src\Box2D\Collision\b2Distance.as:56]

It either is a bug, or I am doing something wrong. Any suggestions?
Not sure what your problem here is - you'd have to debug into it. Looks like you have a null shape at some point?

Anyway, I think there are better ways. Firstly, you can use b2Contact.IsTouching (which has already been determined) to cull contacts you aren't interested in.
Secondly, your desired behaviour is as you described "as if non-players have infinite mass". It would be easier to hack the engine to achieve this affect. Go into b2ContactSolver, and where it reads the masses of the bodies, you need to change the values appropriately (i.e. something will be needed to be set to zero as we deal with 1/mass for most calculations). You are still on your own for determining when you are crushed though.
Null shape? I'm checking if the contact manifold (between outerplayer body and body in the world) has at least two points, so there always is at least one shape (body in the world). The other shape is a dummy b2CircleShape with zero radius that is submitted to a distance function with transform taken from outerplayer body. So I don't see where shape could be null.

Fortunately, the other solution - proposed by Erin in the mentioned topic - is working now, so all this hacking around using b2Distance is not necessary now.

I'm extracting the contact normals from manifold and then use my own code (based on b2PositionSolverManifold) to get the penetration depth of my circle. From this I will compile a list of constraints - each collision would be simplified to an edge, which cannot be passed by my player circle - and solve them iteratively. This will provide a valid position for a player body. If after a certain number of iterations at least one constraint is not solved completely (with some margin perhaps) it means that the player has been squished.

The solution you mention - setting the invMass (and invI) - was also mentioned by Erin in my previous topic. I didn't manage to make it working properly. I looked of all occurrences of invMass and invI and each time these were used, I checked for the flag on the bodies and zeroed them on one body (big body, ie. not the player). It turned out weird - player body became sticky and required significantly more force to be removed from the surface of the other bodies; most of the time player body did not push other bodies around, which was a good thing, but then again in some situations it did.

Making it work properly would require in-depth understanding of how all the algorithms in box2d work. It's much easier just to disable player contacts, extract collision data and solve them with my own code.

Thanks for your answer, anyway.
Thanks for the great work on the box2das3 even more 🙂.
You forgot to set the proxy variables in distIn. Then it should work.

Code:
distIn.proxyA = proxyA;
distIn.proxyB = proxyB;

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