SOLVED collision,friction and accel problems

FoufaDjo

Member
i followed a tuto on yt about movement and the dont use the speed var so i cant use the friction function can someone help me:
hspd = (rkey-lkey);
vspd = (dkey-ukey);

if (hspd != 0 or vspd != 0){
dir = point_direction(0,0,hspd,vspd);
hmove = lengthdir_x(spd,dir);
vmove = lengthdir_y(spd,dir);

x += hmove;
y += vmove;
}
 

Slyddar

Member
To get acceleration, you need to be repeatedly applying the spd to the movement variables over time, in order to increase them. So instead of just setting hspd and vspd, you could add the new value onto the previous amount each step. Also applying them to x and y needs to happen even when you are not pressing a key, to achieve the acc and dec, so those lines need to be out of the brackets.

Try it this way:
GML:
if (hspd != 0 or vspd != 0){
    dir = point_direction(0,0,hspd,vspd);
    hmove += lengthdir_x(spd,dir);
    vmove += lengthdir_y(spd,dir);
}

//slowdown over time
hmove = lerp(hmove, 0, 0.05);
vmove = lerp(vmove, 0, 0.05);

//limit speed
hmove = clamp(hmove, -max_spd, max_spd);
vmove = clamp(vmove, -max_spd, max_spd);

x += hmove;
y += vmove;
Something like max_spd = 4 and spd = 1 should work, or whatever you want.
 

FoufaDjo

Member
To get acceleration, you need to be repeatedly applying the spd to the movement variables over time, in order to increase them. So instead of just setting hspd and vspd, you could add the new value onto the previous amount each step. Also applying them to x and y needs to happen even when you are not pressing a key, to achieve the acc and dec, so those lines need to be out of the brackets.

Try it this way:
GML:
if (hspd != 0 or vspd != 0){
    dir = point_direction(0,0,hspd,vspd);
    hmove += lengthdir_x(spd,dir);
    vmove += lengthdir_y(spd,dir);
}

//slowdown over time
hmove = lerp(hmove, 0, 0.05);
vmove = lerp(vmove, 0, 0.05);

//limit speed
hmove = clamp(hmove, -max_spd, max_spd);
vmove = clamp(vmove, -max_spd, max_spd);

x += hmove;
y += vmove;
Something like max_spd = 4 and spd = 1 should work, or whatever you want.
it work perfectly thnx dude but just how to change the accel val and the friction i want the friction to be weaker am changeing the 0.5 but the accel go down
 
Last edited:

Slyddar

Member
Well I just used your variables to make it easy for you, but really the spd variable is the acceleration variable. The correct way should be like this:
GML:
//create event
//omitting other variables
acc = 0.5;
dec = 0.1;

//step event
hspd = (rkey-lkey);
vspd = (dkey-ukey);

if (hspd != 0 or vspd != 0){
    dir = point_direction(0,0,hspd,vspd);
    hmove += lengthdir_x(acc,dir);
    vmove += lengthdir_y(acc,dir);  
}

hmove = lerp(hmove, 0, dec);
vmove = lerp(vmove, 0, dec);

hmove = clamp(hmove, -max_spd, max_spd);
vmove = clamp(vmove, -max_spd, max_spd);

x += hmove;
y += vmove;
 

FoufaDjo

Member
Well I just used your variables to make it easy for you, but really the spd variable is the acceleration variable. The correct way should be like this:
GML:
//create event
//omitting other variables
acc = 0.5;
dec = 0.1;

//step event
hspd = (rkey-lkey);
vspd = (dkey-ukey);

if (hspd != 0 or vspd != 0){
    dir = point_direction(0,0,hspd,vspd);
    hmove += lengthdir_x(acc,dir);
    vmove += lengthdir_y(acc,dir);
}

hmove = lerp(hmove, 0, dec);
vmove = lerp(vmove, 0, dec);

hmove = clamp(hmove, -max_spd, max_spd);
vmove = clamp(vmove, -max_spd, max_spd);

x += hmove;
y += vmove;
thnx for ur reply i thinx i fond the problem i need to set the spdmax and accel the same value like this to not slow down : spdmax = 3 && accel = 0.3
 

FoufaDjo

Member
thnx for ur reply i thinx i fond the problem i need to set the spdmax and accel the same value like this to not slow down : spdmax = 3 && accel = 0.3
tho one problem the collision dont work correctly it just slows down the player:
with instance_place(x+hmove, y, osolid){
if solidstate == 1{
with other {
repeat(abs(hmove)){
if !place_meeting(x+sign(hmove),y,other){
x += sign(hmove);
} else {
break;
}
}
hmove = 0;
}
}
}
with instance_place(x,y+vmove,osolid){
if solidstate == 1{
with other {
repeat(abs(vmove)){
if !place_meeting(x,y+sign(vmove),other){
y += sign(vmove);
} else {
break;
}
}
vmove = 0;
}
}
}
 

Slyddar

Member
Probably because the calcs for a collision event happen after the movement in the step event, so at that point the object is already in the wall.

Here's some working code, which uses a more standard collision method, and the more standard hsp(d) and vsp(d) variables for the hor and vert movement. I've seen many people just use hmove and vmove as local variables initially instead. In the end it doesn't matter of course, but it's more of a standard that you will see around.

GML:
var rkey = keyboard_check(ord("D"));
var lkey = keyboard_check(ord("A"));
var ukey = keyboard_check(ord("W"));
var dkey = keyboard_check(ord("S"));

var hmove = (rkey-lkey);
var vmove = (dkey-ukey);

if (hmove != 0 or vmove != 0){
    dir = point_direction(0,0,hmove,vmove);
    hspd += lengthdir_x(acc,dir);
    vspd += lengthdir_y(acc,dir);    
}

hspd = lerp(hspd, 0, dec);
vspd = lerp(vspd, 0, dec);

hspd = clamp(hspd, -max_spd, max_spd);
vspd = clamp(vspd, -max_spd, max_spd);

if place_meeting(x + hspd, y, o_solid) {
    while !place_meeting(x + sign(hspd), y, o_solid) x+=sign(hspd);
    hspd = 0;
}
x += hspd;

if place_meeting(x, y + vspd, o_solid) {
    while !place_meeting(x, y + sign(vspd), o_solid) y+=sign(vspd);
    vspd = 0;
}
y += vspd;
 
Top