向量绕任意轴旋转

此处旋转轴向量N必须为归一化后的向量,否则旋转后的向量将可能会出错
View Code
 1 float c = (float)Math.Cos(theta);
 2 float c1 =1.0f- (float)Math.Cos(theta);
 3 float s = (float)Math.Sin(theta);
 4 float ny2 = (float)Math.Pow(n.Y, 2);
 5 float nx2 = (float)Math.Pow(n.X, 2);
 6 float nz2 = (float)Math.Pow(n.Z, 2);
 7 Vector3 i = imp;
 8 
 9 
10 
11 Matrix m = new Matrix(
12 nx2 + (1.0f - nx2) * c,
13 n.X * n.Y * c1 - n.Z * s,
14 n.X * n.Z * c1 + n.Y * s,
15 0,
16 
17 n.X * n.Y * c1 + n.Z * s,
18 ny2 + (1.0f - ny2) * c,
19 n.Y * n.Z * c1 - n.X * s,
20 0,
21 
22 n.X * n.Z * c1 - n.Y * s,
23 n.Y * n.Z * c1 + n.X * s,
24 nz2 + (1.0f - nz2) * c,
25 0,
26 
27 0,
28 0,
29 0,
30 1
31 );
32 Quaternion q = new Quaternion(0, 0, 0, 1);
33 Quaternion.CreateFromRotationMatrix(ref m, out q);
34 Matrix m1 = Matrix.CreateTranslation(i);
35 m1 = Matrix.Transform(m1, q);
36 o = m1.Translation;

 

旋转矩阵也可替换为:
 1 Matrix m = new Matrix(
 2 n.X * n.X * (c1) + c,
 3 n.X * n.Y * (c1) + n.Z * s,
 4 n.X * n.Z * (c1) - n.Y * s, 0,
 5 n.X * n.Y * (c1) - n.Z * s,
 6 n.Y * n.Y * (c1) + c,
 7 n.Y * n.Z * (c1) + n.X * s, 0,
 8 n.X * n.Z * (c1) + n.Y * s,
 9 n.Y * n.Z * (c1) - n.X * s,
10 n.Z * n.Z * (c1) + c, 0,
11 0, 0, 0, 1
12 );
posted @ 2013-01-30 14:35  ralflql  阅读(1390)  评论(0编辑  收藏  举报