Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java- creating a simple sprite polygon with rotation

I'm trying to make a simple game in libGDX. The main sprite is a submarine, that can rotate by its center origin (width/2,height/2).

This sprite is the only one that will spin in my game and also the only one that will need a polygon, for the rest of the sprites a rectangle bounding box is enough. I need to create a polygon, so I can handle intersections. The polygon would be a very simple one, with only 8 vertices.

How can I get the vertices of the polygon, if I know the position of the sprite, rotation, and origin?

This image can explain exactly what I want: enter image description here

like image 727
Boldijar Paul Avatar asked Feb 09 '26 23:02

Boldijar Paul


1 Answers

You can use Polygon for that and define the body manually. It might look like the following, starting at the bottom left vertex and going clock-wise until bottom right. You can of course change those values and adjust it to your sprite.

float[] vertices = new float[] {
    -2,   -2,
    -2,    2,
    -0.5f, 2,
    -0.5f, 3,
     0.5f, 3,
     0.5f, 2,
     2,    2,
     2,   -2
};

Polygon submarine = new Polygon();
submarine.setVertices(vertices);

Now you can rotate this polygon, just like the sprite, and move it and scale it. Just make sure you keep the polygon and your sprite synchronized. For collisions you can use Intersector.

For debugging of the polygon you can use a ShapeRenderer to render it on your screen.

shapeRenderer.begin();
shapeRenderer.polygon(polygon.getTransformedVertices());
shapeRenderer.end();
like image 85
noone Avatar answered Feb 12 '26 13:02

noone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!