Jump to content
  • Advertisement
Sign in to follow this  
takis

How using a quaternion to rotate around around a line?

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!