PDA

View Full Version : need help with D3DXVec3Normalize


Anddos
12-21-2008, 09:48 PM
i cant seem to grasp this idea of normalizing a vector in 3d space , i understand it sets the direction to 1 , but i dont know what that means in the 3d scene , ive read countless stuff on sites and there is no examples with diagrams of what happens when you normalize , and what are the benifits ,
thanks if you can help

Reedbeta
12-21-2008, 10:00 PM
It sets the length to 1, not the direction.

You can think of a vector more or less like an arrow. It points a certain direction independent of its length, and has a certain length independent of its direction. Normalizing keeps the direction part intact, but lengthens or shortens all vectors so they have a common length (1).

Another way to visualize it is to think of the unit sphere (sphere of radius 1). Normalized vectors have the endpoint of the arrow lie on the sphere's surface. Un-normalized vectors end at a point either inside or outside the sphere.

As for why you would want to do this, the main reason is to calculate the angle between two vectors. This is simpler when the vectors are normalized because the dot product of the vectors is then the cosine of the angle between them. For instance the standard N . L equation for lightning depends on N and L being normalized.

Another reason is if you want to construct a coordinate system (such as local space, world space, eye space etc). The basis vectors for such coordinate systems often need to be normalized.

Anddos
12-21-2008, 10:04 PM
shortens all vectors so they have a common length (1).

are you saying it sets the x,y to 1.0 float?

Reedbeta
12-21-2008, 10:08 PM
No. The x and y coordinates are not the length of the vector. It's just the length of the straight line from the start to the end of the arrow.

Just imagine an arrow in your head without thinking about coordinates at all. It can grow and shrink in length without changing its direction.

Anddos
12-21-2008, 10:14 PM
ok but how does this work with D3DXVECTOR3 , are the x,y,z values untouched then , ? where is this length value stored ?

KPBeast
12-21-2008, 10:33 PM
I can't draw any diagrams right now, but they would probably help you, so maybe someone else can draw some for you.

Anyways here is the explanation: the length of a 3d vector is

sqrt( x^2 + y^2 + z^2 )

where sqrt is the square root, and ^2 is equivalent to squaring.

Now the length of the vector (3,4,5) is not equal to 1. So how do we make the length equal to 1? We do this:

float len = sqrt( vec.x * vec.x + vec.y * vec.y + vec.z * vec.z );
vec.x /= len;
vec.y /= len;
vec.z /= len;

That is simply for demostration purposes. (I know of the minimal optimizations that could be made.)

At any rate, it does modify the X, Y, and Z values, but now when you get the length of the vector it has a length of 1. The length value is not stored, it is calculated.

Try thinking about things in 2D. Check this triangle, with the vector defined as C:


^ |\
A | \ C
V | \
----
< B >


How do you find the length of C? Well the pythagorean theorem tells use:

C^2 = A^2 + B^2

(for right triangles. There is a more complicated versions for arbitrary triangles.)
Basically his theorem tells us that the length of C squared is equal the sum of the squares of the lengths of A and B. So to find the length of C we have to isolate for it. Which means:

C = sqrt( A^2 + B^2 )


That's how you calculate length. I hope those explanations helped!

Reedbeta
12-21-2008, 11:05 PM
ok but how does this work with D3DXVECTOR3 , are the x,y,z values untouched then , ? where is this length value stored ?

No. The length and direction are not explicit, but they are "encoded" so to speak in the coordinates (that is, the x, y, z values). As KPBeast showed, you can calculate the length from the coordinates, so changing the length also involves changing the coordinates.

Coordinates are one way to look at a vector, and "length and direction" are another way to look at it. They are two different representations of the same underlying geometric object.

starstutter
12-22-2008, 07:09 AM
Hey, don't have time to read all the posts so sorry if this was mentioned.

Basicly, if you're wanting to know what the function specificly does, I'll give you it in the most basic terms I can. What it does is set the sum of all the values (x, y and z) to one. The equasion in C++ code would look like this:

total = x + y + z;
x = x / total;
y = y / total;
z = z / total;

