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; yes that works for returning the angle between the two vectors in radians....

  1. #76
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    yes that works for returning the angle between the two vectors in radians.

  2. #77
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Err... That still doesn't work. Your function still doesn't work. You need to divide by the magnitude of v regardless of what temp is. Also, unless the vector is one of the unit vectors (<0, 0, 1>, etc), it si unlikely that it will ever be exactly of magnitude 1, so checking for that seems to be a bit pointless.

    *edit*
    This is what I'd use:
    Code:
    float angleBetween(const Vector3D &v) const {
      float temp1 = length(), temp2 = v.length();
      if(temp1 == 0.0 || temp2 == 0) // Alternately, if(temp1 * temp2 == 0.0)
        return 0.0;
      else
        return acosf(dotProduct(&v) / (temp1 * temp2))
    }
    Last edited by Zach L.; 11-15-2003 at 07:45 PM.
    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.

  3. #78
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Are you kidding? Any normalized vector has a length of 1. They are VERY common.
    Do you know what normalizing a vector does, or how it is used?
    Do not make direct eye contact with me.

  4. #79
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by Lurker
    Are you kidding? Any normalized vector has a length of 1. They are VERY common.
    Do you know what normalizing a vector does, or how it is used?
    Why do you assume v is normalized?
    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.

  5. #80
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by Zach L.
    Why do you assume v is normalized?
    Why do you assume it never will be?
    Do not make direct eye contact with me.

  6. #81
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I don't. But you never checked for that in the code. Also, normalized vectors with magnitude 1 are rare when represented on a machine with finite memory. Normalized vectors with magnitude 1.00000000026, etc. are not rare.
    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. #82
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Normalized vectors are DEFINED as having a length of 1. They CAN'T have 1.000000000000000000000000000000000000000000000000 000000000000000000000000025501 as a length.
    Do not make direct eye contact with me.

  8. #83
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    that's a good point zach. i would just normalize them first, checking for divide by zero, then just take the dotproduct.

  9. #84
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yes. Mathematically, they are defined that way. Unfortunately, you cannot represent numbers such as the square root of any non-perfect square with infinite precision, so on a computer, they are more likely to have a magnitude of 1.0000000026 than 1.
    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.

  10. #85
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    lurker, you will rarely get a length of 1 when normalizing it's jsut really freaking close to being 1, but unless you make a new vector (0,0,1.0f) you will get what zach said.

  11. #86
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    I see what you mean, I didn't think about that. Thanks.
    Last edited by Lurker; 11-15-2003 at 08:00 PM.
    Do not make direct eye contact with me.

  12. #87
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    just normalize it, realize it's pretty close to being 1, then take the dotproduct.

  13. #88
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Yeah, unless you have an 'approximately-equal-to' operator, I don't think it is worth checking for the magnitude being equal to 1, but that might be too inefficient anyways... I'm sure Silvercord has a better idea of whether or not it is worth it.
    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.

  14. #89
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    So how's this?
    Code:
    float angleBetween(const Vector3D &v) const {
     normalize();
     float temp = length();
     if(temp == 0.0f) {
      return;
     }
     acosf(dotProduct(v))
    }
    Thanks .
    Do not make direct eye contact with me.

  15. #90
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    looks better

Page 6 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, 05:33 PM
  2. A 3D Program for my 3D models...?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 08-19-2004, 10:04 AM
  3. 3D starfield
    By VirtualAce in forum Game Programming
    Replies: 6
    Last Post: 06-26-2003, 12:40 PM
  4. 3D SDK for C++ programmers
    By chand in forum Game Programming
    Replies: 2
    Last Post: 05-20-2003, 07:38 AM
  5. 3d engines
    By Unregistered in forum Game Programming
    Replies: 7
    Last Post: 12-17-2001, 10:19 AM

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