1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GML What's wrong with my collision code?

Discussion in 'Programming' started by Littlesticks, May 31, 2018.

  1. Littlesticks

    Littlesticks Member

    Joined:
    May 27, 2018
    Posts:
    6
    So I've watched a few tutorials and tried to do a collision code from scratch, coupled with movement keys for moving left||right and jumping. I've succeeded in doing the movements but my collisions itself seem to keep making the object stuck, preventing the object from using left||right movements when on the ground, and jumping when stuck in walls. However, I can use both movement keys when I jump the object in the air (supposed to). Also, it seems that before the object reaches the ground||wall, that it will stutter and bounce before touching the ground||wall.

    I'm suspecting that it's in the way I'm calculating the distance for collision checks, but I just can't figure it out. I've been stuck at this for about 2 days.
    =

    OBJECT CREATE EVENT
    Code:
    hspd = 0;
    vspd = 0;
    
    jump_spd = 25;
    jump_stop = 0;
    
    move_spd = 1;
    move_spd_rate = 0;
    move_spd_cap = 20;
    move_stop = 0;
    
    grav = 1.5;
    fall_accel_rate = 1.5;
    grav_cap = 3;
    fall_accel_reset = 1.5;
    
    state_keys = scr_keys;
    state_collision_variables = scr_collision_variables;
    state_collision_detect = scr_collision_detect;

    OBJECT STEP EVENT
    Code:
    script_execute(state_keys);
    script_execute(state_collision_variables);
    script_execute(state_collision_detect);

    KEYS
    Code:
    jump_key = keyboard_check_pressed(vk_space);
    left_key = keyboard_check(ord("A"));
    right_key = keyboard_check(ord("D"));

    COLLISION VARIABLES
    Code:
    argument_vspd = vspd;
    argument_hspd = hspd;
    ground_true = place_meeting(x, y+argument_vspd, obj_solid);
    ground_false = !place_meeting(x, y+argument_vspd, obj_solid);
    horizontal_true = place_meeting(x+argument_hspd, y, obj_solid);
    direc_calc = (right_key - left_key);

    COLLISION CODE && MOVEMENT
    Code:
    if horizontal_true {
        
        argument_hspd = sign(hspd);
        while !horizontal_true {
            x += argument_hspd; // previously was hspd += argument_hspd;
        }
        hspd = move_stop;        //move_stop = 0
    }
    
    //
    if (left_key || right_key) && !place_meeting(x+argument_hspd, y, obj_solid) {
        hspd = (direc_calc)*(move_spd+move_spd_rate);            //Check if the right or left key is pressed (pressed = a value of 1)
        while move_spd_rate < move_spd_cap {                    //And multiply the equaled value by (move_spd+move_spd_rate)
            move_spd_rate += 2;                                    //Which takes the base value of the move_spd additioned to the acceleration value
        }                                                        //example > (1 - 0)*(10+1) = +10+1;
                                                                
                                                                
        if (hspd > move_spd_cap || hspd < -move_spd_cap){
            hspd = sign(hspd)*move_spd_cap
        }
    } else {
        move_spd_rate = 0;
    }//
    
    x += hspd;
    
    if ground_true {
        
        argument_vspd = sign(vspd);
        while !ground_true {
            y += argument_vspd;            // previously was vspd += argument_vspd;
        }                                // Basically, when checking ahead and there's a ground_true, while not ground_true, move obj by 1 pixel
        vspd = jump_stop;                //jump_stop = 0;
        grav = fall_accel_reset;        //reset the value to the default grav value for falling acceleration
        //vspd -= argument_vspd;  << Add this to see the block vibrate
    } else {
        argument_vspd = vspd;            //reset argument_vspd to vspd
        vspd += grav;
        grav += fall_accel_rate;        //Makes the object fall faster for each step that it's in 'falling'
        if grav > grav_cap {
            grav = grav_cap;            //This is the cap for the fall speed
        }
    }
    
    //
    if (jump_key && place_meeting(x,y+argument_vspd, obj_solid)) {
        vspd -= jump_spd;
        }//
    
    y += vspd;

    What am I doing wrong?
     
  2. the_dude_abides

    the_dude_abides Member

    Joined:
    Jun 23, 2016
    Posts:
    605
    I haven't fully gone through your code, but at a quick glance (if I'm reading it right in my haste) you have false conditions inside the true condition statement. Like:
    Code:
    if horizontal_true {
        
        argument_hspd = sign(hspd);
        while !horizontal_true {
            x += argument_hspd; // previously was hspd += argument_hspd;
        }
        hspd = move_stop;        //move_stop = 0
    }
    
    So the false condition will never be executed. The statements should be like this:
    Code:
    if horizontal_true
    {do / don't do whatever}
    else
    {don't do / do whatever}
     
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,662
    ....ground_false is just !ground_true. Never understood the use of two variables for that.

    horizontal_true and ground_true are only updated at the start of the step event in your code. The collision method you are trying to incorporate requires you to set horizontal_true or ground_true multiple times during their respective while loops.
     
    Joe Ellis likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice