How using a quaternion to rotate around around a line?

Started by
0 comments, last by Zakwayda 18 years, 9 months ago
Hello, I just wondering how you can rotate a point p around a line segment [a,b]. I do it like this: [cpp] Quat q; q.rotation((b-a).normalized(),angle); Mtx3 m=q.makeMatrix(); Vec3 pos=m*(p-a); pos+=a; void Quat::rotation(const Vec3& u, float angle) { angle*=0.5f; s=cosf(angle); v=u*sinf(angle); normalize(); } float Quat::magnitude() { return s*s+(v&v); } void Quat::normalize() { float m=magnitude(); if(m==0.0f) return; s/=m; v*=(1.0f/m); } Mtx3 Quat::makeMatrix() const { Mtx3 m; m[0] = 1.0f - 2*(v[Y]*v[Y] + v[Z]*v[Z]); m[1] = 2*(v[X]*v[Y] + s*v[Z]); m[2] = 2*(v[X]*v[Z] - s*v[Y]); m[3] = 2*(v[X]*v[Y] - s*v[Z]); m[4] = 1.0f - 2*(v[X]*v[X] + v[Z]*v[Z]); m[5] = 2*(v[Y]*v[Z] + s*v[X]); m[6] = 2*(v[X]*v[Z] + s*v[Y]); m[7] = 2*(v[Y]*v[Z] - s*v[X]); m[8] = 1.0f-2*(v[X]*v[X] + v[Y]*v[Y]); return m; } [\cpp]
Advertisement
Are you just asking if that's correct? It looks ok to me - is it working?

One observation is that the quaternion really has no purpose in your example. You're just using it as an intermediary step to construct an axis-angle matrix; however, a matrix can be constructed just as easily directly from an axis-angle pair.

Also, are you overloading & for the dot product? If so, let me put in a vote for not doing that; IMO that's a clear case of operator overloading abuse! If you're going to overload anything, make it *, but my preference is to make the dot product a named function.

This topic is closed to new replies.

Advertisement