Legacy GM [SOLVED] Camera jitter on view scroll/view follow

Hello everyone,

I get camera jitter when scrolling the view, and/or scrolling the view to follow an object (with what is probably very well known code by now), below.

I've tried using floor on every aspect possible below, which does improve the result somewhat, but ultimately, if the scroll calculation falls below a certain amount, the camera jitters. I would like to know if there is a 'smart' way or any other means one could apply to eliminate this phenomenon.

Code:
// point the camera in the direction of the mouse
direction = point_direction(x, y, mouse_x, mouse_y);
xTo = mouse_x;
yTo = mouse_y;

// bigger number on division gives slower camera response
x += (xTo - x) / 8;
y += (yTo - y) / 8;

// move the view according to the calculations           
view_xview = -(view_wview / 2) + x;
view_yview = -(view_hview / 2) + y;
 
T

tomsaram

Guest
something you can try:

if abs(xTo - x) <= some_threshold{
x = xTo​
}
else x += (xTo-x)/8
//also add the same thing for y

The threshold should be a small number, but preferably not smaller than 1.
 

DesArts

Member
Setting the direction there wouldn't be needed. Perhaps try the code without that part? It could be affecting the camera object's position in some way since its a built in variable which is intended for movement.

Even if it's not the issue, there's no need for it.
 
I tried a few combinations of things, and have found that I need to move the view a bare minimum of +-9 or +-10 pixels, up until the maximum of whatever (xTo - x) / 8 calculates to, or else I get jitter.
EDIT: With further testing, I found that this only diminishes the jitter effect, but does not remove it.
 
Last edited:
I'm having difficulty beating this. I'd really appreciate it if someone could take a peek.
The object being followed jitters.


View controller object, STEP
Code:
if (instance_exists(object_to_follow))
{
var fx, fy;
fx = object_to_follow.x; // getting the current coordinates from the object we want to follow
fy = object_to_follow.y;
view_xview = floor(fx - (view_wview * 0.5)); // center the view on the object
view_yview = floor(fy - (view_hview * 0.5));
}

The object to follow, STEP
Code:
// toggle view following by setting object_to_follow in the view controller object
if (selected)
{if (obj_view_control.object_to_follow == 0)
{obj_view_control.object_to_follow = id;}
else if (obj_view_control.object_to_follow == id)
{obj_view_control.object_to_follow = 0;}}

// angles
var _c = dcos(image_angle);
var _s = dsin(image_angle);

// controls
var _x = acceleration * (kUp - kDown);
var _y = acceleration * (kRight - kLeft);

// calculate the horizontal and vertical speed
h_speed += _x * _c + _y * _s;
v_speed += _y * _c - _x * _s;

// limit speed, and stop the diagonal speed boost
var _sp = sqrt(h_speed * h_speed + v_speed * v_speed);
if (_sp > max_speed)
{
h_speed = h_speed / _sp * max_speed;
v_speed = v_speed / _sp * max_speed;
}

// move
x += h_speed;
y += v_speed;
Would it be the "right" way, if I instead had the view moved in the code of the object I want to follow?
 
Last edited:
can you explain exactly what you are trying to do?

the picture I get so far is that you have a view centered on an object that moves independantly with keyboard input.
but, according to your first post, you also are moving the view object (presumably the same object as above) with the mouse.
and then, in the last piece of code you post, there doesn't seem to be any hint of the mouse movement. Have you discarded it?
 
I simplified the code just so I can try to fix the jitter.

There is keyboard control moving the object. If view follow is on, the view centers on, and follows the object--working as intended, but with unwanted jitter.

(When view follow is off, the view moves around freely with the mouse. but that is not included now for simplicity's sake)
 
Moving the code to the end step did not fix the jitter. However, removing the call to floor seems to have fixed, or greatly improved the situation.
Code:
view_xview = (fx - (view_wview * 0.5)); // OLD LINE: view_xview = floor(fx - (view_wview * 0.5));
view_yview = (fy - (view_hview * 0.5)); // OLD LINE: view_yview = floor(fy - (view_hview * 0.5));
Regardless, perhaps this movement code for the object to be followed is/was contributing in some way? Should the x/y be 'normalized' or rounded somehow?
Code:
// MOVEMENT CODE IN OBJECT TO BE FOLLOWED'S STEP EVENT
h_speed += _x * _c + _y * _s;
v_speed += _y * _c - _x * _s;
x += h_speed;
y += v_speed;

The remaining (separate, but related) issue I have, is that 4 background images are drawn at differing rates to create a parallax scrolling effect. Unless the result of (xTo - x) / 8); and (yTo - y) / 8); is greater than or equal to approximately 7 or 8 pixels, camera jitter is noticeable.
Code:
// DRAW EVENT IN OBJECT FOR DRAWING BACKGROUNDS
draw_background(far, (view_xview * 0.970), (view_yview * 0.970));
draw_background(nearer, (view_xview * 0.965), (view_yview * 0.965));
draw_background(near, (view_xview * 0.960), (view_yview * 0.960));
draw_background(nearest, (view_xview * 0.950), (view_yview * 0.950));
Code:
// VIEW CONTROL OBJECT END STEP EVENT
// scroll around without following an object
xTo = mouse_x;
yTo = mouse_y;
x += (xTo - x) / 8;
y += (yTo - y) / 8;
view_xview = -(view_wview * 0.5) + x;
view_yview = -(view_hview * 0.5) + y;
EDIT: I've tested scrolling the view incrementing the scroll rate at one pixel each time in all directions, and as suspected, unless the scroll rate is greater than or equal to 6 pixels, then I can notice the backgrounds 'jumping', 'jittering' -- whatever you want to call that. So, I'm thinking it all has to do with the scroll rates I've set in the draw event. I don't think there's anything I can do about those. Therefore, I could try and ensure a minimum scroll rate of 6 or more pixels.
 
Last edited:
I just thought I'd report back, and also mark this post as 'solved'. Turning on "Interpolate colours between pixels" in the global game settings, together with the previously mentioned removal of the calls to the "floor" function, significantly reduced (or perhaps even eliminated entirely) the camera jitter and the awful jittery parallax scrolling problems I was having.
 
Top