Pages

Thursday, November 26, 2009

Parallax scrolling

I want the camera to track the player as they move about the level. GM supports views whereby the user can specify which object to follow (in this case the player vehicle). However just panning the screen around with the player vehicle in the centre of the screen would look dull.

To add a bit of depth to the levels, I also want the levels to be constructed from 3 distinct layers. A foreground, the middleground where the action actually takes place, and a background. The foreground and background images are simply there as graphical eye candy to add a bit of parallax scrolling. This is fairly straightforward to do in GM.

The level is constructed as a room in the usual way. Once you've enabled views, you can specify which portion of the room you want to see on screen. If you have specified the player vehicle in the Object Following field, in simple terms you can get this portion (or view) to slide about the room under the players control.

I have two objects called Background_Scroller and Foreground_Scroller and these must be placed in the room.

Background_Scroller
Depth: 10000
Draw call:
draw_background_tiled(bgnd_image_mountains, view_xview*0.6, view_yview)

Foreground_Scroller
Depth: -10000
Draw call:
draw_background_tiled(foregnd_image_01, -view_xview*0.6, view_yview)

NOTES:
The two draw calls detailed above simply draw the backgrounds, but at different 'scaled' offsets in x. As the player moves the view around, so the value of view_xview changes and hence the speeds of the scrolling of each changes. Hence the parallax.

bgnd_image_mountains and foregnd_image_01 are images defined as backgrounds.

Objects have depths which are simply values used to control the order in which things are drawn (think of this as Z-depth). Remember X is across the screen, Y is up and down the screen, and Z is in and out of the screen. Things that have a +Z are into the screen, whilst things that have a -Z are out of the screen.

My Background Mountains want to be drawn first (far away into the screen) and hence have a large positive value of ZDepth.

My Foreground wants to be drawn last (nearest to the camera and furthest out of the screen) and hence has a large negative value of ZDepth.

No comments: