It would be convenient to use the operator[] on btVector3, I notice the function is somewhat recently commented out in the btQuadWordStorage... can I get it back?
(I have some templated math functions, I could use btVector3's interchangeably with my other math types if they supported operator[])
btVector3 operator[] disabled?
-
- Site Admin
- Posts: 4221
- Joined: Sun Jun 26, 2005 6:43 pm
- Location: California, USA
Re: btVector3 operator[] disabled?
Have you tried using [] on a btVector3? It should automatically use the operator btScalar* operator, which has better performance.
Thanks,
Erwin
Thanks,
Erwin
-
- Posts: 96
- Joined: Mon Nov 03, 2008 9:57 pm
Re: btVector3 operator[] disabled?
It's not a big deal btw... I can also use &v.x()... if you think you might wind up making w the first element for quaternions, I can see why you want to abstract the storage order...
-
- Site Admin
- Posts: 4221
- Joined: Sun Jun 26, 2005 6:43 pm
- Location: California, USA
Re: btVector3 operator[] disabled?
The following works just fine, even without the operator[] implementation. As I mentioned before, the btVector::operator btScalar* takes care of that, so no need for &v.x().
Have you tried it?
Code: Select all
btVector3 pos(1,2,3);
btScalar y = pos[1];
btAssert(y == 2);
-
- Posts: 96
- Joined: Mon Nov 03, 2008 9:57 pm
Re: btVector3 operator[] disabled?
Yup, this works... should've just tried it instead of checking the documentation firstErwin Coumans wrote:Have you tried using [] on a btVector3? It should automatically use the operator btScalar* operator, which has better performance.
So WTF! I didn't think that could work like that. The compiler checks all of the implicit conversions to see if it can make the type I'm passing into a type which supports the operator I'm calling? Now that I think about it, I guess I've seen this done with iostream and operator<<, so maybe this shouldn't be so surprising.
Not every day I learn a new C++ trick anymore, thanks
-
- Site Admin
- Posts: 4221
- Joined: Sun Jun 26, 2005 6:43 pm
- Location: California, USA
Re: btVector3 operator[] disabled?
The implicit conversions can also be very dangerous:
btVector3 a(0,0,0),b(0,0,0);
(a==b) will return false, because it compares the pointers using this same operator btScalar*.
I just added operator != and operator == to make it a bit safer.
Thanks,
Erwin
btVector3 a(0,0,0),b(0,0,0);
(a==b) will return false, because it compares the pointers using this same operator btScalar*.
I just added operator != and operator == to make it a bit safer.
Thanks,
Erwin