# Checking if Body is surrounded by other Bodies

I'm trying to make a game like Rampart as practice. I have gotten to the point where I want to check if the castle is surrounded by walls and decide whether to fill in the area and continue the game, but I have no idea how to do this. I have thought about ray-casting from the castle but it wouldn't work if there was a bend in the walls.

So any help or suggestions would be great!

• Is this the game you are referring to? Apr 6, 2015 at 21:47
• Yes it is! I Think It Is Cool! Apr 7, 2015 at 1:30
• How are your walls represented in box2d? b2EdgeShapes? b2ChainShapes? b2PolygonShapes? Also, are you using bodies with single fixtures or multiple for the walls? Are there discrete locations where walls can be placed? (e.g. only at 90 degree angles and can only be placed at half meter increments) Or can walls be placed anywhere? A screenshot would really help too. Apr 7, 2015 at 20:48

I think easiest way to solve this is to use very simple maze solver tactic. That is the "wall follower".

• Find wall
• Put your hand on the wall, either left or right of you
• Keep hand on wall
• Start moving
• Never let go off the wall, if there is a turn, you turn with the wall and if its "dead end" you turn but you still keep your same hand on the same side ( ie. left or right of you )

Eventually, you will find your way out.

So, how does this help you? You have to check after every wall placement, if you can walk around the wall ( either inside or outside, does not matter ) by walking near the wall until you get back to your starting point. However, it's easier to follow from inside so you can check the location of you newly place wall and go tile by tile towards the castle and find empty spot. That way you know you should be inside the walls.

1. Place wall
2. Find open spot near the wall
3. Check where the wall is considering your position now ( up,left,down or right)

Then, for example if

1. Wall is below you
2. Move left
3. If there is wall below you, goto 5
4. if there is NO wall below you, there might be turn to down, so move down.
5. There should be wall on the right side now, if not break out and fail
6. Move down with the same logic.

Google for: wall follower algorithm or something like that, to find examples

Maze_solving_algorithm -wikipedia

Keep the hand on the wall.

Floodfill from the castle.

• If the fill reaches an edge of the map: castle is not surrounded by walls
• If the fill does not reach the edge of the map: castle is surrounded by walls

Alternatively, you could use a pathfinding algorithm to check if there is a path between the castle and a point that must be outside the walls.