Legacy GM Lengthdir_x and y problem.

darijs1

Member
ive tried a lot of things here, but i couldnt figure it out.

im trying to have a wx and wy follow my car at a certain position with the old lengthdir method, but it only seems to get more and more out of place, the more i keep trying.

currently im using:
step event in car:
Code:
wx = x + lengthdir_x(lenx,image_angle);
wy  = y + lengthdir_y(leny,image_angle);
lenx & leny = 0 (though ive tried messing with these, and it didnt work out)

pls help.wobwobwowbowobwb.png
 

Attachments

TsukaYuriko

☄️
Forum Staff
Moderator
lenx/leny will have to be something other than 0 for that code to have any effect.

Ensure that your sprite resource is facing east, as that's where directions start internally.

Take a look at the manual page of lengthdir_x, specifically the image.

Take a look at your sprite again. Draw an imaginary line between your center point and your target point. The length and direction of that line are the len and dir to use in the lengthdirs.

(Edit: Clarity.)
 
Last edited:
To give an additional pointer on top of @TsukaYuriko excellent advice, just eyeballing it, the angle you need to start with is around 135 degrees. To have it stay in the same place when the car rotates, you'll need to add the cars rotation to the start angle.
 

darijs1

Member
lenx/leny will have to be something other than 0 for that code to have any effect.

Ensure that your sprite resource is facing east, as that's where directions start internally.

Take a look at the manual page of lengthdir_x, specifically the image.

Take a look at your sprite again. Draw an imaginary line between your center point and your target point. The length and direction of that line are the len and dir to use in the lengthdirs.

(Edit: Clarity.)
thank you for replying so quickly.

i am aware that lenx and leny need to be changed depending on what i supposedly want to achieve, however:
hmm.png hmmm.png hmmmmm.png
The only way to make it work is to make both numbers identical, but that way it only keeps them in the center of the car, while im trying to have them at where the wheels are.

it might be worth noting that my car has a scale of 0.6 (xscale & yscale), which might be interfering with my goals, but i still have no idea how to fix this overall.

im very confused about how this works.
 

TsukaYuriko

☄️
Forum Staff
Moderator
In this step:
Draw an imaginary line between your center point and your target point. The length and direction of that line are the len and dir to use in the lengthdirs.
it seems you only calculated the length. You have to calculate the direction between the two points, too. Keeping the direction the same as the instance's rotation will only ever allow you to put something in front of or behind the instance.

GM actually supplies helpful tools to perform these steps - point_direction and point_distance.
Color the center pixel and target pixel of your sprite (make a copy).
Write down the coordinates (in the sprite editor) of both of them.
Calculate the distance and direction between center and target using point_direction and point_distance. The results are your len and dir (relative to rotation) to use in your code.

it might be worth noting that my car has a scale of 0.6 (xscale & yscale), which might be interfering with my goals, but i still have no idea how to fix this overall.
Indeed it does. You will have to scale len accordingly.
 

darijs1

Member
To give an additional pointer on top of @TsukaYuriko excellent advice, just eyeballing it, the angle you need to start with is around 135 degrees. To have it stay in the same place when the car rotates, you'll need to add the cars rotation to the start angle.
Alright, so i had no idea that i had to also work with the direction. Changing image_angle to 135+image_angle seems to have the impact i was looking for.
 

darijs1

Member
In this step:

it seems you only calculated the length. You have to calculate the direction between the two points, too. Keeping the direction the same as the instance's rotation will only ever allow you to put something in front of or behind the instance.

GM actually supplies helpful tools to perform these steps - point_direction and point_distance.
Color the center pixel and target pixel of your sprite (make a copy).
Write down the coordinates (in the sprite editor) of both of them.
Calculate the distance and direction between center and target using point_direction and point_distance. The results are your len and dir (relative to rotation) to use in your code.


Indeed it does. You will have to scale len accordingly.
Thanks a lot for the help. I think i realise my errors now.
 
Top