14 tayangan

Diunggah oleh Ion Mincu

Vectors explained by Wildbunny on his blog

- TEST # 2.4
- chaptersoln1
- ch1ex
- George Lungu - Calculating the Moment of Force
- Force Vector (3)
- CIV100H1 Syllabus Fall 2015
- Vectors II Quiz.pdf
- Dot Product
- Java - Help Converting Math Function Into Code _ Linear Kalman Particle Swarm Optimization - Stack Overflow
- Lec 1 Dynamic
- geom 7 4
- wuamcqmp09
- Concepts of Calc 3
- Force Vectors
- Lecture Notes Ch 1
- Lab1-F13
- Check Points 1-4 Chapters
- Forces
- NbodyOne
- Lab Report 1 Physics

Anda di halaman 1dari 9

Paul's blog@Wildbunny

Proudly powered by WordPress.

Vector

Tw eet

StumbleUpon

subm it

A Vector is a quantity which has both magnitude and direction, but no position.

Figure 1

Figure 1 shows an example. You should be able to see our vector V, along with its magnitude (which is just the length of the vector, i.e. a single number, a scalar, like the height of a person). Our vector also has a direction, which in this case is positive in the x and y axis. The direction of our vector is two dimensional, because its flat on screen and as such is described by two numbers which indicate how much it points in each of the two axis directions. A vector can have any number of dimensions in games, they are commonly two or three dimensional depending on the game.

The demo above shows the world axis x and y and the vector v which follows the mouse position. Note the x and y values of V change sign as the vector is changed and how x and y shrink and grow depending on the mouse.

Magnitude

We can find the magnitude of any vector quite easily using Pythagoras.

Figure 2

He said that the area of the green square is equal to the sum of the areas of the red and blue squares. Since the edges a, b and c are exactly the lengths of the edges of the squares they reside within, the area of each square is:

g r e e n=c * c r e d=b * b b l u e=a * a

c * c=a * a+b * b

Figure 3

So, handily Pythagoras a and b edges look remarkably like the x and y axis of the space our vector resides within. We can now write:

m a g n i t u d e * m a g n i t u d e=V . x * V . x+V . y * V . y

Of course, we dont want magnitude squared, we just want regular magnitude, so we take the square root of both sides:

m a g n i t u d e=s q r t ( V . x * V . x+V . y * V . y )

And there we have it. Vectors with more than two dimensions are handled with an extra term, for example a three dimensional vector:

m a g n i t u d e=s q r t ( V . x * V . x+V . y * V . y+V . z * V . z )

Unit Vector

A unit vector is simply a vector whose length (or magnitude) is 1. These are extemely important in geometry because they allow transformations which do not alter the scale of the things being transformed. Such a vector is said to be normalised i.e. divided by its length:

m a g n i t u d e=s q r t ( V . x * V . x+V . y * V . y ) V . x/ =m a g n i t u d e V . y/ =m a g n i t u d e

Notice how the length of V is always the same in the above demo.

This is one of, if not the most important operator(s) in geometry. Its so important because it gets used for many tasks, such as projecting a vector along another, or to help in finding the magnitude of a vector. It works consistently in any number of dimensions (unlike the cross product, which is only defined in 3d). The text book definition is, for two vectors U and V:

U . V=| | U | | * | | V | | * c o s

Which says that the dot product of two vectors is equal to the magnitude of each multiplied together all multiplied by the cosine of the angle between them. Of course, if you start out with unit vectors then the magnitude disappears from the equation (it is simply 1) and the answer is directly equal to the cosine of the angle between them. This in itself is useful because we can then calculate the angle between the two vectors in radians by inverting the equation:

1 =c o s U V

or in code:

=a c o s ( U . x * V . x+U . y * V . y )

d o t=U . x * V . x+U . y * V . y

This is a whole lot more convenient than the text-book definition and a hell of a lot faster to compute. It should look familiar as weve already used this to calculate the magnitude of a vector above. The result of the dot product of two vectors can be visualised as being either on the positive or negative side of

o

a tennis court with an infinite net down the middle at 0. There are two poles at +1 and -1, with 0 at 90o on each side.

In the demo, you control unit vector V as it is dotted against unit vector U. Notice how the dot product is 0 when the vectors are at 90o to each other? Also, note that when they are totally opposite, the dot is -1. So when two unit vectors point exactly the same way, the result of the dot product is 1 and when they are exactly opposite -1. The dot product returns 0 for any two vectors (unit or otherwise) at 90o to each other in some plane. Its very useful in games when we want to tell if an enemy is facing the player, for instance (the dot product of their forward vectors will be negative although we must also check the dot of their right vectors to make sure theyre not back to back (the dot should be negative as well), since the dot product will only tell us if two vectors are opposed, which is true of facing *and* opposing). The really useful part comes when we realise that this geometric description also allows us to tell how much of a vector is in the direction of another, because when we know that we can calculate this new projected vector which is useful in all sorts of geometric problems, rotation being one of the most fundamental.

Projection

What is projection? It is the procedure of taking something in n dimensions and collapsing it down to the dimension below (n-1). So in 3d rendering we often want to project the 3d world onto the near plane of the camera. When we do a dot product we are projecting one vector onto another and the by product is a scalar. We start out with two vectors, one being unit (A) and the other not (V). A is the projection axis and the V the vector we want to project onto this axis, resulting in a scalar d, which tells us how far along A V is.

Figure 4

d=V A

or

d=V . x * A . x+V . y * A . y

We can even get the full projected vector (the vector between the black and green dots in Figure 4):

V * d a x i s =A

or

V . x=A . x * d a x i s V . y=A . y * d a x i s

Because A was a unit vector, the projection of V onto it resulted in a quantity with the same length units as V. I.e. If they both pointed in exactly the same direction the dot product tells us the length of V. If both vectors were unit length we get the same result except that the lengths drop out of the equation and we are left with the cosine of the angle between the two vectors.

Optimisation

If neither vector is unit length we can still find d, but it will be in parametric units along A (i.e. from 0 to 1):

d=VA/AA

You can see why this works if you consider what happens when V = A. The first quantity is the squared length of both vectors and so is the second quantity; thereyby giving a value of 1, which corresponds to the end time along the axis. Similarly total squared length divided by half the squared length = 0.5, which is exactly what we expect. Often this is more useful than having d in axis length units, which is nice.

In many ways the wedge-product (^) is the opposite of the dot product as it does the reverse of projection: extension. In 2d the wedge product of two vectors gives the signed area of the parallelogram formed by the extension of one vector by the other.

In Figure 5, the area A^B is formed from the wedge product of vectors A and B. This is useful in backface culling and tells us when our triangles face the wrong way because the area will be negative: Two vectors A and B

a r e a=A . x * B . y-A . y * B . x

Notice that when the vectors in this demo are coplanar the area goes to 0? This fact is useful when inverting a matrix because it tells you whether the matrix is degenerate or not (i.e. cannot be inverted). This signed area is called the determinant of a 2d matrix (formed by axis vectors A and B). In 3d the determinant is a volume, but the principle is the same and can again be computed using the wedge product of three vectors. You can also use the wedge-product to compute the area of a triangle; its simply:

a r e a=A ^ B/2 .

Indeed, you can compute the area of a convex polygon using this as well; here is a code snippet from this site which does exactly that:

In 3d, the wedge product is equivalent to what we know as the cross product. The cross product produces a vector perpendicular to the input vectors:

xa r e a=A . y * B . z-A . z * B . y ya r e a=A . z * B . x-A . x * B . z za r e a=A . x * B . y-A . y * B . x

You will notice that the z area in 3d is the same equation which gave us the wedge product in 2d. The textbook definition of the cross-product is:

| | AxB | |=| | A | | * | | B | | * s i n

Which says that the magnitude of resultant vector is equal to twice the area of the triangle formed by A B with an implicit missing edge. The cross product results in a vector perpendicular to the other two vectors which is useful in all sorts of applications (generating face normals for example).

P e r p e n d i c u l a rP=AxB

The cross product does not preserve magnitude generally, so that two unit vectors A and B crossed will only result in a unit vector C if A and B are at 90o to each other in some plane. Most of the time the resulting vector will be smaller than the two input vectors and indeed, tends toward 0 as the two inputs move towards being parallel. Interestingly you can also use the cross product operator for projection of a vector onto a plane which is useful in lots of circumstances: (e.g. in physics when you want to calculate the tangential velocity given a plane and the velocity):

P l a n ep a r t i a l l yd e f i n e db yn o r m a lN V e c t o rf o rp r o j e c t i o nV

N e wv e c t o rQ=( N x V ) x N

This is a handle little operator which only works in 2d. It simply gives a vector which is perpendicular to the original. Its especially useful in 2d physics engines for finding the tangential velocity given a contact normal.

Figure 6

Figure 6 shows A and the perpendicular vector. This is a length preserving operator and is computed quite simply:

A p e r p . x=A . y A p e r p . y=A . x

There is of course another perpendicular vector, opposite this one, but convention seems to be to chose this one, presumably because if you take the unit right vector {1,0} and apply this operator you get the unit up vector {0,1}.

10 Responses to Vector

Dunge says:

February 14, 2012 at 6:04 pm

Its very useful in games when we want to tell if an enemy is facing the player, for instance (the dot product of their forward vectors will be negative). This will tell if the enemy and player are facing each other. The facing vector of the player have nothing to do with whether the enemy is facing him or not.

Reply

Dunge says:

February 14, 2012 at 6:32 pm

Reply

February 14, 2012 at 8:51 pm

Yes, this is true I will update the post to say you need to check the dot of their right vectors as well

Reply

jc_lounge says:

March 31, 2012 at 1:50 am

Love the article. But Im not sure how dotting the right vectors helps here either. Maybe dotting the offset vector between the entities with the facing vector of one of them is more useful?

Reply

February 14, 2012 at 8:50 pm

Reply

April 3, 2012 at 1:57 am

I think what you meant is the dot product between the enemys forward and (enemy_position player_position), not between the respective forward vectors.

Reply

ghafoor says:

April 3, 2012 at 12:52 pm

Product of two Vectors Product of vectors, cross product, scalar product, dot product, vector product, properties of vector product. http://www.infoaw.com/article.php?articleId=865

Reply

Robert says:

May 15, 2012 at 7:25 pm

The last part of the last sentence in the first paragraph shouldnt be there. The part about a single value d, which I see is actually part of the Projection section.

Reply

May 16, 2012 at 10:34 am

Hi Robert, Yes, youre right! I wonder how that got there Oh well, its removed now! Cheers, Paul.

Reply

- TEST # 2.4Diunggah olehKamran Ali
- chaptersoln1Diunggah olehJuan F Lopez
- ch1exDiunggah olehIsrael Lives
- George Lungu - Calculating the Moment of ForceDiunggah olehZachary Lee
- Force Vector (3)Diunggah olehMirrovshan Sagdullaev
- CIV100H1 Syllabus Fall 2015Diunggah olehDarsh Jain
- Vectors II Quiz.pdfDiunggah olehPng Poh Sheng
- Dot ProductDiunggah olehLim Jing Zhan
- Java - Help Converting Math Function Into Code _ Linear Kalman Particle Swarm Optimization - Stack OverflowDiunggah olehSilvister Stalon
- Lec 1 DynamicDiunggah olehShahzaib Bukhari
- geom 7 4Diunggah olehapi-232613595
- wuamcqmp09Diunggah olehDavid Chang
- Concepts of Calc 3Diunggah olehJohn Schrage
- Force VectorsDiunggah olehparkl
- Lecture Notes Ch 1Diunggah olehMahadevan
- Lab1-F13Diunggah olehVivek Patel
- Check Points 1-4 ChaptersDiunggah olehSohaib Hassan Khan
- ForcesDiunggah olehChella Ganesan
- NbodyOneDiunggah olehNono4138
- Lab Report 1 PhysicsDiunggah olehraphael
- Measurement IDiunggah olehTilak K C
- Chapter 2_Concepts of VectorsDiunggah olehSyahidah Shikamaru
- cm_part_1Diunggah olehnawidrasooly
- 1. Componentes Rectangulares de FuerzasDiunggah olehAndrés R. Duarte G
- VectorsDiunggah olehCarly Jena
- Linear AlgebraDiunggah olehDavid Lichaa
- PhysicsDiunggah olehJohn Aldren Molina
- ph101Diunggah olehRobo Form
- 201-02.3 Force Vectors (3D) [Compatibility Mode]Diunggah olehnapirin123
- Degree Regular 1Diunggah olehAjibade Taofik

- ClausesDiunggah olehAvelino Bato
- The Nursing ProcessDiunggah olehAhmadHazrulW
- Baydoun OpinionDiunggah olehFrederick Bartlett
- Tax 2 cases Hizon - ReyesDiunggah olehToni woni
- sales chapter 2-3.docxDiunggah olehRock Stone
- (2)+Rabbit_diseases_JPG.pdfDiunggah olehrajeevunnao
- Week of February 10, 2019Diunggah olehSt. Augustine Church
- 5th International Probabilistic WorkshopDiunggah olehbino
- Full TextDiunggah olehvacances2003
- annotatedbibliography eng2100 chadiamond moodyDiunggah olehapi-314120239
- pre-production booklet 3Diunggah olehapi-237720244
- Brown v. Department of Corrections - Document No. 5Diunggah olehJustia.com
- monografía bipolarDiunggah olehMarcela Valencia
- 1 the Language of MusicDiunggah olehFredy López
- ouiyDiunggah olehneozen
- narrativeDiunggah olehapi-288745662
- BanyuwangiDiunggah olehaghits7629
- IHRM Indian CultureDiunggah olehredkarravina
- Challenges in Protecting Tor Hidden Services From Botnet Abuse_fc14_submission_152Diunggah olehfunny04
- Organizational DevelopmentDiunggah olehInam Ul Haq Minhas
- Management severe dengue.pptDiunggah olehMuhammad Fhandeka Israr
- Game Programing (C++) unknown aoutherDiunggah olehThePhreak
- MamDiunggah olehHawayana Gonzalez
- 20180201_VoiceDiunggah olehcn_cadillacmi
- 5 Dewi Genital HerpesDiunggah olehSa'adah Munawaroh HD
- Glossary - GISDiunggah oleh101dalmatians
- UT Dallas Syllabus for hcs6399.05a.11u taught by Emily Tobey (etobey)Diunggah olehUT Dallas Provost's Technology Group
- aristophanes_birds_notes.pdfDiunggah olehFrum Femme
- Break Even Analysis. presentation by MOhammad NawazDiunggah olehnwzashraf
- There is no Word for Grammar in SetswanaDiunggah olehAtang-Wabone Malik