Assertion failure when creating multiple bodies in real-time

Hey there,
first of all, thank you very much for Box2d. It works very well.


I integrated it into hge (Haafs Game Engine) and use the "physical" position of the bodies as center for the hge hotspots. This connection is done via a class that is called centity. It contains a dynamic array of pointers to a hgeanimation object (pointers only, not actual objects) and one b2body as actual object. The pointer to the body is not set by centity, but by a class called clevel, which manages tiles and objects.

With this setup, I have done a small game in which I can create bodies dynamically (this I use as a level editor right now). However, after I created a few bodies (amount varies), I get an Assertion error (see screenshot). The same problem I encountered when I tried to delete bodies, reuse the pointer to the body (e.g. for differing animation sizes), and create new bodies with that pointer.

As a compiler, I use bloodshed and MiniGW. I have compiled box2d into a static (.a) library.

My question is, could this be a box2d error or is it more likely my insufficient programming skills? Where am I to look first?
Your help is very much appreciated, cheers, hawkei
You have too many overlapping shapes. Perhaps you are creating a bunch of bodies at the origin and they are all overlapping. Look at the value for b2_maxPairs and see if it is enough for your game.
Hi Erin,

thank you very much for your fast reply! I changed the number as you recommended, but now the program just "stops working" without producing an error message after about the same number of centities as before. This now feels more like an error I produced self, so I shall look into it further.

One question remains, however, and I fear this might be connected to me understanding the shape concept wrongly. I use a tile engine , as I have read another forum user also does. However, the tiles I create do not overlap, because I create a tile each second meter (meaning a static body of 1x1 meter at 1 meter, 3 meters, 5 meters, and so on). In my imagination this should prevent shape overlapping, unless I understood the concept wrongly. Can you bring light into that matter?

Thank you & best wishes
hawkei
Hmm, I think static bodies can still create pairs if their AABBs overlap. But it sounds like this doesn't apply in your case.
I'm having the exact same error after creating about 20 boxes (sometimes 18, sometimes 25). I create them with mouseClicks, so they're 'spawned' in different places.

I've only used c++ for a week, so most likely a lot of it is messy and not the way it's supposed to be, but I'll attach the code I'm using. Maybe it can help you solve the problem.
HarryVermeulen: I didn't run your code yet, but you have a fixed size array of size "makeboxes = 20". There is no code to prevent numberOfBoxes from exceeding 20, after which your are getting a buffer overrun.

http://en.wikipedia.org/wiki/Buffer_overflow
Erin Catto wrote:
HarryVermeulen: I didn't run your code yet, but you have a fixed size array of size "makeboxes = 20". There is no code to prevent numberOfBoxes from exceeding 20, after which your are getting a buffer overrun.

http://en.wikipedia.org/wiki/Buffer_overflow


hmm, I see I uploaded a half edited file hehe. I had an if-statement at the makeBox function to keep the boxes under 15 (so it wouldn't crash). If I put the max array size at 100 and remove the if-statement, it still crashes between 18 and 25 boxes.
Hey HarryVermeulen,

I tried out your example, and managed to find and fix the problem. (:

Image

I debugged the program and found it was reaching the pair limit of 4096 pairs and Asserting false when it tried to create the next pair.
The reason it has 4096 pairs with only 15-25 boxes, is you aren't recreating the bodyDef each time you created a new body with a mouse click.
This is a problem because you are adding a new shape to the bodyDef each time, so by the 20th body, you have 20 small boxes packed in to that one body and with all these bodies with all those boxes packed in to that tight space, the pair count begins to grow very very quickly.
This is also why it was running horribly slow. :P

You can solve the problem nice and easy by taking your bodyDef variable out of global scope and placing it in the makeBox function, like so:
Code:
void makeBox(float32 x, float32 y, float32 size) {
   b2BodyDef bodyDef;
   boxDef.extents.Set(size, size);
   boxDef.density = 1.0f;
   boxDef.friction = 0.3f;
   boxDef.restitution = 0.3f;
   bodyDef.position.Set(x, y);
   bodyDef.AddShape(&boxDef);
   body[numberOfBoxes] = world->CreateBody(&bodyDef);   
   bodySize[numberOfBoxes] = size*10;
   numberOfBoxes++;
}


If you don't already know how, I suggest learning how to use your debugger, it is very useful for solving problems like this one.

It is likely you have a similar problem Hawkei, but if you can post your code it will be easier to help you track down the source of the problem.

All the best,
-Matt
Wauw, that's awesome. You're right, I have to learn how to use the debugger, just started with xcode/c++. Thanks a lot for your help! and just call me Harry 😉
Dear Matt and Erin,
thanks for your help! In fact, the combination of your suggestions helped me solve the problem posted here and another one of my problems. Now I can continue working on the game.
All the best for your work too, Harry!
Cheers, hawkei

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