27
\$\begingroup\$

I am trying to understand vector arithmetic (and specifically its use in the Unity engine). I am not able to figure out how a vector can have a length (magnitude) even though it only represents a point (position and direction)?

Does that mean that the magnitude is simply its distance from the origin point (0, 0, 0)? Or am I missing something?

\$\endgroup\$
  • 14
    \$\begingroup\$ Consider a scalar, also known as a number. It can mean an absolute value, a difference, a percentage, etc. \$\endgroup\$ – Peter - Unban Robert Harvey May 30 '17 at 13:22
  • 1
    \$\begingroup\$ Normalized in the context means a new vector that preserves the Direction but has Magnitude of 1. That is, the Normalized vector is created by scaling the original vector. \$\endgroup\$ – Theraot May 30 '17 at 13:34
  • \$\begingroup\$ @Theraot, Thank you very much, that sentence helped me a lot! \$\endgroup\$ – Mohammed Noureldin May 30 '17 at 13:38
  • 19
    \$\begingroup\$ It doesn't. It represents a displacement. It only points to some point if you consider it an position vector, in which case it denotes the displacement from (0, 0, 0). The length of such a position vector is the distance of the point to the origin. \$\endgroup\$ – Polygnome May 30 '17 at 15:06
  • 1
    \$\begingroup\$ @Peter I'm afraid I have to disagree with you. Standard algebraic definitions of a vector pretty much mean its not a point. its often useful to consider it as such since position vectors can be used to represent points, but they are not points. "5 meters" is always a distance (or length), it will never be a time or color. It often useful to use different symbols - I personally would never use (5, 5, 5) to denote a vector, I'd always use (5, 5, 5)^T (T for transposed) or use proper column-representation where supported. Because saying a vector is a point introduces inaccuracies. \$\endgroup\$ – Polygnome May 31 '17 at 19:50
20
\$\begingroup\$

Does that mean that the magnitude is simply it is distance from the origin point (0, 0, 0)?

The tl;dr answer may be: Yes, you can imagine it like that.

But I'm not sure whether this might not lead to a wrong understanding.


A vector is not a point, and there is a crucial difference between the two!

The fact that a vector is usually represented as an "arrow" might give a wrong impression. A vector is, in fact, not a single arrow. It would be more precise to say that a vector is the set of all arrows that have the same length and direction. (The arrow that is usually painted is just one representative of all these arrows). But I don't want to go too far into the boring details of mathematics here.

More importantly, there is a crucial difference between a point and a vector, that becomes obvious in graphics programming when you transform the point or vector. I'm not familiar with Unity, but from a quick glance at the documentation, they are modeling the most important difference between a point and a vector in the Matrix4x4 class. It has two different functions:

The difference is, roughly speaking, that a vector is not translated, whereas a point is. Imagine the following 4x4 matrix:

1.0   0.0   0.0   1.0
0.0   1.0   0.0   2.0
0.0   0.0   1.0   3.0
0.0   0.0   0.0   1.0

It describes a translation about (1,2,3). Now, when you have the following pseudocode

Vector3 tp = matrix.MultiplyPoint (new Vector3(2,3,4));
Vector3 tv = matrix.MultiplyVector(new Vector3(2,3,4));

Then tp will be (3,4,5), wheras tv will still be (2,3,4). Translating a vector does not change it (because, as mentioned above, it is the set of all arrows with the same magnitude and direction).


The fact that Unity uses the Vector3 class for both, vectors and for points, is legitimate, but may be confusing. Other libraries dedicatedly differentiate between Point3D and Vector3D, sometimes with a common base like Tuple3D.

