• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Move Relative to Rotation Help

How are you turning them? If you're using image_angle:

GML:
x += lenthdir_x(_speed_,image_angle);
y += lenthdir_y(_speed_,image_angle);
 

pikachurian

Member
I am using direction. I am think of a fps style of movement where w a s d moves the player relative to their direction. Also, what's the best way of implementing collision with this style of movement?
 

rytan451

Member
Then instead of using image_angle in BattleRifle's code, you use direction.

If you're moving left (so the player is pressing A), then you're moving 90 degrees counterclockwise from forwards, so you should add 90 degrees to the second parameter. Likewise, moving right is -90 degrees counterclockwise (or 270 degrees clockwise) and backwards is 180 degrees counterclockwise.

(By the way, the functions were misspelled: they're lengthdir_x and lengthdir_y.)
 

TheouAegis

Member
You could use speed, but just be aware of how GM handles it. As soon as you set speed, at the start of the End Step event you will move in the direction of direction.
 

pikachurian

Member
Thanks for the replies. I tried using the lengthdir functions but it wasn't working properly. Here's the player's code:

GML:
///CREATE

spd = 2
hspd = 0
vspd = 0

rotateSpd = 3//30
rspd = 0

/// STEP:
hspd = -keyboard_check(ord("A")) + keyboard_check(ord("D"))
vspd = -keyboard_check(ord("S")) + keyboard_check(ord("W"))
hspd *= spd
vspd *= spd

//direction = point_direction(x,y,mouse_x,mouse_y)
rspd = keyboard_check(ord("A")) + -keyboard_check(ord("D"))
direction += rspd * rotateSpd
image_angle = direction

if(keyboard_check(ord("W")))speed = 2
else if(keyboard_check(ord("S")))speed = -2
else speed = 0

//Collisions
var _nX = x+lengthdir_x(hspd,direction-45)
if(place_meeting(_nX,y,obj_wall))
{
    while(!place_meeting(x+lengthdir_x(sign(hspd),direction-45),y,obj_wall))
    {
        x += lengthdir_x(sign(hspd),direction-45)
    }
    _nX = 0
    hspd = 0
}
x += lengthdir_x(hspd,direction-45)
//
var _nY = y+lengthdir_y(vspd,direction+45)
if(place_meeting(x,_nY,obj_wall))
{
    while(!place_meeting(x,y+lengthdir_y(sign(vspd),direction+45),obj_wall))
    {
        y += lengthdir_y(sign(vspd),direction+45)
    }
    _nY = 0
    vspd = 0
}
y += lengthdir_y(vspd,direction+45)
 
Top