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

GM:S 1.4 slope/ramp

Discussion in 'Programming' started by graviax, Jun 27, 2016.

  1. graviax

    graviax Guest

    Hi,
    I'm aware that's there is another thread on a similar topic, but mine is slightly different.
    I don't want to react with every kind of slopes, instead, I want the player to interact with 2 object a 45° wall and a 22.5° wall.
    I tried to do it with basic trigonometry, and it clearly does not work.
    I need help.

    EDIT : i forgot to say but, the tricky part is that i need to keep my momentum.
     
  2. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    How to do this best depends on what code you currently have,
    so what code do you currently have for movement and collisions?
     
  3. graviax

    graviax Guest

    left and right :

    if(key_left && place_meeting(x,y+1,obj_wall))//mouvement gauche
    {
    if(hsp >= -maxspeed)
    {
    hsp -= acc
    }

    }


    if(key_right && place_meeting(x,y+1,obj_wall))//mouvement droite
    {
    if(hsp <= maxspeed)
    {
    hsp += acc
    }

    }

    the inertia :

    if(key_right == 0 && key_left == 0 && place_meeting(x,y+1,obj_wall)) //inertie
    {
    if(hsp < acc*2 && hsp > -acc*2)
    {
    hsp = 0;
    }
    }

    the gravity :

    if(!place_meeting(x,y+1,obj_wall))
    {
    if(vsp < 30)vsp += 0.1;
    airborn = 2;
    }

    "airborn" is for animation don't worry about it

    and finally
    x += hsp;
    y += vsp;
     
  4. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    And how do you deal with collisions?
     
  5. graviax

    graviax Guest

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

    //Vertical Collision
    if (place_meeting(x,y+vsp,obj_wall))
    {
    while(!place_meeting(x,y+sign(vsp),obj_wall))
    {
    y += sign(vsp);
    }
    vsp = 0;
    }
    I don't remember from who I got this, but this is from a youtube tutorial
     
  6. jo-thijs

    jo-thijs Member

    Joined:
    Jun 20, 2016
    Posts:
    2,844
    Try changing the collision code to:
    Code:
    //Horizontal Collision
    if place_meeting(x + hsp, y, obj_wall) {
        while true {
            while abs(x - xprevious) < hsp && !place_meeting(x + sign(hsp), y, obj_wall)
                x += sign(hsp);
            if abs(x - xprevious) < hsp
            && place_meeting(x + sign(hsp), y, obj_slope)
            && !place_meeting(x + sign(hsp), y - 1, obj_slope)
                y--;
            else
                break;
        }
        hsp = 0;
    } else
        x += hsp;
    
    //Vertical Collision
    if place_meeting(x, y + vsp, obj_wall) {
        while !place_meeting(x, y + sign(vsp), obj_wall)
            y += sign(vsp);
        vsp = 0;
    } else
        y += vsp;
     

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