Goutte / godot-addon-animated-shape-2d

Godot addon to animate a CollisionShape2D along with the frames of an AnimatedSprite2D. Useful for making changing hitboxes, hurtboxes and hardboxes. Comes with an Editor, making things easy.
MIT License
130 stars 1 forks source link

About making convex polygon shapes #13

Open Goutte opened 10 months ago

Goutte commented 10 months ago

This plugin is meant to be used along with a CollisionShape2D, and not a CollisionPolygon2D.

There is a Shape2D Resource named ConvexPolygonShape2D, but Godot's Editor is not activating a GUI to edit the points like it does for CollisionPolygon2D.

This is because the CollisionPolygon2D allows "concave" polygons by using multiple convex shapes internally.

How to generate a ConvexPolygonShape2D

  1. Create a CollisionPolygon2D
  2. Use the Editor to add/edit points
  3. Ensure you made a single convex shape
  4. Use the following Node to extract a ConvexPolygonShape2D from it.

@tool
extends Node
class_name CollisionPolygon2DConvexShapeExtractor

## Extracts a ConvexPolygonShape2D from a CollisionPolygon2D.
## THE SHAPE MUST BE CONVEX -- DO NOT MAKE CONCAVE SHAPES.
##
## Usage:
## - Add anywhere
## - Target an input polygon and an output shape
## - Reopen the scene in the Editor
## - Save the generated shape to a file using the Editor
##
## Be careful if you leave `shape` defined, it will write in it.

@export var polygon: CollisionPolygon2D
@export var shape: ConvexPolygonShape2D

func _ready():
    extract()

func extract() -> int:
    if polygon == null:
        push_error("CollisionPolygon2D is null")
        return ERR_CANT_ACQUIRE_RESOURCE

    if shape == null:
        shape = ConvexPolygonShape2D.new()

    shape.points = polygon.polygon

    return OK