Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

How to detect collision only in non-transparent texture?

$
0
0

I am developing a game using libgdx. I am trying to detect the collision between bee and tube. I am using the following code to detect the collision.

if(player.getBounds().overlaps(boundsBot)){

}

player is a bee and boundsBot is Rectangle of tube. Since there is some transparency in bee image, this returns true even if it doesn’t seem to collide. I understand the reason why this is returning true.

With Transparency

Without Transparency

I read this solutions. I understood the theoretical concept to the solve the problem the couldn’t achieve using libgdx.

Here is what I tried,

if(Intersector.intersectRectangles(player.getBounds(), boundsBot, rectangle)) {
    //Gets the intersected rectangle
    System.out.println("Rectangle Bottom: " + rectangle);

    //Converting texture to pixmap
    Texture texture = player.getTexture().getTexture();
    if (!texture.getTextureData().isPrepared()) {
        texture.getTextureData().prepare();
    }
    Pixmap pixmap = texture.getTextureData().consumePixmap();

    //Trying to find transparency.....  
    System.out.println("Format: "  + pixmap.getGLFormat());
}

Is there any built in functions in libgdx for detecting this kind of collision? Or how can I solve this issue?

Thank You!


Viewing all articles
Browse latest Browse all 434

Trending Articles