🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Tiles and animating movement between them?

Started by
1 comment, last by SyncViews 3 years, 1 month ago

My project has tiles (think dungeon crawler) and all positioning and movement is in discrete x/y values so that everything happens in the middle of the tile. This is a chosen style/aesthetic plus a coding convenience. With clever function calls and drawing order I can operate entirely on the grid system and push x/y coordinates back to lower level drawing and rendering functions (I'm using native Love2d/lua - no game engine).

I now need to work out movement and animation between tiles where an object/sprite is travelling between tiles and not aligned to my tidy x/y grid. Not only that, collision detection and interactions might happen during these transitions.

For example, a wolf is on tile (1,1) and needs to move to (2,2). I'm guessing the (1,1) needs to be converted to a graphics/screen x/y for drawing purposes. I'm then thinking vectors and vector math can move the sprite to the destination. At that point, it is 'safely' centred on the tile and can once again exist in the tiled world of (2,2).

An additional challenge is having the player target the wolf because it won't be in any one tile so I guess the 'targeting cursor' needs to free roam between tiles.

If the player shoots towards the wolf then I guess normal bounding box comes into play, again, abandoning the tile system.

Am I thinking about this the right way?

Advertisement

If game logic can happen between tiles so “offgrid”, including physics, I think you should just have the objects position be a fixed or floating point value. You can still restrict movement to be along the grid (either 4 directions or 8 directions) and have movement end only fully in a cell.

If mostly just visual, you could move the object to the destination cell instantly, then have additional fields for the previous cell, how long movement will take, and how long as elapsed already. Then you can interpolate the position to draw. So collision detection etc. will be immediately using the new tile.

visual_pos = last_cell + (new_cell - last_cell) * (time_elapsed / time_to_move)

togfox said:
An additional challenge is having the player target the wolf because it won't be in any one tile so I guess the 'targeting cursor' needs to free roam between tiles.

There's a couple of options:

Either target objects (like the wolf), not tiles (or, optionally, tiles could be objects, in which case you'd have fixed targets + moving ones on top of them).

Alternatively, since you mention that you only need movement interpolation for drawing, you can still target tiles and forget about a fancier targeting system - you could simply consider the wolf to be in tile A during the first half of the move and and in tile B once it passes the midpoint. A simple lerp operation like what @syncviews suggested should be good enough.

togfox said:
Am I thinking about this the right way?

There's no right or wrong way - just make your game the way you want it to play ?.

This topic is closed to new replies.

Advertisement