Difficulty: Advanced
CPU Cost: High - dependent on number of asteroids and stars
1. Description- The Parallax Scrolling Engine lets you create stars that float behind the asteroids in your level, giving a faux-3D effect.
- This engine is the most difficult to understand and configure. However, in its default form it is cut back a great deal. You should be able to massively increase the number of stars that are displayed with some tweaking, especially if this is the only engine you are using in your level.
- This version is designed to display stars, but you can also use it to display other things. Lots of different visual effects are possible. The engine is set up to use Sprite Index 0 which in my view is the most star-like, however you can also set it to display other sprite indexes. Possibly with some interesting results.
2. Implementation- 1. Download the file attached to this post called "Parallax Engine.lua".
- 2. Open the file in notepad or whatever text editor you use.
- 3. Select all of the code and copy it to your clipboard.
- 4. Open your own level and scroll down to the very bottom, and paste the code in. It is imperative that it appears below everything else - including the "end" from your function LevelLogic(). The code you are pasting is two entirely seperate functions so if you inadvertantly put it inside function LevelLogic() then the map won't work.
- 5. At the bottom of your function LevelSetup(), insert this command: ParallaxInit()
- 6. Insert a LevelDraw function in between your LevelSetup() and your LevelLogic() using this command: function LevelDraw()
- 7. Ensure that you place an "end" after it, to indicate where the code in that function stops.
- 8. Inside your LevelDraw, insert this command: ParallaxEngine()
Now the engine is implemented, and will work immedietely. However, be sure to change the variables to your own design before releasing it.
3. VariablesThe Parallax Engine has a large number of variables. Some of the variables are arrays, so you will need to set values for all of the arrays too. Because some of the arrays can span hundreds of slots, you may wish to combine formulas and
For loops in order to set all the values efficiently, without having to manually specify each one.
This engine is not nice and friendly like gravity. If you forget to set one of the values, the level will probably not load.
The settings for the Parallax Engine all appear in the
ParallaxInit() function that you pasted in.
The section marked "User-Changeable Variables" roughly marks out the bounds of which variables are designed to be modified easily.
Single VariablesThese are just single values that you can specify to govern global properties of the engine.
- numberofstars - The total number of stars in your level. Be careful with setting this too high - stars require a lot of processing power, especially if you have a lot of asteroids in your level.
- numberoflayers - How many Parallax layers are there? The stars will be split equally among the layers, so if you have 50 stars and 10 layers, there will be 5 stars in each layer. You can set some attributes per-star, and other attributes per-layer.
- widthofstarfield, heightofstarfield - How large an area shall the starfield occupy? Change this to be optimum for your size of map. The engine always centres the starfield at coordinates 0,0.
- starsize - How big should each star be? 200 is good for average sized stars. Setting this value very high creates some interesting effects, but the higher the value, the greater the CPU cost.
- stoprenderingbelow - When you zoom out, the engine will start to reduce the alpha of certain layers (exactly which layers is definable in the arrays section below). When the alpha falls below a certain point, the engine will just stop rendering them completely, saving CPU. Set that threshold here. Allowed values 0-1.
Setup ArraysThis is where you specify the attributes for individual stars and layers.
All these arrays are required by the engine and their values must be specified by you.
Red, Green, Blue - How much of each colour in the star? 0,0,0 is black. 1,1,1 is white.
For example, if you have 3 stars:
red[0] = 1
green[0] = 0.2
blue[0] = 0.2
red[1] = 0.2
green[1] = 0.2
blue[1] = 1
red[2] = 1
green[2] = 1
blue[2] = 1
This code would make the first star red, the second star blue, and the third star white.
The number in square brackets refers to the individual star we are colouring.
If you have hundreds or thousands of stars, this method is not a good approach. Instead, set the values with a formula. For example, this formula would colour the stars different shades of grey:
for setcol = 0,numberofstars do
red[setcol] = math.sqrt(1 / numberofstars)
green[setcol] = math.sqrt(1 / numberofstars)
blue[setcol] = math.sqrt(1 / numberofstars)
end
How to make the right formula?
Well, first think of what sort of values you are going to need. I know that for grey stars I need the values for red, green and blue to be the same for each star. So the formula should be the same for each of them (which isn't always the case - it depends entirely on the effect you're trying to achieve).
Next, I know that the values should vary between 0 and 1. I also know that the one thing that I can rely on changing between one star and another, is its ID number. So we can use that as the seed of difference for our formula.
"
setcol" is going to vary between 1 and however many stars we have. Say it was 50. What could we do with the number 50 to get a value between 0 and 1?
One answer is to divide 1 by that 50, and use the result. But that's only 0.02 - if we have 100 stars then it means most of them will basically be black.
We need to find a way to increase the result we get, without making it stray above 1.
An ideal way to do this is to take the square root. The square root of 0.02 for the 50th star would be around 0.14, so its r, g and b values would be at 14%. That's a dark grey, but not black. The 10th star would be the square root of 0.1, which is about 32% - a gunmetal grey. So it looks like this formula would work well for creating stars with different shades of grey.
It's up to you to develop your own methods for setting the colours of these arrays. Lets move onto the next array.
threshold - How zoomed out do we have to be before each layer starts to fade out/disappear?
This is useful for keeping lots of detail when zoomed in, but only displaying a certain amount of stars when zoomed out so that the FPS doesn't suffer.
You need to specify a value for each layer. Unlike stars, the numbering of layers always starts on 1,
NOT 0.
You can specify values manually:
threshold[1] = 0.3
threshold[2] = 0.2
threshold[3] = 0.1
threshold[4] = 0.07
threshold[5] = 0.04
Or you can do it with some formula that you invent:
for setthresh = 1, numberoflayers do
threshold[setthresh] = 1 / setthresh
end
zdepth - How far away does this layer look?
This should normally be a value greater than 1. The higher the value, the closer the stars look.
If the value is equal to 1, the stars will look like they are so far away that they don't move with the camera at all.
If the value is less than 1, the stars will move backwards!
If the value is 0, the stars will not be rendered.
If the value is less than 0, the stars will seem to be in the foreground - in front of the asteroids (but they will still disappear when they actually move in front of one)
In the template, the line looks like this:
zdepth[numberoflayers - lnumber + 1] = 1 + (5 * (1 / lnumber))
You can replace the bit after the = and write your own formula, or you can replace that line entirely and specify the z-depths for all the layers manually:
zdepth[1] = 1.1
zdepth[2] = 1.4
zdepth[3] = 1.8
zdepth[4] = 2.5
...or with any other method of your choosing.
Additional ArraysAs well as the arrays that are required for startup, there are also two others that you may be interested in using.
They are
SetStarX and
SetStarY. These are the "given" positions of stars. The engine generates these automatically at the start of the level but there is no reason why you couldn't refer to it and change it during play using some code in your
function LevelLogic().
For example, if I want to make a star (lets say star number 50) move from side to side, I can do it with this:
function LevelLogic()
While GameRunning() do
SetStarX[50] = 1000 * math.sin(GetGameTime())
coroutine.yield()
end
end
That would cause star number 50 to drift left and right.
Using this, it's possible to make some - or all - of the stars move. You could move lots of them at once by employing a
for loop.
You can do a similar thing with the other variables and arrays. You could have stars that flash/twinkle, or pulsate, or anything you like. You can even have stars that come closer or move further away! (by changing the zdepth of their layer..)