Search code examples
androidlibgdx

libgdx - rotating collisions around a point


for (int i = 0; i < 13; i++) {
            circleSprites.get(i).setOrigin(circleSprites.get(4).getX(), circleSprites.get(4).getY());
            circleSprites.get(i).rotate(20 * delta);
            circleCollisions.get(i).setPosition(circleSprites.get(i).getX() + 1, circleSprites.get(i).getY() + 1);
        }
        for (int i = 13; i < 26; i++) {
            circleSprites.get(i).setOrigin(circleSprites.get(17).getX(), circleSprites.get(17).getY());
            circleSprites.get(i).rotate(-20 * delta);
            circleCollisions.get(i).setPosition(circleSprites.get(i).getX() + 1, circleSprites.get(i).getY() + 1);
        }

Want something like this! The sprite is not rottating as indended, but I can figure that out. But the thing is that .rotate does it just visually, I checked the coordinates and they stayed the same. So how do I set up collision for a rotation like in the picture above?


Solution

  • LibGDX has a Intersector class which is useful in these situations.

    it has multiple methods for all types of geometric shapes to test intersection against each other.

    first you would need to give each of these sprites a Circle

    com.badlogic.gdx.math.Circle circle = new Circle(x,y,r);
    

    then move these with your sprites so we have a geometric representation to test against(these Circles will be what we check collisions with, not the sprites, so we need to move these Circles with the sprites they represent at all times).

    then in every update check with the Intersector class all your circles against the things you wish to collide with, i.e.

    overlaps(Circle c, Rectangle r) 
    overlaps(Circle c1, Circle c2) 
    

    etc

    https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Circle.html

    https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html

    see this post...

    Circle and Polygon Collision with Libgdx