debug draw and polygon

General discussion about Box2D tutorials
FLYNSXCOOL
Posts: 25
Joined: Tue Sep 19, 2017 4:30 pm

debug draw and polygon

Post by FLYNSXCOOL »

Code: Select all

SDL_Point worldToPixel(b2Vec2 worldLocation)
{
	SDL_Point p;

	p.x = worldLocation.x * SCALE - camera.x;
	p.y = worldLocation.y * SCALE - camera.y;

	return p;
}

Code: Select all

void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
   // RUBEScene* scene = getCurrentScene();
    SDL_Renderer* renderer = getRenderer();

    Sint16 xs[8];
    Sint16 ys[8];

    int32 i;
    for (i = 0; i < vertexCount; ++i) {
      //  SDL_Point p = scene->worldToPixel( vertices[i] );
           SDL_Point p = worldToPixel(vertices[i]);

        xs[i] = p.x;
        ys[i] = p.y;
    }

    polygonRGBA(renderer, xs, ys, vertexCount, (uint8)(color.r*255), (uint8)(color.g * 255), (uint8)(color.b * 255), SDL_ALPHA_OPAQUE);
}


How get const b2Vec2* vertices and int32 vertexCount ??


Example for draw circle for body (bb16_r):
object made from json file.
b2Body *bb16_r;
bb16_r = (b2Body*)jsonR.getBodyByName("service_wheel");

Code: Select all

void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
{
   
    SDL_Renderer* renderer = getRenderer();

    const float32 k_segments = 16.0f;
    const float32 k_increment = 2.0f * b2_pi / k_segments;
    float32 theta = 0.0f;

    Sint16 xs[16];
    Sint16 ys[16];

    int32 i;
    for (i = 0; i < k_segments; ++i) {
        b2Vec2 v = center + radius * b2Vec2(cosf(theta), sinf(theta));
        SDL_Point p = scene->worldToPixel( v );
        xs[i] = p.x;
        ys[i] = p.y;
        theta += k_increment;
    }

    polygonRGBA(renderer, xs, ys, (int)k_segments, (uint8)(color.r * 255), (uint8)(color.g * 255), (uint8)(color.b * 255), SDL_ALPHA_OPAQUE);
}

Code: Select all

DrawCircle(bb16_r->GetPosition(), bb16_r->GetFixtureList()->GetShape()->m_radius, { 255, 0, 0, 0 });
FLYNSXCOOL
Posts: 25
Joined: Tue Sep 19, 2017 4:30 pm

Re: debug draw and polygon

Post by FLYNSXCOOL »

i found
but this is old code, the new version of Box2D a little differently

Code: Select all