If each value was 1.0, normalizing the vector would make each value 0.3333333 (although as stated below, in practice it would be 0.578 because of the square root step). Now why would you do this? Well, for practical reasons, you need to preform this funtion on direction vectors before doing things like lighting (which in the shader is prefomed with the "normalize()" function).

Mathematicly speaking, the reason for needing to do this is to have resulting values be in a controled range. If you did a dot() function using 2 normalized vectors, you always get a value between -1 to 1 (at least I think). With 2 unnormalized vectors, you end up with absurdley high values that would make your small, dim light look like a saturated signal flare.

.oisyn
12-22-2008, 07:24 AM
Starstutter: you're using the wrong metric (L1 norm or "manhatten distance"), which is very uncommon in computational geometry. We mostly use Euclidian metric, where the length is defined by the square root of the sum of the squared components. In which case, the components of a unit-length vector with direction (1, 1, 1) are sqrt(1/3) =~ 0.578 rather than 1/3.

Anddos
12-22-2008, 09:38 AM
can you draw it on a diagram say with a triangle , with 3 points , i dont seem to grasp this idea of a length or sqrt at all

starstutter
12-22-2008, 10:07 AM
Starstutter: you're using the wrong metric (L1 norm or "manhatten distance"), which is very uncommon in computational geometry. We mostly use Euclidian metric, where the length is defined by the square root of the sum of the squared components. In which case, the components of a unit-length vector with direction (1, 1, 1) are sqrt(1/3) =~ 0.578 rather than 1/3.
Ah, thank you, forgot about that, the values need to add back up to 1. I'll fix the post.

