Game Development Community

Platform game parallax scrolling

by Jacob Vann · in Torque Game Builder · 08/26/2005 (10:28 pm) · 5 replies

I am trying to get parallax scrolling working in my game.

Essentially, I would scroll the each tile layer as a percentage of the "main" tile layer's scroll speed. So, for example each new position of the background tile layer would be 25% that of the main tile layer.

There's no way to see what the pan position of a tile layer is (getPanPosition?). But I don't think this is what I want anyway, as pan position seems to be relative to the camera only. So I decided to capture the position of the camera, and base the background tile layer's position on the camera. Here's the code I have in my onUpdateScreen() function:

%position = sceneWindow2d.getCurrentCameraArea();
	%positionx = getWord(%position, 0);
	%positiony = getWord(%position, 1);

	%x0 = (%positionx / 4) - %positionx;
	%y0 = (%positiony / 4) - %positiony;

	%layer0 = $level.getTileLayer(0);
	%layer0.setPanPosition(%x0 SPC %y0);

This works great, and scrolls the background layer at 25% the speed of everything else. However, since the camera can move out of the bounds of the view (the view stops but the camera seems to keep moving), the background layer will scroll even while the rest of the layers are not scrolling.

I tried to see if there was an onWorldLimit() function for the camera, or some other way to know when the camera is off-center so I can stop scrolling the background layer.

I suppose I could get the camera position, and check to see if it is within %screensizex/2 and %screensizey/2 of the level size every frame, and set a global variable like $cameraAtWorldLimit to true or false depending on the result.

How is everyone else handling parallax scrolling in a 2d platform-type game?

thanks for any insight!

#1
08/26/2005 (10:55 pm)
Well, I'm going to answer my own question. I figure this would be useful to someone, so here's my solution.

Basically, I just put this in my fxScene2d::onUpdateScreen() and have really cool parallax scrolling for my background layer. I didn't test this all that thoroughly, but it appears to work just fine. I'm not sure what would happen of the camera was zoomed, or if the other layer was a different size etc.

oh, and the below assumes you have $levelSizeX and $levelSizeY set, which are the unit size of the level / 2.

%position = sceneWindow2d.getCurrentCameraArea();
	// grab camera position
	%positionx = getWord(%position, 0);
	%positiony = getWord(%position, 1);
	// grab camera view size
	%sizex = getWord(%position, 2);
	%sizey = getWord(%position, 3);
	
	// check to see if camera is out of bounds in x direction
	// $levelSizeX is assumed to be the unit size of the level / 2.
	if ((%positionx + %sizex) <= $levelSizeX && %positionx >= -$levelSizeX) {
		// set layer 0's x scroll to be 25% that of the main layer
		%x0 = (%positionx / 4) - %positionx;
	} else {
		%x0 = 0;
	}
	
	// check to see if camera is out of bounds in y direction
	// $levelSizeY is assumed to be the unit size of the level / 2.
	if ((%positiony + %sizey) <= $levelSizeY && %positiony >= -$levelSizeY) {
		// set layer 0's x scroll to be 25% that of the main layer
		%y0 = (%positiony / 4) - %positiony;
	} else {
		%y0 = 0;
	}

	// set the appropriate pan for the background layer
	%layer0 = $level.getTileLayer(0);
	%layer0.setPanPosition(%x0 SPC %y0);
#2
08/27/2005 (5:05 am)
If you look at setupT2DScene() of the spacescroller demo, how does that differ than what you are trying to implement? This seems a lot simpler
...
// *******************************************************
// Generate Scrolling Dunes
// *******************************************************
%hillsLayer.setPosition( "0 11" ); 
%hillsLayer.setSize( "140 10" );
%hillsLayer.setLayer( 27 );
%hillsLayer.setWrap( true, false );
%hillsLayer.setAutoPan( "50 0" );
...
#3
08/27/2005 (5:10 am)
Instead of setting the panPosition manually, maybe you can just adjust the setAutoPan() as you go? I haven't tried it yet though...
#4
08/27/2005 (11:59 am)
But setting the auto pan, as far as I know, just makes the tile layer scroll automatically in units per second, which is not what I want. I want it to only scroll if the view moves.

To set a different auto pan every frame, I'd have to figure out how fast (units per second) the view window is moving, and then set the auto pan as a fraction of that each frame.

Mine might not be the optimal solution, but it definitely works and looks great!

-Jacob
#5
08/27/2005 (11:17 pm)
OK I get it you want interactive scrolling not just constant scrolling. I don't really understand your onUpdateScreen but that's OK I'm pretty new to T2D. Let me know if you release any demos or resources; it sounds interesting.