## Take Up Code « » 246: GameDev: Points Vs. Vectors. Which Should You Use?

8:42

Manage episode 219882938 series 125918
By , , , , and . Discovered by Player FM and our community — copyright is owned by the publisher, not Player FM, and audio is streamed directly from their servers. Hit the Subscribe button to track updates in Player FM, or paste the feed URL into other podcast apps.

Do you know the differences between points and vectors? You might be surprised. I learned a few things myself recently when I implemented points and vectors in the TUCUT library. Listen to episode 240 to learn more about TUCUT.

As to which you should use, that’s sort of a trick question. You’ll most likely use whatever your game engine provides. Unless you’re writing your own library like I’m doing, the idea of a point and a vector are very basic concepts that would be provided for you.

Any type of drawing needs to know where to do the drawing.

Animations need to know which directions to move.

Game items need to know where they exist in the world.

Listen to the full episode to learn what you can do with points and vectors, how they can interact, and why many game engines provide only vectors. You can also read the full transcript below.

## Transcript

You might be surprised. I learned a few things myself recently when I implemented points and vectors in the TUCUT library. Listen to episode 240 to learn more about TUCUT.

As to which you should use, that’s sort of a trick question. You’ll most likely use whatever your game engine provides. Unless you’re writing your own library like I’m doing, the idea of a point and a vector are very basic concepts that would be provided for you.

Any type of drawing needs to know where to do the drawing.

Animations need to know which directions to move.

Game items need to know where they exist in the world.

And because game engines provide these features, they need a way for you to specify points and vectors.

Traditionally, points have been mostly a geometry topic and vectors an algebra topic. I see them as very close concepts and the differences between them can sometimes be a matter of preference.

One of the strange things I found in my research was the idea that points have no coordinates. They exist at some location and the reason they have no coordinates is because there could be multiple coordinates that end up in the same location. For example, if you have a sphere like earth and a point is some city. Then you can pick an origin and go to the city. But you can also keep going and come all the way around the sphere until you reach the same city again. Depending on how your points exist in the universe, they might have many different coordinates.

Or maybe you have multiple origins or an origin that moves around. This would cause the coordinates to change. Think of it like this, if you live near the border of a city and the city border changes, then you might get a new mailing address. It’s not like your house suddenly changed its location. But the origin changed and now your address is relative to a different city or county.

Or maybe you have different ways of measuring distance. It could be millimeters or inches. The same point will have different coordinates if you use different measurement systems.

To me, these are not good reasons for saying that points have no coordinates. I’m okay with the idea that coordinates need to be matched with a specific origin and measurement system. If the origin changes, then the coordinates will change. And if there’s different ways to get to the same point, then to me that’s okay too.

A vector is more flexible in this sense. Because vectors have no origin.

Let’s take an example in a single dimension. Imagine a point at coordinate 8 and another point at 10. A vector will tell you how to get from 8 to 10. Just move two unit positions to the right. That’s all a vector is. Just a magnitude and a direction. In this example, the magnitude is 2 and the direction is towards the right.

What if you want to go from point 5 to point 7? You get the same vector, two units to the right.

Now, the points themselves are based on the origin. So you get to a point at position 5 by starting at the origin and moving five units to the right.

This is where the differences between points and vectors start to become blurred. Because a points coordinates define the location of a point from the origin and because a vector also defines how to get from one location to another, then it’s easy to see how everything can be thought of as a vector.

A lot of game engines may only have a vector class. And vectors are used to represent both points and vectors.

Let’s say we have two dimensions. If we move in the positive x direction by 2 units and then in the positive y direction by 2 units, then we arrive at the point at 2, 2. Now, I mentioned moving first in x and then in y. That was just to help explain the position. The point at 2, 2 really has nothing to do with moving. It just exists at that location. A vector is about moving. And a 2, 2 vector starting at the origin would also arrive at the same 2, 2 point.

You can think of any point as the result of starting at the origin and moving along a vector with the same axis components as the point coordinates.

You can also think of the origin as a point with all zero coordinates. And adding a vector to the origin point is what takes you to another point.

A vector can be added to any point which will cause you to arrive at another point. Unless, of course, the vector zero is used. Then you don’t really go anywhere and stay on the starting point.

Now, you could, I suppose, add a one dimensional vector to a 2 dimensional point. Or even add a 2 dimensional vector to a 3 dimensional point. But that’s stretching things a bit too far, I think. Let’s stick with the vectors and the points all having the same number of dimensions. At least I’m not aware of any practical uses for adding vectors and points with different number of dimensions. If anybody knows of a use for this, then please let me know.

I should also say that when adding vectors, it doesn’t always mean that the result will be positive numbers or even bigger than what we started with. You could have a -1, 0 vector that if you add it to the 2, 2 point, then you’ll end up at the point 1, 2. Notice that you can have a vector with components set to zero. That’s another reason that I don’t think there’s any value in considering vectors and points interacting with different number of dimensions. If you really only want to move along a single axis, then just set everything else to zero.

Before ending, let’s think about some other operations we might want to perform on points and vectors. I mentioned already that you can add a vector to a point. You then get another point. You can also subtract a vector from a point. That’s just like changing the sign of the vector components before adding it. When you start out with a point, you can add or subtract a vector and you end up with another point.

I mentioned earlier that you can think of a vector as telling you how to move from a point at position 8 to a point at position 10. This is what you get when you subtract points. So taking the point at 10 and subtracting the point at 8 gives you a vector of 2.

What about adding points? We can add vectors. But adding points doesn’t make any sense.

What about multiplying points or dividing points? Again, these don’t really make any sense.

But you can multiply and divide vectors by a fixed number. This just means that you multiply or divide each component of the vector by the same number. This has the effect of making the vector longer or shorter without changing its direction. Unless you multiply by a negative number. That’ll reverse a vector. But multiplying or dividing won’t cause a vector to change its direction slightly. It can grow and shrink, continue to point in the same direction or turn it completely around to point in the opposite direction.

It’s because of these differences between what you can do with vectors vs. points that I decided to keep the concepts separate in the TUCUT library. Anytime the library needs a fixed position, it uses a point. And anytime it needs a direction, it uses a vector. I might later change my mind and use only vectors for everything. But until I find a reason otherwise, I’d like to keep these two ideas separate.

I didn’t go into what it means to multiply vectors in this episode. That’s a whole different topic.

296 episodes