a load of shapes - possible?

heyoha again,

i have an image which i want to transform into a b2body 😉
i thought about reading the pixel values and add a shape of a box at every position where the image's alpha value isn't 0.

it compiles and runs, and my body is even colliding with the ground, but when i add a second body to my world
- the two bodies do not collide and
- the programm just exits after a few seconds

so my question: is it possible to add a boxshape for every non-transparent pixel of an image to the body-def?

this is the code
Code:
SDL_Surface* test = IMG_Load("test.png");;
   Uint8 r,g,b,a;

   for(size_t y = 0; y < test->h; y ++)
      for(size_t x = 0; x < test->w; x ++)
      {
         SDL_GetRGBA(GetPixel(test, x, y), test->format, &r, &g, &b, &a);
         printf("R: %d, G: %d, B: %d A: %d at %d/%d\n", r, g, b, a, x, y);

         if(a > 0)
         {
            b2BoxDef boxDefTest;

            boxDefTest.extents.Set(1.0f / 2, 1.0f / 2);
            boxDefTest.density = 0.0f;
            boxDefTest.friction = friction;
            boxDefTest.density = density;
            boxDefTest.restitution = restitution;
            boxDefTest.localPosition.Set(x - test->w / 2, y - test->h / 2);

            groundBodyDef.AddShape(&boxDefTest);
         }
      }

      groundBodyDef.position.Set(position.x, position.y);
   }
b2Body* dynamicBody = m_world->CreateBody(&groundBodyDef);
I don't think that's likely a good solution to your problem - you're trying to create thousands of shapes on a body, and the engine isn't really built to handle that many things. The way sweep and prune works, every time two of those shapes cross over each other on an axis (which will happen millions of times a second if the thing is rotating), the broadphase needs to do work so that a list is maintained in sorted order. That's a lot of activity to handle a single shape! Plus, you're probably crashing because there's a limit to the number of proxies allowed.

The best solution would be to extract the edges of the body and decompose those into convex shapes. If that's too much work (edge finding isn't actually all that difficult, so you might do some Googling before you discard this idea), then at the very least you should try to merge as many pixels as possible together into single, larger boxes, before adding the shapes. You'll still end up with a lot of tiny 1-pixel boxes at the edges, which is not ideal, but most of the interior at least will be covered by just a few shapes.

How big are the images you're working with, btw?
hmmm... i already thought that this might be the problem...

anyway... i'm now playing around with your convex-functions (made it working with vc 6) and looking for an edge-finding logarithm. guess that this may be the best solution then.

thanks! 🙂
Although this is pascal rather than c/c++, there may be something you can get from the code here http://www.partow.net/projects/fastgeo/index.html

I use this code to generate convex hulls for images which I them pass as shapes to a dll built using Box2D.
Heh... I went down this path already (viewtopic.php?f=3&t=192) and decided to take an easier route. You can try loading up your image as a background, and then create a tool for placing vertices so you can trace your image by hand, and then run them through ewjordan's convex shape decomposer.
darkzerox wrote:
Heh... I went down this path already (viewtopic.php?f=3&t=192) and decided to take an easier route. You can try loading up your image as a background, and then create a tool for placing vertices so you can trace your image by hand, and then run them through ewjordan's convex shape decomposer.

great idea! i'll load the image, write a simple editor for the letter's coordinates and save the coordinates in a file. that will save a lot of time! 🙂
okay... i'm back 😉

i'm using the convex decomposer now. but unfortunately my game just exits (right after a m_world->createbody call) when i change the b2_maxPolyVertices variable in b2Settings.h (i need to do so since i need more vertices for a few objects...). strangely enough my game doesn't exit when i start it with the windows command-line.

i'm currently loading and saving the positions of a file. it works, but only when i don't change the b2_maxPolyVertices-value or something:

Code:
std::vector<b2Vec2> positions;
   if(LoadElementPositions(m_editElement, positions))
   {
      b2Vec2 *pos = new b2Vec2[positions.size()];

      for(size_t i = 0; i < positions.size(); i++)
      {
         pos[i] = positions[i];
         pos[i].x *= scale;
         pos[i].y *= scale;
      }

      polyDef.friction = friction;
      polyDef.density = density;
      polyDef.restitution = restitution;

      b2Polygon pgon(pos, positions.size());
      b2PolyDef* deleteMe = DecomposeConvexAndAddTo(&pgon, &groundBodyDef, &polyDef);
      delete[] deleteMe;
      delete[] pos;

      groundBodyDef.position.Set(position.x, position.y);
   }
By increasing the number of polygon vertices you might be exceeding the largest object size supported by the allocator. Try running in debug mode so you can see the assertion.
Hierro wrote:
i'm using the convex decomposer now. but unfortunately my game just exits (right after a m_world->createbody call) when i change the b2_maxPolyVertices variable in b2Settings.h (i need to do so since i need more vertices for a few objects...).
[/code]



Nope. ewjordan's convex shape decomposer will make sure each shape has less than b2_maxPolyVertices. Just stuff all your vertices into an array of b2Vec2's, or an stl vector, then pass em off to the decomposer, and it'll do all the work.
Something could be going wrong with the decomposition, still, depending on both the version of Box2d and the version of the decomposition files - the earlier Box2d versions are a bit more lenient, and the earlier decomposition versions are less careful about fulfilling Box2d's requirements on shapes (even the latest version is not anywhere near perfect). Try debugging, and let's see exactly where the problem is; if it has something to do with angle or volume, it's likely my error and I'd like to track it down.

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