0
\$\begingroup\$

In the PolygonShape object, there is the method setAsBox() which allows for creating rectangles easily, but there is no getAsBox() for cases where you know the polygon is a box. I know Box2D comes with it's own renderer, but I need to make custom renders.

Also, PolygonShape objects have a getVertex() method, but it has no return, and it takes a second argument of a vector2, for some reason. I don't know how I'm supposed to get a vertex from that. Why isn't there just a method that returns an array of Vector2 objects?

How can I easily render Box2D polygons with ShapeRenderer in LibGDX?

\$\endgroup\$
0
\$\begingroup\$

The Vector2 parameter of getVertex(Vector2); is the output of the method (I.e. the vertex). I believe you have to supply your own Vector2 so you can limit garbage (by using pooling or something else).

This means that you should use the supplied Vector2 as your vertex when drawing. You can then use this Vector2 and drawLine(); to draw your shape.

\$\endgroup\$
0
\$\begingroup\$

It depends on what you want to achieve.

If what you're looking for is to, for debugging purposes, see the position of all your bodies you can use the Box2DDebugRenderer which takes a World and a transform matrix and renders a lot of information for you (it will for example show you contacts and the difference between a static and a dynamic body).

Box2DDebugRenderer renderer = new Box2DDebugRenderer();
renderer.render(world, camera.combined);

If that doesn't fit your purpose you need to iterate over the bodies in the World and then for each Body get all the Fixtures and render their respective Shapes. How to do that is slightly different depending on the type of Shape but for polygons it might look something like this;

void render(Body body, ShapeRenderer renderer) {
  for(Fixture f : body.getFixtureList()) {
    switch(f.getType) {
       case Shape.Type.Polygon: 
       {
         PolygonShape s = (PolygonShape)f.getShape();
         // NOTE: You don't want to be newing this up every time, make sure you keep an array of Vector2s and reuse that!
         Vector2[] vertices = new Vector2[s.getVertexCount()];
         // Build list of transformed vertices
         for(int i = 0; i < s.getVertexCount(); ++i) {
           vertices[i] = new Vector2();
           s.getVertex(i, vertices[i]);
           body.getTransform().mul(vertices[i]);
         }  

         // This assumes a closed polygon, the last vertex is connected with the first.
         for(int i = 0; i < s.getVertexCount(); ++i) {     
           Vector2 a = vertices[i];
           Vector2 b = vertices[(i + 1) % s.getVertexCount()];
           renderer.line(a.x, a.y, b.x, b.y);
         }
       }
       break;
       // And then cover the other types, Circle and Egde here.
    }
  }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.