How to make polygons hitboxes accurate, and how to import rotation values into python

So im making a game in python with the pygame module, and im using tiled to create my map with pytmx to interpret the map. The game i am making is a racing game, and i dont want my player to be able to leave the track, but i am struggling to get the hitboxes to work correctly. As i am new to both python and tiled, im not sure on how to fix my issues.

The first issue which doesnt include python, is the hitboxes with the polygons as i can draw a shape, but the hitbox is just a big rectangle.

As you can see, the hitbox is the rectangle around the triangle, but i want the hitbox to match to polygon. In the game, with the hitboxes made visible for bug testing, the hitbox is also a rectangle. If this is intentional and there is no way to match the hitbox to the polygon, then i instead could used rotated rectangles.

However, this is the second issue, which i assume is happening due to the lack of my python knowledge.

This image is showing a curve in the track, with thin rotated rectangles for hitboxes, however in game they look like this

Your screenshot of the triangle just shows the resize handles - those are based on the bounding box, but the triangle in your file is the actual triangle you drew. Read in the polygon and use it, you do not have to use its bounding box.

Unfortunately I can’t help with the Python side of this. Check the documentation for the Tiled map loader you’re using regarding rotation, and for the physics system you’re using (whether it’s built into Pygame or something separate).

Ive had a look through the docs, and tested multiple things but i cant figure this polygon thing out for the life of me, i am going to try github or stack overflow and see what info i can get
Thanks for the advice :slight_smile:

If you’re using PyTMX to load maps, the objects should have an as_points property, which presumably has the list of points that make up the Object. This is what you would use to construct your in-engine triangle.
There is also apply_transformations() , which returns a list of points for the object after taking rotation into account, you’d probably want this for both your triangles and your rotated rectangles.

Source: pytmx package — pytmx stable documentation