Game Development Community

How to code join the dots?

by Philip Mansfield · in Torque Game Builder · 07/06/2006 (4:50 am) · 2 replies

I'm in the planning stage of a new game, and I'm having trouble getting my head around the best way of determing if the player has joined some points on the screen together.

Imagine the play field is made up of a series of points. The player can move from point to point and leaves a trail in his wake. The player needs to use this trail to join points together to form a closed shape. The points aren't necessarily arranged in a simple grid fashion.

What's the best way determining if the player has joined together a series of points into a closed shape? Here's a little pic to demonstrate:
img146.imageshack.us/img146/6613/image12em.gifImagine the player starts on the blue square and the arrows indicate his movement. He will fill in square 1, and then square 2.

There are no rules to prevent the player from going over a line he has already drawn, so there may be one or two lines left 'orphaned' that don't enclose a square.

I know I will need to keep track of lines and their start and end points, it's just following those lines around the play field to determine if they enclose a shape that I'm having trouble with.

Any help on how to approach this would be appreciated :)

#1
07/06/2006 (6:43 am)
Digging in the back of my mind for old CS lectures... it looks like a convex hull problem. Search for it on google and you'll probably find more than you want.

With the points you've defined, the convex hull touches every point except for the crossover point. Think of an elastic band wrapped around pegs that you've put in each point. And once you have the hull defined, I'm sure there's a way to break up the shape into triangles.

Of course, if you do stick to a square grid pattern then you just look at the 4 corners of each cell and check if the points have been clicked on.
#2
07/06/2006 (7:45 am)
Right, I'll do a search for that and see what I turn up. Thanks :)