2

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.

lelgetrekt
  • 33
  • 4

1 Answers1

2

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.

aslg
  • 342
  • 2
  • 10
  • 1
    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. – jmegaffin Apr 02 '16 at 20:32