want to create ice physics w/ friction in a 2d platformer (GM8.1)

Arlan64

Member
Hello guys!

I'm stuck with some complexes codes (bc i'm a noob but nvm) when I try to put friction physics to create an awesome ice block for place it on my rooms.

I search to set the friction at 0.4 at begin of the hsp movement, and to set the friction at -0,4 at end of the hsp movement

I'll put my codes affected with I want to find:

Create Event:
Code:
hsp = 0;
movespeed_normal = 6;
movespeed = movespeed_normal;
Step Event:
Code:
//Les commandes
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check(vk_up);
key_jump_held = keyboard_check(vk_up);
key_jump_pressed = keyboard_check_pressed(vk_up);
key_down = -keyboard_check_pressed(vk_down);
key_down_held = -keyboard_check(vk_down);
key_space_pressed = keyboard_check_pressed(vk_space);

//Réactions aux déplacements basiques
move = key_left + key_right;
hsp = move * movespeed;
{if (vsp < 10)
    {
     vsp += grav_normal;
    }
}

//Prise d'appui au sol pour sauter
{if place_meeting(x,y+1,obj_floor)
    {
     if (key_jump) vsp = -jumpspeed;
    }
}

///Collisions avec le sol/mur

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

//Collision verticale
{if place_meeting(x,y+vsp,obj_floor)
    {
     while(!place_meeting(x,y+sign(vsp),obj_floor))
        {
         y += sign(vsp);
        }
     vsp = 0;
    }
}
y += vsp;
(And sorry if my english is really disgusting, I'm french...)
 

NightFrost

Member
I've actually been testing something like this recently. When you have low-friction platforms, your hsp must persist across steps just like vsp does.
Code:
...
// IF player is NOT moving.
if(move == 0){
   // Decelerate player down by friction factor. We use max() so direction doesn't flip around when it attempts to cross zero.
   hsp = max(0, abs(hsp) - friction) * sign(hsp);
}
// Otherwise player is moving
else {
   // Accelerate by friction factor.
   hsp += friction * sign(move);
   // Do not accelerate beyond maximum movement speed.
   hsp = clamp(hsp, -movespeed, movespeed);
}
You would set friction to 1 on normal ground and 0 < friction < 1 on slippery ground, where smaller numer means more slippery.

There is one design decision you must make: air control. Air control means player's ability to control character (left and right) while in air. You must decide: does current walk speed on ground affect your move speed while jumping? If you say "no" the following issues come up. Firstly, players can exploit jumping. If they are sliding across platform edge and cannot slow down in time, they can just jump and land back onto the platform, as they can freely move at full speed while jumping. Second, imagine player sliding on the platform to right at high speed. They press down left but cannot slow down before they fall off. Now as they are in air, they regain full movement speed, and will immediately flip back towards left, so they essentially look like bouncing back as they slide off. If you make horizontal movement check before vertical, they will even move back onto the platform instead of falling. On the other hand if you say "yes" and let walk speed affect air control speed, you must design the entire level with this in mind. Players will need to accelerate to be able to make long jumps. If the platform is short, they can only make short jumps. If they are standing still, they can only make vertical jumps.
 

Arlan64

Member
Okay ! I would rather say "no", because when you press the left key or the right key, the player moves 6 pixels per step horizontally. So I try to make the player progressively accelerate (from 1 to 6 pixels (I already tried to make an absolute horizontal speed limit "if abs(hspeed) > 6 {hspeed = 6;}") but I know how to do this kind of thing, just put a negative value to the rub, but I do not know how to apply it, or else the code does not want to get started because of other codes affecting these...) when he starts to move on ice. And so do the opposite effect when the player does not touch any button, and therefore the player slows down from 6 to 0 pixels per step.

(Still sorry if I misunderstood, I'm French and has a little trouble with English... :/)

PS: And GM8.1 doesn't understand the expression "clamp"...
 

Arlan64

Member
Thanks 3dgeminis! ^^

And nothing happened NightFrost... :/
Idk if I make the ice solid, or if I make the ice child of obj_floor, because the player have not actions with the ice...

upload_2019-8-25_23-7-25.png

upload_2019-8-25_23-8-24.png
 
Top