kevinthompson / godot-generate-polygon-from-sprite-tool

A simple tool script for Godot 3 and 4 that generates a collision polygon from a sprite shape.
MIT License
12 stars 0 forks source link

Account for centered sprites #2

Open Merilax opened 3 days ago

Merilax commented 3 days ago

The current functions do not account for centered sprites, whose collision shapes should have an offset of 0, so they are both centered on (0,0). Trying to use a shape generated by this tool on a centered sprite causes nodes to spin weirdly, because the physical center of the node does not match the visual center of it.

I suggest adding this code to the tool, with a new check button:

func _center_collision_polygon(poly:PackedVector2Array, sprite:Sprite2D):
    var adjusted_polys = []
    var regex:RegEx = RegEx.new()
    regex.compile("\\d+, \\d+")
    for match in regex.search_all(str(poly)):
        var x:int = match.get_string().split(", ", false)[0].to_int() - floor(sprite.texture.get_size().x / 2)
        var y:int = match.get_string().split(", ", false)[1].to_int() - floor(sprite.texture.get_size().y / 2)
        adjusted_polys.append(Vector2i(x, y))
    return PackedVector2Array(adjusted_polys)

Where poly is the current polygon shape being iterated at the polys array loop.

Merilax commented 3 days ago

I have not forked and provided a PR because I don't know how editor tools work yet, so I'm leaving the dirty work for you. Sorry not sorry :p