Creating Multiple Bodies in the same place - bad collisions?

Hi All,

I have been doing some work linking Box2D with OpenFrameworks and OpenCV to allow for people to create their own simulation by moving in front of a camera, everything is moving along nicely, but when I start allowing the creation of multiple bodies in the same frame in very similar locations, I am crashing.

Is there a limit on how many bodies can be created simultaneously in the same location? Is lots of interpenertration bad?

I found I could get the PolyShapes test to crash if I changed (in PolyShapes.h, line 135) from:

Code:
void Keyboard(unsigned char key)
   {
      switch (key)
      {
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
         CreateBody(key - '1');
         break;

      case 'd':
         DestroyBody();
         break;
      }
   }



To:

Code:
void Keyboard(unsigned char key)
   {
      switch (key)
      {
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
         for(int bodyCount = 0; bodyCount < 32; bodyCount++)
{
    CreateBody(key - '1');
}
         break;

      case 'd':
         DestroyBody();
         break;
      }
   }


If I tap away at my keys, I can get a crash fairly quickly.
Do you have the crash message and call stack?
Hey Erin,

Here you go.

Code:
   TestBed.exe!_NMSG_WRITE(int rterrnum=10)  Line 198   C
   TestBed.exe!abort()  Line 59 + 0x7 bytes   C
   TestBed.exe!_wassert(const wchar_t * expr=0x004f4848, const wchar_t * filename=0x004f47f0, unsigned int lineno=133)  Line 212   C
   TestBed.exe!b2PairManager::AddPair(int proxyId1=20, int proxyId2=279)  Line 133 + 0x35 bytes   C++
   TestBed.exe!b2PairManager::AddBufferedPair(int id1=20, int id2=279)  Line 214 + 0x10 bytes   C++
   TestBed.exe!b2BroadPhase::CreateProxy(const b2AABB & aabb={...}, void * userData=0x00a58a6c)  Line 307   C++
   TestBed.exe!b2PolyShape::b2PolyShape(const b2ShapeDef * def=0x00a5cad8, b2Body * body=0x003e689c, const b2Vec2 & newOrigin={...})  Line 500 + 0x16 bytes   C++
   TestBed.exe!b2Shape::Create(const b2ShapeDef * def=0x00a5cad8, b2Body * body=0x003e689c, const b2Vec2 & center={...})  Line 217 + 0x35 bytes   C++
   TestBed.exe!b2Body::b2Body(const b2BodyDef * bd=0x0012f7d0, b2World * world=0x00a10068)  Line 114 + 0x17 bytes   C++
   TestBed.exe!b2World::CreateBody(const b2BodyDef * def=0x0012f7d0)  Line 79 + 0x31 bytes   C++
   TestBed.exe!PolyShapes::CreateBody(int index=2)  Line 118 + 0x12 bytes   C++
   TestBed.exe!PolyShapes::Keyboard(unsigned char key='3')  Line 147   C++
   TestBed.exe!Keyboard(unsigned char key='3', int x=310, int y=247)  Line 152 + 0x18 bytes   C++
   TestBed.exe!glui_keyboard_func(unsigned char key='3', int x=310, int y=247)  Line 299 + 0x16 bytes   C++
   TestBed.exe!fgWindowProc(HWND__ * hWnd=0x00010452, unsigned int uMsg=258, unsigned int wParam=51, long lParam=262145)  Line 1683 + 0x3b bytes   C
   user32.dll!7e418734()    
   [Frames below may be incorrect and/or missing, no symbols loaded for user32.dll]   
   user32.dll!7e418816()    
   user32.dll!7e41b4c0()    
   user32.dll!7e4586ed()    
   user32.dll!7e418816()    
   ntdll.dll!7c90eae3()    
   user32.dll!7e4194be()    
   user32.dll!7e466615()    
   user32.dll!7e427cbc()    
   user32.dll!7e41c665()    
   opengl32.dll!5ed2699b()    
   nvoglnt.dll!697351e0()    
   user32.dll!7e418734()    
   user32.dll!7e418816()    
   user32.dll!7e4189cd()    
   user32.dll!7e42e051()    
   user32.dll!7e4196c7()    
   TestBed.exe!glutMainLoopEvent()  Line 991 + 0xc bytes   C
   TestBed.exe!glutMainLoop()  Line 1053   C
   TestBed.exe!main(int argc=1, char * * argv=0x003e37e8)  Line 286   C++
   TestBed.exe!__tmainCRTStartup()  Line 318 + 0x19 bytes   C
   TestBed.exe!mainCRTStartup()  Line 187   C
   kernel32.dll!7c816fd7()    


Should I be checking for "space" in the simulation before adding a Body?

Joel
So you are reaching this assertion:

Code:
b2Assert(m_pairCount < b2_maxPairs && m_freePair != b2_nullPair);


There is a finite number of collision pairs available as specified by b2_maxPairs. If you need more collision pairs, you should edit b2Settings.h.

What computer/OS are you running? Did you get that assertion message?
Hi Erin,

Yes I did get to that assertion. I will try changing the settings file.

I am running on XP, using Visual Studio 2005.

Best,

Joel
I should add that when you create a bunch of overlapping shapes, there is a quadratic growth in the number of collision pairs required. So um, try not to do that. 🙂
Hi Erin,

Thanks for the response. The problem is that multiple overlapping shapes may occassionally occur, as I am spawning objects based on peoples movement. Is there any way to stop processing if we get close to the maximum pairs? It would be better for the program to keep running, albeit without total accurary, rather than crashing when the assertion failed.

At the moment, the maximum pairs is set to 8 times the maximum number of proxies:

Code:
(from b2settings.h)
// Collision
const int32 b2_maxManifoldPoints = 2;
const int32 b2_maxShapesPerBody = 64;
const int32 b2_maxPolyVertices = 8;
const int32 b2_maxProxies = 512;            // this must be a power of two
const int32 b2_maxPairs = 8 * b2_maxProxies;   // this must be a power of two


I currently have maxProxies set to 1024 in my code.

How would you suggest I monitor the number of current pairs? And how should I stop Box2D evaluating if we get near to the number of maxPairs?
In your case it may make sense to stop responding to collisions, but in general it is really bad because objects may begin to fall through the ground, walls, etc.

You can access the current number of pairs in b2PairManager.

Instead of increasing the number of proxies, you should increase the number of pairs. Since you want to have quadratic interaction, set:
Code:
const int32 b2_maxPairs = (b2_maxProxies >> 1) * b2_maxProxies


This can be quite a bit of memory. Each pair is 12 bytes. If you have 512 proxies, then you get 256*512 = 131072 (2^17). This is about 1.5MB of memory for pairs.

I suggest trying to limit the rate of overlapped shape creation.

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