flutterquadtreeflame

Flutter Flame - Performant Radar Mechanism


Assumed we would like to create a strategy game. What is the best way (using flutter together with the flame engine) to detect other units (e.g. enemies) around an element?

I've checked out the raycastall function https://docs.flame-engine.org/1.4.0/flame/collision_detection.html?highlight=ray#raycastall which somehow seems to do what I want but this also seems to be somehow inefficient. If one would for example create some kind of artillery with a huge radius, a couple of hundred rays may not be enough and a couple of thousand rays over a huge distance will most likely not be performant.

The other idea would be to just use a Circle ShapeHitBox https://docs.flame-engine.org/1.4.0/flame/collision_detection.html?highlight=ray#raycastall. As the game uses a QuadTree https://docs.flame-engine.org/1.4.0/flame/collision_detection.html?highlight=ray#quad-tree-broad-phase this should be performant.

Which one, in the context of a modern strategy game running a couple of thousand units, would be the better choice or are there any other mechanisms which would work out better?


Solution

  • A solid CircleHitbox should work if you need to know the components within the radius in every tick.

    add(CircleHitbox(radius: <your radius>)..isSolid = true);
    

    isSolid is needed since otherwise you'll only detect hitboxes the intersect the radius, and not the hitboxes that are fully within the radius.

    Note that isSolid will be an argument in v1.5.0 so that you don't need the cascade operator.