for( b2Shape *shape = body->GetShapeList(); shape; shape = shape->GetNext() )
{
   if( shape->GetType() == e_polygonShape )
   {
      b2PolygonShape *poly = (b2PolygonShape*)shape;

      int count = poly->GetVertexCount();
      b2Vec2* verts = (b2Vec2*) poly->GetVertices();

      for( int i = 0; i < count; i++ )
      {
         verts[i] = body->GetWorldPoint( verts[i] );
      }
strange... but I tried and I couldn't make it, probably somewhere made a mistake. :roll: :roll: :roll: :roll:
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: debug draw and polygon

Post by iforce2d »

It's difficult to make a helpful answer if you only say "couldn't make it" because that gives no useful information. Please be more specific, like what is the exact error message etc...

Looks like Erin has been working on Box2D in the last few months. In this commit the GetVertexCount function was removed for some reason:

https://github.com/erincatto/Box2D/comm ... 55536376a9

Maybe you can ask him why he would do that, it makes no sense to me.

Try an older version of Box2D is the first suggestion. As a rough guide, the b2dJson stuff was written in 2012. I used Box2D version 2.3 or so at that time. You could also just add back the functions as you see in that github diff that were taken out.
FLYNSXCOOL
Posts: 25
Joined: Tue Sep 19, 2017 4:30 pm

Re: debug draw and polygon

Post by FLYNSXCOOL »

Code: Select all

	 for (b2Body* it = World.GetBodyList(); it != 0; it = it->GetNext())
	 {
		 for (b2Fixture *fixture = it->GetFixtureList(); fixture; fixture = fixture->GetNext())
		 {
			 b2Shape *shape = (b2Shape*)fixture->GetShape();
			 if (shape->GetType() == b2Shape::e_polygon) // 
			 {
				 b2PolygonShape* poly = (b2PolygonShape*)shape;
				 int count = poly->GetVertexCount();
				 b2Vec2* vertices = (b2Vec2*)poly->m_vertices;
				 for (int i = 0; i < count; i++)
				 {
					 vertices[i] = it->GetWorldPoint(vertices[i]);
				 }
				 DrawPolygon(vertices, count, { 255, 0, 0, 0 });
			 }

			 if (shape->GetType() == b2Shape::e_circle)  //
			 {
				 DrawCircle(fixture->GetBody()->GetPosition(), fixture->GetShape()->m_radius, { 255, 0, 0, 0 });
			 }
			 
		 }
	 }

and I got this

Image

https://i.yapx.ru/cQOv.gif full screen
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: debug draw and polygon

Post by iforce2d »

How about just drawing one single thing at a time, I don't see how you can debug a crazy situation with dozens of things all moving around that fast. Start small.

Questions about rendering are not really the topic for this forum. The method will depend on your rendering API and there are many different cases. If you are using SDL2, maybe you could try the example I made:
https://www.iforce2d.net/rube/?panel=loaders
FLYNSXCOOL
Posts: 25
Joined: Tue Sep 19, 2017 4:30 pm

Re: debug draw and polygon

Post by FLYNSXCOOL »

I try another code:

Code: Select all

if (shape->GetType() == b2Shape::e_polygon) // Если полигон
			 {
				 b2PolygonShape* poly = (b2PolygonShape*)shape;
				 int count = poly->GetVertexCount();
				 
				 b2Vec2* vertices= (b2Vec2*)poly->m_vertices;
				 for (int i = 0; i < count; i++)
				 {

					 vertices[i] = poly->GetVertex(i);
					 
				 }


				 debugDraw.DrawPolygon(vertices, count, { 255, 0, 0, 0 });
				 
			 }
Image
FLYNSXCOOL
Posts: 25
Joined: Tue Sep 19, 2017 4:30 pm

Re: debug draw and polygon

Post by FLYNSXCOOL »

How about just drawing one single thing at a time, I don't see how you can debug a crazy situation with dozens of things all moving around that fast. Start small.
I did a lot of things for clarity. One object of the same flies
FLYNSXCOOL
Posts: 25
Joined: Tue Sep 19, 2017 4:30 pm

Re: debug draw and polygon

Post by FLYNSXCOOL »

Questions about rendering are not really the topic for this forum. The method will depend on your rendering API and there are many different cases. If you are using SDL2, maybe you could try the example I made:
https://www.iforce2d.net/rube/?panel=loaders

I did it using your example. But in your example there is no drawing of the polygon. You drew a point and a segment.

Code: Select all

            debugDraw.DrawPoint(p1, 4.0f, c);
            debugDraw.DrawPoint(p2, 4.0f, c);

            c.Set(0.8f, 0.8f, 0.8f);
            debugDraw.DrawSegment(p1, p2, c);
FLYNSXCOOL
Posts: 25
Joined: Tue Sep 19, 2017 4:30 pm

Re: debug draw and polygon

Post by FLYNSXCOOL »

it would be great if you added in the example drawing the polygon
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: debug draw and polygon

Post by iforce2d »

Drawing lots of things does not help clarity. Just draw one.

If you run the SDL2 example you can see I already did polygons, look at it:
rube SDL2_091.png
rube SDL2_091.png (15.38 KiB) Viewed 30069 times
Please look at the source code (DebugDraw.cpp), all shapes are already implemented.
Post Reply