简体   繁体   中英

libGdx collision detection Polygons

I started learning LibGdx and Java recently, and it has been going well so far. I'm facing an issue with collision detection.

I have two sprites which can be represented as two shapes, a polygon and a circle, which will collide/intersect at any given moment. Once these two shapes collide, something will get triggered.

So far, this is what I have done. It kinda works but it is not accurate. This is called inside the Render() function:

   public boolean CollectPowerUp(PowerUps powerUp) {
        if (powerUp.position.dst(position) < Constants.PLAYER_HEIGHT -3) {
            Gdx.app.log("Collected PowerUp", "TRUE");
            EnablePowerUp(powerUp);
            return true;
        }
        return false;

I have searched many websites, and most of the solutions include other softwares like 2DCube or PhysicsEditor. Is it possible to perform this intersection solely by using LibGdx and Java? If so, what should I look into?

Thanks

Intersector class having many static method that can be used for collision detection.

If your polygon is rectangle you can use :

Intersector.overlaps(Circle c, Rectangle r)

else

Polygon polygon=new Polygon();
polygon.setVertices(new float[]{0,0,.......});
Circle circle=new Circle(x, y, radius);

float points[]=polygon.getTransformedVertices();

for (int i=0;i<points.length;i+=2){
    if(circle.contains(points[i],points[i+1])){
        System.out.println("Collide");
    }
}       

EDIT

Above code only detect collision if polygon vertices are inside circle, what if

  • circle is completely inside polygon
  • some part of circle is inside polygon but vertices are outside the circle

Create a polygon for circle that act as circle in view and polygon in model

float radius=100;
FloatArray floatArray=new FloatArray();
int accuracy=24;       // can be use 1 for complete circle

for (int angle=0;angle<360;angle += accuracy){
    floatArray.add(radius * MathUtils.cosDeg(angle));
    floatArray.add(radius * MathUtils.sinDeg(angle));
}

Polygon circle=new Polygon(floatArray.toArray()); // This is polygon whose vertices are on circumference of circle

float[] circularPoint=circle.getTransformedVertices();
for (int i=0;i<circularPoint.length;i+=2){
    if(polygon.contains(circularPoint[i],circularPoint[i+1])){
        System.out.println("Collide With circumference");
        break;
    }  
}

There's a nice article on collision detection on www.gamedevelopment.blog which shows how to detect collisions with most shapes. This is the Libgdx circle, polygon collision detection method shown in the article.

public boolean contains (Polygon poly, Circle circ) {
    final float[] vertices = poly.getTransformedVertices(); // get all points for this polygon (x and y)
    final int numFloats = vertices.length; // get the amount of points(x and y)
    // loop through each  point's x and y values
    for (int i = 0; i < numFloats; i += 2) {
        // get the first and second point(x and y of first vertice)
        Vector2 start = new Vector2(vertices[i],vertices[i + 1]);
        // get 3rd and 4th point (x and y of second vertice) (uses modulo so last point can use first point as end)
        Vector2 end = new Vector2(vertices[(i + 2) % numFloats], vertices[(i + 3) % numFloats]);
        // get the center of the circle
        Vector2 center = new Vector2(circ.x, circ.y);
        // get the square radius
        float squareRadius = circ.radius * circ.radius;
        // use square radius to check if the given line segment intersects the given circle.
        return Intersector.intersectSegmentCircle (start, end, center, squareRadius);
    }
}

There are many useful methods in the Intersector class which can be used for collision detection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM