Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / How do I make parallax scrolling backgrounds in 2D?

Author
Message
Programmer2000
14
Years of Service
User Offline
Joined: 19th May 2010
Playing:
Posted: 19th May 2010 09:35
I have three layers of images for a 2D game that I want to use parallax scrolling for. The first (sky) layer does not move, the second (background) layer moves at a constant speed, and the third (foreground) layer moves at twice the background's speed.

Can someone please share some code on using two moving layers as backgrounds?

Here's the code I've been using for scrolling one background at a time. I got it from another thread on this forum:

zeroSlave
15
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 19th May 2010 22:08
I don't really know much about sprites, but if I was going to do it I would do it on planes and scroll the texture. I'm sure there is another (probably better ) way of doing it, but try looking at:



I also attached the pictures used in this example. Nothing fancy, just some quickly(poorly) drawn backgrounds.

My green thumb grew the tree my Trojan War horse was crafted from. With roses in our pockets we rally round the tombstones. Ashes to ashes, we all fall down.

Attachments

Login to view attachments
Programmer2000
14
Years of Service
User Offline
Joined: 19th May 2010
Playing:
Posted: 20th May 2010 06:39
That looks like it will work. Thanks. The included backgrounds are pretty good too.

Would you happen to know of a way to use parallax in 2D, by any chance?
zeroSlave
15
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 20th May 2010 19:08
Glad you like the backgrounds!

I'm not sure about 2D. I have spent most of my DB time learning 3D and all of the little bits that go with it like collisions, cameras, lights, and such. Somebody, please correct me if I'm wrong, but I'm pretty sure that even when using sprites, either DB or DirectX just puts an image on a plane and calls it a sprite.

I tried looking around the help file for scrolling images, but couldn't find anything in the image or sprite commands. If you still wanted to use sprites for this, you will probably have to position each layer of the background individually and then clone each one and put it next to it depending on which side of the screen the sprite is going off.

My green thumb grew the tree my Trojan War horse was crafted from. With roses in our pockets we rally round the tombstones. Ashes to ashes, we all fall down.
Programmer2000
14
Years of Service
User Offline
Joined: 19th May 2010
Playing:
Posted: 20th May 2010 20:18
Well, I was planning on putting sprites in the foreground ("BackGround4.png"), even if the background is a 3D plane. And enemies would be in the foreground too.

I could probably still do that with 3D, right?
zeroSlave
15
Years of Service
User Offline
Joined: 13th Jun 2009
Location: Springfield
Posted: 20th May 2010 23:25 Edited at: 20th May 2010 23:37
Most definitely! Take for example (from above):


The syntax for POSITION OBJECT is ObjectID, X Position, Y Position, Z Position. Since The camera's Z Position is -400, which was set in the beginning of the code, the lower the Z Position of the planes are, the closer to the camera it is. This kind of layers it.

The first iteration of the FOR NEXT loop, makes LoadBG = 1. So the Z position in POSITION OBJECT LoadBG, 0, 0, -0.1 - LoadBG is -1.1

The next iteration of the FOR NEXT loop, makes LoadBG = 2. So the Z position in POSITION OBJECT LoadBG, 0, 0, -0.1 - LoadBG is -2.1

etc.

Also, I am pretty sure you can still use sprites on top of all the 3d stuff in the background if you want to go that route.

My green thumb grew the tree my Trojan War horse was crafted from. With roses in our pockets we rally round the tombstones. Ashes to ashes, we all fall down.
Programmer2000
14
Years of Service
User Offline
Joined: 19th May 2010
Playing:
Posted: 21st May 2010 00:52 Edited at: 22nd May 2010 19:35
Thanks for all your help!

Login to post a reply

Server time is: 2024-11-24 15:55:38
Your offset time is: 2024-11-24 15:55:38