Smooth top down movement?

I

IndieCrypt

Guest
Hello,
as the title suggests I am wondering how to come about making smooth top down movement...
 

jo-thijs

Member
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?
...
 
I

IndieCrypt

Guest
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?
...
Ah right... (Next time I ask a question I will be more precise!)

Well I want to have it so you use (normally) WASD or the arrow keys and I want some kind of acceleration and deceleration, (Kind of like how in Mario or Sonic if you are moving at max speed and you try to go the opposite way you skid, decelerate and then accelerate in the opposite direction)... Along with this I want some kind of collision built into the movement system...

Edit: Basically I want normal top down movement system but there is collision checking and acceleration and deceleration...
 

jo-thijs

Member
So something like this?
Code:
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;
 
S

Stef

Guest
/// @description Move
var len, dir, moveX, moveY, inputHorizontal, inputVertical;
var spd = 4;

if (gamepad_is_connected(0)) {
gamepad_set_axis_deadzone(0, .1);
inputHorizontal = gamepad_axis_value(0, gp_axislh);
inputVertical = gamepad_axis_value(0, gp_axislv);
} else {
inputHorizontal = keyboard_check(ord("A")) - keyboard_check(ord("Q"));
inputVertical = keyboard_check(ord("S")) - keyboard_check(ord("Z"));
}

dir = point_direction(0, 0, inputHorizontal, inputVertical);

moveX = lengthdir_x(abs(inputHorizontal*spd), dir);
moveY = lengthdir_y(abs(inputVertical*spd), dir);

// Collisions
// Horizontal
if (place_meeting(x+moveX, y, oSolid)) {
while (!place_meeting(x+sign(moveX), y, oSolid)) {
x += sign(moveX);
}
moveX = 0;
}

x += moveX;

// Vertical
if (place_meeting(x, y+moveY, oSolid)) {
while (!place_meeting(x, y+sign(moveY), oSolid)) {
y += sign(moveY);
}
moveY = 0;
}
 
Q

queauem

Guest
steph : you should add :

y += moveY;

or it won't work :)

anyway , i begin on GML (came from construct2) and i struggle.
i dont understand where in your code there isd acceleration or deceleration.

i tried to make the var "spd" evolve but it seems to be set to 4 each step so its not working
and when i try to declare spd on create event, its not working either...

i gonna try to understand tho-thijs example. *
but thanks
 
Top