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
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);
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);