\$\endgroup\$
  • 3
    \$\begingroup\$ Are you sure “a vector is the set of all arrows that have the same length and direction” makes sense, mathematically? Sounds like you're talking about some equivalence classes, but vector spaces are not something I've ever read defined as equivalence classes. — Whatever, you raise a very important... ahem, point, with the distinction between vector spaces and affine spaces, which are the mathematical names for the types of all vectors / of all points, respectively. \$\endgroup\$ – leftaroundabout May 30 '17 at 20:38
  • 3
    \$\begingroup\$ A vector is, in fact, not a single arrow, you are right, representing Vector3 as a single arrow is exactly exactly what confused me. +1 for mentioning this critical sentence. \$\endgroup\$ – Mohammed Noureldin May 30 '17 at 23:47
  • \$\begingroup\$ @leftaroundabout There are different possible definitions for vectors (beyond being "some n-tuple..." or so). In linear algebra, imagine the set of all arrows, and the (equivalence!-) relation "Has the same length and direction". Factorizing the set of all arrows by this relation yields the equivalence classes. I didn't want to nitpick about mathematical detials (I'm also not a mathematician), but hoped to make clear that a vector is not "an arrow that starts at (0,0,0)". The point (...) is : A vector does not have a "position". \$\endgroup\$ – Marco13 May 31 '17 at 2:12
  • 2
    \$\begingroup\$ It's even further complicated by the computer science use of the term vector to mean array or multiple! In C++ you can have a std::vector<Vector3> for example. A vector of Vectors. \$\endgroup\$ – user1118321 May 31 '17 at 5:17
  • \$\begingroup\$ Ah, so what you mean is, starting from an affine space X, you define for any two points (p, q) an arrow sA (X) as the shortest path (i.e. differentiable function with minimal integrated absolute derivative) s : [0,1] → X such that s (0) = p and s (1) = q. Then the space of vectors is the set of equivalence classes A (X) / ~ where s ~ σ if ∂ s / ∂ t = ∂ σ / ∂ t for all t ∈ ]0,1[? That makes sense, though I don't think you can use this as a definition of vectors because the differentiation already depends on them. \$\endgroup\$ – leftaroundabout May 31 '17 at 9:03
36
\$\begingroup\$

Does that mean that the magnitude is simply it is distance from the origin point (0, 0, 0)?

That's exactly that.

Among other things, a vector can represent a point (a position), a direction and/or a velocity, depending on the context.

If you have this variable:

Vector3 mPosition;

It generally represents only the position, i.e. where it is located in 3d space.

If you have this variable:

Vector3 mDirection;

It generally represents the direction. Typically, these vector are unit vectors, i.e. vectors of length 1 (but it's not always needed). A unit vector and a Normalized vector are the same thing, they're both of length 1. These vectors are often used with other vectors to change their positions.

When normalizing a vector, you lose its length (its magnitude), but the direction stays the same. There are situations when you only need the direction (e.g. when you want to move an object in that direction), and having the (non-unit-length) magnitude in the vector would introduce unexpected calculation results.

If you need a normal vector for a single calculation, you can use myVec3.normalized, it will not affect myVec3, and if you intend to use that normalized vector often, you should probably create a variable:

Vector3 myVec3Normalized = myVec3.normalized;

to avoid repeated calls to the normalized method.

And if you see variables:

Vector3 mVelocity;

It generally represents a force/speed: these vectors represent a direction and their magnitude (their length) is important. They could also be represented with Vector3 mDirection; and a float mSpeed;.

All of these are used with regard to their local origin, which can be (0, 0, 0), or can be another Position.

\$\endgroup\$
  • 4
    \$\begingroup\$ It destroys a part of the information contained in the vector, and that information is the magnitude. The direction stays the same however. \$\endgroup\$ – user87553 May 30 '17 at 13:36
  • 6
    \$\begingroup\$ @Eldy It's more accurate that to note that myVec3.normalized returns a new Vector3, having the same direction but magnitude 1. myVec3 is unchanged \$\endgroup\$ – Caleth May 30 '17 at 16:27
  • 4
    \$\begingroup\$ @NPSF3000 Those would be a jerk and a jounce, there is no consensus on names beyond that. We are all glad jerks are not common. \$\endgroup\$ – Theraot May 30 '17 at 19:24
  • 1
    \$\begingroup\$ @NPSF3000 Some suggest the 4th, 5th and 6th derivatives of position should be snap, crackle and pop! :-D en.wikipedia.org/wiki/Snap,_Crackle_and_Pop#Physics \$\endgroup\$ – gbmhunter May 30 '17 at 23:53
  • 1
    \$\begingroup\$ Maybe change these vector are unit vectors to direction vectors are unit vectors or something? Because as it is now a reader may be confused thinking that these refers to the both preceding examples,mPosition and mDirection. (That's how I read it at first.) \$\endgroup\$ – Supr May 31 '17 at 8:12
8
\$\begingroup\$

Does that mean that the magnitude is simply it is distance from the origin point (0, 0, 0)?

You can see it that way, but only seeing it that way may lead to a wrong understanding.


First of all, a vector is not a point, and a point is not a vector.

The difference between a vector and a point is the same as between a duration and a time of day. The former is an interval of time, the latter is a single point in time. Its obviously that 6 hours is not the same as 6 o'clock. You wouldn't say "The race lasts 1 o'clock" and neither would you say "Lets meet at 13 hours". The race lasts one hour - an interval - and you meet at 13 o'clock - a specific point in time.

The same applies to vectors and point. A vector is an intervall - a displacement if you will. It points in a certain direction, and yes, it has a length.

Points and vectors are therefore related, just as durations and times of day. The race starts at 13 o'clock and ends at 15 o'clock. Both are points in time. But 15 o'clock - 13 o'clock = 2 hours, a duration. The race lasts two hours, not 2 o'clock.

The same applies to points. The difference between point A and B is denoted as ⃗v = B - A, where ⃗v denotes a vector and A and B denotes points.

Now, there is something that called a position vector. You can consider a vector a point to a certain degree, when you say that the vectors points from the origin to a certain other point. In other words: If all your friends know that you call times of day as durations since midnight (0 o'clock), you can say "We meet at 6 hours". They would know that 0 o'clock + 6 hours = 6 o'clock and therefore, when to meet you. This is in fact what naval times do. "We meet at o-six-hundred hours" means 6 o'clock.

So the vector <1,2,3> points to the point (1,2,3), if you consider the origin the anchor point, and yes, the length of this vector is the distance of that point from the origin.

But the vector <1,2,3> also points from (1,1,1) to (2,3,4), and in that case its length denotes the distance between those two points.


So, as you can see a vector has a length because it is not a point, but an interval -- a displacement.

\$\endgroup\$
  • \$\begingroup\$ Related reading: Torsors \$\endgroup\$ – Buster Jun 1 '17 at 15:07
5
\$\begingroup\$

A vector can represent a line between two points in 3d space (direction and distance) or a location in 3d space (length is the distance from origin).

If you have point A, and point B, then B-A = AB = the direction and distance you would have to travel to get from A to B.

\$\endgroup\$
  • \$\begingroup\$ Thank you, but then what does it mean to use Vector3.Normalized? the documentation says: Returns this vector with a magnitude of 1, so doesn't that destroy the information saved in the vector? actually that Magnitude and Normalized are what made me confused. \$\endgroup\$ – Mohammed Noureldin May 30 '17 at 13:31
  • \$\begingroup\$ Whether it's a point in space or an arrow indicating velocity is all in your head. The same data represents both. \$\endgroup\$ – Omnifarious May 31 '17 at 0:56
  • \$\begingroup\$ @MohammedNoureldin A normalised vector is one of unit length (that being 1). Yes, if you normalise a vector, you lose the length, or magnitude information. If you need both (useful in many occasions), you get the length of the vector, then normalise it. \$\endgroup\$ – Ian Young May 31 '17 at 8:17
1
\$\begingroup\$

What Unity says about points vs. vectors is meaningless in the long run, because geometry APIs just pick distinct definitions to make the tool more accessible, they don't correspond to how these things are conceptualized in geometry. Take a look at the implementations of the classes, if you can. Because it's arbitrary, to know its definition is the only way to understand what the concept is. Full disclosure, I don't have Unity experience.

A vector is a point in a vector space, in that the concept of a point in the geometry is encoded by elements of the underlying set. A vector space has a distinguished vector, called the origin or 0. Linear algebra is an attempt to encode a fragment of euclidean geometry w/ an origin algebraically.

The arrow and its length

Motions across a space of points are frequently interpreted as all the arrows from source/before points to their target/after points.

A function of two arguments can be applied to one argument to produce a function of one argument - we can speak of x+, the function which takes each vector y to the vector x+y. This is the translation associated w/ adding x. The associated arrows run from points y to points x+y. See: partial application, currying.

So why do we only use the one arrow? The arrow from the origin points to a specific vector, the x in x+ - the origin is the identity of vector addition. So, we can recover the translation x+ from just its value x+0 = x.

As a graphical representation of the space, the arrow representation has to do with our ability to visually or physically extrapolate the effect of a translation from the value determining it. When do we have that ability?

To give the vector space a norm making it a normed vector space is to provide a notion of the length of a vector that makes sense as its distance from 0. As well, this is to be a distance satisfying the triangle inequality, which is a strong constraint on how the lengths of two vectors relate to that of their sum. From length we can define distance to make this a metric space, and a geodesic is a path that's intrinsically straight in that it's as short as possible. The euclidean norm induces euclidean distance and the geodesics are the line segments of the arrows, but if you draw the arrows as geodesics using different norms, you could extrapolate the geometric effect of the translation from the geodesics to learn about the geometry.

The meaning of point and vector

In some cases in doing games geometry, your space of points is not a vector space. An affine space of dimension n can be embedded in a projective space of dimension n. Affine maps reduce to projectivities. Projectivities also let you do FOV, w/c I think is not affine. Projectivities have benefits:

The projective n-space over a field can be constructed from the linear (n+1)-space (vector space), by treating the points of the projective space as the lines through the origin of the linear space. Planes through the origin in turn give projective lines. Multiplying vectors by a fixed matrix is a linear map, this is what matrix multiplication is for. Linear maps preserve the origin and are compatible w/ incidence. In particular, if f is a linear automorphism (corresponding to an invertible (n+1)x(n+1) matrix), and two lines L, M through the origin span a plane A, then f L, f M are lines through the origin spanning f A, so f will preserve incidence on the projective space as well - an invertible matrix has an associated projectivity. Matrix multiplication encodes composition of linear maps, and hence of the projectivities.

Removing the origin from the linear space, all the points on a given line through the origin are scalar multiples of one another. Exploiting this fact, homogenization picks a linear point to stand in for each projective point and an invertible matrix to stand in for each projective transformation (as in this 2D -> 2D affine maps as 3D -> 3D linear maps video), in such a way that the representatives are closed under matrix-matrix and matrix-vector products and give and are given by unique projective things. This description of construction of the projective plane from the linear plane ties some things together.

So, in the model-view-projection matrix pipeline, we're using vectors to represent the points of our projective space, but the projective space is not a vector space, and not all vectors in the vector space we're using represent points of our geometry (see picture of affine plane at right). We use translation matrices instead of vector sum if we want translations. Sometimes, people call projective or affine points vectors, especially when using a setup in this vein.

\$\endgroup\$
  • 2
    \$\begingroup\$ +1. But my gut feeling is that most people who understand the language you're using are already aware of the answer to the original question, so I recommend to adjust the answer for casual readers. \$\endgroup\$ – Peter - Unban Robert Harvey Jun 1 '17 at 13:44
  • \$\begingroup\$ @Peter I found it difficult to address everything. I would like to make it more accessible, but don't know how to do that without elaboration. However, when I was first working with OpenGL I wondered about the meaning of homogeneous matrices, perspective matrices, and how translation matrices were discovered as an alternative to translation by summing, so it's possible this isn't too far into the deep end. Formalism is language, and giving the right phrasing, I think how to discuss the concepts will come across. However, it's very opaque to be concise, so this is more like a Wiki reading list. \$\endgroup\$ – Loki Clock Jun 1 '17 at 15:24
  • \$\begingroup\$ I added some links, in particular a video of affine maps being done in a higher dimension as linear maps. Hopefully that will help. \$\endgroup\$ – Loki Clock Jun 8 '17 at 15:07
  • \$\begingroup\$ nice. deserves more upvotes. \$\endgroup\$ – Peter - Unban Robert Harvey Jun 8 '17 at 17:31
-1
\$\begingroup\$

The length (or magnitude) of the vector is square root of (x*x+y*y+z*z). Vectors are always considered as a ray passing from the origin of <0,0,0> through the point described in the vector <x,y,z>

The unity documentation on this is found here.

\$\endgroup\$
  • \$\begingroup\$ Sorry, but this is completely wrong. If I have two points A and B, then v = B-A is the vector that goes from A to B. v does not go through the origin at all in this case. A vector is not a point. it can be used to represent a point (as position vector), but it is something different. Please get the algebraic basics straight. \$\endgroup\$ – Polygnome Jun 1 '17 at 12:35
  • \$\begingroup\$ I've updated the answer to remove the confusion, but I am providing reference to the documentation of what a Vector3 is in Unity, and my answer was in line with all of the higher ranked answers including your own. \$\endgroup\$ – Stephan Jun 5 '17 at 16:37
  • \$\begingroup\$ If you read the unity documentation carefully, you will notice that it never mentions the origin, because the origin has nothing to do with the length of the vector anyways. The vector between (1,1,1) and (2,3,4) is <1,2,3> and has a length of sqrt(1^2 + 2^2 + 3^3) =~ 3.9, which is the distance between those two points. It never even touches the origin at all. I am confused how you could possibly think my answer agrees with you, because it doesn't, at all. \$\endgroup\$ – Polygnome Jun 5 '17 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.