Working with a virtual barrier

Hello all, first time poster here with a pretty general question.
I apologize if it has been asked before, as I did look around before posting this.
I am working on a project in C++ where I have a virtual robot moving in a virtual map. The first step to this is defining an encompassing barrier wall, that is not limited in number of walls (as long as the figure is closed).
I am stuck on how to make sure my virtual robot will stay inside this area.

So if I defined a polygonal barrier to be the points (1,1),(0,-1),(-1,-1),(-1,2),(and back to 1,1) in a list, how could I determine if another point is inside or outside of the area given?
In this case (0,0) would return true, but (0,-2) would be false.

Also, any tips on how to make it not go past such a wall would be much appreciated!
You have a few options here. They both involve standard game collision detection methods.

1. Build a series of polygons that encompass the area your wanting your robot to be able to move in. Before each move, do a collision test to ensure it's still going to be in contact with this polygon.

2. Build your points and setup a virtual "fence". Then you can do the same as above, but if the next move means the robot is going to collide with a wall, then he is unable to move.

Do a google for "C++ collision detection" :)
You could also split your "world" into a grid, and just enable/disable certain locations. A lil bit more simplistic, but will achieve same results.
if((testx > leftboundary) && (textx < rightboundary) && (testy > topboundary) && (testy < bottomboundary))
return inside;
else
return outside;

Not so many parenthesis are necessary but I think they make it easier to read.

Use the same code to test where your robot will be before it moves, and then move it or not based on the return value.
This is a very simple form of collision detection.
Last edited on
@Malachi: Your method only works for a square. The OP's sample Co-ordinates are not a square. And he/she has explicitly said that it's a polygon barrier, not a square barrier.

Please read the questions carefully before posting answers.
oh my, you're completely right. I assumed. sorry :/
Wow, thank you for the quick responses! I will be sure to look around some more for collision detection info.
Malachi had the same initial response that I would have, which is why I asked the question- I kept coming back to an idea that wouldn't work in all the cases I need.

In the next few days I think I am going to try to implement a 'fence' type, and have a simple openGL app test. :-)
I came up with a bit of an odd idea, but I think it might just work. After looking over some collision detection stuff, I just sat down and scribbled out some ideas. The one I like the best is:
I take each pair of points and at the start of the program, calculate the slope between them. If at any time the robot's slope relative to either of these points is approx. the same, then there must have been a wall collision.

Now, say I want to add multiple floors. Each floor shares the same X-Y cords, but the Z axis differs. When the robot drops to a new level, there is an entirely different layout, such that (0,5) may be in-bounds on one floor, but out on the floor below. If it lands out of bounds it has to return to the previous floor. That was part of why I asked the previous question, but I wasn't specific enough on this part of it.

I haven't figured out if there is a clever way to locate this problem or not, though my weak understanding of
"1. Build a series of polygons that encompass the area your wanting your robot to be able to move in. Before each move, do a collision test to ensure it's still going to be in contact with this polygon."
appears to handle this case?
Topic archived. No new replies allowed.