Legacy GM Flipping gravity

Q

Quelandoris

Guest
Hello,

Currently working on a rhythm platformer game (long story), And I want to add a feature that allows the player to flip their own gravity. At the moment, gravity does flip, but just before the player would collide with the ground/ceiling, they hover slightly above the ground for a few moments, then disappear (Presumably to the other side of the ground/ceiling, outside of the room). I'm not sure how to fix this so any help would be appreciated

Heres my relevant code:
Code:
//Jumping "Space"
if(gravity_direction==270){
    if (place_free(x,y+1)){
        gravity=.25;
    }
    else{
        gravity=0;
    }
    if(up){
        if(!place_free(x,y+1)){
            vspeed=-6;
        }
    }
}
else{
    if (place_free(x,y-1)){
        gravity=.25;
    }
    else{
        gravity=0;
    }
    if(up){
        if(!place_free(x,y-1)){
            vspeed=6;
        }
    }
}
//Switch Gravity "X"
if(keyboard_check_pressed(ord("X"))){
    if(gravity_direction==270){
        gravity_direction=90;
    }
    else if(gravity_direction==90){
        gravity_direction=270;
    }
}
Prior to pressing X, jumping works fine.

Not entirely sure whats causing the issue, so any help would be appreciated. Thank you in advance
 
I see no obvious logic issues. I'm assuming that:
Code:
up = keyboard_check(vk_up);
Here are some questions:
Do you have horizontal movement right now? (Shouldn't effect the vertical movement, just checking).
What is gravity initially set to?

I would check to make sure your player actually collides with the floor/ceiling. place_free checks for solid objects, so if the walls and ceilings are not solid you might have issues. I might recommend using place_meeting instead, and use a parent object for all the ones you want to collide with, but that's up to you.
 
Q

Quelandoris

Guest
Floor/ceiling is solid and actually the same object. There is no horizontal movement, gravity is defaulted to .5, but gets set almost immediately to .25 in the code for jumping.
 
So, it's fine until you switch gravity?

Does the player start on the floor? Try starting the player in midair and see what happens. I'm curious if it is having issues stopping when it reaches the object, and it has nothing to do with the gravity switching code.

Note, I did know that the floor/ceiling was the same object.
 
Q

Quelandoris

Guest
So, it's fine until you switch gravity?

Does the player start on the floor? Try starting the player in midair and see what happens. I'm curious if it is having issues stopping when it reaches the object, and it has nothing to do with the gravity switching code.

Note, I did know that the floor/ceiling was the same object.
The player does start on the floor. I tried starting him in the air and the same thing happens; object hangs for a moment before disappearing beneath the floor. So it does seem to be having issues reaching the object, which is strange since the jumping works fine otherwise.
 
Does the speed of which he hangs and then falls change if you change the gravity amount?

It's a little hard to know exactly what you're talking about at this point. Can you include a video or a download link?

I've never been good at platforming games so this might be the most I can help you...
 

Soso

Member
Debug your player x and y and see where they go when the player disappears might be helpful in pin pointing your problem
 
Q

Quelandoris

Guest
Debug your player x and y and see where they go when the player disappears might be helpful in pin pointing your problem
They end up falling. Once the player pops off the screen, Y just keeps increasing forever.
 
I'm pretty sure I figured this out.

So, the object stops before it hits the floor because gravity changes the vspeed, and if, say, the vspeed is 3, then the player can quite reach the very edge of the floor. The vspeed moves it by that many pixels, and if it's 2 pixels away, but the speed is 3, it will stop, instead of just going the last 2 pixels. (They're both solid, so GM stops them automatically). This part I know for certain. However, your check to set gravity back to zero is checking only one pixel away. So based on the case above, it's actually not seeing the floor, after all, if it DID move 1 more pixel it would be fine. That means the gravity is still 0.25 and will keep increasing the vspeed until it jumps all the way through the floor.

I'd recommend trying:

Code:
if (place_free(x, y+vspeed) == true)
That way, instead of checking one pixel, you are checking the place it's trying move for real.
 
Q

Quelandoris

Guest
I'm pretty sure I figured this out.

So, the object stops before it hits the floor because gravity changes the vspeed, and if, say, the vspeed is 3, then the player can quite reach the very edge of the floor. The vspeed moves it by that many pixels, and if it's 2 pixels away, but the speed is 3, it will stop, instead of just going the last 2 pixels. (They're both solid, so GM stops them automatically). This part I know for certain. However, your check to set gravity back to zero is checking only one pixel away. So based on the case above, it's actually not seeing the floor, after all, if it DID move 1 more pixel it would be fine. That means the gravity is still 0.25 and will keep increasing the vspeed until it jumps all the way through the floor.

I'd recommend trying:

Code:
if (place_free(x, y+vspeed) == true)
That way, instead of checking one pixel, you are checking the place it's trying move for real.
Okay that fixed the issue with falling through the floor, but now there tends to be an odd gap between the player and the floor. I think i have some code from an older game that can fix something like this; If i find it, ill post it here for anyone whos coming through later
 
Yeah. There is that issue. My previous post didn't cover the solutions to that.

You can try using move_contact_solid, which I think still exists (I did that in GM 8 last time I used it). Or maybe move_snap. Or a complex system that gradually reduces speed until it reaches one, to ensure that it always goes up against the floor. At any rate, glad I could help.
 
Q

Quelandoris

Guest
Yeah. There is that issue. My previous post didn't cover the solutions to that.

You can try using move_contact_solid, which I think still exists (I did that in GM 8 last time I used it). Or maybe move_snap. Or a complex system that gradually reduces speed until it reaches one, to ensure that it always goes up against the floor. At any rate, glad I could help.
So the code I thought would work doesn't. Move_contact_solid makes jumping much heavier than it originally was, and move_snap only seems to work some of the time. Not sure what else to do to fix the gap, but I am glad that I have what I've got working. Thank you both!
 
M

montiedragon

Guest
Are you using move_contact_solid only if "if (place_free(x, y+vspeed) == false)"?
 
Top