I want to color polygon of box2d in LibGDX. Found below useful class for that.


But, it is not coloring the body instead making colored shapes. I want colored bodies having all the property like gravity, restitution etc. In brief, I want to make colored ball and surface.And i don't want to attach sprite on bodies. Want just fill color in bodies.

Need some guidance????


1 Answer 1


You have to separate your physics simulation (bodies) from the graphical representation of the physics world.

Lets assume you have a Box2d body defined like this

float radius=3.0f;
CircleShape shape=new CircleShape();
FixtureDef fd = new FixtureDef();
fd.shape = shape;
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;

Body body=world.createBody(bd);

Now in the render() method your first step the physics world


Then you draw your body

Vector2 pos = body.getWorldCenter();
float angle = body.getAngle(); //if you need rotation

shapeRenderer.circle(pos.x, pos.y, radius);

The same goes for all bodies (static and dynamic) in the physics world you want to render.

Of course you have to set up the physics world, camera and so on properly first.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .