Advertisement

Side-Scroller Movement in a Tile-Based Game

Started by June 16, 2019 10:02 PM
7 comments, last by akshayMore 5 years, 5 months ago

Hello - 

I've been in the process of writing my first game, and have just finished my tile rendering engine. The next step that I need to take is to implement a moveable character, and I have no idea where to start.

 

The character in the game is larger than the block tiles, meaning I can't do anything along the lines of creating a character tilemap over my block tilemap (of the same size) and moving between those tiles.

 

I do not want the character mobility to be restricted, I want it to move relatively freely throughout the world (with physics, of course). The only examples I've seen have involved making a smaller tilemap (i.e. a map 2x smaller) for the character to traverse. The issue with this is that in order to get the level of freedom I would like, that map's tiles would be way too small, and would take up far too much memory.

 

Thanks for any insight.

A common response to this sort of question would be to recommend using an existing physics engine, such as Box2D. Would that be an option?

If not (or if you just prefer to do it yourself), some more info might be useful, such as programming language, target platforms, libraries used, etc., and exactly what type of movement you're looking for (e.g. platformer).  Maybe you don't want to share your art at this point, but some screenshots showing the tilemap and character(s) might be helpful as well.

Advertisement

Can't you check several tiles for movability every time the character moves?

On 6/16/2019 at 6:58 PM, Zakwayda said:

A common response to this sort of question would be to recommend using an existing physics engine, such as Box2D. Would that be an option?

If not (or if you just prefer to do it yourself), some more info might be useful, such as programming language, target platforms, libraries used, etc., and exactly what type of movement you're looking for (e.g. platformer).  Maybe you don't want to share your art at this point, but some screenshots showing the tilemap and character(s) might be helpful as well.

Yeah, Box2D would be an option. 

As for tools, I have been using Haxe and OpenFL. For art, I have been using custom 8x8 tiles that I've scaled up to fill in 16x16 space. Right now all I have is a tilemap that is very basic, with a layer of grass, section of dirt, and section of stone.

On 6/16/2019 at 9:46 PM, Alberth said:

Can't you check several tiles for movability every time the character moves?

By this, do you mean have a sort of "reference tilemap," that isn't used unless there is a move input?

I have a few tips for you.

 

1.) Sonic physics guide: http://info.sonicretro.org/Sonic_Physics_Guide

In the past this helped me implementing pure tilebased platformer physics successfully.

2.) Box2D as your pure physics solution

This has a lot of pitfalls: Friction, Sensors, Ground tracing, etc. Also you may need to convert the solid tiles to connected line segments, otherwise you may encounter ghost collisions when you just create one body for each tile. Doing this conversion is not that easy, so i have written a library for that: https://github.com/f1nalspace/final_game_tech/blob/master/final_tiletrace.hpp

3.) Box2D but dont use dynamic bodies for all entities, use kinematic bodies and implement all the movement code yourself.

4.) Speculative contacts: https://wildbunny.co.uk/blog/2011/12/11/how-to-make-a-2d-platform-game-part-1/

Great solution, especially for platformer - with very easy math. The only hard part is to write a stable contact generator for angular dynamics. If you dont need angular dynamics, then its really easy.

 

11 hours ago, ItsQuesoTime said:

By this, do you mean have a sort of "reference tilemap," that isn't used unless there is a move input?

You have tiles. Your character occupies several tiles. So to move eg left, you check passability of all tiles that will be occupied when you would move left. If one of them cannot be passed, you cannot move to the left.

In general, given a position of the character at the tile map, you can compute which tiles are covered by it. You can compute this on the fly, or in advance (and in that case store that information in another map). You do this every time the character moves relative to the tilemap.

If the tilemap itself also has moving parts (eg platforms), you'll need to check more often.

Concretely (in Python):


# Character positionb and size.
xpos = 125
ypos = 18
xsize = 27
ysize = 47

# Size of a tile
tile_xsize = 16
tile_ysize = 27

# At which tile is position (x, y)
def tile_at(x, y):
    return x // tile_xsize, y // tile_ysize

topleft_x, topleft_y = tile_at(xpos, ypos)
bottomrigt_x, bottomrigt_y = tile_at(xpos + xsize - 1, ypos + ysize - 1)
print("top-left of character at tile {}, {}".format(topleft_x, topleft_y))
print("bottom-right of character at tile {}, {}".format(bottomrigt_x, bottomrigt_y))

print()
for tx in range(topleft_x, bottomrigt_x + 1):
    for ty in range(topleft_y, bottomrigt_y + 1):
        print("Test tile {},{}".format(tx, ty))

# Output:
# top-left of character at tile 7, 0
# bottom-right of character at tile 9, 2
#
# Test tile 7,0
# Test tile 7,1
# Test tile 7,2
# Test tile 8,0
# Test tile 8,1
# Test tile 8,2
# Test tile 9,0
# Test tile 9,1
# Test tile 9,2

 

Advertisement
16 hours ago, Finalspace said:

I have a few tips for you.

 

1.) Sonic physics guide: http://info.sonicretro.org/Sonic_Physics_Guide

In the past this helped me implementing pure tilebased platformer physics successfully.

2.) Box2D as your pure physics solution

This has a lot of pitfalls: Friction, Sensors, Ground tracing, etc. Also you may need to convert the solid lines to connected line segments, otherwise you may encounter ghost collisions when you just create one body for each tile. Doing this conversion is not that easy, so i have written a library for that: https://github.com/f1nalspace/final_game_tech/blob/master/final_tiletrace.hpp

3.) Use Box2D but dont use dynamic bodies for all entities, use kinematic bodies and implement all the movement code yourself.

4.) Try Speculative contacts: https://wildbunny.co.uk/blog/2011/12/11/how-to-make-a-2d-platform-game-part-1/

Great solution, especially for platformer, with very easy math. The only hard part is to write a stable contact generator for angular dynamics. If you dont need angular dynamics, then its really easy.

 

I forgot, there is a fifth solution:

5.) Use handmade hero style physics

In the very beginnings of the video series handmade hero, casey built a very easy and stable custom physics solution which relies  heavy on minkowski difference and velocitiy projection. I used that technique in a platformer project a few years ago successfully, but i never got moving platforms working. If you got time, watch the episodes Day 042 to Day 050, which explains equation of motion, line segment intersection tests and other stuff in very detail: https://handmadehero.org/watch

I am working on this game:

 

 

I assume that this is the type of character movement and the side scrolling is what you want. I hope I am right.
There is character position and there is world(map) position.
Let us say that at the beginning the character and the map is at origin (0,0).
When character moves left , the map moves right by the amount at which character has displaced from origin.
Similarly ,
when character moves right , the map moves left.
when character moves up , the map moves down.
This gives an illusion of scrolling in a tile-based environment.
Try keeping tile objects under one class (say Map). Now give the map a position vector variable. Now let all the tile objects use the map position as reference point(or origin).
Also, It is better to keep a track of player position on screen and player position on map. It will help.
 

 

This topic is closed to new replies.

Advertisement