[SOLVED]Diagonal movement help :(

G

GRArthas

Guest
I am making a top down game using physics (phy_potition) but i am having problems with diagonal movement.

in my player object i created the speed variable 5 but when i press for example UP/RIGHT the variable goes to 10 and i can't find any way to fix it (i am using physics on top down game because i like the collisions and it is simple).
 
M

Marcos Ferreira

Guest
could you post your movement code? It's make more easier to help!
 

Bingdom

Googledom
This is when lengthdir_ functions come in handy ;)

Code:
xaxis = right - left;
yaxis = up - down;

dir = point_direction(0,0,xaxis,yaxis);

phy_position_x += lengthdir(5,dir);
phy_position_y += lengthdir(5,dir);
 
G

GRArthas

Guest
tried this and fixed some of your code but if i just spam every 1 pixel up or down or left it keeps on getting some pixels pushed on the right side and if i don't add the spd = 0; part it wouldn't stop going right :/

Code:
up=(keyboard_check(ord('W')) || keyboard_check(vk_up));
down=(keyboard_check(ord('S')) || keyboard_check(vk_down));
left=(keyboard_check(ord('A')) || keyboard_check(vk_left));
right=(keyboard_check(ord('D')) || keyboard_check(vk_right));

xaxis = right - left;
yaxis = down - up;

dir = point_direction(0,0,xaxis,yaxis);

phy_position_x += lengthdir_x(spd,dir);
phy_position_y += lengthdir_y(spd,dir);

if !up && !down && !right && !left
{
    spd = 0;
}
else
{
    spd = 5;
}
 

YanBG

Member
The way i do it is: get back to what you initially had, and just add 4 more cases for each time 2 keys are held, then do spd*=0.7071 inside them. Now you need to reset the speed value before the up, down etc(at the begining of the step script).
 

YanBG

Member
Code:
spd=5;
if(left){
    phy_position_x-=spd;
}
if(right){
    phy_position_x+=spd;
}
if(up){
    phy_position_y-=spd;
}
if(down){
    phy_position_y+=spd;
}
if(left&&up){
    spd*=0.7071;
}
if(right&&up){
    spd*=0.7071;
}
if(left&&down){
    spd*=0.7071;
}
if(right&&down){
    spd*=0.7071;
}
 
G

GRArthas

Guest
I tried this but the diagonal speed is still x2
Code:
up=(keyboard_check(ord('W')) || keyboard_check(vk_up));
down=(keyboard_check(ord('S')) || keyboard_check(vk_down));
left=(keyboard_check(ord('A')) || keyboard_check(vk_left));
right=(keyboard_check(ord('D')) || keyboard_check(vk_right));

spd=5;

if(left)
{
    phy_position_x-=spd;
}
if(right)
{
    phy_position_x+=spd;
}
if(up)
{
    phy_position_y-=spd;
}
if(down)
{
    phy_position_y+=spd;
}
if(left&&up)
{
    spd*=0.7071;
}
if(right&&up)
{
    spd*=0.7071;
}
if(left&&down)
{
    spd*=0.7071;
}
if(right&&down)
{
    spd*=0.7071;
}
 

Yambam

Member
This is what I always use to limit diagonal speed:
Code:
spd=6

var dx,dy,dlen;
dx=keyboard_check(vk_right)-keyboard_check(vk_left)+
   keyboard_check(ord('D'))-keyboard_check(ord('A'))
dy=keyboard_check(vk_down)-keyboard_check(vk_up)+
   keyboard_check(ord('S'))-keyboard_check(ord('W'))
dlen=point_distance(0,0,dx,dy)
dx*=spd/dlen
dy*=spd/dlen

phy_position_x+=dx
phy_position_y+=dy
 

Yambam

Member
It's most likely the order of YanBG's code, he first uses the spd variable and then sets the spd variable to something else! :D
 
G

GRArthas

Guest
I tried this and it works perfectly thanks for all your help :)
Code:
up=(keyboard_check(ord('W')) || keyboard_check(vk_up));
down=(keyboard_check(ord('S')) || keyboard_check(vk_down));
left=(keyboard_check(ord('A')) || keyboard_check(vk_left));
right=(keyboard_check(ord('D')) || keyboard_check(vk_right));

spd=5

xaxis = (right - left);
yaxis = (down - up);


dir = point_direction(0, 0, xaxis, yaxis);

if(xaxis == 0 and yaxis == 0)
{
len = 0;
}
else
{
len = spd;
}

hspd = lengthdir_x(len, dir);
vspd = lengthdir_y(len, dir);

phy_position_x += hspd;
phy_position_y += vspd;
 
Top