• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[Solved] Need help with collision, use of position vs speed variables

T

TaPeace

Guest
Hi there, i trying to prototype with some collision stuff and need some help to unterstand it...
The Code works fine if i hit the objects just horizontal or vertical. But if i hit a slope diagonal, then i get stuck sometimes. The same when i hit a corner from a rectangle.

Any advice?

Thanks :)

Code in Player Step Event
Code:
//Get Inputs
kUp = keyboard_check(ord("W"));
kDown = keyboard_check(ord("S"));
kLeft = keyboard_check(ord("A"));
kRight = keyboard_check(ord("D"));
kRestart = keyboard_check(ord("R"));
kEscape = keyboard_check(vk_escape);

//Movement
if kUp {vspeed -= MySpeed};
if kDown {vspeed += MySpeed};
if kLeft {hspeed -= MySpeed};
if kRight {hspeed += MySpeed};

//Decceleration
if speed > 0 {
    speed -= Decceleration;
} else {
    speed = 0;
}

//Limit Speed
if speed >= MaxSpeed {
    speed = MaxSpeed;
}

//Horizontal Collisioncheck
if place_meeting(x + hspeed,y,oSolids) {
    while !place_meeting(x + sign(hspeed),y,oSolids) {
        x += sign(hspeed);
    }
    hspeed = 0;
}

//Vertical Collisioncheck
if place_meeting(x,y + vspeed,oSolids) {
    while !place_meeting(x,y + sign(vspeed),oSolids) {
        y += sign(vspeed);
    }
    vspeed = 0;
}

//Wrap Screen
move_wrap(1,1,0);

//Restart
if kRestart {game_restart()};

//Exit Game
if kEscape {game_end()};
 

Attachments

T

TaPeace

Guest
The collision masks from the player object are circle also tested with precise collision mask. Slopes has precise collision masks.
 
T

TaPeace

Guest
Ok, i rewrite the code into this one...

Code:
//Horizontal Collisioncheck
if place_meeting(x + hspd,y,oSolids) {
    while !place_meeting(x + sign(hspd),y,oSolids) {
        x += sign(hspd);
    }
    hspd = 0;
}
x += hspd;

//Vertical Collisioncheck
if place_meeting(x,y + vspd,oSolids) {
    while !place_meeting(x,y + sign(vspd),oSolids) {
        y += sign(vspd);
    }
    vspd = 0;
}
y += vspd;
This will work fine when i use the x and y variables. But it doesn't work properly when i use hspeed and vspeed instead (mostly diagonal against slopes, corners or curves).
And i'd rather use the speed variables. Does someone know why?

Code:
//Horizontal Collisioncheck
if place_meeting(x + hspd,y,oSolids) {
    while !place_meeting(x + sign(hspd),y,oSolids) {
        x += sign(hspd);
    }
    hspd = 0;
}
hspeed = hspd;

//Vertical Collisioncheck
if place_meeting(x,y + vspd,oSolids) {
    while !place_meeting(x,y + sign(vspd),oSolids) {
        y += sign(vspd);
    }
    vspd = 0;
}
vspeed = vspd;
 
Last edited by a moderator:
T

TimothyAllen

Guest
Because in your first code (not using built in speed) the instance moves on the x axis before the vertical collisions are check. In your second code, this is not the case. I think you can fix this by simply making this change to your second code:
Code:
//Horizontal Collisioncheck
if place_meeting(x + hspd,y,oSolids) {
   while !place_meeting(x + sign(hspd),y,oSolids) {
        x += sign(hspd);
    }
    hspd = 0;
}
hspeed = hspd;

//Vertical Collisioncheck
if place_meeting(x + hspeed,y + vspd,oSolids) { 
    while !place_meeting(x + hspeed,y + sign(vspd),oSolids) {
        y += sign(vspd);
    }
    vspd = 0;
}
vspeed = vspd;
But why use the built in variables if they are giving you trouble?
 
T

TaPeace

Guest
Because in your first code (not using built in speed) the instance moves on the x axis before the vertical collisions are check. In your second code, this is not the case. I think you can fix this by simply making this change to your second code:
Code:
//Horizontal Collisioncheck
if place_meeting(x + hspd,y,oSolids) {
   while !place_meeting(x + sign(hspd),y,oSolids) {
        x += sign(hspd);
    }
    hspd = 0;
}
hspeed = hspd;

//Vertical Collisioncheck
if place_meeting(x + hspeed,y + vspd,oSolids) {
    while !place_meeting(x + hspeed,y + sign(vspd),oSolids) {
        y += sign(vspd);
    }
    vspd = 0;
}
vspeed = vspd;
But why use the built in variables if they are giving you trouble?
OMG, the solution was so close. Thank you Timothy. It works know with this code.

Code:
//Horizontal Collisioncheck
if place_meeting(x + hspd,y,oSolids) {
   while !place_meeting(x + sign(hspd),y,oSolids) {
        x += sign(hspd);
    }
    hspd = 0;
}
hspeed = hspd;

//Vertical Collisioncheck
if place_meeting(x + hspeed,y + vspd,oSolids) {
    while !place_meeting(x + hspeed,y + sign(vspd),oSolids) {
        y += sign(vspd);
    }
    vspd = 0;
}
vspeed = vspd;
 
Top