Archived

This topic is now archived and is closed to further replies.

boohillie

Vectors and such

Recommended Posts

boohillie    210
This is probably a fairly newbish question, but I have read tutorials, and articles that attempt to explain vectors and matrices and the math behind them, and I can''t seem to grasp the concept. I must have fallen asleep in geometry and trig. Anyways can anybody explain what a vector is and how it''s used? How is it applied to an object or what not in 3D? Also matrices have got me quite confused, like how are they used in transformations and things of that nature. When I picture a scene I just picture 3D objects or even 2D, where does all the vectors and matrices come into play. Thanks ahead of time for any help.

Share this post


Link to post
Share on other sites
Escherial    130
Mathematically defined, a vector is kind of like a line segment, but with a direction. It possesses three attributes: an origin (start point), a direction, and a magnitude (length). As you can probably tell, you can deduce the termination point of a vector with those three attributes. Since this is the case, in 3d graphics, we often represent a vector as simply the termination point and assume an origin of zero. If you wish to change the vector''s origin, you simply add it to a point that defines the origin you wish to set it to.

Since vectors in 3d graphics are really just three values (x, y, and z), they can be thought of as a 3x1 matrix; using matrix multiplication with a 4x4 matrix, then, you can perform operations on this 3x1 matrix such as translating, rotating, or scaling it and combinations of these thereof. Remember that matrix multiplication is not commutative: i.e., the order you multiply in will change the effect (if you translate before scaling, for instance, your origin of scaling will be different than if you scale and then translate). Since you can multiply several matrices to obtain a transformation, you can multiply your various transformation matrices together (also called "concatenating"), which results in a matrix that performs all the translations you multiplied into it in one go.

I hope that wasn''t too much...I really suggest reading a few articles on the topic, it helps a lot.

Share this post


Link to post
Share on other sites
JohnBolton    1372
quote:
Original post by Escherial
Mathematically defined, a vector is kind of like a line segment, but with a direction. It possesses three attributes: an origin (start point), a direction, and a magnitude (length)...


This is not quite correct because it is assuming a lot.

In the most basic sense, a vector is a "quantity" with one or more "dimensions". Ok, that is mumbo jumbo and maybe not even correct (strictly speaking). How about an example: a 3-dimensional vector is a "quantity" with 3 elements, commonly (but not always) designated as x, y, and z.

Vectors are used for many many many different things. In geometry and computer graphics, the most common usage is to treat each element of the vector as an offset along a dimension. For example, (1,2,3) means an offset of 1 unit in the X dimension, 2 units in the Y dimension, and 3 units in the Z dimension. This is usually visualized as an arrow whose length along each dimension is the corresponding element in the vector.

If the vector is not associated with any particular location, then it can be thought of as a direction with a length. No matter where that arrow starts, it always points is the same direction and it always has the same length.

If a vector is associated with a location, then it can be thought of as a location (that is in the specified direction and at the specified distance). The location that the vector is associated with is usually called the vector''s "origin", and is not actually part of the vector.

If the vector''s origin is the Origin of the space, then the vector specifies an "absolute" location, otherwise it specifies a "relative" location (relative to the vector''s origin). If the vector (1,2,3) is used as an absolute location (meaning that the vector''s origin is at the Origin), it specifies a location that is 1 unit from the Origin in the X dimension, 2 units from the Origin in the Y dimension, and 3 units from the Origin in the Z dimension.

So there you go. That is what a vector is. The only thing left is to remember which vectors are used as directions and which are used as locations. For example, "Normal" vectors specify direction and never a location. You might see them with their origin at a particular location but that is just for convenience and clarity.

Matrices

Matrices are (without getting too abstract and esoteric) simply tables of values. There are certain operations on matrices and/or vectors that have useful results. There is nothing magic or special about those operations or the elements of a matrix(other than their usefulness and the significance applied by the system).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Vectors aren''t really covered in high school geometry and trig, and really the only place they are covered in high school is possibly a little in pre-calc or calc.

Mathematically, vectors are things that you can linearly combine to get a vector(if u and v are vectors and a and b are scalars, you can do something like au + bv = a vector) and that their product is a vector ( u * v = a vector).

For most purposes, such as computer graphics, a vector is just a bunch of numbers, for example (2.3 7.1 -3.7 1) is a four dimensional vector.

A vector (x y z) can be viewed as representing a point in a three dimensional space. (x y) could be a point in two dimensional space. In this way, the vertices of 3d models can be thought of as vectors.

Now if you have some models specified with their vertices as vectors like this and you want to place them in a scene, you need a way to move them around, resize them, etc. Everything done with computer graphics (well, with the exception of the perspective...) is done with linear transformations. Linear transformations can rotate and scale the space the vectors occupy (but they can''t translate it, that isn''t a linear transformation). To translate the space (move objects from side to side) we change to what''s called homogeneous coordinates, where instead of (x y z) we now have (x y z 1).

All linear transformations can be represented with a matrix. There is a rule for multiplying matrices and vectors (actually just a rule for multiplying matrices, vectors are like really thin matrices).

A 4 by 4 matrix might look something like this:

2 0 0 1
0 2 0 0
0 0 2 0
0 0 0 1

This matrix can actually be used to scale a homogeneous vector to twice its size and translate it one unit along the x axis in the positive direction. Hmmm...this is a complicated subject...first, here''s how vectors are multiplied:

(a b c) * (d e f) = ad + be + cf

so the product of two vectors (called the dot product, the multiplication symbol is really a dot, not an asterisk) is a scalar (a single number).

