I
IndieCrypt
Guest
Hello,
as the title suggests I am wondering how to come about making smooth top down movement...
as the title suggests I am wondering how to come about making smooth top down movement...
Ah right... (Next time I ask a question I will be more precise!)It depends on how your game works.
Do you just want movement or also collision?
Should horizontal and vertical speeds be considered seperate or should they be taken together?
Is there any kind of acceleration?
Does the player move in the direction the arrow keys are pressed?
What if opposite keys are pressed?
...
CREATE EVENT:
///Initialize variables
vx = 0; // horizontal velocity
vy = 0; // vertical velocity
mv = 6; // maximal velocity
a = 0.2; // acceleration
nd = 0.2; // normal deacceleration
sd = 0.3; // skid deacceleration
STEP EVENT:
///Movement and Collision
var h = keyboard_check(vk_right) - keyboard_check(vk_left);
var v = keyboard_check(vk_down) - keyboard_check(vk_up);
var spd = sqrt(vx * vx + vy * vy);
if h == 0 && v == 0 {
// deaccelerate when not moving
if spd <= nd {
vx = 0;
vy = 0;
} else {
vx -= vx / spd * nd;
vy -= vy / spd * nd;
}
} else {
if vx * h + vy * v < 0 {
// skid
if spd <= sd {
vx = 0;
vy = 0;
} else {
vx -= vx / spd * sd;
vy -= vy / spd * sd;
}
} else {
// accelerate
vx += h * acc;
vy += v * acc;
spd = sqrt(vx * vx + vy * vy);
if spd > mv {
vx = vx / spd * mv;
vy = vy / spd * mv;
}
}
}
// Change the below to have some collision in it
x += vx;
y += vy;