Legacy GM Parallaxing background objects

D

Dibidoolandas

Guest
I'm using objects as background elements so I can sort of build my levels piece by piece. However I want some of these background (or foreground) objects to parallax slightly. I've got the effect working in a way that I like using the following code (this is a snippet for a foreground object):

Create:
Code:
off_x = x;
off_y = y;
Step:
Code:
x = off_x+(-view_xview[view_current])/5;
y = off_y+(-view_yview[view_current])/5;
This works great, and you can see variants of it in this video, in the background tree, the shinto gates, and the foreground rocks.

However, I've noticed if I place something near the left side of my room (which is quite long), it renders relatively close to where I placed it. But if I place one of these objects halfway into the room, it is very far from where I placed it, as far as the x position is. I assume this is because it's checking the view against the relative position of the room. Not to oversimplify things, but is there a way to do the same thing, but just check against where the object is in relation to the current viewport?

I know it's a bit hard to explain, but if the camera moves over the center of where I placed the object in the room, I'd like the object to appear in the center, but if the camera is to the left or right it should adjust its position accordingly.
 
D

Dibidoolandas

Guest
I had some additional thoughts about this. I could try positioning the object relative to the camera's x position... Something like if camx < myx {x += (myx - camx) * 0.2}.

Alternately, I could try running the same code I currently am, but account for the width of the room somehow. Not sure yet how that would work thought.
 
D

Deleted member 13992

Guest
It might have to do with the origin point of the object. Where do you set the origin on these sprites?

You can also maybe fix it if you offset the "offset travel" by half the total amount of travel possible. Something like this

x = off_x+((-view_xview[view_current])/5)-off_tweak;

added a minus offset tweak that brings the object back towards the left, even when it's on the left side of the screen. I would experiment different numbers.

ideally you want the object to be relatively where you placed it when it's in the center of the view.
 
D

Dibidoolandas

Guest
Thanks for the suggestion Muki. I think I have a solution now that's fairly elegant, posting here for anyone who has the same problem.

Code:
x = off_x + ((off_x - cam_x) * 0.1);
In the create event, off_x is just defined as the starting x point of the object, and in the step cam_x is defined as the center-point of the view ((view_xview[0] + view_wview[0])/2).

This way, when the view passes over the object's center point, the object is in the center of the view.
 
D

Deleted member 13992

Guest
glad it works!

I would also make sure that it doesn't do any sort of offset calculation when the object is off view. so that it doesn't end up offset off-room when you're at the other end of it. probably won't break anything if you don't, but it's safer.

either through instance deactivation or just an offset clamp.
 
Top