Now think of the matrix as being 4 rows. The product of the matrix and the vector is a vector. Each element of that vector is a row of the matrix multiplied by the vector. Oh, and these vectors I''ve been talking about are really column vectors... (x y z 1) would be written as

x
y
z
1

now if we do the product of that matrix and that vector...

2 0 0 1 x 2x + 0y + 0z + 1*1
0 2 0 0 * y = 0x + 2y + 0z + 0*1
0 0 2 0 z 0x + 0y + 2z + 0*1
0 0 0 1 1 0x + 0y + 0z + 1*1

so the result is (2x+1 2y 2z 1)

Anyway this stuff is a big topic, and I probably haven''t done much of a job explaining it...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
And that matrix multiplication got mangled...I''ll try again...


2 0 0 1 x 2x + 0y + 0z + 1*1
0 2 0 0 * y = 0x + 2y + 0z + 0*1
0 0 2 0 z 0x + 0y + 2z + 0*1
0 0 0 1 1 0x + 0y + 0z + 1*1

Share this post


Link to post
Share on other sites
boohillie    210
Thanks for all the help guys. I actually do remember some of the stuff from my math classes, dot product, adding vectors etc. Also I remember covering them slightly in physics. I kind of get what they are, but I guess I'm really curious as to how they're applied to lighting and things of that nature. ie When somebody says that normalizing a vector is used in lighting, or the cross product is used in collision detection. I know how to normalize a vector, and find the cross product of two vectors. Just as an example how would I use that?

[edited by - boohillie on May 29, 2003 11:22:39 AM]

Share this post


Link to post
Share on other sites
Well, the cross-product can find a vector perpendicular to the two vectors multiplied. And since three points represents a plane, you can change two of these points to vectors relative to the third point (I use point to mean an absolute vector). Then, after you cross multiply the two vectors, you have a vector perpendicular to the plane. This vector is called a normal line and can be VERY useful for collision detection.

--------------------------------------
I am the master of stories.....
If only I could just write them down...

Share this post


Link to post
Share on other sites
S1CA    1418
Nathaniel Hammen has given the example regarding physics and normals, I''ll give you an example from lighting.

An important property of the dot product:
a dot b == |a||b|cos(angle)

i.e. the value is the same as the magnitude (length) of vector a multiplied by the magnitude of vector b, multiplied by the cosine of the shortest angle between the two vectors.

If both the vectors are normalised, their magnitudes are both 1. So plugged into the above you get 1*1*cos(angle) which is just the same as cos(angle).

Now imagine you''ve got a flat surface such as a table top, i.e. a plane, a side on view in crap ASCII would be:
  
+--------+
|| ||
|| ||


The normal vector of the table top would point outwards from the plane, combined with the any point on the surface of the table top, the normal vector defines the plane:


N
^
|
|
+--------+
|| ||
|| ||



Next, imagine you''re shining a spotlight at the table top, the vector representing the direction that the light is pointed in is marked below as L:


N L
^ /
| /
|/
+--------+
|| ||
|| ||



In terms of purely diffuse lighting (i.e. ignoring any shininess - assume the table is made of matte rubber or something rather than varnished or at all shiny), the more directly the light is pointing at the table, the more light will be reflected from the surface of the table (i.e. the brighter it''ll appear). So if the light is pointing directly at the top of the table, the brightness is maximum, and if it''s at the side, not much will be cast on the surface.

So the brightness level is dependent on the angle between the light and the surface of the table.

The surface of the table is defined by the normal vector, N. The light direction is defined by the direction of the light L.

The dot product gives you cosine of the angle between the two vectors, which leads to the classic Lambertian diffuse calculation of:

I=L.N

i.e. the cosine of the angle between the light and the surface of the table.

The cosine is actually better than just the angle. If you instead got the angle with more traditional trigonometry you''d have a value between 0 and 90 degrees (or the same in radians) ranging between the side and top of the table - to convert this to a light intensity would require some extra work to put the numbers into a nice range (e.g. 0 to 1) and flip them so that 0 means maximum intensity and 90 means minimum.

A cosine is just a ratio, and it gives us a nice value between -1 and 1 with 0 degrees (i.e. the light pointing straight down) being 1 and 0 being side on (i.e. 90 degrees) which is perfect for lighting.

Something I''ve deliberately glossed over is that what you''re really trying to do with I=L.N is see how much of the light can be seen FROM the top of the table rather than how much of the table can be seen from the light. So the L (light) vector needs to point out from the table so that both vectors (conceptually) start from the same point on the table and point outwards. So the L vector for a spotlight should be negated (e.g. L=-L) which will simply change the direction of the vector (swap the end that you''d draw the arrowhead on).

The reason that normals and light vectors need to be normalised should already have become clear. We want the lengths of the vectors to be 1 so that we only get the cosine of the angle between the vectors without their lengths to be taken into account.

IMO with this kind of stuff there really isn''t any substitute for:

a)drawing this stuff on paper and working through the maths by hand with some sample values. See what happens when you pass in the same vector for both, see what happens when one vector is 0,0,0, see what happens when one value or one element is negative etc. Work it through with a calculator, draw the resulting vectors.

b)find a good textbook on this stuff targeted at an appropriate level, preferably one with excercises and answers. Work through as much of it as possible.

c)implement them on a computer and start using them. Make a simple testbed that lets you draw simple things like lines, points, boxes etc and do things like show the columns of a matrix as vectors, try adding scales to the matrix, try adding two vectors together and see what the resultant vector does (try drawing the vectors end to end). A lot of things that you''ve previously thought of as just arbitrary numbers and arbitrary operations that don''t make much sense DO tend to fit into place when you can visualise them in realtime.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites