-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Comments
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 |
May I ask how to embed code? Sorry, I'm really new to this. |
Here is a minimal reproduction:
|
@DomPTech I was able to get results out of these functions. I suspect that the problem is with setting the 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) |
@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. |
I think you want the clip_polygons function for that instead, since it performs the "difference" operation? |
I've tried that, but it produces odd results if one polygon is completely inside the other (which is what I want). |
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) |
I tried the workaround, but it still produces odd results...I wonder if this only works for NavigationPolygon2D? |
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:
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 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! |
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? |
Oops, sorry about that. |
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
The text was updated successfully, but these errors were encountered: