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

Cannot define cuts/holes in Polygon2D #10296

Closed
DomPTech opened this issue Nov 20, 2024 · 12 comments
Closed

Cannot define cuts/holes in Polygon2D #10296

DomPTech opened this issue Nov 20, 2024 · 12 comments
Labels

Comments

@DomPTech
Copy link

Your Godot version:
Godot 4.3

Issue description:
I'm a high school developer, and I've just started learning Godot, so please bear with me. I'm currently developing a 2D game where you play as a doctor performing various surgical operations. One of the features I'd like to have is to allow the player to make "cuts" in the patient by drawing polygons. The drawing works great, but when I call Geometry2D.merge_polygons or the related clip_polygons, intersect_polygons, or exclude_polygons, nothing happens at all. I was hoping that this statement from the documentation, "If polygon_b is enclosed by polygon_a, returns an outer polygon (boundary) and inner polygon (hole)," would turn out to be true, but nothing seems to be working. And if this isn't how I should be adding holes in polygons, then I would appreciate any help since my project is on a tight deadline for a competition. Thanks in advance!

URL to the documentation page:
https://docs.godotengine.org/en/stable/classes/class_geometry2d.html

@DomPTech DomPTech added the bug label Nov 20, 2024
@DomPTech DomPTech changed the title Cannot define holes in Polygon2D Cannot define cuts/holes in Polygon2D Nov 20, 2024
@tetrapod00
Copy link
Contributor

If you're looking for help on a deadline, I would suggest one of the help channels listed here, maybe the discord since it is real-time. Documentation issue reports really are not the place for tech support, unfortunately.

This very well could be an issue with the docs, but it's hard to tell without seeing example code. Do you have a minimal script example of the code that should work but does not, using merge_polygons or the related functions?

@tetrapod00 tetrapod00 added area:class reference Issues and PRs about the class reference, which should be addressed on the Godot engine repository topic:2d labels Nov 20, 2024
@DomPTech
Copy link
Author

If you're looking for help on a deadline, I would suggest one of the help channels listed here, maybe the discord since it is real-time. Documentation issue reports really are not the place for tech support, unfortunately.

This very well could be an issue with the docs, but it's hard to tell without seeing example code. Do you have a minimal script example of the code that should work but does not, using merge_polygons or the related functions?

May I ask how to embed code? Sorry, I'm really new to this.

@DomPTech
Copy link
Author

DomPTech commented Nov 20, 2024

Here is a minimal reproduction:

var polygon = Polygon2D.new()
polygon.polygon = [
	Vector2(0, 50),
	Vector2(100, 50),
	Vector2(0, -50)
]
polygon.color = Color.RED
add_child(polygon)

var hole = Polygon2D.new()
hole.polygon = [
	Vector2(10, 30),
	Vector2(50, 30),
	Vector2(10, -25)
]
hole.color = Color.BLUE
add_child(hole)

var merged = Geometry2D.merge_polygons(polygon.polygon, hole.polygon)
var result = Polygon2D.new()
result.polygons = merged
result.color = Color.PURPLE
add_child(result)

@tetrapod00
Copy link
Contributor

@DomPTech I was able to get results out of these functions. I suspect that the problem is with setting the polygons property of a Polygon2D node, though I am not very familiar with it.

extends Node2D

func _ready() -> void:
	var polygon = Polygon2D.new()
	polygon.polygon = [
		Vector2(0, 50),
		Vector2(100, 50),
		Vector2(0, -50)
	]
	polygon.color = Color.RED
	add_child(polygon)

	var hole = Polygon2D.new()
	hole.polygon = [
		Vector2(10, 30),
		Vector2(50, 30),
		Vector2(10, -25)
	]
	hole.color = Color.BLUE
	add_child(hole)
		
	var merged = Geometry2D.merge_polygons( hole.polygon, polygon.polygon,)
	var result = Polygon2D.new()
	result.polygon = merged[0]
	# This doesn't work for me.
	#result.polygons = merged
	result.color = Color.PURPLE
	add_child(result)
	result.position = Vector2(-100,0)
	
	var intersected = Geometry2D.intersect_polygons( polygon.polygon,hole.polygon, )
	var result2 = Polygon2D.new()
	result2.polygon = intersected[0]
	# This doesn't work for me.
	#result2.polygons = intersected 
	result2.color = Color.ORANGE
	add_child(result2)
	result2.position = Vector2(100,0)

Image

@DomPTech
Copy link
Author

@tetrapod00 Thanks, the code above works. However, this still doesn't really address my original problem, which was creating holes in a Polygon2D as described in the documentation for Geometry2D. As shown in your screenshot, the merge result basically ignores the internal polygon instead of making a cutout.

@tetrapod00
Copy link
Contributor

I think you want the clip_polygons function for that instead, since it performs the "difference" operation?

@DomPTech
Copy link
Author

I've tried that, but it produces odd results if one polygon is completely inside the other (which is what I want).

@tetrapod00
Copy link
Contributor

Oh, I see what you mean... I'm not actually sure if you can make a polygon with a hole in it, actually, at least with the Polygon2D node. The polygons seem defined by a single series of points. You would need outlines to define a polygon with a hole (and by a technical definition I don't think it's a polygon anymore?)

It looks like this is indeed not a feature: godotengine/godot-proposals#9127. There is a workaround which may work: godotengine/godot-proposals#9127 (comment)

@DomPTech
Copy link
Author

I tried the workaround, but it still produces odd results...I wonder if this only works for NavigationPolygon2D?

@tetrapod00
Copy link
Contributor

Yeah, I'm not sure if there is some notable difference between the two classes.

To return to the original topic, from the testing I did, I believe that the statement:

If polygon_b is enclosed by polygon_a, returns an outer polygon (boundary) and inner polygon (hole),"

is true, but the Polygon2D node does not have a way to render that pair of polygons as a shape with a hole in it. The current docs for the various clip_ploygon, merge_polygon functions seem accurate to me, so I'm going to close this docs issue.

I'd recommend looking for help with an alternate way to display polygons with holes in one of the previously linked help channels. I don't know any other ways, and a docs issue is not really the right place for the discussion. Good luck with your project!

@tetrapod00 tetrapod00 added archived and removed bug area:class reference Issues and PRs about the class reference, which should be addressed on the Godot engine repository topic:2d labels Nov 20, 2024
@DomPTech DomPTech reopened this Nov 20, 2024
@tetrapod00
Copy link
Contributor

If you still think there are docs improvements to make here feel free to leave a comment. But I think you just accidentally reopened the issue?

@tetrapod00 tetrapod00 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 20, 2024
@DomPTech
Copy link
Author

Oops, sorry about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants