3
\$\begingroup\$

I'm writing my game on libgdx w/o any of those extensions like box2d and whatsoever. But right now I'm forcing problem with overlapping detection. I need to detect if bullet (which is always in different degree) overlapped my rocket. At first I was using Rectangle, but it sometimes is giving me false overlap.

I have visualized it.

How it is now:

enter image description here

My goal:

enter image description here

as you see I also want to make bullets rectangle rotated so it was more accurate. And as I said earlier I need to detect collision/overlapping between these polygons. Do I need to use that box2d extension??? Because right now I've written working prototype entirely w/o it.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ How did you define the input polygons of your textures in Java with Libgdx? \$\endgroup\$
    – trinity420
    Feb 19, 2018 at 22:50

2 Answers 2

1
\$\begingroup\$

Polygon A is overlapping polygon B if any of its vertices is inside B and vice versa.

bool isOverlap(Polygon A, Polygon B){
  foreach(Vertex v in A)
  {
    if(isInside(v, B))
      return true;
  }
  foreach(Vertex v in B)
  {
    if(isInside(v, A ))
      return true;
  }
  return false
}

bool isInside(Vertex v, Polygon p)
{ //you can choose either Crossing Number or Winding Numer, you can google implementation
}

if you use Winding Number, the collision will be even for complex polygons. However I advice you to stick axis-aligned to bounding boxes as detection is fast and simple, plus you can make the boxes smaller then you sprite. That would give you nearly same results(from player perspective) as polygon-perfect algorithm.

\$\endgroup\$
4
  • \$\begingroup\$ How can one calculate the required polygons for the sprite? \$\endgroup\$
    – trinity420
    Feb 19, 2018 at 12:55
  • \$\begingroup\$ @trinity420 You cannot. Or more precisely, you can but it not worth the effort not even mentioning it would most likely be lower quality than human made. It is the artist or whoever makes the asset job. \$\endgroup\$
    – wondra
    Feb 19, 2018 at 13:14
  • \$\begingroup\$ Thank you, but does this mean I have to use a tool like Libgdx Physics Body Editor to get the polygon of complicated shapes, export and somehow parse the resulting JSON into verticles using Java to make Polygons in Libgdx, scale them & place their x, y coordinates over my sprite and then use the method provided in your answer? \$\endgroup\$
    – trinity420
    Feb 19, 2018 at 14:03
  • \$\begingroup\$ @trinity420 you would be best to ask the OP about the exact method how did he get the input polygons. Any (your)described way should work just as good as any other even, say, by hand. \$\endgroup\$
    – wondra
    Feb 19, 2018 at 15:05
0
\$\begingroup\$

There's a faster way to accomplish this nowadays, you will need to be using the com.badlogic.gdx.math.Polygon class for your game entities' collision shapes.

Intersector.overlapConvexPolygons(polygonA, polygonB);

The Polygon class also has a rotate(float degrees) method, as well as getters and setters for rotation.
Polygon LibGDX API Reference
Intersector LibGDX API Reference

\$\endgroup\$

You must log in to answer this question.

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