I’m making a game where there are many Area2Ds of different countries, but I want to know the size of each country to calculate their surrender limit (bigger countries take longer to surrender, for example).
I don’t want to find the area of just one collision2D as some countries have multiple (see island nations, overseas colonies…), but looping that with the children could work if finding area using AREA2ds isn’t a thing.
I don’t want to simply use the outermost points of a collisionshape2D and draw a big rectangle, as it would make some countries way bigger than they really are (see especially the British and French empires)
Is it possible to find the area of an Area2D? If not, is it possible to calculate the area of CollisionShape2Ds (then I could just loop through the children).
p.s. Country size will not be the only factor for surrender limit, things like equipment stockpiles and something similar to war support in HOI4 might also affect it…
I think, the Area2D node doesn’t have any size. But you can use the underlying shape object.
If it’s a basic shape (rectangular, circle) it should be trivial to calculate. If you have a poligon2D as shape, I think there is no build in Godot function, but you could calculate it with shoelace formula, I think.
Does this make sense to you?
Making video games has taught me most of the maths that i missed in school. You really do need those pesky formulas like they told you back then lol.
This doesn’t super solve your immediate problem, but it might help you keep progress on your game:
Remember — it’s a game. Maybe yes, you could calculate the area of the polygon you drew, and then you’d have it working like that for good. But then what happens if you want to fudge a number, like the area calculates that 2 troops can fit in France but for balance you really want 3? At that point you’re going to need to fudge the calculation, add another factor or redraw your polygon (the latter which might not be possible).
An alternative way to solve this is you can look up on wikipedia the different areas of these countries. You can assign a factor for each one (like Risk does) based on various factors - area, population, etc, but ultimately balance, that allows you to set whatever you want on each country. Not that the math isn’t exciting, but you’re probably going to end up doing this anyway after you figure the math out.
Unfortunately, it looks like drawing a big rectangle is the only method to calculate area that’s baked into Godot. You could get the Area2D’s CollisionShape2D children, and then for each child you can
child.shape.get_rect().get_area()
Now, if you’re getting each shape anyway, and you know what kind of shape it is, you can use the appropriate formula to calculate area. For instance, if it’s a CircleShape2D, you can check the radius and get something like
var area = shape.radius * shape.radius * PI
For a polygon it becomes more complicated. You can get the points of the polygon and then use Geometry2D.triangulate_polygon() to get an array of triangles inside that polygon. You can then calculate the area of each triangle and sum them to get your total area. There’s no built-in way to do this, so it’s left as an exercise for you.
The question I have to ask: from a design standpoint, is the exact size of each country truly important to the game? For the sake of argument, Canada is a really quite large country, but it’s sparsely populated. A lot of the land is frankly not very habitable. If I was going to abstract a surrender-factor, I’d think about “population” filling a role that you’re thinking of for “area.” If that were the case, you could just attach a property to each country manually. Maybe a little more work upfront depending on how many countries or regions you’re including, but it’s definitely cognitively simpler.
https://en.m.wikipedia.org/wiki/Shoelace_formula
But don’t do this on the fly… do it before you compile your program and use a lookup table at runtime.
You mentioned the areas being countries. This leads me to believe that they are concave figures, correct? If you are unfamiliar, a concave figure is something that has a space that doubles back into the interior of the shape. So an o is convex, and a c is concave, as an example. Convex shapes are much simpler to find the area of. You can use a Riemann Sum as others have suggested. I would probably just pick a point inside the shape and do a bunch of triangles around with the point as an apex and the bases as two points on the edge of the surface, then sum up the areas of each triangle. You could even probably use a triangulation algorithm built into the engine to do this. (I am unfamiliar with the specifics of the Godot engine).
For concave shapes it becomes a little more complex. It has been mentioned that you can draw a bounding box around the shape, so that would allow you to calculate it using a numerical method. Take random samples inside the bounding box and count up the number that are inside the shape and divide by the total number of samples. The value you get will be the % of the area of the bounding rectangle that the shape takes up, so just multiply the easy area by the % and you will get an answer that is close enough. It may take a bit to get the sample count right, but it will get there. Try to make sure the samples are as uniform as possible. You could even scatter the sample points then relax them for a couple iterations before counting to increase accuracy without increasing samples.
To get the area of a random 2d polygon it’s probably easiest to triangulate the area, then sum the area of all the triangles.
Not sure how to program that, but that’s how I’d solve it with pen+paper.
You can use Geometry2D to triangulate the polygon and then determine the area of each of the triangles from their points. I would do this in an editor script and put that results into a text file or maybe generate a dictionary and keep it directly in code.
Good afternoon! While, I’ll be honest, I don’t do much video game development as I’m finishing up my Master’s program in Computer Science and my Data Science graduate certificate, the best advice I can give you for purposes regarding this problem is to look to the Riemann Sum formula regarding approximation of definite integrals on a two-dimensional plane, which is presented as follows:
∑ᵢ=₁ⁿ f(xᵢ)Δx
(P.S. - This formula was copied and pasted via generative AI, as I struggled to figure out how to write formulas in Lemmy response windows)
I suggest this as the imperfect dimensions of the sizes of countries on a global map appear to suggest necessity of calculations of area by means of integral calculus. While I was exposed to these formula during my high school AP Calculus course several years ago, I will admit I had to look it up again on Google and was reminded with an AI-generate response. Basically, the integral of an imperfect shape that cannot be defined by algebraic equations can be approximated on a curve with a series of rectangle of varying heights with equivalent widths. The more rectangles are placed to approximate the curve, the more accurate your assessment becomes. So, if you’re trying to approximate the size of the country on your global map for purposes of a country-surrendering mechanic in your game via Godot Engine, I would highly suggest defining a method that takes in the standard width of all rectangles present in your graph (each represented as a distinct CollisionShape2D component/element) with heights extending just beyond the outline of the country’s graph, and returns the approximate summation of the country’s area as a whole in a two-dimensional context for purposes of applying your country’s surrender limitation game mechanic.
I think the easiest solution to this is the ruler to measure your Area2D and then find the total area using a math formula based on the shape. Then you can use the total area in pixels that you calculated to create the surrender variable.