# How do I... figure out how much of a level is "covered"

From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)          • The basic idea is, in one mode you place cameras in a level so you can view the area remotely later. I'm trying to require that the whole area be covered by the cameras before moving on. Unfortunately, I can't figure out a good way to do this.

It may be that this is more of a math problem than a Construct 2 problem, in which case, I'm sorry.

From my screenshot, you can see two cameras, of seven in the level, and the minimap showing the location of all seven cameras and their 'square of influence.' These cameras are placed by the player. As the squares can overlap, how do I figure out how much of the level is covered- since it's not just the amount of area each camera *can* cover times the number of cameras in use.

• Could you use a invisible grid of smaller squares and then using "is not overlapping" divide the uncovered grid squares from the grid squares overlapping and then multply by 100 to get a percent?

• Since I needed to take a break from my current project, and enjoy interesting challenges, I worked up a demo and posted a Capx example:

Calculating Rectangular Coverage Area

Hopefully it has everything you need.

• This is really cool! But I have a question: in event 11, you set TestRectangleRight to (Xcoordinates.At(loopindex("X")))+(Xcoordinates.At(loopindex("X")+1)-Xcoordinates.At(loopindex("X")))

Doesn't this always evaluate to the same as Xcoordinates.At(loopindex("X")+1)?

Approximately the same expression appears for TestRectangleBottom.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• in event 11, you set TestRectangleRight to (Xcoordinates.At(loopindex("X")))+(Xcoordinates.At(loopindex("X")+1)-Xcoordinates.At(loopindex("X")))

Doesn't this always evaluate to the same as Xcoordinates.At(loopindex("X")+1)?

You are correct. The overly complex expression was left over from a slightly different calculation method. I've updated the Capx. Thanks for pointing it out.

• Great example! I would have headed towards some sort of tilemap based solution, but this is far more elegant and efficient.

• Great example! I would have headed towards some sort of tilemap based solution, but this is far more elegant and efficient.

It is super elegant and very efficient!

I added it to a .capx with just about 10 events, given that the existence of the objects and their overlap was already there.

Now, though, I must decide; since my cameras can potentially rotate... do I prohibit rotation? Do I ignore their rotation, and simply estimate coverage? (if this algorithm finds a square rotated 45 degrees, for example, it will treat it as a square covering twice the volume.)

Do I allow rotation up to a certain number of degrees?

• Thanks

The algorithm uses the fill object's bounding box, which are always rectangular regardless of the objects rotation. To answer your question directly, yes, the algorithm would double the covered area:

If you need precise coverage calculations for rectangles (or other polygon) that allows for rotation, you'd need to break the entire area into triangles based on each rectangle's vertices, and test if each triangle overlaps the fill object. It's exponentially more difficult, though not impossible.

If you only need semi-accurate calculations, you could do as others have suggested and create a small sprite, or high-density tilemap, and loop through the entire area checking for overlaps. You'd probably end up balancing accuracy vs. efficiency.