# Thread: My 3D Vector class

1. Yeah, looks better. I still think you ought to be checking for v being 0 though, and dividing by it's magnitude... I haven't looked at the new version of the class, so I may be reading things a bit incorrectly (my apologies if I am). Are all vectors automatically normalized?

2. Thanks .

BTW, Silvercord, do you actually believe this :

void main is totally sweet

3. naw I don't

I kind of forgot I had that up there. I was hoping more people would get ........ed at me for it

4. Originally posted by Zach L.
Are all vectors automatically normalized?
No, but don't look at previous ones. I'm uploading a zip right now.
files.zip:

5. You have to normalize both vectors and also check whether v's length is 0.
Code:
```float angleBetween(const Vector3D &v) const {
normalize();
v.normalize( )
float temp = length();
if(temp == 0.0f || v.length( ) == 0.0f ) {
return;
}
acosf(dotProduct(v))
}```

6. There are a couple problems I found with that particular function. The trivial ones: a missing semi-colon, and a return statement with no value.

There are a couple of other problems, however. You call normalize. This cannot be done from the angleBetween function because that function is const, and you can't modify the class from a const function. Regardless, you really don't want to change the user's vector without their knowledge (i.e. determining the angle between two vectors should not change either).

The other thing still relates to the vector dot product. The dot product is:

u*v = |u| |v| cos a
cos a = u*v / (|u| |v|)

Therefore, you have to divide by both their magnitudes to get the angle (which requires checking to see if both are 0 magnitude vectors).

Cheers

7. Originally posted by Zach L.
There are a couple problems I found with that particular function. The trivial ones: a missing semi-colon, and a return statement with no value.

There are a couple of other problems, however. You call normalize. This cannot be done from the angleBetween function because that function is const, and you can't modify the class from a const function. Regardless, you really don't want to change the user's vector without their knowledge (i.e. determining the angle between two vectors should not change either).

The other thing still relates to the vector dot product. The dot product is:

u*v = |u| |v| cos a
cos a = u*v / (|u| |v|)

Therefore, you have to divide by both their magnitudes to get the angle (which requires checking to see if both are 0 magnitude vectors).
Already fixed all of the problems .
Try the zip again, I changed it before you posted .

8. Uhh... Seems to be the same file that was there a few minutes ago.

9. Try again - I've changed a few things. You may have to delete some temporary files .

10. Hmm... Odd thing is I did. Is this still current?
Code:
```float Vector3D::angleBetween(const Vector3D &v) const {
normalize();
float temp = length();
if(temp == 0.0f) {
return;
}
acosf(dotProduct(v))
}```

11. Nevermind, cache problem with my browser.

12. Yes, it is annoying, isn't it? I can never tell when I have actually uploaded something .

13. Indeedy... One thing I noticed when testing... Just a minor bug: dotProduct(v) needs to be vn.dotProduct(v2n).

Something else is a bit screwy with it at the moment (its not returning the right value), but I can't pinpoint why right now... It looks correct. I'll get back to you when I figure it out.

14. Umm... I have no clue what was going on, but it is working now.

15. Hey Silvercord - it seems kind of interesting your vector class is EXACTLY the same as another one on NeHe .