最近在看3D相关的一些基础,因为搞as3这么多年了,决定3D基础这块还是从AS3入手,3D游戏开发这块从U3D入手,扯远了,研究Matrix3D类时发现了矩阵处理转换时的一些方法均分为appendXXX和prependXXX两种,帮助文档中的解释是后置和前置的区别,搞不清楚,所以决定花点时间弄一下这个东西。
直接上代码,最后一行注释是前四行代码的执行结果:
1 var m:Matrix3D = new Matrix3D(); 2 m.appendRotation(45, Vector3D.X_AXIS); 3 m.appendTranslation(10, 20, 30); 4 trace(m.decompose()); 5 //Vector3D(10, 20, 30),Vector3D(0.7853981852531433, 0, 0),Vector3D(1, 1, 1) 6 7 var m:Matrix3D = new Matrix3D(); 8 m.appendTranslation(10, 20, 30); 9 m.appendRotation(45, Vector3D.X_AXIS); 10 trace(m.decompose()); 11 //Vector3D(10, -7.071067810058594, 35.35533905029297),Vector3D(0.7853981852531433, 0, 0),Vector3D(1, 1, 1) 12 13 var m:Matrix3D = new Matrix3D(); 14 m.prependRotation(45, Vector3D.X_AXIS); 15 m.prependTranslation(10, 20, 30); 16 trace(m.decompose()); 17 //Vector3D(10, -7.071067810058594, 35.35533905029297),Vector3D(0.7853981852531433, 0, 0),Vector3D(1, 1, 1) 18 19 var m:Matrix3D = new Matrix3D(); 20 m.prependTranslation(10, 20, 30); 21 m.prependRotation(45, Vector3D.X_AXIS); 22 trace(m.decompose()); 23 //Vector3D(10, 20, 30),Vector3D(0.7853981852531433, 0, 0),Vector3D(1, 1, 1)
上面的示例可以看出两者之间的差距是运算顺序不同,prependXXX的运算顺序是反过来的,好吧,我们再看一个示例:
1 var m:Matrix3D = new Matrix3D(); 2 m.appendRotation(45, Vector3D.X_AXIS); 3 m.appendTranslation(10, 15, 20); 4 m.appendTranslation(10, 15, 20); 5 m.appendRotation(45, Vector3D.Y_AXIS); 6 m.appendTranslation(10, 15, 20); 7 m.appendTranslation(10, 20, 30); 8 m.appendRotation(45, Vector3D.Z_AXIS); 9 trace(m.decompose()); 10 //Vector3D(-1.8198051452636719, 90.10408020019531, 64.14213562011719),Vector3D(0.7853981852531433, 0.7853982448577881, 0.7853981852531433),Vector3D(1, 1, 1) 11 m.appendTranslation(10, 20, 30); 12 trace(m.decompose()); 13 //Vector3D(8.180194854736328, 110.10408020019531, 94.14213562011719),Vector3D(0.7853981852531433, 0.7853982448577881, 0.7853981852531433),Vector3D(1, 1, 1) 14 15 var m:Matrix3D = new Matrix3D(); 16 m.appendRotation(45, Vector3D.X_AXIS); 17 m.appendTranslation(10, 15, 20); 18 m.appendTranslation(10, 15, 20); 19 m.appendRotation(45, Vector3D.Y_AXIS); 20 m.appendTranslation(10, 15, 20); 21 m.appendTranslation(10, 20, 30); 22 m.appendRotation(45, Vector3D.Z_AXIS); 23 trace(m.decompose()); 24 //Vector3D(-1.8198051452636719, 90.10408020019531, 64.14213562011719),Vector3D(0.7853981852531433, 0.7853982448577881, 0.7853981852531433),Vector3D(1, 1, 1) 25 m.prependTranslation(10, 20, 30); 26 trace(m.decompose()); 27 //Vector3D(25.857860565185547, 107.78174591064453, 82.0710678100586),Vector3D(0.7853981852531433, 0.7853982448577881, 0.7853981852531433),Vector3D(1, 1, 1)
总而言之,appendXXX比较适合我们的逻辑,即在之前的基础上添加转换,而prependXXX则可以看做先弄一个新的矩阵添加当前的转换,再添加其本身的所有转换,即prependXXX添加的转换总是在所有的转换之前;
或者appendXXX可以看做按全局坐标来处理,而prependXXX可以看做按本地坐标来处理。