Jump to content
  • entries
    14
  • comments
    81
  • views
    38,484

7800basic tutorial - sprite and tile interactions


RevEng

2,043 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.

 

blogentry-23476-0-28846800-1431882359_thumb.png

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.

 

blogentry-23476-0-87098500-1431878646.png

 

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.

 

blogentry-23476-0-59312100-1431878661.png

 

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.

  • Like 8

3 Comments


Recommended Comments

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?

Link to comment

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.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...