Do I need to re-think moving over a 2D tile map?

Started by
2 comments, last by dmatter 16 years, 10 months ago
I'm currently having some issues with my 2D tile map and how units are moving over the map. At this moment, I have 2 pieces of data: 1 holds the actual map, and what each tile represents, and the other object holds which tiles are occupied by which unit. When a unit is placed on the map, it's added to the scene, and located on the tile that belongs to it's location. When a unit starts moving, it will check if it's target tile it free, and if yes, it will mark the tile are reserved and start moving towards it. After updating the position of the unit, the previous tile is 'freed' and the new tile is marked as in use. However: When a unit starts moving diagonally, it will at some point enter a tile it's not supposed to enter. Theoretically that shouldn't happen, but I blame it on round-off errors. However, this causes exceptions in case another unit or building sits on that tile. And in some rare case(Which I was trying to debug) a unit just runs over another unit, making it disappear. Should I revise my approach, or just stick with it and iron out the problems with diagonal movement? Since the code is becoming quite, large now, and I wonder if there are simpler alternatives.

Advertisement
Conceptually for me it's more intuitive to have a list in each tile of objects that occupy said tile.

However, I don't see anything technically wrong with your approach and it may actually offer better performance down the road. It just sound like you have some simple bugs in your implementation. You could probably trap the bug with asserts; i.e. assert if someone enters a tile that is occupied by someone else.

-me
Why not completely omit the "real position" of a unit in determining on which tile it is located. Just have a origin and destination tile when a unit is moving and interpolate the movement in between. And use the "real position" only for rendering purposes.
This way you can not have a unit accidentally move onto an unexpected tile due to rounding errors in its "real position".
Do you now where these rounding errors come from?
I would think that moving into an undesired tile is an unacceptable bug, so perhaps you need to rethink how youre calculating unit/tile positions

This topic is closed to new replies.

Advertisement