13
\$\begingroup\$

The game Steel Division 2 has a very nice looking frontline system, where a smoothly curved line is drawn between friendly and enemy units, dividing the map into territorty owned by two teams.

enter image description here

Does anyone know what algorithm I should research for dividing an area using smooth curves based on a few (sparse) points?

Bonus points for pointing me in the right direction for creating a shader/line renderer to shade INWARDS

https://store.steampowered.com/app/919640/Steel_Division_2/

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Looks like a normal spline system with control points. The harder question is, how to set the points. \$\endgroup\$
    – Zibelas
    Commented yesterday
  • \$\begingroup\$ Yes, splines can be used for the visuals. One brainstormed method: Divide world into grid Every grid cell finds nearest unit If unit is close enough, cell is controlled by that team If no units within X distance, cell is not controlled by any unit (left to default ownership at match start) But drawing a spline along the 'frontline' in that case would create a blocky line, not smooth at all \$\endgroup\$
    – DeeCeptor
    Commented yesterday
  • \$\begingroup\$ I think the problem of how to find the right border curve to draw is separate from the problem of how to render that curve once you've found it. You may want to ask your second question, about creating a shader to achieve this effect, as a separate post. \$\endgroup\$
    – LudoProf
    Commented yesterday

1 Answer 1

32
\$\begingroup\$

I'm not familiar with the game in question, but I suspect that they're a combination of Bézier curves and Voronoi cells.

A Voronoi diagram partitions an area into cells using a distance metric (typically, standard Euclidean distance) based on a series of (seed) points. Each seed point gets an associated region. And everything in a given region is closer to the that region's seed point than to any other seed point. Because of that property, Voronoi diagrams are often used to model are of influence. And we have algorithms that are efficient enough to calculate them in real time.

As for finding the front line, I would use a series of Bézier curves. Each curve goes from the middle of a given border line segment to the middle of the next border line segment. The control points depend on the type of Bézier you have. My first choice would be to use quadratic Béziers which have a single control, which I would set to be the corner between the two segments. The drawing tool I had on hand for my diagrams only makes cubic Béziers, which requires two control points. So in that case I'm using the halfway points between the corner of the two border segments and the border midpoints. I think these two methods give equivalent curves - if you want to check the underlying math I recommend Pomax's Bézier curve primer as a resource.

To illustrate, let's say I start with the following and want a frontline for the cross hatched region:

starting map

Here are the mid points along the border segments:

mid points added

Next I'll add in the control points:

control points added

Finally, I'll add in the Bézier curves:

final curve

Because of how underlying Voronoi diagram dictates the locations of the control points, the individual Bézier curves should join up together into a smooth continuous curve. That said, very short edges in the Voronoi diagram will result in some very sharp turns on the front line. That actually seems consistent with this example animation I found for the game, but it's something to look out for. Applying LLoyd's algorithm to the Voronoi before calculating the front line will reduce the likelihood of short edges, but also skews the area of influence. You may need to experiment to find a suitable balance.

As for the shading, two options come to mind:

\$\endgroup\$
7
  • 6
    \$\begingroup\$ +1, this is the approach I was going to recommend (but you've done a much nicer job of it than I would have!) \$\endgroup\$
    – DMGregory
    Commented yesterday
  • 4
    \$\begingroup\$ I wish mods could give extra upvotes for great answers like this. \$\endgroup\$
    – Almo
    Commented yesterday
  • \$\begingroup\$ Wow, what a well-made answer. Will give Voronoi a try. Also thanks for pointing out two options for shading/visuals \$\endgroup\$
    – DeeCeptor
    Commented yesterday
  • 1
    \$\begingroup\$ Do you have any suggestions for limiting the influence area of a unit? Ex: you maneuver an enemy behind the enemy lines. All of a sudden it's the closest point to 100+km of area, which is a bit much \$\endgroup\$
    – DeeCeptor
    Commented 23 hours ago
  • 1
    \$\begingroup\$ You can bias your distance calculations when computing the Voronoi diagram. If you multiply the distance to unit X by a scalar that grows as it gets farther from friendly units, its polygon in the Voronoi diagram will shrink. You could also add "virtual units" in areas of territory that have been held uncontested for a long enough time. The trick there is adding/removing them without causing a sudden "pop" in the frontier — maybe splitting/merging and migrating them to fill gaps gradually (like a force-directed graph?) rather than spawning/despawning them in-place. \$\endgroup\$
    – DMGregory
    Commented 21 hours ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.