My 3D Vector class

This is a discussion on My 3D Vector class within the C++ Programming forums, part of the General Programming Boards category; so we're back to my point that float should be used for 3D programming, not double....

  1. #31
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    so we're back to my point that float should be used for 3D programming, not double.

  2. #32
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    and only float, not templated

  3. #33
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #34
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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
    Any questions just ask.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #35
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    OK, I'm making some changes based on all input. BTW Mr. Wizard: I already have +=, ect, just havent updated it yet .
    Do not make direct eye contact with me.

  6. #36
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #37
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    I've upgraded the class, but I'm only including the files this time: first is vector.h, next post is vector.cpp. Thanks !
    Attached Files Attached Files
    Last edited by Lurker; 11-14-2003 at 05:15 PM.
    Do not make direct eye contact with me.

  8. #38
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Vector.cpp :
    Attached Files Attached Files
    Last edited by Lurker; 11-14-2003 at 05:15 PM.
    Do not make direct eye contact with me.

  9. #39
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Looking better...

    Again, check for divide by 0 in normalize, operator / and /=.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  10. #40
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    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. #41
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    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 ?
    Do not make direct eye contact with me.

  12. #42
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    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.

  13. #43
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    OK, thanks, I'll update the class in a minute...
    Do not make direct eye contact with me.

  14. #44
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    I added divide by 0 checking and the cross product . 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 !
    Do not make direct eye contact with me.

  15. #45
    Registered User grady's Avatar
    Join Date
    Oct 2003
    Posts
    27
    == and != aren't going to work the way you think. You need to do a |x-v.x|<&sigma;,|y-v.y|<&sigma;,|z-v.z|<&sigma; for some small &sigma;. You can rotate a vector around 360 degrees and because of very small round off erros, == will say false;

    When you want to divide by a number, w. Compute temp=1/w one time and do x*=temp; y*=temp; z*= temp;. Division is much slower than multiplication.

    Why not make a function distance2 or something that does just like distance but not the sqrt at the end. You can still use this value for comparing the lengths of vectors, which is often needed.

Page 3 of 8 FirstFirst 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D Network Analysis Tool
    By durban in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 11-08-2005, 06:33 PM
  2. A 3D Program for my 3D models...?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 08-19-2004, 11:04 AM
  3. 3D starfield
    By VirtualAce in forum Game Programming
    Replies: 6
    Last Post: 06-26-2003, 01:40 PM
  4. 3D SDK for C++ programmers
    By chand in forum Game Programming
    Replies: 2
    Last Post: 05-20-2003, 08:38 AM
  5. 3d engines
    By Unregistered in forum Game Programming
    Replies: 7
    Last Post: 12-17-2001, 11:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21