Jump to content
  • Advertisement
Sign in to follow this  
uglybdavis

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.

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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!