# Using lengthdir to lock object to another

J

#### J.J.

##### Guest
I have two objects (Wagon and Turret) and I want the turret attached to the wagon. I found out that lengthdir is the one to use for this. But I'm no programmer/scripter so who can help me out step for step.

#### GMWolf

##### aka fel666
The easiest way is to find the angle from the origins and distance of the turret.
Then, use something like:
Code:
``````x = wagon.x + lengthdir_x(wagon.image_angle + aoffset, offset);
Y = wagon.y + lengthdir_y(wagon.image_angle + aoffset, offset);``````
Where aoffset is the angle offset, and offset is the distance offset.

A more complicated approach is using matrices.
You must first find the x and y offset of the turret. Let's call them xoff and yoff.
Then, use the following code:
Code:
``````//build the transform matrix from the wagon
var mat = matrix_build(wagon.x, wagon.y, 0, 0, 0, wagon.image_angle, 1,1,1);
//transform our offsets
var o =  matrix_transform_vertex(mat, xoff, yoff, 0);

//get our x and y coords
x = o;
y = o;``````

Last edited:
J

#### J.J.

##### Guest
@Fel666 Do I put this in the creation code?

Last edited by a moderator:

#### GMWolf

##### aka fel666
Do I put this in the creation code?
No, the step event.
Note - it won't work right out of the box. You should adapt it to your project.