I'm fairly new to Libgdx and have some good basic knowledge on Java. Right now I am stuck on how to add collision to my TiledMap, I am using the Mapeditor to create the tilemap. I read so many different ways how to approach this problem, but it is very difficult to understand how exactly this works.
Then I read about using Box2d for collision detection. Transforming the tiles I want into static bodys and creating a dynamic body for my player.
Right now I have created a MapBodyBuilder, wich should transform a tile layer into a specific box2d object. My problem is now, I don't know where I load this mapBuilder, how I can make my already set up Player to a dynamic body, where to create the player and how to combine the static tile bodies and the player body.
Hope someone can help me out!
As you can see I already tried myself didn't work tho .... I didn't post the whole player class, because most of it is for animation and userInput.
EDIT :.... I don't know what to do anymore.
public class MapBodyBuilder {
static Array <Body> bodies = new Array<Body>();
private static float ppt = 0; // pixels per unit
public static Array<Body> buildShapes(Map map, float pixels, World world) {
ppt = pixels;
MapObjects objects = map.getLayers().get("Kachelebene 1").getObjects();
for (MapObject object : objects) {
if (object instanceof TextureMapObject) {
continue;
}
Shape shape;
if (object instanceof RectangleMapObject) {
shape = getRectangle((RectangleMapObject) object);
}
// else if (object instanceof PolygonMapObject) {
// shape = getPolygon((PolygonMapObject) objects);
//}
/* else if (object instanceof CircleMapObject) {
shape = getCircle((CircleMapObject) objects);
} */
// else if (object instanceof PolylineMapObject) {
// shape = getPolyline((PolylineMapObject) objects);
// }
else {
continue;
}
BodyDef bd = new BodyDef();
bd.type = BodyType.StaticBody;
Body body = (Body) world.createBody(bd);
((com.badlogic.gdx.physics.box2d.Body) body).createFixture(shape, 1);
bodies.add(body);
shape.dispose();
}
return bodies;
}
private static PolygonShape getRectangle(RectangleMapObject rectangleObject) {
Rectangle rectangle = rectangleObject.getRectangle();
PolygonShape polygon = new PolygonShape();
Vector2 size = new Vector2((rectangle.x + rectangle.width * 0.5f) / ppt, (rectangle.y + rectangle.height * 0.5f) / ppt);
polygon.setAsBox(rectangle.width * 0.5f / ppt, rectangle.height * 0.5f / ppt);
return polygon;
}
/*private static CircleShape getCircle(CircleMapObject circleObject) {
Circle circle = new circleObject.getCircle();
CircleShape circleShape = new CircleShape();
circleShape.setRadius(circle.radius / ppt);
circleShape.setPosition(new Vector2(circle.x / ppt, circle.y / ppt));
return circleShape;
} */
private static PolygonShape getPolygon(PolygonMapObject polygonObject) {
PolygonShape polygon = new PolygonShape();
float[] vertices = polygonObject.getPolygon().getTransformedVertices();
float[] worldVertices = new float[vertices.length];
for (int i = 0; i < vertices.length / 2; i++) {
System.out.println(vertices[i]);
worldVertices[i] = vertices[i] / ppt;
}
polygon.set(worldVertices);
return polygon;
}
private static ChainShape getPolyline(PolylineMapObject polylineObject) {
float[] vertices = polylineObject.getPolyline().getTransformedVertices();
Vector2[] worldVertices = new Vector2[vertices.length / 2];
for (int i = 0; i < vertices.length / 2; i++) {
worldVertices[i] = new Vector2();
worldVertices[i].x = vertices[i * 2 ] / ppt;
worldVertices[i].y = vertices[i * 2 + 1] / ppt;
}
ChainShape chain = new ChainShape();
chain.createChain(worldVertices);
return chain;
}
}
public class WorldRenderer {
// rectangle
TiledMapTileLayer tilelayer;
private ShapeRenderer sRDebugRectangel = new ShapeRenderer();
private static final float UNIT_SCALE = 6 / 16f;
public SpriteBatch batch;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;
private World world;
private MapBodyBuilder bodyBuilder;
private Player player;
private Level level;
public WorldRenderer() {
level = new Level("Leveltry.tmx");
renderer = new OrthogonalTiledMapRenderer(level.getMap(), UNIT_SCALE);
// int columns = tilelayer.getWidth();
// int row = tilelayer.getHeight();
//Cell cell = tilelayer.getCell(columns, row);
world = new World(null, false);
bodyBuilder = new MapBodyBuilder();
player = new Player();
/*BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
Body body = world.createBody(bodyDef);
CircleShape circle = new CircleShape();
circle.setRadius(8); */
//player.world = new World(null, false);
camera = new OrthographicCamera();
camera.setToOrtho(false, 200, 100);
camera.update();
batch = new SpriteBatch();
// Rectangle playerRect = new Rectangle(player.position.x, player.position.y, 32, 32);
}
public void render(float delta) {
// bodyBuilder.buildShapes(level.getMap(), 32, world);
//world.getBodies(null);
camera.position.set(player.getX(), player.getY(), 0);
batch.setProjectionMatrix(camera.combined);
renderer.setView(camera);
camera.update();
renderer.render();
player.render(batch, delta);
//System.out.println(player.position.x + " " + player.position.y);
}
public void dispose() {
batch.dispose();
}
}
public Player() {
// createBody();
position = new Vector2(200, 100);
createPlayer();
}
public void createPlayer() {
tex = new Texture("angel.png");
frames = TextureRegion.split(tex, 48, 48);
walkingHorizontalLeft = new Animation(0.2f, frames[1]);
walkingHorizontalRight = new Animation(0.2f, frames[2]);
walkingVeritcalDown = new Animation(0.2f, frames[0]);
walkingVerticalUp = new Animation(0.2f, frames[3]);
animations = new Animation[4];
animations[0] = walkingHorizontalLeft;
animations[1] = walkingHorizontalRight;
animations[2] = walkingVeritcalDown;
animations[3] = walkingVerticalUp;
}
public void render(SpriteBatch batch, float delta) {
handleInput();
frameTime += delta;
position.x += velocity.x * delta; //distance travelled is equal to the velocity * the time taken
position.y += velocity.y * delta;
velocity.x = (float) (velocity.x * 0.8f);
velocity.y = (float) (velocity.y * 0.8f);
boolean idle;
//Lets check current state
if ((Math.abs(velocity.x) + Math.abs(velocity.y)) < 4f) { //Some small value)
//Its idle
idle = true;
} else {
//Its moving
idle = false;
}
if (idle) {
currentFrame = animations[facing].getKeyFrame(frameTime, false);//idleAnimation.getFRame(...), or maybe just a non moving region, whatever you want
} else {
currentFrame = animations[facing].getKeyFrame(frameTime, true);
}
//So Ive created the "facing" short variable, we can encode the facing direction to this variable
//Value of 0 left
//Value of 1 right
//value of 2 up
//value of 3 down
// System.out.println(velocity.x + velocity.y);
batch.begin();
batch.draw(currentFrame, position.x, position.y, 15, 15);
batch.end();
//System.out.println("this is x " + position.x + "this is y " + position.y);
}
/* private void createBody() {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
Body body = world.createBody(bodyDef);
CircleShape circle = new CircleShape();
circle.setRadius(8);
} */