Basic Game Math: Vector Length

Development3 min read

In the last couple post, I write about basic Vector operation addition, subtraction, multiplication, and division. Not only that, but I also give an example of how to use them in game development. Now, in this post, I will write about Length on Vector.

Vector, as far as I know, mostly used to represent a position of some point or some object, relative to one point or another object. That mean because of how vector work, a vector has a length that you can use for calculating the distance between a point to another point for example.

But first, we need to know how to calculate vector length. It’s quite easy, really. If you know how to calculate the hypotenuse of a triangle, then this shouldn’t be that hard. See this Vector:

Vector

If you look closely, when you draw a straight line from Vector A head (head is in the arrow) to axis-x, you can get a right triangle. And with that in mind, you can calculate the hypotenuse side of the triangle which is:

length = Sqrt(a.x^2 + a.y^2)

Simple right? And you know what? You can do this for 3-dimensional vector, only add one more axis to it.

length = Sqrt(a.x^2 + a.y^2 + a.z^2)

That it?

Of course. But we don’t stop here. With that knowledge, we can calculate point distance too.

Vector Distance

You may already know this. An object in the game usually or will always have a Vector, in XY (2D) or XYZ (3D). Usually, it used to determine it’s size or position relative to center (0,0) or to the object parent. Since it has a point position, you may need to calculate the distance between 2 of them.

If you read my previous post which is about vector subtraction, you may already get the idea how to achieve this.

First, you need to calculate vector direction between two points/object, just subtract them. Second, you just need to calculate it’s length. In short:

length = Sqrt((a.x-b.x)^2 + (a.y-b.y)^2)

Small note, it doesn’t matter if the direction you calculate is from a to b or b to a, the result will still be the same because the distance is still the same even if the direction change. And you can do the same equation with 3-dimensional vector too, like this:

length = Sqrt((a.x-b.x)^2 + (a.y-b.y)^2 + (a.z-b.z)^2)

Normalized Vector

If you do a game development in unity or any public engine out here, you may already know this or at least see this, normalized vector. So what is it?

Have you ever want to move your object to some direction with a vector direction, but you want to control the distance it’s move.

Let’s say, you have this vector as direction: direction = Vector2(9, 12)

And want to move your character at position = Vector2(0, 0), you will do it like this:position = Vector2(0, 0) , you will do it like this:

position = position + direction

But since the direction length is too big which is around 15 unit in this scenario, your object will move too fast which is you probably don’t want. You need to control how much the distance it will move. For example, a car will move at 15 unit per seconds but a bicycle will only move 3 unit per seconds.

To do that, you need your direction length to grow or shrink depend on what speed you want to achieve. The easiest way is to normalize your direction and multiply it by some number.

By normalizing your vector direction, its length will change to 1 except if your direction is 0. To do that, you just need to divide your vector with its length, like this:

normalized = direction / length

And with that, you just need to multiply your direction with some number to determine it’s speed. Like this:

position = position + (normalized * speed)

If your object moves 13 unit per seconds, then set your speed to 13. If your object moves 2 unit per seconds, then set your speed to 2.

But in game, usually, you want to move it per frame to make its movement smooth. To do this you just have to change speed, instead per second, set it to per frame.

speedPerFrame = speedPerSeconds * deltaTime

Where deltaTime is your frame time. If your application runs on stable 60 fps, your deltaTime probably around 0.01666666666666666666666666666667.

In short, your equation will look like this when you want to move your object per frame:

position = position + (normalizedDirection * speed * deltaTime)

This way, your object position suppose to change smoothly.

And that it for now. If you have any question or suggestion to make, write it in the comment section bellow. Have a good day!