Jump to content
• Advertisement

# Vectors, cosines, trig

This topic is 1515 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hi all,

I'm just working through the 'introduction to game programming with directX' book, and I'm getting stuck frequently on a lot of the maths areas. The first area, dealing with vectors, I can't get my head around how to input these equations into my calculator.

At the moment I'm trying to wrap my head around how I'd calculate k = ||v|| cos(theta). I tried setting v to 3 or something, multiplying that by cos to the -1 power, then by n (1), and it just gave me a syntax error.

Any help would be greatly appreciated, thank you!

#### Share this post

##### Share on other sites
Advertisement

What exactly are you trying to do? That piece of text seems to be related to projecting a vector onto the direction of another vector. If you give me a value for v and a value for n, I can try to product k, p, w and theta for you, step by step. Perhaps that would help. You can give me 2D or 3D vector, whichever you think will be more helpful.

#### Share this post

##### Share on other sites

Well it's really just to understand, I'm just working through the vectors chapter to try and get the formulas down in my head.

Ok let's say:

v = (1, 2, 3)

I'm not too sure what the value of n would be, would it be (1,1,1) or (1,0,0) for example? Or is it just 1? I know ||n|| = 1, as it's a unit vector. But you can give it any value if you like.

Thanks for the reply by the way! :)

#### Share this post

##### Share on other sites

First things first...

v is a vector and is most likely not a vector in R1, but probably in R3 (i.e. a 3-tuple), n is a normalized vector (i.e. unit length) in the same coordinate system, theta is the angle between v and n. Not arbitrary values you just plug in at random.

You're dealing with projecting one vector onto another, the picture is pretty straight forward, and you don't need to use trigonometric functions to perform this operation, as you can clearly see by the equation they derive in the end. If n is normalized then it is simply p = dot(v, n) * n which simply scales n by the distance v expresses its self onto the line represented by the vector n (i.e. the shadow of v on the line represented by n). Resulting in a vector pointing at a point p such that the angle between p and the vector from p to v is a right angle. (i.e. the vectors v, p, and p - v form a right triangle). If n is not unit length then you need to divide by the square of the magnitude of n, i.e. p = dot(v, n) / dot(n, n) * n, note that the dot product of a vector against its self is equivalent to the square of the magnitude of the vector.

In dealing with these equations on your calculator you need to construct the appropriate vectors, draw them on some graph paper, and things become much clearer. Then you can calculate things like the angles between your vectors and understanding projection.

Edited by Washu

#### Share this post

##### Share on other sites

I know ||n|| = 1, as it's a unit vector.

IF n is a unit vector, that's correct.

In the lower part of the page, ||n|| may not be 1, but is a scalar (a number) which is the absolute (unsigned) magnitude of vector n (the length of the vector) - the text mentions that, "if n is not a unit vector", then the unit vector would be n / ||n||. That representation results in a vector in the same direction as n, but is of unit length, whether the original n is a unit vector or not.

For the purpose of the page you posted, the actual value of n doesn't matter. The formula merely demonstrates how a vector dot product, and the projection of v on n, is calculated.

For the purposes of illustration, assume v = (1,2,3), and n = (2, 0, 0). First, determine the unit vector in the direction of n:

||n|| = sqrt( nx2 + ny2 + nz2), where nx, ny, and nz are the x, y and z components of n. Because (in this case) ny and nz are 0, ||n|| = sqrt( 22 ) = 2.

So the unit vector along the x-axis is n / ||n|| = ( 2, 0, 0 ) / 2 = (2/2, 0/2, 0/2) = (1,0,0). The division of a vector by a scalar is defined as a vector formed by the each component of the vector divided by the scalar.

The projection p of v on n = ( v dot n/||n|| ) n/||n||

The dot product of two vectors v and n is (vx*nx + vy*ny + vz*nz). Since it was determined that n/||n|| = (1,0,0), the dot product v dot n/||n|| = 1*1 + 2*0 +3*0 = 1.

The projection p = 1 * n/||n|| = (1,0,0).

Does that help the understanding?

#### Share this post

##### Share on other sites

I am unable to read the paper. I havo no clue what the question/issue to evaluate is.

Anyway, dot product of two vectors (marked in the paper as the floating dot? pehaps not , the dot was used between scalars too) is an operation that if runs in 3d results in:

dot(A,B)=|A|*|B|*cos(alpha) where |A| is length of A vector, |B| is length of B vector, and alpha is angle between them.

the dot operation of two vectors to produce the scalar that can be interpreted like that in 3d (multiple of the two lengths and angle cosine) is like this:

[x,y,z].[a,b,c]=x*a+y*b+z*c=|A|*|B|*cos(alpha)

Knowing this relation, you should be able to for example answer question like:

vectors A and B hold the same angle as vector A and unit vector N. The dot product of A and B is given and the N vector is given and the A vector is given. Evaluate vector B upon those given information .

If that is what the paper asks to evaluate?

#### Share this post

##### Share on other sites

At the moment I'm trying to wrap my head around how I'd calculate k = ||v|| cos(theta). I tried setting v to 3 or something, multiplying that by cos to the -1 power, then by n (1), and it just gave me a syntax error.

The notation ||v|| means "the length of the vector v" which you can find using pythagoras as sqrt(v.x2 + v.y2 + v.z2). Theta is an angle which you may need to convert into radians (or make sure your calculator is on the right mode -- either "deg" or "rad"). Once you've got that sorted out it should be a simple matter of plugging the numbers into your calculator, or writing a simple program.

I'm not sure why you're trying to compute cos to the power -1 as I didn't see it in any of the formulas you mentioned. Note that cos-1(x) is *not* "cos to the power -1" but actually the "inverse cosine" function. The cosine function takes an angle and returns a number between -1 and 1. The inverse cosine function takes a number between -1 and 1 and returns one of the many the angles that has that cosine. For example, cos(pi / 3) is 0.5, so cos-1(0.5) is pi / 3.

#### Share this post

##### Share on other sites

Hey guys sorry I haven't replied in a few days! Thank you all for your replies, I've been working hard at this, I now understand how to find the angle between two vectors, which is the dot product of two vectors over the magnitude of both vectors multiplied by each other to the cosine inverse (not sure of correct name, sorry!)

I haven't read through these yet but I certainly will do so now, so thank you all for your replies! :)

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5
• Advertisement

• 9
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013665
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!