Rotation by pi corrupts vector

N0vember
Posts: 10
Joined: Sat Mar 06, 2010 6:39 pm

Rotation by pi corrupts vector

Post by N0vember »

I am facing a technical problem when using btVector3 where rotating by Pi or half a circle will yield corrupted values in the resulting vector.
My specific example is as simple as this :

Code: Select all

btVector3 vec(1, 0, 0);
btVector3 other(-1, 0, 0);
btVector3 axis(0, 1, 0);

float angle = vec.angle(other);

btVector3 clockwise = vec.rotate(axis, angle);
But I think it can be even simpler and just rotate the same vector by the value of Pi.
Am I doing something wrong ? Or am I even more naive and this is "expected" behavior ? Or is it really a bug ? (I'm using bullet 2.82 and compile it with visual C++ 2013) What would be the way to work around this ?
N0vember
Posts: 10
Joined: Sat Mar 06, 2010 6:39 pm

Re: Rotation by pi corrupts vector

Post by N0vember »

This is actually quite a dumb question due to my non-understanding of how floating point numbers work.
So the value returned is just very very close to 0, but not 0, because we only have a floating point approximation of Pi. I'm just so used to interpreting these -8.74227766e-008 values in coordinates as an error lying somewhere in my program, that I didn't take the time to ponder and realize it's actually mathematically exact and nothing wrong is going on.
Now is there a simple way to round this vector ?
bwelch
Posts: 48
Joined: Thu Dec 12, 2013 4:04 pm

Re: Rotation by pi corrupts vector

Post by bwelch »

If you want simple, you could just have an if statement where if the value is less than some threshold, set the value to 0.
Basroil
Posts: 463
Joined: Fri Nov 30, 2012 4:50 am

Re: Rotation by pi corrupts vector

Post by Basroil »

N0vember wrote: Now is there a simple way to round this vector ?
Why not just check against btFuzzyZero? When using floats it should default to about 1e-7, so difference under that distance from zero should be zero.