Using the android.graphics.Matrix library:
使用android.graphics.Matrix库:
Matrix foo = new Matrix();
foo.setTranslate(10.0f, 0.0f);
Log.d("MatrixTest", foo.toString());
foo.postRotate(30.0f, 0.0f, 0.0f);
Log.d("MatrixTest", foo.toString());
I obtain the following output:
我获得以下输出:
foo = {[1.0, 0.0, 10.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]}
foo = {[0.8660254, -0.5, 8.660254][0.5, 0.8660254, 5.0][0.0, 0.0, 1.0]}
Which is precisely what I would expect.
这正是我所期待的。
It would now be useful for my application to also run on the desktop, so I am porting to libgdx.
现在,我的应用程序也可以在桌面上运行,所以我移植到libgdx。
Using the com.badlogic.gdx.math.Matrix3 library:
使用com.badlogic.gdx.math.Matrix3库:
Matrix3 bar = new Matrix3();
bar.translate(10.0f, 0.0f);
System.out.println(bar.toString());
bar.rotate(30.0f);
System.out.println(bar.toString());
I obtain the following output:
我获得以下输出:
bar = [1.0|0.0|10.0] [0.0|1.0|0.0] [0.0|0.0|1.0]
bar = [0.8660254|-0.5|10.0] [0.5|0.8660254|0.0] [0.0|0.0|1.0]
In this case, the translation component of X has not been rotated.
在这种情况下,X的平移分量尚未旋转。
Is this the correct behaviour? The API for both the android postRotate() method and the libgdx rotate() method describe them as a post-rotate functions.
这是正确的行为吗? android postRotate()方法和libgdx rotate()方法的API将它们描述为后旋转函数。
I get similar results for libgdx Matrix4, which is what I would actually like to use.
我得到类似的libgdx Matrix4的结果,这是我真正想要使用的。
Can someone suggest a nice way to reproduce the results obtained from the android library using libgdx Matrix3 or 4?
有人能建议一个很好的方法来重现使用libgdx Matrix3或4从android库中获得的结果吗?
1 个解决方案
#1
0
Just to get things clear...
只是为了清楚明白......
It is least ambiguous if you just stick to matrix multiplication then trying to use convenience methods to use the operations on the matrix themselves.
如果你只是坚持矩阵乘法然后尝试使用便利方法来使用矩阵本身的操作,那么它是最不模糊的。
In most cases when doing an operation on the matrix such as translate what will happen is a translation matrix will be created and the original matrix will be multiplied by the new matrix as originalMatrix*translationMatrix
. This is what happens in your second example. Your first example has a prefix "post" which seems to do the invers operation translationMatrix*originalMatrix
.
在大多数情况下,当对矩阵进行操作时,例如平移将发生的事情,将创建平移矩阵,并且原始矩阵将乘以新矩阵作为originalMatrix * translationMatrix。这是你的第二个例子中发生的事情。你的第一个例子有一个前缀“post”,它似乎做了invers操作translationMatrix * originalMatrix。
I can understand this "post" may be useful and for someone who is not very familiar with matrix operations it may even be more natural to use. What I can not understand is "postMultiply". This is not how it works at all, this is not how the matrix multiplication is used and will only confuse most of the developers. Not only in the process of learning but even if later someone will read this code. Sure it works but to me this would be the same as writing something like this:
我可以理解这个“帖子”可能是有用的,对于不太熟悉矩阵操作的人来说,它甚至可能更自然。我无法理解的是“postMultiply”。这根本不是它的工作原理,这不是如何使用矩阵乘法,只会混淆大多数开发人员。不仅在学习过程中,即使以后有人会阅读此代码。当然它有效,但对我来说,这与写这样的东西是一样的:
Print first N
integer values:
先打印N个整数值:
for(int i=N; i>0; --i)
{
int printValue = N-i+1;
// print the printValue
}
Sure this works but for me it is totally unreadable.
当然这有效,但对我来说这是完全不可读的。
If in any case you have trouble imagining matrix multiplication you should think of it as if you would be looking at it from the first person. For instance translate in X axis would mean step forward, Y left and Z up. The rotations are then only around your center and will never effect your position.
如果在任何情况下你都无法想象矩阵乘法,你应该把它想象成你从第一个人那里看它。例如,X轴平移意味着向前迈步,Y向左和Z向上。然后旋转只围绕你的中心,永远不会影响你的位置。
So in first example you first rotate towards your left for 30 degrees and you are now facing toward point (0.8660254, 0.5)
. Then you translate by 10 along X axis which means step forward for 10 points and your position becomes (0.8660254, 0.5)*10.0
so you end up at (8.660254, 5.0)
facing towards (8.660254 + 0.8660254, 5.0 + 0.5)
.
因此,在第一个示例中,您首先向左旋转30度,然后您将面向点(0.8660254,0.5)。然后沿X轴平移10,这意味着向前迈出10个点,你的位置变为(0.8660254,0.5)* 10.0,所以你最终在(8.660254,5.0)面向(8.660254 + 0.8660254,5.0 + 0.5)。
In the second example you first translate by 10 points along X axis which means go forward by 10 points ending at (10, 0)
still facing the same direction and then you rotate by 30 degrees to your left and simply end up at the same location at (10.0, .0)
facing toward (10.0+0.8660254, .0+.5)
.
在第二个例子中,你首先沿着X轴平移10个点,这意味着前进10个点,结束于(10,0)仍然面向相同的方向,然后你向左旋转30度,直接在同一个位置在(10.0,.0)面向(10.0 + 0.8660254,.0 + .5)。
As you can now probably see from the results you are getting the columns in your matrix (not the rows in your case) define the matrix base vectors. So simply from those vectors you can understand which way the object is facing and where is it's center. You may also simply construct a matrix from those vectors. If you want to create a matrix for an object which is at position P
and is faced in the direction D
then the matrix is:
正如您现在可以从结果中看到的那样,您将获得矩阵中的列(而不是您的情况中的行)定义矩阵基矢量。因此,只需从这些矢量中,您就可以了解物体面向哪个方向以及它的中心位置。您也可以简单地从这些向量构造矩阵。如果要为位于P位置且面向D方向的对象创建矩阵,则矩阵为:
D.x, D.y, .0
-D.y, D.x, .0
P.x, P.y, 1.0
In your case transposed
在你的情况下转置
D.x, -D.y, P.x
D.y, D.x, P.y
.0, .0, 1.0
#1
0
Just to get things clear...
只是为了清楚明白......
It is least ambiguous if you just stick to matrix multiplication then trying to use convenience methods to use the operations on the matrix themselves.
如果你只是坚持矩阵乘法然后尝试使用便利方法来使用矩阵本身的操作,那么它是最不模糊的。
In most cases when doing an operation on the matrix such as translate what will happen is a translation matrix will be created and the original matrix will be multiplied by the new matrix as originalMatrix*translationMatrix
. This is what happens in your second example. Your first example has a prefix "post" which seems to do the invers operation translationMatrix*originalMatrix
.
在大多数情况下,当对矩阵进行操作时,例如平移将发生的事情,将创建平移矩阵,并且原始矩阵将乘以新矩阵作为originalMatrix * translationMatrix。这是你的第二个例子中发生的事情。你的第一个例子有一个前缀“post”,它似乎做了invers操作translationMatrix * originalMatrix。
I can understand this "post" may be useful and for someone who is not very familiar with matrix operations it may even be more natural to use. What I can not understand is "postMultiply". This is not how it works at all, this is not how the matrix multiplication is used and will only confuse most of the developers. Not only in the process of learning but even if later someone will read this code. Sure it works but to me this would be the same as writing something like this:
我可以理解这个“帖子”可能是有用的,对于不太熟悉矩阵操作的人来说,它甚至可能更自然。我无法理解的是“postMultiply”。这根本不是它的工作原理,这不是如何使用矩阵乘法,只会混淆大多数开发人员。不仅在学习过程中,即使以后有人会阅读此代码。当然它有效,但对我来说,这与写这样的东西是一样的:
Print first N
integer values:
先打印N个整数值:
for(int i=N; i>0; --i)
{
int printValue = N-i+1;
// print the printValue
}
Sure this works but for me it is totally unreadable.
当然这有效,但对我来说这是完全不可读的。
If in any case you have trouble imagining matrix multiplication you should think of it as if you would be looking at it from the first person. For instance translate in X axis would mean step forward, Y left and Z up. The rotations are then only around your center and will never effect your position.
如果在任何情况下你都无法想象矩阵乘法,你应该把它想象成你从第一个人那里看它。例如,X轴平移意味着向前迈步,Y向左和Z向上。然后旋转只围绕你的中心,永远不会影响你的位置。
So in first example you first rotate towards your left for 30 degrees and you are now facing toward point (0.8660254, 0.5)
. Then you translate by 10 along X axis which means step forward for 10 points and your position becomes (0.8660254, 0.5)*10.0
so you end up at (8.660254, 5.0)
facing towards (8.660254 + 0.8660254, 5.0 + 0.5)
.
因此,在第一个示例中,您首先向左旋转30度,然后您将面向点(0.8660254,0.5)。然后沿X轴平移10,这意味着向前迈出10个点,你的位置变为(0.8660254,0.5)* 10.0,所以你最终在(8.660254,5.0)面向(8.660254 + 0.8660254,5.0 + 0.5)。
In the second example you first translate by 10 points along X axis which means go forward by 10 points ending at (10, 0)
still facing the same direction and then you rotate by 30 degrees to your left and simply end up at the same location at (10.0, .0)
facing toward (10.0+0.8660254, .0+.5)
.
在第二个例子中,你首先沿着X轴平移10个点,这意味着前进10个点,结束于(10,0)仍然面向相同的方向,然后你向左旋转30度,直接在同一个位置在(10.0,.0)面向(10.0 + 0.8660254,.0 + .5)。
As you can now probably see from the results you are getting the columns in your matrix (not the rows in your case) define the matrix base vectors. So simply from those vectors you can understand which way the object is facing and where is it's center. You may also simply construct a matrix from those vectors. If you want to create a matrix for an object which is at position P
and is faced in the direction D
then the matrix is:
正如您现在可以从结果中看到的那样,您将获得矩阵中的列(而不是您的情况中的行)定义矩阵基矢量。因此,只需从这些矢量中,您就可以了解物体面向哪个方向以及它的中心位置。您也可以简单地从这些向量构造矩阵。如果要为位于P位置且面向D方向的对象创建矩阵,则矩阵为:
D.x, D.y, .0
-D.y, D.x, .0
P.x, P.y, 1.0
In your case transposed
在你的情况下转置
D.x, -D.y, P.x
D.y, D.x, P.y
.0, .0, 1.0