This article is a supplementary write-up to the YouTube video, posted below, of the same name. The video, and this reading by extension, are brief presentations on GameMaker’s lengthdir functions. These lengthdir functions — specifically lengthdir_x and lengthdir_y — exist in all current versions of the GameMaker Studio software. So whether you’re using GMS2, GMS1, or even an older version of the software like GM8, you should find value in this content.

There are use-cases for these two functions riddled throughout the world of game development. By the time we’re through here, you’ll know how you can apply the lengthdir_x and lengthdir_y functions to create a top-down shooter game. More specifically, you’ll understand how it’s possible to fire a projectile, like a bullet, from the end-point of a line - like a gun.

## What are lengthdir_x and lengthdir_y?

Simply put: you can use lengthdir_x and lengthdir_y to find coordinates in relation to an angle.

If you’re familiar with trigonometric functions like sine and cosine, you might already know how to achieve the results of lengthdir_x and lengthdir_y *without *using them. Regardless, this walk-through is intended for novice developers as the lengthdir functions are arguably essential tools for every game maker’s toolkit.

## Finding a coordinate using lengthdir_x and lengthdir_y

To better understand the two lengthdir functions, we’ll need to create a scenario first. The setting is in two-dimensional space, or a generic GameMaker room. Refer to the image below for the specific dimensions we’re working with.

Great! Now, let’s visualize a point in this two-dimensional space located at coordinates (0, 0). We’ll call this point our **origin point**. The zero-zero location is arbitrary, but chosen out of simplicity.

Extending from the right of our origin point is a line. For this example, let’s declare the length of the line to be 200 units. As a result, the coordinates at the end-point of this line are (200, 0).

Just as (0, 0) is an arbitrary point, the 200-width line is an arbitrary measurement. The graph wouldn’t look much different if the line were 100 units long.

Now, rather than extend 200 units to the right, let’s see what happens when a 200-unit line extends upwards from our origin point. The end-point is located at (0, -200).

So far, finding the end-point locations has been a fairly easy task. We simply add or subtract the extending line’s units from the origin point’s x or y coordinate respectively. You’ll notice we’ve only ever moved about the x or y axis when performing our transformations.

When the line extends 0 degrees (to the right) or 180 degrees (to the left), only the x-value of the end-point changes. The y-value will remain the same. Likewise, when the line extends 90 degrees (upwards) or 270 degrees (downwards), only the y-value of the end-point changes while the x-value stays static.

Unfortunately, graphing in relation to only one axis is quite limiting. There’s more than four different degrees — 0, 90, 180, 270 — we can use while translating coordinates. In fact, there’s 360 from our origin point if we’re counting whole numbers.

If we extend a line from the origin point at non-90 degree interval, you’ll see we have a little problem. What is the end-point of a line that extends from the origin point at, say 135 degrees? We’ll refer to this particular end point as our **mystery coordinate** from here on out.

To solve for our mysterious coordinate, we’ll use… drum roll, please… the lengthdir_x and lengthdir_y functions! So, our mystery coordinate, the end-point of a line extending 135 degrees from the origin point, lays at coordinates (x + lengthdir_x, y + lengthdir_y).

Both lengthdir_x and lengthdir_y accept two arguments — the *same *arguments.

The first argument is

*length*.The second argument is

*direction*.

*length *+ *dir = lengthdir*. Mind blown.

You’re probably getting ahead of me. And that’s great! Let’s plug in **200 **for our first argument, *length*, and **135 **for our second argument, *direction*.

The mystery coordinate isn’t such a mystery anymore. It can be referred to as (x + lengthdir_x(200, 135), y + lengthdir_y(200, 135)) and GameMaker will handle the rest!

We could leave it at that, *but *it’s beneficial to understand what exactly these functions are doing. It’s not magic. In fact, it’s quite straightforward.

We can see the logic behind lengthdir_x and lengthdir_y unravel if we work backwards. Using the image above as reference, start at the mystery coordinate and move downwards. Trace along the dotted line parallel to the vertical line labeled “lengthdir_y”. You’ll intersect the end-point of the line labeled “lengthdir_x”. Let’s try again, but with the other dotted line. See if you notice a pattern.

Starting at the mystery coordinate, trace the horizontal dotted line extending to the right. Quickly, you’ll intersect the end-point of the vertical line labeled “lengthdir_y”.

Are you able to see what’s happening when we use these two functions?

The horizontal line labeled “lengthdir_x” extending from our origin point has an end-point that shares the x-value with the our mysterious point. And the vertical line labeled “lengthdir_y” extending from our origin point has an end-point that shares the y-value with out mysterious point.

To reiterate, lengthdir_x tells us how many units along the x-axis we need to move and lengthdir_y tells us how many units along the y-axis we need to move.

Remember that at the end of the day (*erm*, CPU cycle), we’re getting actual numbers as output from lengthdir_x and lengthdir_y.

In the case of (x + lengthdir_x(200, 135), y + lengthdir_y(200, 135)) , where (x, y) is (0, 0), we get (-141.42, -141.42).

## Quadrants

Whether x or y are positive or negative values is determined by which quadrant the end-point lay in relation to the origin point.

Following the examples, we’ve already looked at end-points that lay in the second quadrant. Consider an end-point that lays in the first quadrant.

Next, consider an end-point that lays in the third quadrant.

Finally, consider an end-point that lays in the fourth quadrant.

## Closing words

Hopefully you now know what the lengthdir_x and lengthdir_y functions are in GameMaker Studio 2. Now that you’ve read this text-based tutorial, you should be able to answer the following questions:

How can I rotate an object’s sprite to face towards the cursor?

How can I find a point off an angle?

How can I fire an instance of an object (bullet) from the tip of another object (gun)?

Thank you for reading! If you found this tutorial useful and want to see more like it, spread the word. If you want to learn more about GameMaker, consider checking out my popular post 20 GameMaker Language Code Snippets, Tips, and Tricks!