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

create/fix offset_polygon_2d(), an offsetting function for PoolVector2Array by a single vector #777

Closed
AriWD40 opened this issue May 1, 2020 · 7 comments · Fixed by godotengine/godot#38390
Milestone

Comments

@AriWD40
Copy link

AriWD40 commented May 1, 2020

Describe the project you are working on:

an acid node that eats up polygons2d/collisionpolygons2d, or a bomb that does the same.

Describe the problem or limitation you are having in your project:

When creating a new polygon, correct positional placement is key.

creating FOR loops in GDscript that offset the polygons is extremely taxing on performance but wouldn't be in C++. There is no function in polygon2d/collisionpolygon2d nor PoolVector2Array nor Geometry that offsets polygons. The function Geometry.offset_polygon_2d() doesn't offset a polygon by a vector amount, but inflates or deflates them, which is pretty silly.

Describe the feature / enhancement and how it helps to overcome the problem or limitation:

Either using (Transform2D * PoolVector2DArray) or .translate(), create a function in PoolVector2Array, Polygon2D & CollisionPolygon2D or Geometry that would add an input vector to all vectors in a given PoolVector2Array

I think the function should be called offset_polygon_2d, and the function that already has that name should be changed to inflate_polygon_2d.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:

  1. take input poolvector2array
  2. take input vector
  3. in a loop, add vector to array
  4. return array

If this enhancement will not be used often, can it be worked around with a few lines of script?:

It will be used often, by most developers that use polygons a lot

Is there a reason why this should be core and not an add-on in the asset library?:

This is a very basic vector function that belongs at the core.

@Calinou
Copy link
Member

Calinou commented May 1, 2020

Can you move a parent node of the Polygon2D/CollisionPolygon2D instead?

@AriWD40
Copy link
Author

AriWD40 commented May 1, 2020

I am using Geometry.clip_polygons_2d(), which don't accept coordinates of polygons, only poolvector2array. So no

@AriWD40
Copy link
Author

AriWD40 commented May 1, 2020

Perhaps another solution (for me) would be to add an offset vector argument to the clip_polygon_2d() function, but as a general solution, I think an offset function would be more practical.

@Xrayez
Copy link
Contributor

Xrayez commented May 1, 2020

I've chosen the offset_polygon name for inflating/deflating polygons because that's what the Clipper library uses internally, and creating a separate method just for inflating and deflating polygons might be redundant and not as flexible as simply providing either positive or negative delta. I'm not sure of a better name for this. Also with godotengine/godot#36369 this could potentially make the API more complex. Note that I'm not really against making dedicated methods for this, it's up for other core devs to decide.

For translating polygon's vertices specifically, I've added support in Transform2D/3D to transform any Vector2/3Array, see examples in godotengine/godot#31761. You can take the Transform2D(rotation, offset).xform(polygon) to translate polygon vertices like that, and any other transform operation of course.

So this is a documentation issue I guess. Adding a note to offset_polygon about the mentioned Transform2D feature may be enough, but the ability to transform a PoolVector2/3Array is already documented though.

@AriWD40
Copy link
Author

AriWD40 commented May 1, 2020

That should do it, though I am surprised at how little performance boost this gave me.

Thank you Xrayez.

@Xrayez
Copy link
Contributor

Xrayez commented May 1, 2020

There are many places where you could have a performance bottleneck, the polygon clipping is quite an intensive operation in and of itself, so you'll likely want to come up with optimization techniques like splitting polygons into smaller chunks.

For translating polygons, see documentation changes in godotengine/godot#38390 which may be merged to 3.2/4.0 built-in docs.

@Xrayez
Copy link
Contributor

Xrayez commented May 2, 2020

Also, you might be surprised how this can be little to do with the polygon clipping, but rather the rendering of polygons, which might be quite the case for you (and myself): godotengine/godot#19943, especially with many vertices which might result from this operation (more vertices → more shapes decomposed into convex → the number triangles to draw → slowdown).

@aaronfranke aaronfranke added this to the 4.0 milestone Aug 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants