Author Topic: Parallax scrolling  (Read 3275 times)

0 Members and 1 Guest are viewing this topic.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Parallax scrolling
« on: November 03, 2010, 02:28:33 PM »
I had this idea that it would be really cool to create a backdrop for Eufloria levels.  Perhaps a high-tech looking green grid.  What I wanted is to create a sort of parallax scrolling effect.

Here is an example of one-dimensional Parallax Scrolling:




As well as the foreground, there are a few different layered backgrounds, and as you travel through the level, they move with the terrain - but at slower speeds, as if they were far away.


Any parallax scrolling system for Eufloria would necessarily need to be 2D because you can scroll up and down, as well as left and right.
Also, you would need some way of deciding what to do when the user zooms in and out.

My feeling is that the backdrop should zoom in with the rest of the game field, but to a lesser degree...governed by that particular backdrop layer's depth value.  That means if the screen is really zoomed in, the zoom value will be something like 0.05 and the X and Y values will end up being extremely high - resulting in a slightly closer look at the backdrop when zoomed in, and a slightly further away view when we're zoomed all the way out.



I propose the system be split into 2 parts; the Paralax engine itself, and a section that contains the code for the backdrop artwork.  The parallax engine's job would be to shift each point that is drawn on the backdrop by an amount proportional to the various offsets and zoom levels that we are monitoring.  By splitting it in this way, you could create an artwork "template" and that would allow others to create their own artwork to use as parallax layers.




1) PARALLAX ENGINE

Read the parameters for the current view; how many layers have been defined?  What are their depths?  How zoomed in are we?  Where is the screen centred?.  Use these to calculate vertical and horizontal offsets for each layer as follows:

Left or Right by an amount equal to the horizontal screen offset from the center divided by the amount we are zoomed in or out.
Up or Down by an amount equal to the vertical screen offset from the center divided by the amount we are zoomed in or out.

Then, for each point that we wish to draw, divide both X and Y values by the SHIFT VALUE.  The SHIFT VALUE is calculated by taking the zoom value and dividing it by the layer depth value for that particular layer.  This is necessary so that we shift far away (ie deep) layers less than layers close to the foreground.


2) BACKDROP ARTWORK

Render each backdrop layer by creating a standardised vector image with all the drawing points as parallax-engine-approved variables, declaring a layer depth for each parallax layer, and shifting each "draw point" by the Shift Value calculated by the engine.






It would be possible to add multiple layers with different depths, I think.  For example, for the deepest layer, you could have a starfield, and for an intermediate layer you could have a green Tron-style grid.  This would look really awesome when scrolling around the map, hopefully.


To pad this idea out some more, I'm going to attempt to list the variables you would need to keep track of.

First of all, you would need a complicated data structure to store the information about the artwork of the parallax layer..
You could create a couple of matrices for this - lets call them matrix X and matrix Y.  Lets take Matrix X as our example, each column in this would represent a whole parallax layer, and each cell within a particular column would contain sequential drawing point coordinate pairs.

For example, if I have a parallax layer containing a single line drawn from coordinates 50,50 to coordinates 100,100, and a second parallax layer containing two lines - one from 125,500 to 15,-400 and the other line -100,350 to -75,175, this is what the matrices would look like:

Code: [Select]
Matrix X
| 50 | 125|
| 100| 15 |
| nil|-100|
| nil| 175|

Matrix Y
| 50 | 500|
| 100|-400|
| nil| 350|
| nil| 175|

Then we know that to draw the line, we just work down the column and use odd numbers as the draw start point, and even numbers as the draw end point.

Columns = parallax layers
Pairs of values going down each column = the coordinates of the lines.


Ok, so what other variables are there?  Well, we need to know how zoomed in we are.  GetCameraZoom() I believe returns a value for that.
We need to know where the screen is centred - we need to know for both X and Y.
We need to know the depth of each layer because this is needed in order to calculate the Shift Value.

Ultimately, we divide each draw point by the Shift Value, and this is what causes what is drawn onscreen to move about realistically like something very far in the distance might do.


Please let me know whether you think this would work the way I am proposing, or if you can spot any flaws in my pseudo-maths.  :>

Sniped50

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 97
  • Don't ask. About anything.
Re: Parallax scrolling
« Reply #1 on: November 03, 2010, 06:35:57 PM »
Wow... The room's spinning...

Seriously though, I can't see any flaws - mostly because I couldn't be bothered to read most of the text. I'd prefer numbers and equations where possible, if you'd be so kind. 8)

On a side note, you said the artwork requires vector coordinates that the engine could read? Does that mean that simply having some asteroids in the background won't work, or would their coords work fine? Plus, how many layers do you think might be possible; 5, 10, 25, or more?
"Sometimes, the simplest solutions work the best."
- Mythbusters

"But the complex solutions look prettier."
- Me

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Parallax scrolling
« Reply #2 on: November 03, 2010, 07:05:57 PM »
I tend to conceptualise and generalise first, and fill in numbers later.  I mean... I don't yet know what sort of scale of layer depth would look good.  Maybe 0.1... maybe 0.01.... who knows.  I'll have to play around with the engine and see.  :>  The layer depth affects how far away that particular layer will look when you're scrolling around/zooming in and out.  Is there some specific numbers you were after?

I did fill in some pseudo-equations, though.  Just that they are kind of in sentence form.. :p

Regarding using asteroids as parallax layers.  This WOULD work, but the asteroids would still be selectable by the player while they are moving around, which would look kind of wrong.  So after a lot of thought about this I figured it's probably best to keep asteroids as foreground-only, interactive objects like they normally are.
It would mess with your head though.  You would be able to change the layout and scale of the level simply by scrolling and zooming around it....  It would be like some kind of crazy Escher thing :p

I am not sure how many layers would be possible, it depends on how efficiently drawings are rendered by the game on each game cycle, and also on the complexity of the layers.  If you only had 1-2 lines in each layer, it would handle that rather easily, I imagine.. :>  But if you have complciated vector art, EG 400+ lines, it might not manage so many layers without slowing down a lot.

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 17
  • Posts: 899
  • Eufloria: Yes
Re: Parallax scrolling
« Reply #3 on: November 03, 2010, 07:39:41 PM »
Would a MASSIVE LevelDraw() work? I can't work on it right now - college is being...well, college, so this is only guesswork. You could draw something a bit like the rally button with a few tweaks, though it would be on top of the asteroids.

Sniped50

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 97
  • Don't ask. About anything.
Re: Parallax scrolling
« Reply #4 on: November 03, 2010, 11:33:52 PM »
I'm not looking for specific numbers, annikk. I just prefer maths over english as school subjects. It's much easier to understand... :P

As for the background asteroids, I thought you could make them non-selectable. Or did I fall asleep at the computer that time... ? Anyways, even if you can't do that, you could actually make your own(admittedly crude) 3D levels. Oh, how cool would THAT be? Hmm?!? :D

The resulting levels might make my head spin even more though...
"Sometimes, the simplest solutions work the best."
- Mythbusters

"But the complex solutions look prettier."
- Me

Terrial

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 66
Re: Parallax scrolling
« Reply #5 on: November 04, 2010, 03:31:51 AM »
HA! annikk!!! you never cease to amaze! 

bryseron

  • Seed
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
Re: Parallax scrolling
« Reply #6 on: November 04, 2010, 09:23:07 AM »
Sounds incredibly cool.

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Parallax scrolling
« Reply #7 on: December 31, 2010, 03:39:38 AM »
I made a start on this tonight.  :>

It looks freaking awesome.

Main problem right now is that it doesn't look right when you zoom in and out.  For panning around it looks sick though :D


UPDATE: Oh god...Why can't I just pick something easy...
« Last Edit: December 31, 2010, 04:23:50 AM by annikk.exe »

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Parallax scrolling
« Reply #8 on: December 31, 2010, 06:02:00 PM »
Got it working :>

This is something really quite special.  I am super-excited to show it to you guys, but I'm going to tweak the artwork a bit first to make it look as nice as possible.  :>

Widget

  • Sapling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 98
Re: Parallax scrolling
« Reply #9 on: December 31, 2010, 07:40:57 PM »
Wow! Can't wait to see what you've managed to make of it. You never cease to amaze me  ;)

Pilchard123

  • Tester
  • Old Oak
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 17
  • Posts: 899
  • Eufloria: Yes
Re: Parallax scrolling
« Reply #10 on: December 31, 2010, 09:11:26 PM »
You don't go around under "omnicoder" anywhere do you? He's sickeningly good at scripting/programming (though he doesn't use use your chevron-smiley, so I'm guessing the answer's 'no').

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Parallax scrolling
« Reply #11 on: December 31, 2010, 09:40:46 PM »
That's a negatory.  annikk.exe is my main screen name, though there are a few others I use for certain things.  Omnicoder is not one of them.


The stars disappear when they go behind an asteroid now.  However, doing that is extremely computationally expensive so I've had to scale back the number of stars as well as fade them out when the user has zoomed out a lot (because too many are displayed at once and the FPS drops to 1)

It's looking pretty fluid and nice at the moment.  The alpha fades out just before they disappear from teh zoom level which looks really sweet.


There's still something not _quite_ right with the zooming.  My pro 1337 hax coder cousin Joel is coming over tomorrow so maybe I can persuade him to have a look at it.  :>

AWS

  • Achiever
  • Arboreal Being
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 274
Re: Parallax scrolling
« Reply #12 on: January 01, 2011, 05:05:02 PM »
so... any sneak peeks at your progress so far? just a still image or 3 will suffice?!

 ;D

annikk.exe

  • Achiever
  • Ent
  • ****
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1,794
Re: Parallax scrolling
« Reply #13 on: January 02, 2011, 04:08:07 AM »
No previews yet I'm afraid.  It's getting better by the hour right now so I don't want to show you the shabby version when the final one is going to be epix.  :>

Cousin Joel came over today and we had a good look at the engine.  We've managed to improve it radically.  What was once 200 lines of code and comments is now about 40.  The engine is the most performance-oriented thing I have ever written.  With Joel's considerable help, we've been able to make it extremely efficient.  Whereas before, trying to display 500 stars would actually crash the game upon trying to load, now I can display 1000 stars and still get around 5fps.

The appearance of the stars has drastically improved too, as I discovered I could draw a single sprite that looks _exactly_ like a star.  It's a lot more star-like than the crosses I was using before, and has a nice soft glow around it.  :>

The next change will be to add variable numbers of parallax layers - currently it does only 4 layers, and I want 100 layers for a 1000 star map.  It's quite an undertaking because each layer has a number of attributes associated with it, namely Red channel, Green channel, Blue channel, Alpha, z-depth, and Fade-Out threshold.

Due to the number of layers involved, I may also then need to find some mathematical way of specifying the z-depth and fade-out threshold so that I don't have to write out 200 individual values, try them, discover it's a bit off, and have to redo them all.  :>

pigpenguin

  • Seedling
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 37
Re: Parallax scrolling
« Reply #14 on: January 02, 2011, 04:36:17 AM »
*cough* *cough* 100 layers?!?!?! you my friend aim high... may i ask why you would need that many layers?