Domanda

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

È stato utile?

Soluzione

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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top