# My 3D Vector class

Show 80 post(s) from this thread on one page
Page 3 of 8 First 12345678 Last
• 11-11-2003
glUser3f
so we're back to my point that float should be used for 3D programming, not double.
• 11-11-2003
Silvercord
and only float, not templated
• 11-11-2003
Zach L.
Quote:

So you mean something as simple as this:

Code:

```void Vector3D::normalize() {  double temp = length();  x /= temp;  y /= temp;  z /= temp; }```

Yep. You got it. I just think that three calls (especially to the sqrt function) may be a bit expensive.
• 11-11-2003
MrWizard
Not too bad for your first try but it could certainly be much faster :) First of all let's start with your normalize function. Don't call length 3 times, that's a lot of multiplications, and I see you have already corrected this. You don't check division by 0 in your normalize function. What if the length of the vector is 0? Make sure you check for these things!

Overloaded operators: Definitely implement +=, *=, /=, etc.. these will return by reference and be much faster than +, *, / etc, will be...so..

Vector3D& operator+=( const Vector3D &v );

would be the declaration. Which brings me to another point.

Whenever you are passing a Vector3D class as a parameter pass it as const reference if you can! You are going to get murdered in efficiency if you continue to pass by value. You are getting crazy overhead with that. You will get hidden stuff like copy constructors and the compiler won't be able to optimize as good when you just pass by value.

Also don't worry about writing your own assignment operator if you don't need one. You will be given a default one that will do exactly what your code is doing now. Not necessary.

On constructors, prefer to use initializer lists instead of direct assignment.

Code:

`Vector3D( float x, float y, float z ) : m_x(x), m_y(y), m_z(z) { }`
Hope this gets you going a little faster ;)
• 11-12-2003
Lurker
OK, I'm making some changes based on all input. BTW Mr. Wizard: I already have +=, ect, just havent updated it yet :D .
• 11-12-2003
Zach L.
About the initializer lists - that is an optimization to. If you use the initializer lists, then the constructor for those member variables will be called with the argument(s) you provide it. Otherwise, the default constructor is called, and then you assign to the object, which is not as efficient (especially for user defined types).
• 11-13-2003
Lurker
I've upgraded the class, but I'm only including the files this time: first is vector.h, next post is vector.cpp. Thanks :D !
• 11-13-2003
Lurker
Vector.cpp :D :
• 11-13-2003
MrWizard
Looking better...

Again, check for divide by 0 in normalize, operator / and /=.
• 11-14-2003
Silvercord
yeah I had a problem with that divide by zero crap in my code...it took me about half an hour trying to figure out why when I would hit the D and A keys at the same time (both strafe directions) i would disappear completely out of the world.
• 11-14-2003
Lurker
Quote:

Originally posted by MrWizard
Looking better...

Again, check for divide by 0 in normalize, operator / and /=.

What should I do if it is 0? Return -1 :D ?
• 11-14-2003
glUser3f
if length == 0, then do nothing, something like this:

Code:

```float len = length(); if (len == 0.0f) {     return; } x /= len; y /= len; z /= len;```
in other words, if the vector is (0, 0, 0), do nothing.
• 11-14-2003
Lurker
OK, thanks, I'll update the class in a minute...
• 11-14-2003
Lurker
I added divide by 0 checking and the cross product :D . Should I add 2 functions: void dotProduct and void crossProduct to have it automatically set the vector to it? Such as operator+= would be to operator+? Thanks for opinions, I really appreciate it :D !
• 11-14-2003