Thread: My 3D Vector class

  1. #61
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by Silvercord
    Do you plan on making a game? I assume you are, but you could also be doing something like a CAD program.
    I've Private Messaged you.
    BTW, this is a general class, no specific idea in mind as of yet .
    Do not make direct eye contact with me.

  2. #62
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Well, the project I'm working on is extremely complex, and I'm still at the "get stuff to work" phase. I have a lot of organization in the code, but 90% of the effort is going into making sure the algorithms and the incredibly complex mathematics work correctly and are aesthetically pleasing. I'm trying to rely on stl containers as much as possible to avoid bugs (i.e memory management with std::auto_ptrs), and I'm also looking into the boost library. Optimizations and what I consider little things (easy things) will come later, but I actually do fundamentally agree with you cat, peace d00d.

    EDIT: lurker, an email with my Vector class and my Quaternion class is on my way. Both of these could be optimized, i.e I know that in both i pass by value a lot which is especially dumb when using quaterinions. Some of the things I just made up, i.e the 'fast homo morphism' function in the quaternion code is something you'll never find on a tutorial. It just cancels calculations when the w component of the quaternion is zero.
    Last edited by Silvercord; 11-15-2003 at 07:51 PM.

  3. #63
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    OK, thanks. BTW, does this look like a good (err....okay ) function to return the angle between 2 vectors?

    Code:
    float angleBetween(const Vector3D &v) const {
    	float temp = length();
    	if(temp == 1) {
    		return acos((dotProduct(&v) * (3.1415926 / 180)));
    	}
    	return acos((dotProduct(&v) / temp * v.length()) * (3.1415926 / 180))
    }
    It will be optimized later, but I mean the basic structure .
    Do not make direct eye contact with me.

  4. #64
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    Shouldn't you get the angle using acosf, and *then* convert it to degrees?
    i.e

    return (acos((dotProduct(&v)) * 180) / PI

    I normalize all of my vectors, and actually, I keep everything in radians all the time (even in my other code).
    Last edited by Silvercord; 11-15-2003 at 08:09 PM.

  5. #65
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    If you're not going to normalize the vectors first, you have to divide the dot product by the product of their magnitudes before you can take its arccosine.

    Code:
    u dot v = |u||v|cos(x)
    x = arccos( u dot v / |u||v| )
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #66
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    that's what he did

  7. #67
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    XSquared, you haven't been concentrating on my posts much, have you ?
    Do not make direct eye contact with me.

  8. #68
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Didn't notice that code at the end there. Heh.

    Also, you're missing a set of parentheses, and why are you converting to radians before you pass to acos? Acos returns an angle in radians, its parameter is just a ratio, so you'd actually want to convert to degrees after the call.

    Code:
    return acos((dotProduct(&v) / ( temp * v.length()))) * (180/(4 * atan(1)))
    Last edited by XSquared; 11-15-2003 at 08:24 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #69
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    shouldn't you be taking the acos, and then converting it to degrees?

  10. #70
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Right, thanks. How this:
    Code:
    float angleBetween(const Vector3D &v) const {
     float temp = length();
     float toRads = ((1 / 3.1415926) * 180);
     if(temp == 1) {
      return acos((dotProduct(&v) * toRads));
     }
     return acos((dotProduct(&v) / (temp * v.length())) * toRads)
    }
    Do not make direct eye contact with me.

  11. #71
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Originally posted by Silvercord
    shouldn't you be taking the acos, and then converting it to degrees?
    I'm converting it to radians, and then leaving them that way.

    EDIT: The dot product returns an angle in degrees, right? Or should I not have to even convert them to radians in the firstplace?
    Do not make direct eye contact with me.

  12. #72
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    the dotproduct gives you the cosine in radians. acosf accepts its parameter in radians. No conversion is necessary. If you want to convert to degrees, you have to get an answer from acosf, then convert that to degrees

    //if it's normalized already
    float radian = acosf(dotproduct(a,b));
    float degree = (radian * 180) / PI

    EDIT: it wouldn't make sense for it to be in degrees by the way, because degrees doesn't measure any real values, but radians are a real ratio of the distance travelled divided by the radius of a the circle, so just always assume radians are being used. degrees are only for readability, and im such a dork i keep everything in radians anyway.

  13. #73
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Duh, I was thinking that the dot product was returning degrees .
    Do not make direct eye contact with me.

  14. #74
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    don't worry about it, mistakes are fun

    I suggest you stop implementing things until you are ready to use them, that way you find bugs immediately and know what in the holy hell caused them. If you hadn't posted that code you would have waited a week to use it, had a bug, and had no clue where it was and you would've eaten your neighbor's family.

  15. #75
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Lets try this:
    Code:
    float angleBetween(const Vector3D &v) const {
     float temp = length();
     if(temp == 1) {
      return acosf(dotProduct(&v));
     }
     return acosf(dotProduct(&v) / (temp * v.length()))
    }
    Do not make direct eye contact with me.

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, 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, 11:19 AM