# GM:S 1.4 slope/ramp

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

1. ### graviaxGuest

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-thijsMember

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

3. ### graviaxGuest

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-thijsMember

Joined:
Jun 20, 2016
Posts:
2,846
And how do you deal with collisions?

5. ### graviaxGuest

//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-thijsMember

Joined:
Jun 20, 2016
Posts:
2,846
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;```