5
\$\begingroup\$

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 but I couldn't achieve it 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());
}

How can I solve this issue?

Thank You!

\$\endgroup\$
  • 1
    \$\begingroup\$ Although I have no particular experience with libgdx, this question has an answer that should make you able to determine the colour of a pixel. Pixel perfect collision is not easier than that, though you could try a different shape than a rectangle. I think a bounding circle, for example, would fit your shape a lot better. \$\endgroup\$ – Athos vk Jan 4 '16 at 7:58
3
\$\begingroup\$

I had faced similar problem, when I was building a tower defense game.Where I needed to check for collision between tower boundary (ellipse) and enemies (rectangle),enter image description here

first method : I have used the condition for checking if these points exist inside the ellipse like

if(ellipse.contains(point1X,point1Y)||ellipse.contains(point2X,point2Y)...)
{
    System.out.println("attack");
}

I picked those points that was in the boundary and that will collide with boundary most often.And I had 20 to 25 enemies rendered onto screen at same time and 3 to 5 forts checking for above condition whether any enemies are inside fort,My game was successful and had frame rate between 55fps and 60fps.

what you can do is check for condition if bee collided with the wall like

if(rectangle.contains(point1X,point1Y)||rectangle.contains(point2X,point2Y)...)
{
    System.out.println("bee collided with wall ");
}

Second method: You can also solve this using another approach ie using polygons and Intersector,for this you have to plot a polygon over the bee and wall. Like this

enter image description here

create polygons for bee and also for the wall

For bee

float[]verticesForBee=new float[]{-20,-20, -20,20,-10,20,-10,30,10,30,10,20, 20,20, 20,-20 };
Polygon beePoly=new Polygon();
beePoly.setVertices(verticesForBee);

For Wall

float[]verticesForWall=new float[]{-10,-20, -20,20,-30,20,-300,30 };
Polygon wallPoly=new Polygon();
wallPoly.setVertices(verticesForWall);

Now using Intersector.overlapConvexPolygon() method check if the polygons overlap with each other

if(Intersector.overlapConvexPolygons(beePoly, wallPoly))
{
    System.out.println("bee got collided with the wall");
}
\$\endgroup\$
  • \$\begingroup\$ I am trying this logic but I am facing the problem: since the tubes are moving, it is hard to find the vertices in every check. \$\endgroup\$ – Pradip Kharbuja Jan 5 '16 at 17:51
  • 1
    \$\begingroup\$ The tubes can be moved using wallPoly.setposition(x,y) ,Now make your wall or tube sprite follow the wallPoly polygon for that you can use wallSprite.setPosition(wallPoly.getX(),wallPoly.getY()) \$\endgroup\$ – Shersha Fn Jan 5 '16 at 17:57
  • 1
    \$\begingroup\$ You can also set orgin for your sprite as wallSprite.setOrgin(orginX,orginY) \$\endgroup\$ – Shersha Fn Jan 5 '16 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.