3
votes

Using accelerometer my sprite image is moving left and right and if I touch my screen the sprite is moving to Y-axis.I want to make a collision detection between different sprites so that if the sprite pass through an object it will stop to hide in that object. I already watch a tutorial this https://www.youtube.com/watch?v=T1aN--vTqLc but nothings happen. What is the proper way to make collition detection? I don't know what's wrong with my coding.Any suggestion or much better tutorial Thank's and Advance

Here is my coding

private Rectangle rectangleCat;
private Rectangle rectangleShoes;
private float yPosition = -40;

Sprite

    cat = new Texture(Gdx.files.internal("cat.png"));
    catsprite = new Sprite(cat);
    catX=300;
    catY=0;

    sprite_shoes = new Sprite(new Texture("equip/Shoes.png"));
    sprite_shoes.setPosition(260,580);

   rectangleShoes=new Rectangle(sprite_shoes.getX(),sprite_shoes.getY(),sprite_shoes.getWidth(),sprite_shoes.getHeight());
   rectangleCat = new Rectangle(catsprite.getX(),catsprite.getY(),catsprite.getWidth(),catsprite.getHeight());

Render method

    boolean isOverlaping = rectangleCat.overlaps(rectangleShoes);
    if(!isOverlaping) {
        System.out.println("not overlap");
        yPosition = yPosition + (20 * delta);

    }

My Sprite image and Object is not overlapping

Full source code http://pastebin.com/Dxfx9f65

1
When you say that nothing happens, are you saying that overlaps never returns true? - munyul
For rectangular object, you need to update your rectangle each time when sprite change its position. - Abhishek Aryan

1 Answers

2
votes

First of all your sprite is not sharp Rectangle it's looks like polygon so you need to detect collision between two polygon.

Still if you want to detect collision between rectangle then get Bounding Rectangle of your sprite in render() method.

In render method

rectangleShoes=sprite_shoes.getBoundingRectangle();
rectangleCat=catsprite.getBoundingRectangle();

boolean isOverlaping = rectangleCat.overlaps(rectangleShoes);
if(!isOverlaping) {
    System.out.println("not overlap");
    yPosition = yPosition + (20 * delta);

}