• entries
14
81
• views
38,547

# 7800basic tutorial - sprite and tile interactions

2,167 views

checking sprite and tile "collisions"

The 7800 has 2 types of display objects, sprites and character/tile graphics. Usually a game's display is made up of tiles for the background, and sprites for the moving objects.

As a game designer, at some point you'll probably want to check which tile(s) your game sprites are moving over, or about to move over. Maybe you need to check if pac-man is eating a dot, or if the adventure hero is going to hit a barrier.

Checking tiles underneath a sprite is slightly complicated by the fact that tile graphics use a different positioning resolution than sprite graphics do.

tile/characters coordinates overlaid on a 160x192 screen.

We can check which tile is under any single point on our sprite by taking the sprite coordinates and dividing the X and Y coordinates by X and Y conversion factors. The exact conversion factors you use will depend on the tile resolution you're using.

Lets take the example of a full screen of 320A tiles with a zoneheight of 8. The tile resolution for this format is 40x24.

The sprite positioning resolution is always 160x192, in all modes on the 7800, even the 320 ones. If we divide our max sprite resolution (160x192) by our max tile resolution (in this case, 40x24) we'll have the conversion factor.

In our example with 40x24 tiles, the conversion factor is 4x8. (160/40=4, 192/24=8 ). To convert any sprite coordinate to a tile coordinate, we'll divide the sprite X by 4, and the sprite Y by 8.

Now that we've converted the sprite coordinate to a character coordinate, looking up the character underneath that coordinate is a snap, with peekchar...

`lookupchar=peekchar(mapdata,tilex,tiley,40,24)`

The value returned will be the character index - literally the location that tile within the graphics block. If you're unsure which value you need to check for, you can always display the value returned by peekchar using the plotvalue command.

which points should you check?

If the sprites in your game are always aligned with your characters - if the sprites never overlap any tiles - then you can use any coordinate underneath the sprite. The one you use to plot the sprite is probably handy.

But most games have smoothly moving sprites, which can overlap multiple tiles in both X and Y. While checking every point underneath the sprite isn't computationally feasible, in these cases you'll probably want to consider more than one point when checking for movement collision.

To do a check to see if a sprite can move upwards without interference from character barriers, it's typical to check 2 points above the player - one at the top-left position of the sprite, and another at the top-right position - and see if both contain character #'s you have designated as "walkable". If so, then the player is allowed to walk upwards.

For other directions you'd check another 2 points. For example, when moving left, you'd check 2 points just left of your sprite, one at the top-left corner of the sprite, and another at the bottom-left corner.

Keep in mind that this isn't a recipe that you can adapt to every game. Other games will need to check other sprite points, as game mechanics require - the pac-man code checks the sprite point near the center of the pac-man sprite, to see if he's eaten a dot tile. Similarly, you'll need to decide which parts of your own sprites need to interact with the tiles.

• 8

Hi! Thanks for this tutorial, peeking after chars was a new concept for me and it's indeed a very good way to find platforms and walls etc.

That's a cool trick, Mike, but where in the .78b/.bas file to put it? Can it be part of a subroutine or has to be its own?

It can go anywhere - inline with your main loop, or within a sub that gets called from the main loop. The main consideration is that it needs to be part of your control handling code, since it allows/denies player movement in a given direction. As such you'd usually run it once per frame.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.