• Advertisement
Sign in to follow this  

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

This topic is 3869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement