Lengthdir x

Explanation
The lengthdir_x function is a mathematical function that returns the horizontal x-component of the vector determined by the indicated length and direction.

The following image will make this clear:



As you can see, on the image are arrows starting at the origin and pointing to a certain point in the xy-plane. These arrows are called vectors. They have a certain length and form an angle with the positive x-axis. The distance in horizontal direction between those two points (as seen on the x-axis) is lengthdir_x, or mathematically: cos(angle) * length, angle must be in radians.

You might encounter such a situation in Game Maker where you've calculated that length and direction with the functions point_distance and point_direction. The first point (x1,y1) would then be the origin and the second point (x2,y2) would be the arrow top.

To know the distance in horizontal direction (so the distance on the x-axis) between those two points, you can use lengthdir_x(l,x), where l is point_distance(x1,y1,x2,y2) and x is point_direction(x1,y1,x2,y2).

On the image, you should notice two things:


 * When the second point is to the left of the first, lengthdir_x is negative.


 * When the second point is to the right of the first, lengthdir_x is positive.

Examples
Imagine a sprite representing the top of a tank. The origin of that sprite is placed in the center of the hull. The barrel is sticking out and is pointing right on the original sprite. The distance from the origin to the end of the barrel is 14 pixels. if (can_shoot==true) { instance_create(x+lengthdir_x(14,image_angle),y+lengthdir_y(14,image_angle),obj_bullet); can_shoot=false; alarm[0]=30//alarm that sets can_shoot back to true }

Remarks
It is faster than its mathematical trig equivalent since it runs precompiled Delphi.