How to effectively simulate a grid of static rectangles in a physical engine?

I am making a space shooter that takes place in a large dungeon that consists of large rectangles to define the walls. Everything in the game is physically modeled using Farseer Physics. However, there is one problem: I want the dungeon to look big enough, but this requires that my grid has at least 80x80 rectangles, which means that in the worst case, I have 6400 physically modeled bodies, which is not exactly how you can guess.

My temporary solution was to divide the grid into vertical slices, so that for each column, all the rectangles are added using the Boolean add operation, and then a body is created using the resulting concave polygon. This slightly increases performance, but polygons tend to go bad, become non-existent, block paths that usually need to be shared, or even become invalid and cause Farseer to crash.

I was thinking about creating some kind of algorithm that somehow finds the largest areas of the walls and combines them into one big rectangle, and continues to do this for small rectangles until all the holes are filled, but I have no idea how to implement this. This is the perfect solution because it solves performance issues, as well as the concave polygon that I have right now. Does anyone know how to implement something like this?

This is not a decision to stop using the physics engine at all, because many things in my game rely on it.

EDIT: Here is a small example of how bodies look right now: (each number is a body) http://i.imgur.com/6x06o.png

And this is how I want them to be:

enter image description here

+5
3

, , , , , .

, . - , , - .

  • , , . , . .
  • , . , , . , 1.

, , , .

, , , .

n*m O(n*n*m*m). , . , , ( ). , .

+1

( #, ):

, Wall, intergers x, y, width height, , .

, y height, (x₁ + width₁ = x₂).

, x y ( ) width height ( ).

, , . , , .

38 ( , , ):

36 :

+1

Farseer, EDGES , // , .

, , , , ...

1 FIRST edge:

Body dungeon = BodyFactory.CreateEdge(world, start, end);

Then we sort through all the other vertex coordinates sequentially, and then attach each new edge to the previous coordinate of the end of the edge. (linking the ribs together in sequence to completion)

FixtureFactory.AttachEdge(start, end, dungeon);

The result is 1 body instead of 35+.

0
source

All Articles