Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Representing a Gameworld that is Irregularly shaped

Tags:

c#

I am working on a project where the game world is irregularly shaped (Think of the shape of a lake). this shape has a grid with coordinates placed over it. The game world is only on the inside of the shape. (Once again, think Lake)

How can I efficiently represent the game world? I know that many worlds are basically square, and work well in a 2 or 3 dimension array. I feel like if I use an array that is square, then I am basically wasting space, and increasing the amount of time that I need to iterate through the array. However, I am not sure how a jagged array would work here either.

Example shape of gameworld

X
XX
 XX   X XX
 XXX XXX
  XXXXXXX
XXXXXXXX
 XXXXX XX
   XX   X
  X

Edit: The game world will most likely need each valid location stepped through. So I would a method that makes it easy to do so.

like image 888
Aaron M Avatar asked Jun 03 '10 18:06

Aaron M


2 Answers

There's computational overhead and complexity associated with sparse representations, so unless the bounding area is much larger than your actual world, it's probably most efficient to simply accept the 'wasted' space. You're essentially trading off additional memory usage for faster access to world contents. More importantly, the 'wasted-space' implementation is easier to understand and maintain, which is always preferable until the point where a more complex implementation is required. If you don't have good evidence that it's required, then it's much better to keep it simple.

like image 89
Dan Bryant Avatar answered Sep 21 '22 20:09

Dan Bryant


You could use a quadtree to minimize the amount of wasted space in your representation. Quad trees are good for partitioning 2-dimensional space with varying granularity - in your case, the finest granularity is a game square. If you had a whole 20x20 area without any game squares, the quad tree representation would allow you to use only one node to represent that whole area, instead of 400 as in the array representation.

like image 39
Jordan Lewis Avatar answered Sep 23 '22 20:09

Jordan Lewis