SOLVED Player starts shaking when moving diagonal

I tried giving my character 8 directional movement and it seems fine at first, but when I make him move diagonally he starts shaking violently and I don't know why.

Details:

The room size is 640x360.

The camera and viewport settings are 320x180 with following oPlayer.

And the player sprite is W: 36 and H: 57.

And the player script is here:

Code:
//Create event

image_speed = 1;

hSpeed = 0;
vSpeed = 0;
walkSpeed = 1;
Code:
//Step event

keyLeft = keyboard_check(vk_left) or keyboard_check(ord("A"));
keyRight = keyboard_check(vk_right) or keyboard_check(ord("D"));
keyUp =  keyboard_check(vk_up) or keyboard_check(ord("W"));
keyDown =  keyboard_check(vk_down) or keyboard_check(ord("S"));

inputDirection = point_direction(0,0,keyRight-keyLeft,keyDown-keyUp);
inputMagnitude = (keyRight - keyLeft != 0) || (keyDown - keyUp != 0);

hSpeed = lengthdir_x(inputMagnitude * walkSpeed, inputDirection);
vSpeed = lengthdir_y(inputMagnitude * walkSpeed, inputDirection);

x += hSpeed;
y += vSpeed;
Any ideas?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
What happens if you simplify the code? You really have no need for the dir stuff...

GML:
keyLeft = keyboard_check(vk_left) or keyboard_check(ord("A"));
keyRight = keyboard_check(vk_right) or keyboard_check(ord("D"));
keyUp =  keyboard_check(vk_up) or keyboard_check(ord("W"));
keyDown =  keyboard_check(vk_down) or keyboard_check(ord("S"));

hSpeed = (keyRight - keyLeft) * walkSpeed;
vSpeed = (keyDown - keyUp) * walkSpeed;

x += hSpeed;
y += vSpeed;
 
What happens if you simplify the code? You really have no need for the dir stuff...

GML:
keyLeft = keyboard_check(vk_left) or keyboard_check(ord("A"));
keyRight = keyboard_check(vk_right) or keyboard_check(ord("D"));
keyUp =  keyboard_check(vk_up) or keyboard_check(ord("W"));
keyDown =  keyboard_check(vk_down) or keyboard_check(ord("S"));

hSpeed = (keyRight - keyLeft) * walkSpeed;
vSpeed = (keyDown - keyUp) * walkSpeed;

x += hSpeed;
y += vSpeed;
Thank you. I guess I just made that harder for myself
 

Coded Games

Member
What happens if you simplify the code? You really have no need for the dir stuff...

GML:
keyLeft = keyboard_check(vk_left) or keyboard_check(ord("A"));
keyRight = keyboard_check(vk_right) or keyboard_check(ord("D"));
keyUp =  keyboard_check(vk_up) or keyboard_check(ord("W"));
keyDown =  keyboard_check(vk_down) or keyboard_check(ord("S"));

hSpeed = (keyRight - keyLeft) * walkSpeed;
vSpeed = (keyDown - keyUp) * walkSpeed;

x += hSpeed;
y += vSpeed;
Doesn’t this code have the bug where you will move faster diagonally than any other direction? Because pythagoras. The original code was trying to account for that I think.
 
Doesn’t this code have the bug where you will move faster diagonally than any other direction? Because pythagoras. The original code was trying to account for that I think.
Yes I had an issue like this once. You need to add this before the x+= hSpeed y += vSpeed:
GML:
    if (hSpeed != 0) and (vSpeed != 0) { hSpeed *= 0.707107; vSpeed *= 0.707107; }
That sorts out the diagonal speed so you can't walk faster diagonally.

I had the jitter / shaking issue after addressing this though months ago, might've been because I'm using a camera that follows the player and mouse together, so you might not get the issue again. However if you do, I believe the key was making sure you avoid odd divisions / multiplications because they cause half pixels (idk the correct terminology). Let us know if it happens again.
 
Last edited:

Nocturne

Friendly Tyrant
Forum Staff
Admin
Doesn’t this code have the bug where you will move faster diagonally than any other direction? Because pythagoras. The original code was trying to account for that I think.
Yup, although it's not really a bug, just a by-product of the maths behind it. :) However, given the nature of the question, I suspected that the OP was using copy/paste code, so offered the most simple solution to achieve the goal - stop the "jittering". I wasn't really thinking about the actual speeds involved!!!
 
Top