Jump to content
  • Advertisement
Sign in to follow this  
EvilNando

Proper parallax scroll with tiles

This topic is 2908 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi again Im still having trouble getting my parallax scroll right, this is mainly because Im using tiles to render my backlayers instead of whole images

I know I have to render tiles at least to cover the whole viewport view, problem is how to apply the scroll offset and still getting stuff rendered on screen


Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
What do you use to draw the tiles with, sir?

If you have problems displaying a tile, you can clip the quads
if you use D3D or OpenGL. Meaning, where your offset tile would normally just be:

1 wide
1 high,

and they're located at the edge of the display, you shrink to ex.

0.6 wide
1 high,

If the tile is to be displayed at the left or right border of the screen.
-And shift the UV-coordinates accordingly.

Does this solve your problem?

Share this post


Link to post
Share on other sites
the actual problem is where do I render rather than how :D

I have 3 background layers made of tiles to save memory but I want those layers to scroll at different speeds to show a parallax effect

so the problem is:

I dont know how to offset those layers in relation to the camera, I know it has to be something like

offset = layer_speed_x * camera.position.x

but somehow this is not working


also since Im rendering tiles I need to know a way to calculate how many draw calls do I need to cover the current viewport with tiles, this is easy when I dont have to scroll them at different speed because I just check if the current tile is inside the viewport


better now?


Share this post


Link to post
Share on other sites
Yup. Although, the tile speed shouldn't affect how many
calls you should make to the tile rendering part.

The scaling of the layer should.

That'd be ( tile_size_x * layer_scale_x ) / screen_width_x

The offset part is correct.

Do you use tile maps for each layer?

What you use to render with is important, as the tiles may not be drawn
when close to the edge. But then, this is not your problem anyway.

-Won't you post some code?

Share this post


Link to post
Share on other sites
here is some code

Im not liking it very much because it doesnt make sense from a theory point of view I was just trying values until it worked

the most important problem is that my speed when 0 is 1:1 with the camera whereas 1 is static and I was intending to make this the be the opposite.

[SOURCE]
for (int index = 0; index < BackgroundList.Count; index++)
{
BackLayer current_layer = BackgroundList[index];

float offset_x = current_layer.Position.X + (Camera.Location.X - Dimensions.Center.X) * current_layer.ScrollRate.X;
float offset_y = current_layer.Position.Y ; // DONT KNOW HOW TO OFFSET VERTICALLY :(

int start_pos_x = (int)(Camera.Location.X - Camera.Viewport.Width / 2 - current_layer.Position.X - current_layer.Texture.Width);
int end_pos_x = (int)(Camera.Location.X + Camera.Viewport.Width + current_layer.Texture.Width);

if (current_layer.Tileable)
{
for (float idx = offset_x; idx <= Dimensions.Width + Math.Abs(offset_x); idx += current_layer.Texture.Width)
{
if (idx >= start_pos_x && idx <= end_pos_x) // could be further optimized by checking the y-coordinate
{
SBatch.Draw(current_layer.Texture, new Vector2(idx, offset_y), Color.White);
draw_calls++;
}
}
}
else
{
SBatch.Draw(current_layer.Texture, new Vector2(offset_x, offset_y), Color.White);
}
}
[/SOURCE]

Share this post


Link to post
Share on other sites
Nando,

Does your idx count pixels or tiles or some tile unit?

I would suggest you abstract from the texture size, use repeating tilemaps
for every layer. This is my personal experience that this will work well,
and you can always add an extra layer in the same scale if you want to add iregularities.

EDIT: Btw, vertical parallax ain't much different from horizontal parallax. Do you mean because of "empty" space above and below the tiles, it would look weird when
scrolling up or down? Because it's a matter of plastering a repetitive texture (or tilemap) onto the edges, then you should be fine.

Share this post


Link to post
Share on other sites
idx = pixels

Im using tiles they just are saved into different textures hence 1 tile width == the texture width

Share this post


Link to post
Share on other sites
Why not make it so for every pixel you move the main layer you move the parallax layers by a percentage of that.

layer1X = cameraX;
layer2X = cameraX * 0.75;
layer3X = cameraX * 0.5;

You just got to make sure you put enough tiles in the map to cover the lengths, so if the map is 200 tiles wide, then layer two needs to be 200 * 0.75 wide and so forth.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!