Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About making convex polygon shapes #13

Open
Goutte opened this issue Jan 27, 2024 · 0 comments
Open

About making convex polygon shapes #13

Goutte opened this issue Jan 27, 2024 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@Goutte
Copy link
Owner

Goutte commented Jan 27, 2024

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

@Goutte Goutte added the documentation Improvements or additions to documentation label Jan 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant