# Thread: My 3D Vector class

1. 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 .

2. 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.

3. 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 .

4. 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).

5. 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| )

6. that's what he did

7. XSquared, you haven't been concentrating on my posts much, have you ?

8. 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)))

9. shouldn't you be taking the acos, and then converting it to degrees?

10. 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) / (temp * v.length())) * toRads)
}

11. 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?

12. 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

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. Duh, I was thinking that the dot product was returning degrees .

14. 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. 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()))
}