• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Centered Parallax Background

N

NeXy

Guest
Hello,
I've been trying to apply a parallax effect to background images of my room while still keeping the images centered relative to the room itself.

Here is the visual representation of the effect I'm going for:

Reference.png

I've found many tutorials and scripts for applying the parallax effect, however they all seem to be geared toward "tiling" the backgrounds thus' having no code to center the content while still maintaining the parallax effect.

Here is the code I'm currently using for applying the parallax effect (Courtesy of Rupeck):

CREATE EVENT:
Code:
image = noone;
x_follow = 0.8; //The rate at which the layer follows the view (0-1)
y_follow = 0.8;
x_speed = 0;  //The movement speed of the layer outside of view movement
y_speed = 0;

previous_xview = view_xview;
previous_yview = view_yview;

END STEP EVENT:

Code:
var x_diff = view_xview - previous_xview;
var y_diff = view_yview - previous_yview;
x += x_diff * x_follow + x_speed;
y += y_diff * y_follow + y_speed;

previous_xview = view_xview;
previous_yview = view_yview;

DRAW EVENT:
Code:
draw_background_ext(image, x, y , 2, 2, 0, image_blend, image_alpha);

So the problem is the parallax effect is achieved with adjustment of the x_follow or y_follow values (1.0 will make the background move in tandem with the view, and any value other than 1.0 will simulate the distance of the background by making it move faster or slower than the view). However, changing the x_follow, or y_follow value will also offset the background layer from the center - this is the issue I would like to fix.

So my best guess was that the solution is to reduce x value and y values in the draw event until the image is centered, and by trial and error I came up with some irrelevant numbers and centered the image. But the single room has 12 unique layers and each would require different values to reduce the x with.

Is there a mathematical formula I can apply to center the parallax image regardless of it's x_follow and y_follow values? I would like to be able to set the x and y values of the background image relative to room and then have parallax effect go off from there.

The solution doesn't necessary have to be applied to the code I have provided. If there is a more efficient way to accomplish what I need that involves discarding Rupeck's code that is alright (I would still need to be able to apply scrolling effect to certain backgrounds).

~Thanks advance.
 
Last edited by a moderator:
Top