0
\$\begingroup\$

I am making a simple side scrolling space shooter game using Libgdx and everything is going nicely. However until now I haven't been testing collisions for my players ship with other ships. I have lasers colliding with baddie ships using simple rectangle overlap method that exists already in libgdx.

But the "white/transparent" space around the edge of many of my sprites is quite a lot and when i am now applying the code to check collision with my player it is happening from a long way off from actual collision. In the past I used XNA Framework C# and there was quite a simple way to do a box collision and then within that test if both pixels in question where transparent and ONLY WHEN both pixels were NOT transparent would it return true as a collision.

I'm really struggling to find this method or how to implement this using LibGDX , can anyone help me out here??

Here is the code snippet in question, if you need the full games code please let me know:

        if (ship.getBoundingRectangle().overlaps(baddiePurple.getBoundingRectangle())){
        purpleDyingSound.play();
        KillPurpleSoloEnemy();
        game.setScreen(new MenuScreen(game));
    }

Of course I'm aware that I could just trim down all the images to have almost zero transparent edges but the problem with doing that is they are animated sprites in sprite-sheets and i can't get them into the right position if I try that. (Also, I plan on having LOTS of different enemies etc and trimming would be a major ballache)

THANKS IN ADVANCE FOR ANY HELP! peace out!

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Most games that need the type of accuracy that you want use polygonal collision. My favorite is the Hyperplane Separation Theorem. You wouldn't get pixel-perfect collision, but it will definitely reduce the problem you're experiencing. The collision doesn't need to be absolutely perfect; the player won't notice when they're in the heat of the action. Collision just needs to be good enough.

For LibGDX, you can use the Intersector class to detect polygonal collisions:

Polygon polygonA = new Polygon(new float[] {
        0, 0  // Vertices (x,y)
        5, 0,
        5, 5
    });
Polygon polygonB = new Polygon(new float[] {
        -10, 0
        10, 0,
        5, 10
    });    

if(Intersector.overlapConvex(polygonA, polygonB)) {
    // Polygons overlap = collision
}

The vertices should be declared in counter-clockwise order (I'm not sure if this is a requirement for LibGDX's particular intersection algorithm, but most do so it wouldn't hurt to get into the habit of doing so). The only limitation is that the polygons must be convex. However, this can be easily worked around by splitting the polygons into smaller polygons that are convex while the general shape of all the polygons is concave.

\$\endgroup\$
1
  • \$\begingroup\$ thanks for your answer pjrader1. This looks very detailed and when I get home from work this evening I will spend time trying to understand it and apply it into my game. If I have any problems I'll write back here hoping that you may help me further. thanks \$\endgroup\$ Commented Dec 7, 2016 at 11:49
1
\$\begingroup\$

Does xna support gpu based collision detection out of the box? Libgdx does not have such a thing.

In general I would not advice to write your own collision implementation and rather use box2d. Of course you can use intersector for a more simple approach but you will quickly run into performance issues and you will need to solve problems like spatial partitioning and checking broad/ narrow phase which box2d already has.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .