Moving sprite or object relative to viewport and not room?

D

Dario8676

Guest
Hey everyone, for a while now I've been having an issue with my layer backgrounds with horizontal speeds such as clouds. They scroll at the speed set just fine, however when the camera moves, the clouds give the appearance of slowing down when the camera is moving in the opposite direction of the clouds, and speeding up when moving in the same direction.

I forsee this as a future issue with pretty much anything that moves across the screen that you don't want effected by the camera movement. Any suggestions?
 

SoapSud39

Member
One way you can fix the issue with the backgrounds is to set the layer x in the same object that you set your camera x. So for instance:
GML:
//create
camera = view_camera[0];

//step
cam_x = obj_player.x; //or however you set your camera position
cam_y = //etc.
clouds_x = cam_x; //this can be altered with fractions to get a parallax effect between different background layers
camera_set_view_pos(camera, cam_x, cam_y); //plus offset, of course
var cloud_layer = layer_get_id("clouds");
layer_x(cloud_layer, clouds_x);
Something like this should work (I would verify real quick but I don't have access to GMS at this moment). If you look up tutorials for parallax backgrounds on GMS those will go more in-depth.

For other things that you might not want affected by camera movement, such as ui, you can set their x and y to camera_get_view_x() and camera_get_view_y(), or the easier and probably more efficient solution would be to use the Draw GUI event, which will draw according to the window/display coordinates instead of room coordinates.
 
Last edited:

SoapSud39

Member
I've just seen your other post, and I now see that you did not need to know how to do parallax.

I do believe that the problem with your code is that your background position is contingent on your camera position. Your cloud speeds are affected by the line layer_x(layer, x / offset);; because of this line, every time your camera x changes, your background x changes at a different rate. Now, this works really great for background layers like trees and mountains (etc., etc.) because those are realistically static, and they move away from camera when you move. Clouds, however, aren't. Furthermore, they are really far away and their speed seems to be constant no matter your own speed.

So basically, what you'll want to do is, for cloud layers, set the layer position to your camera position, so that the background follows the camera. If I'm not mistaken, that is all you will need to do because once your background position is consistent with camera position, the layer speed you have already set will take care of cloud movement.
 
D

Dario8676

Guest
Hi soap thanks for the help but a few things.

"Your cloud speeds are affected by the line layer_x(layer, x / offset);"

I do a check before running the above line to only /offset for the backgrounds with no horizontal speed like mountsins, etc.. so the clouds aren't effected by that code.

bg_scroll_speed_tracker -= backgrounds[i,BGLAYERS.scroll_speed_x]; //BG location tracker

layer_x(backgrounds[i,BGLAYERS.layer_id], x+ bg_scroll_speed_tracker

That first line above controls the cloud scrolling to the left. The second line uses the layerx function to position it and you can see that im adding x (meaning the camera x to it.) So is this not what you suggested?

It's important to note the the speed of the clouds never change but it gives the perception of changing when the camers moves.

The same happens normally with anything in the room which is normal. For example if you have an enemy player running at a speed of 5 to the right and you are running at a speed of 5 to the right ss well its going to give the perception that the enemy is not moving.
 

SoapSud39

Member
Okay, if we're both understanding your code correctly, yes you've already basically done what I've suggested (I will admit that I may have ignored it at first, but because the distinction between your constant names isn't super clear). While we're at it, I don't think the check you mentioned above is really necessary, given that your clouds code overrides the normal parallax background code.

Now, if your code works as I think it would, it should make the cloud layer look as I'd expect it to. There seems to be nothing wrong in that aspect. However, after looking through these two threads a few more times, I'm starting to think that our expectations of what the cloud layer should look like is different. So at the top of this thread, you said
They scroll at the speed set just fine, however when the camera moves, the clouds give the appearance of slowing down when the camera is moving in the opposite direction of the clouds, and speeding up when moving in the same direction.
Exactly what do you mean by clouds slowing and speeding up? Do you mean that when the camera is moving away the clouds should move faster and vice versa, or is the cloud layer doing something entirely unexpected?

You don't have to, but a recording (a gif will do, or a video if you'd like) will let me and/or others see more clearly what the specific issue is.

sorry about the unhelpful first replies, by the way
also, for future reference, use code tags for array[i] so it doesn't italicize your post
 
D

Dario8676

Guest
I confirmed that the clouds are moving always at the same speed, its the parralex backgrounds that plays the trick on the eyes that the clouds are slowing down or speeding up. So confused...
 
Last edited by a moderator:

SoapSud39

Member
Yeah, I think they really do move as you had intended, after watching it a few times through. I also didn't realize it would be this trippy with both parallax and moving parts. If you're open to suggestions, how about giving the cloud layers both a parallax value and a scroll speed value? Then the clouds will both move on their own and give the illusion of depth.
 
Top