collisiongodotgdscriptgodot4

Dynamically update CollisionPolygon2D at runtime?


I have a stacked area chart like the supplied image (but often slightly more complex).

stacked area chart with multiple bands

Each polygon doesn't have all too many points and renders efficiently.

This chart often updates with a tween that takes ~2 seconds, changing the Polygon2Ds.

But I want to be able to highlight a specific polygon by hovering it, so I added a CollisionPolygon2D to each Polygon2D. So when updating a Polygon2D I also do:

collision.polygon = polygon.polygon

This operation works but takes several hundred milliseconds to perform so I definitely can't run it every tween loop or every frame. My best bet, I think, is to run it in the middle of the tween (since I use ease-ease). But even this lags the UI for a good half a second when the update is performed.

I think this is due to the CollisionPolygon2D needing to be rebuilt everytime a new polygon is assigned which takes a crazy long time even for simple shapes.

Any suggestions what I can do differently?


Solution

  • The best course of action was to skip using a CollisionPolygon2D at all and instead just check Geometry2D.is_point_in_polygon(pos, polygon.polygon) on the visual Polygon2D. Where pos is the position of the mouse.