Sprites


Following on from the previous devlog.  Mode 7 allows drawing a floor plane, but does not allow drawing any objects (such as the player, enemies, bullets, etc). 

Drawing the player is surprisingly simple, the player's ship can be drawn directly as a 2d image onto the middle of the screen. This gives a fake 3rd person perspective relatively for free.

The same can't be said for enemies. Enemies and other objects have x and y coordinates on the 2d level (texture) but there's not easy way to use the shader code (which works in pixel scanlines) to determine if an enemy sprite should be visible or not. 

Below is an image representing the problem. Given 2 objects (red, blue) how do you determine where on the screen should they be drawn? You know their position in the game's world, but need to work out the screen x,y (yellow lines) and scale to give a fake 3d effect.


Luckily, this can be achieved using algebra and some pen and paper. The shader code can be inverted mathematically, by treating the code as a formula and solving in the opposite way. Given an x,y position on the level (texture), and a known camera location, the screen x,y can be computed:

Using some trigonometry, it can be computed if the sprite is within the visible field of view, and if so what it's x,y should be on the screen.

Then the sprite can be drawn at that location. Sprites further from the player are scaled down to appear smaller. 

A keen reader might notice that the distance from the player is never actually used in the sprite scale calculation! instead, only the screen position is used. A bigger "y" value on the screen means the sprite is closer to the player, so the scale can be computed with a simple ratio of how far down the screen the sprite is! 

Leave a comment

Log in with itch.io to leave a comment.