## Bug in "quaternion.h"

Discussions regarding ARGoS bugs. Report bugs here.
haithamelfaham
Posts: 2
Joined: Mon Feb 10, 2014 12:24 pm

### Bug in "quaternion.h"

BetweenTwoVectors has a problem returning the right vector in the anti-parallel vectors case. A modified version of "quaternion.h" is attached with a suggested bug fix. Please verify the code and update the old one.
Attachments
quaternion.h

pincy
Posts: 512
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

### Re: Bug in "quaternion.h"

Hi haithamelfaham,

Thanks for spotting this bug. You're absolutely right. I have checked your code, refactored it, and optimized it a little.

Can you please check that this version works?

As soon as you confirm it, I'll commit the fix and credit you for it.

Cheers,
Carlo

Code: Select all

``` inline CQuaternion& BetweenTwoVectors(const CVector3& c_vector1, const CVector3& c_vector2) { Real fProd = c_vector1.DotProduct(c_vector2); if(fProd > 0.999999f) { /* The two vectors are parallel, no rotation */ m_fValues[0] = 1.0; m_fValues[1] = 0.0; m_fValues[2] = 0.0; m_fValues[3] = 0.0; } else if(fProd < -0.999999f) { /* The two vectors are anti-parallel */ /* We need to set an arbitrary rotation axis */ /* To find it, we calculate the cross product of c_vector1 with either X or Y, depending on which is not coplanar with c_vector1 */ CVector3 cRotAxis = c_vector1; if(Abs(c_vector1.DotProduct(CVector3::X)) < 0.999999) { /* Use the X axis */ cRotAxis.CrossProduct(CVector3::X); } else { /* Use the Y axis */ cRotAxis.CrossProduct(CVector3::Y); } /* The wanted quaternion is a rotation around cRotAxis by 180 degrees */ FromAngleAxis(CRadians::PI, cRotAxis); } else { /* The two vectors are not parallel nor anti-parallel */ m_fValues[0] = ::sqrt(c_vector1.SquareLength() * c_vector2.SquareLength()) + fProd; CVector3 cCrossProd(c_vector1); cCrossProd.CrossProduct(c_vector2); m_fValues[1] = cCrossProd.GetX(); m_fValues[2] = cCrossProd.GetY(); m_fValues[3] = cCrossProd.GetZ(); Normalize(); } return *this; } ```

haithamelfaham
Posts: 2
Joined: Mon Feb 10, 2014 12:24 pm

### Re: Bug in "quaternion.h"

Yes this version works.. I am not sure though if the parallel case is necessary I think without it would still work fine. I tried with 2 parallel vectors and it works fine. Maybe you can verify that as well and if so I guess you can comment it out as a further optimization.

Haitham

pincy
Posts: 512
Joined: Thu Mar 08, 2012 8:04 pm
Location: Boston, MA
Contact:

### Re: Bug in "quaternion.h"

Hi Haitham,

Thanks for testing it.

Indeed the code would work, but singling out the parallel case spares a little computation.

I'll commit this version.

Cheers,
Carlo