3 ms·
That one seems to be in there now? Or at least in the recent additions section. It was the first one I clicked on :) I'm not very familiar with C++ (it's been
by tripzilch 10y ago
That one seems to be in there now? Or at least in the recent additions section. It was the first one I clicked on :)
I'm not very familiar with C++ (it's been a long time ago), but a friend recently came to me with some questions about vector/matrix algebra (though at first he didn't know his questions went by those names), wanted to write a toy physics engine in C++. I kind of assumed that C++ would have at least a basic selection of your typical vec2/3/4 data types and functions in its standard library--because C++ pretty much invented (or at least made popular, right?) operator overloading, and I don't know about everybody else, but vector math would be the very first thing that comes to mind learning about operator overloading and thinking "huh, what useful thing could I do with this".
But it's not there (unless I didn't look properly?). So I told him (after explaining why vectors and matrices are nice to have in the first place) to write his own, because it's not hard if you don't need all the functionality in algebra3.h and it's a nice exercise for C++ classes and operator overloading.
Now my questions, since we're in a thread with people that know a whole lot more about C++ than I do:
Is there really no mathematical vector/matrix algebra data type with associated functions in the C++ standard libraries? (And I don't mean the one called Vector, that's really sorta-kinda like a list)
If no, what do people generally use? Lightweight, or big framework?
On the lightweight side of things, is algebra3.h a relatively good choice?
I looked through the code and saw a few things that bugged me. Angles are in degrees (math code and functions use radians internally everywhere, this is just burning some useless muls and divs for a unit change that will be mistaken and cause a bug in someone's code some day somewhere). That one I'd immediately change before using this lib :) I'd like to reserve degrees just for displaying and input.
I've been coding a lot of GLSL recently, and IMO their built-in vector math functions are pretty well thought-out, so there's some choices that differ. Like the overloaded asterisk (multiply) operator is sometimes a dot product, other times element-wise mul. From experience, I've found my code to be more readable when using an asterisk for operations just interchangeable with a scalar multiply (which is element-wise), 'dot' and 'cross' as names functions (you don't use the cross-product nearly as much to warrant its own operator, and the name 'dot' hits the sweet spot between being short enough so it doesn't matter you use it a bit more often) and preferably another operator for matrix-multiply (maybe the '@'? is that possible in C++?) to signify it's not commutative and just really really not at all what the asterisk-operator usually does with scalars and vectors (bonus, you also get an element-wise multiply for matrices, yay).
Ooh and here's a nice one, is there any nice way to implement dimension swizzling of vectors in C++ like GLSL has? Like you could say P.xzyy and get a vec4 consisting of elements 0,2,1 and 1 of vector P, respectively. Because that was like the coolest thing ever when first learning GLSL, I've never seen anything like it :)
- Animats 10y agoThe design of that library isn't mine. It's from "Graphic Gems IV". I just rewrote it in C++ as all inline, when I needed it for a physics engine in the 1990s. It's been used in a few college courses. In general, overloading operators beyond arithmetic is a bad idea. It just leads to confusion and strange mixed-mode semantics. Python suffers from this. "+" is concatenate for vectors of numbers, but addition for numpy arrays. You can add a vector to a numpy array in Python without getting an error. Guess which action takes place.