2
\$\begingroup\$

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

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html

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????

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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();
shape.setRadius(radius);
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

world.step(...)

Then you draw your body

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

shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(someColor);
shapeRenderer.circle(pos.x, pos.y, radius);
shapeRenderer.end();

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.

\$\endgroup\$

You must log in to answer this question.

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