0

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?

3 Answers 3

0

You can try these 2 options.

1- Lower resolution of your collision polygon
Instead of doing

collision.polygon = polygon.polygon

do this instead

func CalculateCollisionPolygon(points:PackedVector2Array):
    var new_points:PackedVector2Array
    const DISTANCE_THRESHOLD = 1.0
    for pt:Vector2 in points:
        var alreadyExists:bool = false
        for new_pt:Vector2 in new_points:
            if (pt.distance_to(new_pt) <= DISTANCE_THRESHOLD):
                alreadyExists = true
                break
        if (alreadyExists):
            pass
        else:
            new_points.append(pt)
    return new_points

...
...

collision.polygon = self.CalculateCollisionPolygon(polygon.polygon)

2- Use threads so UI update does not get halted.

var polygonUpdatorThread:Thread = null

...
...
#  when you want to update your polygon, instead of doing
#  collision.polygon = polygon.polygon directly, do this
polygonUpdatorThread = Thread.new()
polygonUpdatorThread.start(self.update_polygon_thread_func)

func update_polygon_thread_func():
    #  update your collision polygon points
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

-2

If simplifying the polygon isn't enough, or if the polygon generation itself is a heavy operation, you can move the polygon calculation to a separate thread. This prevents the main thread (which handles UI, input, and rendering) from freezing while the calculation is in progress.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.