Eric
Eric

Reputation: 2076

Detecting Texture Collision

I have images like the following:

enter image description here

I included the black border to show the width and height of the texture, but they aren't actually part of my image.

Right now I use Texture Packer to store the textures. In my code, I find the region of the texture that I want like so:

TextureRegion enemyRegion = enemyAtlas.findRegion("Enemy");

Using scene2d in my actor I have the following draw method:

@Override
public void draw(Batch batch, float alpha){
    batch.draw(textureRegion.getTexture(),this.getX(),getY(),this.getOriginX(),this.getOriginY(),this.getWidth(),
            this.getHeight(),this.getScaleX(), this.getScaleY(),this.getRotation(),0,0,
            textureRegion.getRegionWidth(),textureRegion.getRegionHeight(),false,false);
}

When two of these objects collide, I want to return a true boolean. So I do my collision check like so:

if(Intersector.overlaps(rectanlge1, rectangle2)){
        System.out.println("intersect!");
        return false;
}

I create the rectangles based on the height and width of the texture region.

Doing it this way works fine, however, the problem is is that the textures aren't really just rectangles. For instance, I should be able to having the following case where they do not collide because the "green and blue areas" don't collide.

enter image description here

This is obviously a collision in my code, but I don't want it to be since the green and blue areas aren't actually touching at all. How can I achieve that using libgdx and scene2d?

Upvotes: 0

Views: 282

Answers (1)

strider
strider

Reputation: 5954

Try using 2 rectangles to describe your image more appropriately, then patching your collision detection to use them.

The collision detection could look something like this:

if(Intersector.overlaps(rectanlge1A, rectangle2A) 
    || Intersector.overlaps(rectanlge1A, rectangle2B)
    || Intersector.overlaps(rectanlge1B, rectangle2A)
    || Intersector.overlaps(rectanlge1B, rectangle2B) ){
        System.out.println("intersect!");
        return false;
}

Upvotes: 1

Related Questions