Question

I just started playing with a very simple game and I do not want to create sprites images for every single body,I just need to colour them with different colours.

As per this thread, ShapeRendere seems to be not a very good idea and it is more for development: When to use ShapeRenderer, Mesh + SpriteBatch, Box2D and Scene2D in Libgdx?

Also, tint can be only be used on textures which require images as well: https://github.com/libgdx/libgdx/wiki/Spritebatch,-textureregions,-and-sprite

This is one the bodies I am trying to colour:

          //Dynamic Body  
      bodyDef = new BodyDef(); 
      //bodyDef.gravityScale
      bodyDef.type = BodyType.DynamicBody;  
      bodyDef.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2);  
      body = world.createBody(bodyDef);  
      CircleShape dynamicCircle = new CircleShape();  
      dynamicCircle.setRadius(10f);  
      FixtureDef fixtureDef = new FixtureDef();  
      fixtureDef.shape = dynamicCircle;  
      fixtureDef.density = 2.5f;  
      fixtureDef.friction = 0.25f;  
      fixtureDef.restitution = 0.75f;  
      body.createFixture(fixtureDef);  

Any ideas?

Was it helpful?

Solution

I suggest not using Sprites directy, but instead turn to scene2d and its Actors. The libgdx wiki already perfectly describes how to do this then.

You would create a class that extends Actor (or Group) and that renders the texture you want.

When creating your box2d bodies, you would set the bodies' user data to the actor via setUserData(actor);

Now after simulating the box2d world with step(), you just need to iterate over the bodies in the world, access the actor through getUserData() and update the actor's position and rotatin to the current values of the body. The wiki already contains the complete loop needed for this...

Hope it helps... :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top