however though, I think for the normalization concept he should know the base logic for the function itself, rather than a post later containing:
1 / 3 = 0.578 <-- !? :(

lol, I'm not extremley great as math myself and a while ago I was very bad at it, so I can relate to the OP trying to get this stuff all at once.

Reedbeta
12-22-2008, 10:34 AM
Starstutter, the values don't need to add up to 1; their squares need to add up to 1.

Anyway, here is a diagram:

BEFORE NORMALIZING:
http://img254.imageshack.us/img254/3271/normalizebeforetf8.png

AFTER NORMALIZING
http://img254.imageshack.us/img254/879/normalizeafterna7.png

The circle in the pictures is the circle around the origin of radius 1. As you see, the vectors still point in the same direction, but their length is changed so their endpoints are on the circle.

This is the basic geometric idea. All the algebra of what you actually do to the coordinates to make this happen are secondary, so you should understand it geometrically first and algebraically second.

Anddos
12-22-2008, 01:18 PM
that just looks like its decreasing the radius , how is that going to represent a d3dxvector3? , see as its x,y,z

starstutter
12-22-2008, 01:22 PM
Starstutter, the values don't need to add up to 1; their squares need to add up to 1.

I understand that. When I talk about math, I'm normally refering to values after all operations have been preformed (as in the very end result after squaring needs to add to 1).

Anddos
12-22-2008, 04:01 PM
http://img254.imageshack.us/img254/879/normalizeafterna7.png

so this a x,y values?,
, this look like 2 vectors, because when you do lines x,y,z ,start xyz end of line , 2 vectors needed? , why does your example show 2 vectors?
i am very confused right now

.oisyn
12-22-2008, 04:25 PM
that just looks like its decreasing the radius , how is that going to represent a d3dxvector3? , see as its x,y,z

Let's go back to 2D, as Reedbeta's diagram is also 2D. Forget x and y for a moment. No matter how you store it, a vector represents a mathematical unit that has a direction and a length. You can see a vector as a long arrow, with the start point at the center of the "universe", and the head at a certain other point. All vectors can be seen like this: all of them start at the center, but they all point at different points in space. Some arrows point in the same direction, but are of different lengths. These arrows have different end-points in space, yet they overlap for the most part (as a matter of fact, they overlap as much as the shortest of the two arrows).

You can also do math with them. If you add to vectors together, simply take one arrow, and move it so that it's starting point is at the other vector's end point. Keep the directions and lengths of the vectors intact while you're doing that, otherwise you're changing the vectors. Then, if you draw a new arrow from the center of the world to the new end-point of the two other arrows combined, this is actually the result of vector addition.
http://upload.wikimedia.org/wikipedia/commons/thumb/2/28/Vector_addition.svg/250px-Vector_addition.svg.png

Negating a vector is the same as reversing the direction of the vector, but not the length. Similarly, subtracting is the same as addition, but with the second vector reversed. Or, more simply put, drawing a vector from one endpoint to the other endpoint.
http://upload.wikimedia.org/wikipedia/commons/thumb/2/24/Vector_subtraction.svg/125px-Vector_subtraction.svg.png

Multiplying a vector with a number simply changes the length of the vector, but not the direction. If you multiply a vector by 2, you get a vector that is twice as long. This makes sense if you go back to adding vectors. If you add two equal vectors, you get a result that is twice as long, but has the same direction. Same goes for division - it divides the length of a vector by a certain amount. Multiplying or dividing by a negative number also reverses the vector's direction.
http://upload.wikimedia.org/wikipedia/en/thumb/1/1b/Scalar_multiplication_of_vectors2.svg/250px-Scalar_multiplication_of_vectors2.svg.png

Now back to the coordinates. A common way to store a vector is using Euclidean coordinates, so x and y in the 2D case. You can basically see a vector as the addition of two arrows, one in horizontal direction of length x, and one in vertical direction of length y. Adding two vectors simply becomes adding the individual components, and multiplying is multiplying the components with a certain value.

Now, the length of a vector stored in euclidian coordinates can be calculated by √(x2 + y2). In fact, this is the famous theorem by Pythagoras: c2 = a2 + b2
http://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Proof-Pythagorean-Theorem.svg/180px-Proof-Pythagorean-Theorem.svg.png

Normalizing a vector simply means making the vector's length equal to 1, without changing it's direction. We know that we can change a vector's length without changing it's direction by multiplying it with a number. So if we have a vector v, we must find a value s such that

length(v * s) = 1

We also know that by multiplying by s, we basically multiply it's length by s. So therefore:

length(v * s) = length(v) * s

Using this in the above equation, we get

length(v * s) = 1 [original equation we need to solve]
length(v) * s = 1 [move s out of the length function]
s = 1 / length(v) [divide both sides by length(v)]

So, in order to normalize a vector, we need to divide it by the length of that vector, making it's length equal to 1.

All pictures courtesy of wikipedia

Reedbeta
12-22-2008, 07:33 PM
[/img]

so this a x,y values?,
, this look like 2 vectors, because when you do lines x,y,z ,start xyz end of line , 2 vectors needed? , why does your example show 2 vectors?
i am very confused right now

The x, y values are not shown in this diagram. The x, y values are irrelevant for understanding what is happening geometrically.

It shows two vectors because I felt like drawing two vectors. But each vector is being normalized individually. There is no relationship between the two.

rouncer
12-22-2008, 11:00 PM
Explaining these simple things is IMPOSSIBLE!

freezzo
12-31-2008, 08:01 AM
If you have a vector3d of 10, 0, 0 we can agree the length of 10 units. same if we have 0, 10, 0 or 0, 0 10. All length of 10.

When we normalize it, we get a vector of (1, 0, 0) or (0, 1, 0), or (0, 0, 1). All we did was take the original vector and make the length of it equal to 1 in the direction of the regular vector.

Whether is a 1d, 2d, 3d, it works the same way. Now the confusion will happen when its not a straight line on an axis.

If we have a vector of either (10, 10) or (10, 10, 0) the length will be 14.142. Now you can see we have a vector with a 45 degree angle in this case. If we are to create a unit/normalized vector, the length has to equal 1 in that 45 degree angle because that is where the original vector is heading.

To create a vector with a length of 1 in the 45 degree we will see that the x and y value will be roughly 0.707. You can also see this by using sin of 45 degree which gives you 0.707 and same for cosine of 45 degrees. (x and y values). The reason for this is because except at the 4 points the circle crosses an axis(0, 0), (0, 1), (1, 0), and (1, 1), the values of x and y can never be 0 or 1. Expand even further to take Z into account.

Of course the proper way to calculate is to take the length of the original vector and divide each peice(x, y, z) by that length and that will give you the unit/normalized vector. (As seen above)

Not sure if this makes sense, easiest way for me to explain it.