GML Jump and Gravity Question

Caio

Member
I'm a beginner to intermediate in the programming language part, and in GML, and I'm always trying different ways to program something.
I've watched different tutorials, and most use the variable "vspeed" for the "Y" in the gravity and jump part.
I tried to program using the same "Y" instead of a variable.
But the result was different at the time of the jump. I did not understand why.
As I am still learning the GML and programming language. I try to program the easiest way to understand first, then leave in the most advanced way.
I would like to know the reason for this difference:
Using a variable for "Y":
-Create Event:
Code:
image_speed = 0.5;
grav = 1;
vspd = 0;

-Step Event:
Code:
if not place_meeting(x ,y+1, obj_solid)
    {
        vspd += grav;
    }
    else
        {
            if keyboard_check_pressed(vk_space)
            {
                vspd = -16;
            }
        }

if place_meeting(x, y+ vspd, obj_solid)
{
    vspd = 0;
}

y += vspd;
Using the "Y" without Variable:
-Create event:
Code:
image_speed = 0.5;
grav = 1;
Step Event:
Code:
if not place_meeting(x, y+1, obj_solid)
{
    y += grav;
}
    else
    {
    if keyboard_check_pressed(vk_space)
        {
            y += -16;
        }

    }
if place_meeting(x, y+ 1, obj_solid)
{
    y += 0;
}
Sorry my english I'm using google translate
 

chamaeleon

Member
I tried to program using the same "Y" instead of a variable.
But the result was different at the time of the jump. I did not understand why.
You don't get the same effect because you cannot switch a speed variable for a gravity variable. Just like in real life, distance traveled over a certain amount of time depends on the speed. The speed over a certain amount of time depends on gravity (for a falling motion). You cannot just add gravity to position to move it, you need to use something that represent speed. It comes down to physics.
 

Slyddar

Member
Applying the movement to the y directly is not a good idea, as the position of the object will change through the step, which could cause code to trigger, but at the end of the step the final y position might be different to what it's been through the step, and thats the position that gets displayed.

It makes more sense to have a placeholder like vspd which can change through the step, then at the end of the step that final value gets applied to y.

Also the value of vspd is cumulative, whereas applying to y directly is not. Applying grav and movement code with acceleration and deceleration needs to be cumulative.
 
Top