# Vector4 W Component

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

## Recommended Posts

I'm working on a Vector4 class, and am having a hard time figuring out what to do with the W Component.

To my understanding if W is 0, the vector is treated as a vector when being multiplied by a matrix. That is, it will not be translated, only rotated and scaled.

If the W is 1, the vector is treated as a point when being multiplied by a matrix. That is, it will be translated, rotated and scaled!

So, what vector operations does W take part in exactly?

I assume not length.... It would be odd if W took part in the Length operation as the vector (2, 2, 2, 0) and the point (2, 2, 2, 1) would have different results.

On that same note, it does not make sense (to me) to include W in the dot product calculation either.

So, should i just ignore W for these operations: Addition, Subtraction, Scalar Multiplication, Dot Product, Cross Product, Length and Projection?

But then what happens when a Vector with a W of 0 and a Vector with a W of 1 are added? Point + Vector = Point makes sense in my head. But Vector + Point = ? That doesn't really make much sense...

Does this make sense to anyone else? How do you handle the W component of a Vector4?

##### Share on other sites
If the W is 1, the vector is treated as a point when being multiplied by a matrix. That is, it will be translated, rotated and scaled!

I'm used to the terms "position vector" (if w!=0) and "direction vector" or "difference vector" (if w==0).

Notice that w can be anything, and that any vector with w!=0 denotes a position.

I assume not length.... It would be odd if W took part in the Length operation as the vector (2, 2, 2, 0) and the point (2, 2, 2, 1) would have different results.

This is because a position has no length. What you want instead is the distance of the position from a reference point, e.g. (0,0,0,1), hence computing

| (2,2,2,1) - ( 0,0,0,1 ) | = | (2,2,2,0) |

You should make a distinction between positions and directions based on axioms like these:

a) position + direction =: position

b) hence position - position =: direction (or difference)

c) position + position =: positionunnormalized

d) direction + direction = direction

So, should i just ignore W for these operations: Addition, Subtraction, Scalar Multiplication, Dot Product, Cross Product, Length and Projection?

Of course not! Certain operations like the cross-product do not make much sense in 4D. The length is something meaningful for direction vectors only (see above).

But then what happens when a Vector with a W of 0 and a Vector with a W of 1 are added? Point + Vector = Point makes sense in my head. But Vector + Point = ? That doesn't really make much sense...

Why should "Point + Vector" be different from "Vector + Point"? Vector addition is commutative.

Does this make sense to anyone else? How do you handle the W component of a Vector4?

A Vector4 has the explicit distinction of position vs. direction semantics. This absolutely makes sense.

Edited by haegarr

##### Share on other sites

I do the exact same thing with 'w' as if it was x/y/z there is no difference.

To my understanding if W is 0, the vector is treated as a vector when being multiplied by a matrix.

It's not "threated","happens to be" is the correct thing. if the zero for 0 "w" means:

The translation part of the transform matrix is going to be ignored if we multiply it by zero(or ignored), because multiplying the translation row/column by 0 will remove it's effect, and because of this we can easily rotate a direction using another matrix just by setting the xyz to be the input direction and w to 0.

And the other "happens to be" part is similar.? Scaling the translation row/column of the matrix by 1 means that we do not change it.

So if w=0 there will be no translation if w=1 the result point is going to be translated and if w=n the (x,y,z) point is going to be translated n times.

Edited by imoogiBG

##### Share on other sites

As imoogiBG said, you're overthinking it.

Personally, I only use Vector4s when it makes sense (4x4 matrices involving projection; dealing with clip space / projection space).

Using 4x4 * Vector4 involves a lot of operations, and contributes to numerical instability.

Otherwise I use Vector3. If I have a matrix with rotation, scale, skew and translation; I use a 4x3 matrix (or an affine 4x4 with an affineTransform function that asserts the matrix is affine and then ignores the last row).

If I have a matrix and only want to apply rotation scale and skew (no translation) I extract the 3x3 matrix and apply it to the Vector3.

And honestly, I try to avoid matrices and use Quaternions instead (Position / Quaternion / Scale) since I don't need skewing and is numerically the most stable method (and memory compact).

Since I only use Vector4 on special cases (ie. projection stuff) that means W almost always starts as 1 for me.

##### Share on other sites

So, what vector operations does W take part in exactly?
I assume not length.... It would be odd if W took part in the Length operation as the vector (2, 2, 2, 0) and the point (2, 2, 2, 1) would have different results.

If I'd wanted to take the length of the XYZ components, I would use a Vector3. If I use a Vector4, I expect the length to account all 4 components. Because a Vector4 represents 4 dimensions, not 3.

On that same note, it does not make sense (to me) to include W in the dot product calculation either.

Same here again. Dot including W is useful for example when dealing with plane equations and quaternions.

So, should i just ignore W for these operations: Addition, Subtraction, Scalar Multiplication, Dot Product, Cross Product, Length and Projection?

Nope, you shouldn't ignore it. Edited by Matias Goldberg

##### Share on other sites

Thanks guys! That pretty much answers all my questions!

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5

• 33
• 13
• 11
• 10
• 12
• ### Forum Statistics

• Total Topics
632572
• Total Posts
3007128

×