# 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.

## 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 on other sites
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.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 9
• 12
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
633348
• Total Posts
3011459
• ### Who's Online (See full list)

There are no registered users currently online

×