2
\$\begingroup\$

The reason I'm asking this is because I saw an implementation which did it that way but I don't quiet understand it. I mean, usually I want to get the distance between two points in 3d space and if W is something other than 0 the result is not correct. The same when calculating lighting using the dot product.

float Vector::Length() const{
    return sqrtf(X * X + Y * Y + Z * Z + W * W);    //W necessary?
}

float Vector::Dot(const Vector& other) const{
    return X * other.X + Y * other.Y + Z * other.Z + W * other.W;
}

Edit: There are suggestions that my question is a duplicate of already existing ones. But the questions referred to are about whether you need the W component at all and for what. I know what I need it for, I'm just not sure about it's role in the operations I specified.

\$\endgroup\$
  • 1
    \$\begingroup\$ Possible duplicate of Do I need the 'w' component in my Vector class? \$\endgroup\$ – Vaillancourt Apr 2 '16 at 15:30
  • \$\begingroup\$ Or even this... \$\endgroup\$ – Vaillancourt Apr 2 '16 at 15:33
  • \$\begingroup\$ @AlexandreVaillancourt I don't think so. As you can read in my edit above, these questions are not the same as mine... \$\endgroup\$ – Leon2806 Apr 2 '16 at 15:43
  • \$\begingroup\$ All right! I'll leave the link there just as a reference. \$\endgroup\$ – Vaillancourt Apr 2 '16 at 15:45
  • \$\begingroup\$ @aslg I think you have enough content there to make an answer :) \$\endgroup\$ – Vaillancourt Apr 2 '16 at 16:25
2
\$\begingroup\$

The only reason you need to extend a vector to 4 dimensions (homogeneous coordinates, not 4-Dimensional space) is so that you can apply transforms or matrices (model, etc) to it. Unless you're explicitly making a game in a 4-Dimensional space you don't need to include the w component in the calculations you mentioned.

Indeed, as you noted, using a w different than 0 will of course yield wrong results for your typical 3D calculations.

\$\endgroup\$
  • 1
    \$\begingroup\$ I'd like to add that various transformations work out nicely if you represent points as coordinates with W = 1 and vectors (more accurately, "directions") as coordinates with W = 0. For example, a point will be affected under translation, but a direction will not be. \$\endgroup\$ – jmegaffin Apr 2 '16 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.