## 3D math library

Member
Posts: 749
Joined: 2003.01
Post: #1
You guys know any good free C++ library for 3D math?

I need stuff like "find plane orthogonal to given line pasing through given point", "find plane passing through 3 given points", "find line lying on given plane orthogonal to given line" and such.

I made my own functions up somehow, but they're very inefficient.

For instance, if you had to find the angle formed by 3 points (in 3D), how would you do that?

Â©hâ‚¬ck Ã¸ut Âµy stuÆ’Æ’ Ã¥t ragdollsoft.com
Moderator
Posts: 1,140
Joined: 2005.07
Post: #2
If you were trying to find the angle between 2 lines, A and B, AdotB = cos(theta), which would then give you the angle. Also, you can just take the x, y, and z components and find angles based on that if that's what you're looking for.

If you have equations for all the different things, but there's just inefficient, why not post them and have some of us optimize them for you?
Member
Posts: 749
Joined: 2003.01
Post: #3
Damn, I always forget the dot product stuff.

I always end up doing some "too creative" stuff.

for instance to find the angle between the (x1,y1,z1), (x2,y2,z2) and (x3,y3,z3) I did like this:

-first you shift everything so that point 2 is in the origin
-then I normalize the length of vectors 1 and 3 to have length 1
-Then I find the distance between the new 1 and 3 points
-then I consider the the triangle formed by 1,2,3 and I know that it's isosceles so I split segment 1-3 in 2 parts and I know the two resulting smaller triangles are right triangles, I know their sides so I can calculate the angle with acos, which is half of the original angle.

When I guess it would have been enough to

-first you shift everything so that point 2 is in the origin
-then I normalize the length of vectors 1 and 3 to have length 1
-calculate the dot product
-find angle with acos

oh well.

The fist method works, but damn, I guess I took it too long.

The other functions use even more twisted ways, I'll post them for your amusement when I get back to the code.

But thanks for your interest akb825, it's hard to get people to look at your code, but it helps a lot if someone can tell you more direct techniques to get the results.

Â©hâ‚¬ck Ã¸ut Âµy stuÆ’Æ’ Ã¥t ragdollsoft.com
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
It's no problem. I had to figure a lot of 3D math out myself with the game I'm working on now, since I did all my collisions etc. myself with polygons, rays, and spheres.

For your plane line problem, just take the dot product of the line equation and the point, and that's the d part of ax + by + cz = d (where a, b, and c are the vector components of the line). For a plane passing through 3 points, pick a center point, then take the vectors from that point to the other 2 points, take the cross product, and that gives you the normal. Take the dot product like the line and a point problem, and there's your plane. For a line laying on a plane orthogonal to a line, just take the cross product of the plane normal and the line. Just feel free to ask any questions you have on specific problems. That's why we're here!
Moderator
Posts: 365
Joined: 2002.04
Post: #5
You could try Geometric Tools (previously known as WildMagic). It has every feature imaginable and you can be pretty sure that it's mathematically correct. It can be a bit intimidating to use because it contains a huge amount of very complex code, but you don't have to use all of it at once. For example, you could just pick out the vector, matrix and quaternion classes and use them with the various intersection operations, but discard the rest.

I used it when it was called WildMagic in my game Rescue. You could check out the Rescue source if you want to see this library in action.

Neil Carter
Nether - Mac games and comic art
Member
Posts: 749
Joined: 2003.01
Post: #6
Thanks akb825, though I guess I lost you. Looks like I need to learn the basics of 3d math instead of looking for a 3d library

Â©hâ‚¬ck Ã¸ut Âµy stuÆ’Æ’ Ã¥t ragdollsoft.com
Moderator
Posts: 1,140
Joined: 2005.07
Post: #7
http://www.geocities.com/SiliconValley/2151/math3d.html

Also, you if you're still in school, you might want to take a math class that pertains to the subject. (I know most of what I do with 3D math based on one of my classes in the calculus series I had to take, and I'd probably be pretty lost without the knowledge I gained from it)
Member
Posts: 89
Joined: 2005.06
Post: #8
Aw man! Neil that library had a quaternion class!? Ah well, I enjoyed making my own with your help, so I don't feel that bad. In fact that's the beauty of making your own classes, you get to not only learn the stuff as you go along, and I don't know about you guys, but I love optimizing my code
Moderator
Posts: 1,140
Joined: 2005.07
Post: #9
I agree. I know I could have found libraries for 3D collisions etc., but I still wanted to create it myself for the fun of it. It's definitely nice when you know every nook and cranny of what you're making.
Member
Posts: 89
Joined: 2005.06
Post: #10
Yea I was thinking about this the other day. I said to my self "self, my OBJ loader is coming along really well." and then I thought to my self "self... I wonder if I should post it for people to use..." and then I pondered further. "no... if most programmers are like me they will simply take whatever they can, rip it apart, and make their OWN obj loader"

So yea, pretty much I love making all my stuff my self. It takes a lot of learning, but its fun to finally have something I WANT to learn about. Not some stuck up teacher trying to cram a book down my throat that is only going to pass through my brain digestive system.

I totally agree about knowing every crook and nanny of your code.