• 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!

GML Why is that gravity value interacting with the y-coordinate on jump on my code?

K

Klangsucht

Guest
Hello there, like many other newbies I am following the famous platformer tutorial from shaun spolding.

Everything works so far, but I try to understand (for a few hours now) why the player really "jumps" like a smooth jump on spacebar-press. From what my logic tells me, the object should get teleported 7 pixels + the gravity value, up and from then on fall down the gravity value every tick. But it's also "sliding up" as fast as it's coming down like the the gravity value is negative (every tick).

Here is the code from my "every tick" elevent:

GML:
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_space);


var move = key_right - key_left;
hsp = move * walksp;
vsp = vsp + grv;

if (place_meeting(x, y + 1, obj_erde)) && (key_jump)
{
    vsp = -7;
}

//horizontal Collision
if (place_meeting(x+hsp, y, obj_erde))
{
    while(!place_meeting(x+sign(x + hsp), y, obj_erde))
    {
        x = x + sign(hsp);   
    }
    hsp = 0;
}

x = x + hsp;

//Vertical Collision

if (place_meeting(x, y+vsp, obj_erde))
{
    while(!place_meeting(x, y +sign(y + hsp), obj_erde))
    {
        y = y + sign(vsp);   
    }
    vsp = 0;
}

y = y + vsp;
and the values in the create-elevent:

Code:
grv = 0.3;
hsp = 0;
vsp = 0;
walksp = 4;
It's hard to form that question great enough for you to understand, and my english is worse too, but I just don't get, why the jump is a real "jump" instead of a teleport, 7,3 pixels up (vsp + grv). I mean, the jump-press is just once in a tick, but the game "remembers" that jump-press and slides the player up, same as it comes down after that.. Why isn't the game teleporting the player 7,3 pixels up in the first tick of the second..
 

kburkhart84

Firehammer Games
I'm not sure exactly what you are asking...but I will say that when you start the jump, you are setting vsp to -7. This means that it will literally be -7, and the fact that just before that if statement you added the gravity to it is irrelevant. The next step, it will then add the gravity to it, but it will have already moved that 7 pixels that very first step. Maybe this is what you are getting at, I'm not sure.
 

Nidoking

Member
What you appear to be describing is how gravity works. Velocity is the rate of change of position, and gravity is an acceleration, the rate of change of velocity. The speed (vsp) changes based on the gravity (grv) every step. That's what it does. This is basic physics/advanced algebra/intermediate calculus.

In short, the speed becomes -7 to start, then increases by 0.3 every step. So -6.7, then -6.4, and so on.
 

woods

Member
it IS teleporting the 7 px and then 6.7 and then 6.4 and slowly coming down to zero before it falls..

it looks so smooth due to the speed at which the game displays each frame. ..the same concept of a movie or a paper flipbook.. the faster the pages are displayed, the smoother the animation.

if you slow your room speed down to 10 from 60 , you will see a noticeable difference and can spot the "teleport"
 
Last edited:
@woods has a good description, it's like a movie. Each frame of a movie is a "teleport" for all the moving characters in it, but each frame played rapidly rapidly in succession emulates motion.

-7 pixels is not a big distance to move on a modern monitor, and the movement is done in 1 frame, or 1/60th of a second. So each frame doesn't actually have a large "distance" the character is moving, but if you were to have no gravity, the character would go shooting up pretty fast at -420 pixels per second (remember, -7 pixels per frame, 60 frames per second, so -7*60 = -420), which is a good portion of the monitors horizontal size each second.

With gravity acting every single frame, it only takes a relatively small number of frames for the -7 to reach 0 (if your gravity is set at +0.1, it takes roughly 1 second before -7 pixels per frame turns into 0 pixels per frame: -7/0.1 = 70 frames before it reaches 0). Then the downward motion starts. So in reality, you're seeing a gradually decreasing "teleport" distance each frame, which is played very rapidly, until the vertical arc turns from negative to positive, and then you're seeing the same thing in "reverse".
 
Top