Search code examples
javagraphicslibgdx

polygon should be centered on mouse x,y


I wrote polygon and circle test in which via cursor, I rotate around polygon 360, against center point x=0, y=0.

However, I have an issue, to center polygon at mouse x,y instead of at edge. Like it is now in screenshot, red circle represents current mouse position and green is my goal.

I tried to use setOrigin but that did not work well for me or I am not setting it right. p.s. ignore white circle.

enter image description here

public class PolygonIntersectsCircle3Test extends AbstractTest {

    private Polygon polygon;
    private Circle circle;
    private boolean collides;

    public PolygonIntersectsCircle3Test(Game game) {
        super(game);
    }

    @Override
    public void create() {
        float width = 300f;
        float height = 50f;
        polygon = new Polygon(game, Color.WHITE_COLOR, new float[] {
            0, 0,
            width, 0,
            width, height,
            0, height
        }, 0);
        //polygon.setOrigin(width / 2, height / 2);
        circle = new Circle(game, Color.WHITE_COLOR, 100, 100, height / 2);
    }

    @Override
    public void update(float deltaTime) {
        Vector2 mouseRelativeToScreen = game.getInputController().getMouseRelativeToWorld();
        polygon.setPosition(mouseRelativeToScreen.x, mouseRelativeToScreen.y);
        float degrees = degrees(mouseRelativeToScreen.x, mouseRelativeToScreen.y, 0, 0);
        polygon.setRotation(degrees);
        collides = polygon.contains(circle.x, circle.y);
    }

    @Override
    public void draw() {
        game.getShapeRenderer().begin(ShapeRenderer.ShapeType.Line);
        if (collides) {
            game.getShapeRenderer().setColor(Color.RED);
        } else {
            game.getShapeRenderer().setColor(Color.WHITE_COLOR);
        }
        game.getShapeRenderer().polygon(polygon.getTransformedVertices());
        game.getShapeRenderer().circle(circle.x, circle.y, circle.radius);
        game.getShapeRenderer().end();
    }
}

  public static strictfp float degrees(float x, float y) {
        float degrees = (float) StrictMath.toDegrees(StrictMath.atan2(y, x));
        if (degrees < -360.0f || degrees > 360.0f) {
            degrees %= 360.0f;
        }
        if (degrees < 0.0f) {
            degrees += 360.0f;
        }
        return degrees;
    }

public static strictfp float degrees(float x, float y, float circleCenterX, float circleCenterY) {
    return angleInDegrees(x - circleCenterX, y - circleCenterY);
}

Solution

  • I've never used LibGDX before, but the documentation seems to suggest that the origin of the polygon might be intended to be used as the origin of the vertices, not the origin of transformations. If so, you should specify the vertices of the polygons as though they were all offset by the center point of the polygon:

    float wd2 = width / 2.0f;
    float hd2 = height / 2.0f;
    polygon = new Polygon(game, Color.WHITE_COLOR, new float[] {
        -wd2, -hd2,
        wd2, -hd2,
        wd2, hd2,
        -wd2, hd2
    }, 0);
    polygon.setOrigin(0.0f, 0.0f); // I doubt this is necessary, but just in case