Hey guys I've been learning to make games with LibGDX. I did a tiled map in Tiled and the rest of code in java using Android Studio. I could make a ball bounce on the flour using Box2D, but I can't make this same ball hit some circular or elliptcal objects from certain Tiled's layer. I've been searching for days, but I'm very newbee to this and I can't understand the explanations the figure this out.
What happens when I click run:
It's showing the the ball image falling next to a circular shape (I think it's made by debug renderer) and the tiled image is shown, but the ball doesn't collide. The ball passes through and nothing happens. I can't see any circular shapes near the tiled image. Maybe the problem is in my for in the code below.
Here is my code:
public class MyBallGame extends ApplicationAdapter {
World world;
Box2DDebugRenderer debugRenderer;
OrthographicCamera cam;
SpriteBatch batch;
Texture ball;
Texture bg;
Body body;
Body circulo;
Sprite sprite;
//TILES
//private TmxMapLoader maploader;
private TiledMap map;
private TiledMapRenderer tiledMapRenderer;
//private OrthogonalTiledMapRenderer renderer;
@Override
public void create () {
//TILES
cam = new OrthographicCamera();
cam.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
cam.update();
map = new TmxMapLoader().load("tiles.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(map);
world = new World(new Vector2(0, -980), true);
debugRenderer = new Box2DDebugRenderer();
batch = new SpriteBatch();
ball = new Texture("circulo.png");
sprite = new Sprite(ball);
// Center the sprite in the top/middle of the screen
sprite.setPosition(100, 400);
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(sprite.getX(), sprite.getY());
// Create a body in the world using our definition
body = world.createBody(bodyDef);
// Create a circle shape and set its radius to 6
CircleShape circle = new CircleShape();
circle.setRadius(6f);
// Create a fixture definition to apply our shape to
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = 1.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.6f; // Make it bounce a little bit
// Create our fixture and attach it to the body
Fixture fixture = body.createFixture(fixtureDef);
circle.dispose();
//Tiled objects:
BodyDef bdef = new BodyDef();
//PolygonShape pshape = new PolygonShape();
FixtureDef fdef = new FixtureDef();
for(MapObject object : map.getLayers().get(1).getObjects().getByType(CircleMapObject.class)){
Circle circ = ((CircleMapObject) object).getCircle();
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(0, 0);
circulo = world.createBody(bdef);
CircleShape cShape = new CircleShape();
cShape.setRadius(circ.radius);
//pshape.setRadius(circ.radius);
//fdef.shape = pshape;
fixtureDef.shape = cShape;
fixtureDef.density = 1.5f;
body.createFixture(fdef);
}
}
@Override
public void render () {
// Advance the world, by the amount of time that has elapsed since the
//last frame
// Generally in a real game, dont do this in the render loop, as you are
//tying the physics
// update rate to the frame rate, and vice versa
world.step(Gdx.graphics.getDeltaTime(), 6, 2);
// Now update the spritee position accordingly to it's now updated
//Physics body
sprite.setPosition(body.getPosition().x, body.getPosition().y);
// You know the rest...
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//TILES
cam.update();
tiledMapRenderer.setView(cam);
tiledMapRenderer.render();
debugRenderer.render(world, cam.combined);
batch.begin();
batch.draw(sprite, sprite.getX(), sprite.getY());
batch.end();
}
}