Movement Help

So I'm using a movement code that has a small problem when I collide with an object and move diagonally on that object, that object jitters, basically the object it's colliding with become shaky when moving diagonally on that object. can someone explain why? Here's the code:

Step Event:

var pixelsPerSecond = 400;

var timePassed = delta_time / 1000000;

var pixelsThisFrame = pixelsPerSecond * timePassed;



//keyboard

var xdir = keyboard_check(vk_right) - keyboard_check(vk_left);

var ydir = keyboard_check(vk_down) - keyboard_check(vk_up);



if (xdir != 0 or ydir != 0) {

var dir= point_direction(0, 0, xdir, ydir);



var movedSuccesfully = false;

for(var i = 0; i<=80; i+=10) {

for (var n = -1; n <= 1; n+=2) {

var modifiedDir = (n*i) + dir;



var xTarget = x + lengthdir_x(pixelsThisFrame, modifiedDir);

var yTarget = y + lengthdir_y(pixelsThisFrame, modifiedDir);



if (place_free(xTarget, yTarget)) {

x = xTarget;

y = yTarget;

movedSuccesfully = true;

break;

}

}



if (movedSuccesfully) {

break;

}

}

}
 
Last edited:
I can't exactly spot the issue in your code. Then again I've never used place_free() and the solid tickbox. I instead use place_meeting() with a parent object.
I'm sharing a code snippet of what I always use in each project so far.

Bare in mind par_collision is just an object that doesn't even need to be in the room.
I just set it as a parent of all objects I don't want the player to pass through (excluding the player).
Basically if player x y is inside a collision object's collision mask, movement = 0;

GML:
/// Player movements
var h, v
h = (keyboard_check(ord("D")) - keyboard_check(ord("A")));
v = (keyboard_check(ord("S")) - keyboard_check(ord("W")));

movh = h*spd;
movv = v*spd;

// Coliision
if place_meeting(x + movh, y, par_collision) {
    movh = 0;
}
if place_meeting(x, y + movv, par_collision) {
    movv = 0;
}

x += movh;
y += movv;

I hope this helps.
 
I can't exactly spot the issue in your code. Then again I've never used place_free() and the solid tickbox. I instead use place_meeting() with a parent object.
I'm sharing a code snippet of what I always use in each project so far.

Bare in mind par_collision is just an object that doesn't even need to be in the room.
I just set it as a parent of all objects I don't want the player to pass through (excluding the player).
Basically if player x y is inside a collision object's collision mask, movement = 0;

GML:
/// Player movements
var h, v
h = (keyboard_check(ord("D")) - keyboard_check(ord("A")));
v = (keyboard_check(ord("S")) - keyboard_check(ord("W")));

movh = h*spd;
movv = v*spd;

// Coliision
if place_meeting(x + movh, y, par_collision) {
    movh = 0;
}
if place_meeting(x, y + movv, par_collision) {
    movv = 0;
}

x += movh;
y += movv;

I hope this helps.
Hey I know I'm late to respond, but I wanted to say thank you, the code works fabulously, I've also changed it up a bit because when you collide with an object at high speed the collision is messy, so if you want to use it for your projects go ahead again thank you so much this really helped.

Code:

var h, v
h = (keyboard_check(vk_right) - keyboard_check(vk_left));
v = (keyboard_check(vk_down) - keyboard_check(vk_up));

movh = h*spd;
movv = v*spd;

// Coliision
if place_meeting(x + movh, y, Object25) {
movh = 0;
}
if place_meeting(x, y + movv, Object25) {
movv = 0;
}

x += movh;
y += movv;



if (place_meeting(x+movh, y, Object25)){
repeat(abs(movh)){
if(!place_meeting(x+sign(movh), y, Object25)){ x += sign(movh); }
else { break; }
}
movh = 0;
}

if (place_meeting(x, y+movv, Object25)){
repeat(abs(movv)){
if(!place_meeting(x, y+sign(movv), Object25)){ y += sign(movv); }
else { break; }
}
movv = 0;
}
 
Top