Lesson 10 – Vector Maths

I write this to you with no idea how to deliver this information. All I know is that I must deliver this information. Something tells me I must. Many of it will be a re-iteration of already explained information. But…what isn’t? This is more for me then it is for you, potential reader. This is for me to tell myself “You’re doing good, kid”. I WILL NEVER STOP UNTIL I MAKE THE LIKES OF MIYAMOTO PROUD!!!!

By now you may have noticed a lot of talk in the Unity docs regarding Vectors. But what are they exactly? Is it an objects position? Does it have anything to do with images? HELP!

What is a Vector?

– Put simply: a Vector is a line drawn between two points. Not exactly the answer you were expecting, huh? Well, tough! A lot of your answers won’t be filled with magic and rainbows!
– You can’t speak about Vectors without mentioning magnitude: the length of a Vector.

That was cute…but seriously, what is a Vector?

I’m not pulling your chain. On the very base, this is what a Vector and magnitude are. Luckily for you, in relation to what you need to know for Game Development and Unity, it gets more complex:

2D-Vectors

– A 2D-Vector is a way of representing a point from the Origin point (0,0) to any point on the 2D plane. Also note: Since it is relative to the Origin point, it has an implied direction.

2DVector

If you see the chart above, you’ll notice the Origin point (0,0) and the current position (12, 5). This is an example of a 2D-Vector. Just keep repeating in your head “A 2D-Vector is any point in the 2D Plane in relation to the Origin point (0,0)”, and you’ll be fine… I think.

We brought up that magnitude is the length of a Vector. more specifically, magnitude would be the length of a distance between 2 points. This is going to get real math-y, but please, bare with me. In order to find the magnitude of a 2D Vector, you can use Pythagorean’s Theorem to work it out mathematically.

The Pythagorean’s Theorem goes as follows: PythagoreanTHeory

If we plug in our current position in the 2D-Plane, which would be (12,5), to the Pythagorean’s Theorem, we work it out as such:

PythagoreansTheoryWorked

I want to make this evident: Math is not magic. Everything has an explanation. Everything is simpler than you think, and equally more complex than you give it credit for. Please, do not be intimidated. Formulas are there to help you. Math is there to help you.

What do we know so far, gents?

So, we know what a Vector and a magnitude are, in it’s simplest forms. We know what a 2D-Vector is, and we know how to find the magnitude for a 2D-Vector. It seems that we have position and length covered, but what about movement? Does this live within the domain of Vectors? You damn right it does!

What is Velocity?

Simply put, Velocity is a change in position over time.

Let’s say you had a position of (5,6), and a velocity of (12,5) per hour. What does this mean? Well for one, we know that in an hours time, you will have traveled 12 positions further in the x axis and 5 positions further in the y axis — from your current position.

Velocity1

From this point, we can add the provided velocity with your current position to come up with the new position:

Velocity2

Now we have two points: the previous position (5,6) and the new position (17,11) after the velocity is applied. The velocity stays the same, as it is not a position, but a change in position over time. Keep in mind that all Vectors, whether current positions or new ones, are relative to the origin. This applies to velocity vectors as well. In the same way that (5,6) is a position relative to (0,0), (12,5) is a velocity relative to 0 motion.

RelativePosition.PNG

AND NOW FOR THE THIRD DIMENSION! 😀

Now, now. Don’t be a wuss. The rules don’t change much from 2D to 3D, we’re just adding another dimension to the rules (sorry…). Where 2D-Vectors work with the X and Y planes, 3D-Vectors work with the X, Y, and Z planes. The 3D-Vector is always in the order of (x,y,z). Lastly, the Z plane represents depth.

XYZplane

Just to show you that 3D is not so different from 2D, let’s find the magnitude for the provided 3D-Vector in the same way we did for the previous example. The current position is (12,7,5).

3DVector

In order to find the magnitude of a 3D-Vector, you can use Pythagorean’s Theorem to work it out mathematically (JUST AS YOU DID FOR 2D-VECTORS :D). All you have to do is add the Z plane to the formula, and your good to go:

PythagoreansTheory3DWorked

Conclusion:

As game developers using Unity, we spend an obscene amount of time dealing with Vectors. Understanding Vectors turns them into something you can comprehend, instead of an abstract value you are dealing with. The scripting resource goes into deeper detail detailing 3D-Vectors, but for the purpose of understanding what a Vector is, I believe that this was the better stopping point. I thought I was going to cry reading this info, but it was surprisingly straightforward! Keep at it, everyone!

Study Resource:
https://unity3d.com/learn/tutorials/topics/scripting/vector-maths?playlist=17117

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s