Missing contacts

Sunken
Posts: 9
Joined: Thu Oct 28, 2010 1:40 pm

Missing contacts

Post by Sunken »

I want to use Bullet to analyse static forces between simple objects in a randomly generated scene.
I random-generate some boxes and wait until all objects have gone to sleep, whereupon I want to read all static contact forces.
However, I seem to be getting consistently too few contacts.

I'm using the Wiki code snippet:

Code: Select all

  int numManifolds = world->getDispatcher()->getNumManifolds();
  for (int i=0;i<numManifolds;i++)
  {
    btPersistentManifold* contactManifold =  world->getDispatcher()->getManifoldByIndexInternal(i);
    btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
    btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());

    int numContacts = contactManifold->getNumContacts();
    for (int j=0;j<numContacts;j++)
    {
      btManifoldPoint& pt = contactManifold->getContactPoint(j);
      if (pt.getDistance()<0.f)
      {
    const btVector3& ptA = pt.getPositionWorldOnA();
    const btVector3& ptB = pt.getPositionWorldOnB();
    const btVector3& normalOnB = pt.m_normalWorldOnB;

      }
    }
  }
...but where I'd expect numContacts to almost always be 4, given that all I have are boxes resting on each other, I see consistently fewer contacts; sometimes zero contacts. The forces (impulses) I get are also insufficient to account for the resting state of the boxes. The simulation behaves just as it should, as far as I can see from visualization (using the demo framework), so I'm assuming I'm just doing something wrong reading the contact information, but I can't figure out what.
I've tried the above code both in the internal tick callback and after finishing a stepSimulation call; the results are identical. I've added code to wake the boxes up and simulate once, as well as 4 times, just in case the sleeping is causing the anomaly, but that changed nothing either.

I'm new to Bullet so there's probably something trivial I'm missing. Ideas?
Sunken
Posts: 9
Joined: Thu Oct 28, 2010 1:40 pm

Re: Missing contacts

Post by Sunken »

Did I post this in the wrong forum? Or is there genuinely no one who has some idea why I get these strange results?
Shouldn't there always be at least 3 contacts when a box is resting on another?
Dirk Gregorius
Posts: 861
Joined: Sun Jul 03, 2005 4:06 pm
Location: Kirkland, WA

Re: Missing contacts

Post by Dirk Gregorius »

The contact points can also have a small positive distance. A negative distance means penetration, but Bullet also deals with so call "shallow contacts" where closest points are within some margin (usually 0.04). You don't have to check the distance yourself. I would *assume* that contact points are discarded if the distance is larger than the sum of the margins.
Sunken
Posts: 9
Joined: Thu Oct 28, 2010 1:40 pm

Re: Missing contacts

Post by Sunken »

Thanks for responding. Yes, I know contacts are "sticky" in this manner for stability.
However, it seems to me that *all* contacts couldn't possibly be at a distance > 0 in a stable configuration - there should be 0 contact force in that case and the box should be accelerated by gravity.

The critical question: Can I or can't I trust Bullet to provide me (somehow, not necessarily by the above method) an approximately correct contact profile (points and forces) for a static scene? (E.g., less than 3 contact points for a cube resting on a surface is not approximately correct. I know I'm not getting more than 4 contact points max, that's OK.)
The answer's important to me - it's basically the reason I'm doing this physics simulation at all. If it's "no" I'll have to use something else than Bullet, though I'd prefer not to since I've already written this code now...
Sunken
Posts: 9
Joined: Thu Oct 28, 2010 1:40 pm

Re: Missing contacts

Post by Sunken »

Actually, it seems that if I place the code in the tick callback and activate the bodies explicitly beforehand and don't filter on distance, it appears to work. Seems i hadn't combined those prerequisites earlier.
Once the callback has read the contact info, running the same code after the simulation step also yields correct results even though it doesn't without the callback. That threw me a little.
Anyway, hopefully this should be all I need.
Dirk Gregorius
Posts: 861
Joined: Sun Jul 03, 2005 4:06 pm
Location: Kirkland, WA

Re: Missing contacts

Post by Dirk Gregorius »

However, it seems to me that *all* contacts couldn't possibly be at a distance > 0 in a stable configuration - there should be 0 contact force in that case and the box should be accelerated by gravity.
I don't think this is correct. The contact points can have positive distances. This basically means that their "shells" are touching. Erwin gave two presentation about contact point creation here:
http://code.google.com/p/bullet/downloa ... f&can=2&q=
http://code.google.com/p/bullet/downloa ... f&can=2&q=
Can I or can't I trust Bullet to provide me (somehow, not necessarily by the above method) an approximately correct contact profile (points and forces) for a static scene?
What do you try to achieve? Bullet will give you contact forces that yield approximately correct dynamic behavior. The reason is that we usually deal with over-constrained systems and the iterative Gauss-Seidel solver tends to divide the error equaly. In static analysis for over-constrained systems you usually use elasticity theory ("Arbeitssatz" is the german word for it - sorry I don't know the english expression).

An example:
Imagine a box resting on a plane. The correct result would be F(i) = m*g/4 at each contact point. A static equilibrium can be reached also with F = m*g/2 for two diagonal points while the oposing two diagonal points have F = 0. Assume now that you have a static construction under the box. The Gauss-Seidel solver might find any of these combinations. So if you are unlucky some elements might not see any of the weight of the box. Do you understand what I want to say? I think B. Mirtich talks about these issues in his PhD or other publications. You might want to take a look.
Sunken
Posts: 9
Joined: Thu Oct 28, 2010 1:40 pm

Re: Missing contacts

Post by Sunken »

I was seeing cases where nContacts was 0, 1 or 2, which is just incorrect, but again I've gotten rid of that now - it was basically me querying improperly as I suspected.

As for the number of contacts with a positive force value, that's of less importance to me. For now, what I want is just:
* Contact manifold geometry, e.g. the shape of the contact patch
* Total force acting between the bodies; not per-contact and not torque

so I should be all right, I think. Except I'd prefer the entire manifold, not just a 4-point approximation, but I'm hoping it will do for my purposes.

As you say, solving for forces is an underdetermined problem with perfectly rigid bodies and as few contact forces as 2 can be a solution for a box on a plane. It seems unstable, though - is it likely to occur in practice in Bullet? Provided the box has been bouncing around and such. What I see now are 4 non-zero forces - not always all equal. As long as the sum is OK I'll be fine.

Question: I'd guess slight perturbations might help fill out the contact manifold - is that correct? Anyone tried it? (This is done under the hood already, isn't it?)
Dirk Gregorius
Posts: 861
Joined: Sun Jul 03, 2005 4:06 pm
Location: Kirkland, WA

Re: Missing contacts

Post by Dirk Gregorius »

Pertubation is one way to go and I think there is some support for this in Bullet. Maybe experimental - I am not sure. Maybe Erwin can answer this. The other solution is clipping. Basically you find the two most parallel faces on each shape to the current separating axis and clip one face against the side planes of the other.

Jan Bender should have something like this here:
http://www.impulse-based.de/

IIRC he also uses Bullet so you can maybe hijack this directly for your needs.


Erin Catto has written about this as well. Look through his GDC presentations here:
http://code.google.com/p/box2d/downloads/list

Most presentations are about constraint solving, but one is particulary about contact creation (around 2007).