Miscellaneous Tricks

For this "Tech" I want to relate some tricks that I've employed on Decaying Orbit that might be of use to others. If I don't describe something adequately please contact me.

You can easily make a crude shadow for any object by placing another object on top of it. This "shadow" object is composed of varying shades of gray. By turning on transparency and using transparency #2 the main sprite colors will be subtracted by the second sprite, creating the appearance of a shadow. See Figure 1 below as an example.

 Figure 1 - =

If your main object can rotate then things get a bit more complicated. Since your light source will probably remain constant you'll need some way to change the shadow to reflect this. In my case this is simple. I use a black background so that I can just make my shadow sprite large enough to encompass the whole object. Since we're using a subtracting transparency any excess shadow will not show up (black - anything = black). If you use a non-black background then you'll want your shadow to be exactly the same size and shape as your object. That sounds tough, glad I don't have to do it :)

A lookup table for angle conversion

Since Decaying Orbit relies on relatively accurate physics I needed some way to convert an angle to a unit vector. I use a look-up table which contains the SIN and COS of all angles from 0 to 89. Based on the input angle I select an entry. If the angle does not lie in the first quadrant (from 0 to 90 degrees), I negate the X and/or Y components as appropriate.

I also have a function which converts a vector back to an angle. It performs a quick-sort type of search in the table to find the correct value. To do this I must first convert the input vector to a unit vector by dividing the X and Y components by the length. Of course this isn't very quick (CPU-wise) so I try to avoid using this function whenever possible.

Bit-wise animation control

To make the game more varied I want my objects to animate at different speeds. I handle this by using a separate 16-bit value for each different animation sequence. Every frame this value gets shifted to the right by 1 bit. Then if the least significant bit is a 1, that object goes to the next frame. When the value becomes all 0's I copy it back from the original and start over. By changing the pattern of 1's and 0's I can control how fast each object animates.

For instance, for a fast animation I would have all 1's (b1111111111111111). This causes it to animate every frame. For half-speed I would alternate 1's and 0's (b1010101010101010). Note that using this method I can also use a value of 0b0000000000001010 (or even 0b0000000000000010). This is because once the last bit is shifted out the bit-pattern would get restored from the original:

```Current Frame #   Next Frame #   Animation Bit Pattern
---------------   ------------   ---------------------
0                 0          0b0000000000001010
0                 1          0b0000000000000101
1                 1          0b0000000000000010
1                 2          0b0000000000000001
2                 2          0b0000000000001010 <- Gets recopied
2                 3          0b0000000000000101
3                 3          0b0000000000000010
3                 4          0b0000000000000001
```

So to animate every third frame I would use 0b0000000000000100. Every fifth frame would be 0b0000000000010000. And so on.

Using this method I can easily tweak the animation speeds of any object in the game with little effort.

Top

This web page and all other pages on this site are © 1999 Scott Cartier