vec3 vec3::rotate3d(vec3 axis, float angle) {
vec3 final;
float c = (float)cos(angle);
float s = (float)sin(angle);
float t = (1.0f-(float)cos(angle));
final.x = (t*axis.x*axis.x+c)*x+
(t*axis.x*axis.y+s*axis.z)*y+
(t*axis.x*axis.z-s*axis.y)*z;
final.y = (t*axis.x*axis.y-s*axis.z)*x+
(t*axis.y*axis.y+c)*y+
(t*axis.y*axis.z+s*axis.x)*z;
final.z = (t*axis.x*axis.y+s*axis.y)*x+
(t*axis.y*axis.z-s*axis.x)*y+
(t*axis.z*axis.z+c)*z;
x = final.x;
y = final.y;
z = final.z;
return *this;
}
When the axis is one of the cardinal axes (i, j, or k), this degenerates into the matrix that we are used to seeing, but if the axis is something else, the rotation doesn''t rotate correctly. Instead of the vector being in a plane, it seems to wobble considerably. Can anyone tell what I''m doing wrong here? I don''t really want to use quaternions, but if I can''t get this to work correctly I may have to.
P.S.
I''m not sure about the code tags. Would a preview option be possible for this forum?
rotation matrix
I have what should be an equation for a matrix that will rotate an arbitary vector around an axis defined by some other vector. The code that I am using is:
OpenGL blue book p443 (glRotate)
The rotation matrix is :
q.v. Quaternions
[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI's STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]
Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
[edited by - Fruny on April 9, 2002 12:42:53 PM]
The rotation matrix is :
| x2(1-c)+c xy(1-c)-zs xz(1-c)+ys 0 || yx(1-c)+zs y2(1-c)+c yz(1-c)-xs 0 || xz(1-c)-ys yz(1-c)+xs z2(1-c)+c 0 || 0 0 0 1 |
q.v. Quaternions
[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI's STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]
Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
[edited by - Fruny on April 9, 2002 12:42:53 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks. Oddly enough, I didn''t even think to look in the blue book...
What''s the "q.v. Quaternions" mean?
What''s the "q.v. Quaternions" mean?
quote:
Original post by tsuraan
Thanks. Oddly enough, I didn''t even think to look in the blue book...
What''s the "q.v. Quaternions" mean?
Quaternions are mathematical objects representing 3D rotations. Look it up somewhere on the net.
[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]
Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement