LibGDX - How to detect collision between player and multiple platforms (Android)

Viewed 34

im triying to create a simple game, where my player, falls into different platforms. However, rigth now, im only able to detect the first collision.

I've tried making a loop, where i call HandleEvent(), but only works for the first platform.

How could i make it to detect every time the player collides with a platform? Thank you!

Player class:

package com.mygdx.juegoreto;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;

import java.util.ArrayList;

public class Personaje {
    int x;
    int y;
    int width;
    int height;
    int xSpeed;
    int ySpeed;
    Color color;

public Personaje(int x, int y, int width, int height, int xSpeed, int ySpeed) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.xSpeed = xSpeed;
    this.ySpeed = ySpeed;
    this.color = Color.WHITE;
}

public void setxSpeed(int xSpeed) {
    this.xSpeed = xSpeed;
}

public void setySpeed(int ySpeed) {
    this.ySpeed = ySpeed;
}

public void update() {
    x += xSpeed;
    y += ySpeed;
    if (x < 0 || x > Gdx.graphics.getWidth() - 70) {
        xSpeed = -xSpeed;
    }
    if (y > Gdx.graphics.getHeight()) {
        System.out.println("tas muerto");
    }
}
public void draw(ShapeRenderer shape) {
    shape.rect(x, y, width, height);
}

public void checkCollision(ArrayList<Plataforma> plats) {
    if(collidesWith(plats)){
        color = Color.GREEN;
    }
    else{
        color = Color.WHITE;
    }
}
private boolean collidesWith(ArrayList<Plataforma> plats) {
    System.out.println("collides");
    for (Plataforma plat : plats) {
        if(x < plat.x + plat.width && y < plat.y + plat.height && x + width > plat.x && y + height > plat.y){
            this.setySpeed(plat.ySpeed);
            System.out.println("si");
            return true;
        }else{
            this.setySpeed(-3);
            System.out.println("no");
            return false;
        }
    }
    return true;
}
}

Main class:

package com.mygdx.juegoreto;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.graphics.GL20;

import java.util.ArrayList;

public class MyGame extends ApplicationAdapter {
    ShapeRenderer shape;
    Personaje player;
    Plataforma plat;
    ArrayList<Plataforma> plats;

    private float timeSeconds = 0f;
    private float period = 0f;

    @Override
    public void create () {
        int x = Gdx.graphics.getWidth() / 2;
        int y = Gdx.graphics.getHeight() ;

        shape = new ShapeRenderer();
        player = new Personaje(x, y - 200, 70, 70, 0, 0);
        plat = new Plataforma(x - 70, y - 800, 200, 20,2);
        plats = new ArrayList<>(1);

        int altura = 0;
        for(int i = 0; i <= 1 ; i++){
            altura -= 160;
            plats.add(new Plataforma((int)(Math.random() * Gdx.graphics.getWidth()), altura, 200, 20, 2));
        }

    }

    @Override
    public void render () {
        ScreenUtils.clear(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        player.update();
        //plat.update();
        shape.begin(ShapeRenderer.ShapeType.Filled);
        player.draw(shape);
        //plat.draw(shape);
        timeSeconds +=Gdx.graphics.getDeltaTime();
        if(timeSeconds > period){
            timeSeconds-=period;
            handleEvent();

        }

        shape.end();

        if (Gdx.input.isTouched()) {
            if (Gdx.input.getX() < Gdx.graphics.getWidth() / 2){
                player.setxSpeed(-2); //IZQUIERDA
            } else {
                player.setxSpeed(2);  //DERECHA
            }
        }

        if(player.y < -40){
            System.out.println("tas muerto je");
        }


        /*for (Plataforma plat : plats ) {
            int x = (int)(Math.random() * Gdx.graphics.getWidth());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(x);
            plat.draw(shape);
        }*/
    }
    
    @Override
    public void dispose () {

    }

    public void handleEvent() {
        for (Plataforma plataforma : plats) {
            plataforma.update();
            plataforma.draw(shape);
        }
        player.checkCollision(plats);
    }
}
0 Answers
Related