GML Object goes 1 pixel too far with anti-gravity

L

Luka Radulovic

Guest
I've tried using my normal method of gravity and vertical collision and making it work for anti-gravity, or in other words, falling upwards and not downwards.

Code:
// Create Event
vsp = 0;
grv = 0.1;

jumpsp = -2;
// jump_delay used for staying on the ground 50 frames before jumping again
jump_delay = 50;
This is just a basic Create Event, don't think there's much to explain here.

Code:
vsp += grv;

if (place_meeting(x, y-vsp, oWall)) {
    // this is where I think the problem arises
    while (!place_meeting(x, y-sign(vsp), oWall)) {
        y -= sign(vsp);  
    }
    vsp = 0;
   
    // code below is for jumping after only 50 frames
    jump_delay--;
    if (jump_delay == 0) {
        vsp = jumpsp;
        jump_delay = 50;
    }
}

y -= vsp;
The logic seems fine to me, but I may be overlooking something easy. When the object falls (upwards) towards the oWall object, it falls one pixel more into the oWall object. The jumping works how it's supposed to work. Everything is alright except that one pixel in the oWall object.

Any help will be appriciated, thank you.
 

Bearman_18

Fruit Stand Deadbeat
you want to put y -= vsp into an else block for "if (place_meeting(x, y-vsp, oWall))".

Code:
Code:
vsp += grv;

if (place_meeting(x, y-vsp, oWall)) {
    // this is where I think the problem arises
    while (!place_meeting(x, y-sign(vsp), oWall)) {
        y -= sign(vsp); 
    }
    vsp = 0;
  
    // code below is for jumping after only 50 frames
    jump_delay--;
    if (jump_delay == 0) {
        vsp = jumpsp;
        jump_delay = 50;
    }
}
else {
y -= vsp;
}
 

Bearman_18

Fruit Stand Deadbeat
Your comment is correct, that is where the problem is. What you want is to change


while (!place_meeting(x, y-sign(vsp), oWall)) {
y -= sign(vsp);
}

to

while (true) {
if (place_meeting(x, y-sign(vsp), oWall)) {
y -= sign(vsp)
} else {
break;
}
}
Aren't they just the same thing with more steps? XD
 
Top