Vipul Behl Vipul Behl - 4 months ago 33
Android Question

Can't hide image on collision in libgdx?

I just started working with LibGDX and can't figure out how to hide an image when it collides with an object. In my game some dots come from the top of the screen and meet the dot at the bottom. When they meet the dots should hide that isn't happening.

This is the main Game Class

public class GameScreen implements Screen,InputProcessor {
final AmazingDot game;

//setting the height and width variables
private int height;
private int width;
private static int touchCounter;

//setting the two dots variables
private Texture playerDotImage;
private Texture gameDotImage;
private Texture gameDotImage1;

private Rectangle playerDotRectangle;
private Map<Rectangle,Texture> gameDotMap;

//storing the time of last dot in nano seconds
private long lastDotTime;

public GameScreen(final AmazingDot gam){
this.game = gam;
Gdx.input.setInputProcessor(this);

//getting the height and width of the user's screen
height = Gdx.graphics.getHeight();
width = Gdx.graphics.getWidth();
touchCounter =0;

//loading the images in the variables
playerDotImage = new Texture(Gdx.files.internal("images/dot2.png"));

gameDotImage = new Texture(Gdx.files.internal("images/dot1.png"));
gameDotImage1 = new Texture(Gdx.files.internal("images/dot2.png"));

//placing the player dot in the middle of the screen
playerDotRectangle = new Rectangle();
playerDotRectangle.x = width/ 2 - 64 / 2;
playerDotRectangle.y = 20;
playerDotRectangle.width = 64;
playerDotRectangle.height = 64;

gameDotMap = new ConcurrentHashMap<Rectangle, Texture>();

populateDots();
}

private void populateDots(){
Rectangle dots = new Rectangle();
dots.x = randomLocation();
dots.y = height;
dots.width = 64;
dots.height = 64;

Random rand = new Random();
int n = rand.nextInt(2) + 1;
if(n==1){
gameDotMap.put(dots,gameDotImage1);
}
else{
gameDotMap.put(dots,gameDotImage);
}

lastDotTime = TimeUtils.nanoTime();
}

private int randomLocation(){
int[] locations = new int[3];
locations[0]=0;
locations[1]=width/2-64/2;
locations[2]=width-64;

Random generator = new Random();
int randomIndex = generator.nextInt(locations.length);
return locations[randomIndex];
}

@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.begin();

game.batch.draw(playerDotImage,playerDotRectangle.x,playerDotRectangle.y,playerDotRectangle.width,playerDotRectangle.height);

for(Map.Entry<Rectangle,Texture> dots : gameDotMap.entrySet()){
game.batch.draw(dots.getValue(),dots.getKey().x,dots.getKey().y);
}

game.batch.end();

// check if we need to create a new dot
if(TimeUtils.nanoTime() - lastDotTime > 1000000000) populateDots();

for(Rectangle dot : gameDotMap.keySet()){

int gameSpeed = 400;
int xSpeed = calXSpeed(gameSpeed);
dot.y = dot.y - gameSpeed * Gdx.graphics.getDeltaTime();

if(dot.x <width/2-64/2){
dot.x = dot.x + xSpeed*Gdx.graphics.getDeltaTime();
}
else if(dot.x>width/2-64/2){
dot.x = dot.x - xSpeed*Gdx.graphics.getDeltaTime();
}
if(dot.y + 64 < 0) gameDotMap.remove(dot);
if(dot.overlaps(playerDotRectangle)) {
//this is where I am trying to remove the map object on collision
gameDotMap.remove(dot);
}
}
}

private int calXSpeed(int gameSpeed){
int seconds = height/gameSpeed;
int distance = width/2-64/2;
int speed = distance/seconds;
return speed;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
touchCounter++;
if(touchCounter % 2==0){
playerDotImage = new Texture(Gdx.files.internal("images/dot2.png"));
}
else{
playerDotImage = new Texture(Gdx.files.internal("images/dot1.png"));
}
return true;
}

@Override
public void dispose() {
playerDotImage.dispose();
gameDotImage.dispose();
gameDotImage1.dispose();
}
}


EDIT

enter image description here

As you can see in the above image when the moving dot reaches the stationary dot, it should disappear. But here in my code the dot just moves past the stationary dot. I am using rectangles(LibGdx) to detect whether the dots overlap each other or not.

Answer

Define you Map as an ArrayMap like so:

private ArrayMap<Rectangle, Texture> gameDotMap;

Then you can rewrite you render method like this:

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    game.batch.begin();

    game.batch.draw(playerDotImage, playerDotRectangle.x, playerDotRectangle.y, playerDotRectangle.width, playerDotRectangle.height);

    for (int i = 0; i < gameDotMap.size; i++) {
        game.batch.draw(gameDotMap.getValueAt(i), gameDotMap.getKeyAt(i).x, gameDotMap.getKeyAt(i).y);
    }

    game.batch.end();

    // check if we need to create a new dot
    if (TimeUtils.nanoTime() - lastDotTime > 1000000000) {
        populateDots();
    }

    for (Iterator<ObjectMap.Entry<Rectangle, Texture>> iter = gameDotMap.iterator(); iter.hasNext();) {
        ObjectMap.Entry<Rectangle, Texture> entry = iter.next();
        Rectangle dot = entry.key;
        int gameSpeed = 400;
        int xSpeed = calXSpeed(gameSpeed);
        dot.y = dot.y - gameSpeed * Gdx.graphics.getDeltaTime();

        if (dot.x < width / 2 - 64 / 2) {
            dot.x = dot.x + xSpeed * Gdx.graphics.getDeltaTime();
        } else if (dot.x > width / 2 - 64 / 2) {
            dot.x = dot.x - xSpeed * Gdx.graphics.getDeltaTime();
        }
        if (dot.y + 64 < 0) {
            iter.remove();
        }
        if (dot.overlaps(playerDotRectangle)) {
            //this is where I am trying to remove the map object on collision
            iter.remove();
        }
    }
}

LibGDX has specific types for collections, they are recommended over the JDK's collections.