简体   繁体   English

Java:二维旋转矩阵不起作用

[英]Java: Matrix rotation in 2D not working

I have this 3x3 matrix which I'm using to preform basic transformations like translation, scaling and rotation, but the rotation part isn't working as it should. 我有一个3x3矩阵,用于执行基本转换,例如平移,缩放和旋转,但是旋转部分无法正常工作。 For any angle larger then 30 degrees or so, it just looks.. wierd. 对于大于30度左右的任何角度,它看起来都很奇怪。

The method looks like this: 该方法如下所示:

public void rotate(float angle) {
    double rads = angle * (Math.PI/180);
    float sin = (float) Math.sin(rads);
    float cos = (float) Math.cos(rads);

    matrix[0][0] = cos;
    matrix[0][1] = -sin;
    matrix[1][0] = sin;
    matrix[1][1] = cos; 
}

And the methods applying the transformation: 以及应用转换的方法:

For X: 对于X:

public float[] applyTransformX(float[] x, float[] y) {
    float[] transformed = new float[x.length];
    for(int i=0; i<x.length; i++) {
        transformed[i] = (x[i] * matrix[0][0]) + (y[i] * matrix[0][1] + matrix[0][2]);
        System.out.println((x[i] * matrix[0][0]) + (y[i] * matrix[0][1] + matrix[0][2]));

    }
    return transformed;
}

For Y: 对于Y:

public float[] applyTransformY(float[] x, float[] y) {
    float[] transformed = new float[x.length];
    for(int i=0; i<x.length; i++) {
        transformed[i] = (x[i] * matrix[1][0]) + (y[i] * matrix[1][1] + matrix[1][2]);
    }
    return transformed;
}

Are there any fundamental things I'm missing here? 我在这里缺少什么基本的东西吗? I'm aware of the fact that I'm not doing the transformations around a fixed point. 我知道我没有围绕固定点进行转换的事实。 Is that perhaps why I'm experiencing this problem? 这也许就是为什么我遇到这个问题吗?

As I said, the translation and scaling is working as it should. 就像我说的那样,转换和缩放正在按预期方式进行。

All that I can think of is that there might be a rounding error resulting from a discrepancy in the sizes of the three terms that you're adding to get each x[i] , and to get each y[i] . 我能想到的就是,由于要获取每个x[i]和要获取每个y[i]而要添加的三个项的大小差异,可能会产生舍入误差。 My instinct would be to add in the translation term last, so that a sizable translation doesn't cause loss of significance in the terms that result from rotation. 我的直觉是最后添加翻译术语,以使大量翻译不会导致旋转产生的术语失去意义。 This just requires you removing some of your parentheses, so that the standard left-to-right order of operations kicks in. In other words, in the method for calculating the X values, write: 这只需要删除一些括号,即可执行标准的从左到右的操作顺序。换句话说,在计算X值的方法中,编写:

transformed[i] = x[i] * matrix[0][0] + y[i] * matrix[0][1] + matrix[0][2];

without any of the parentheses. 没有任何括号。 Similarly, when calculating the Y values, write 同样,在计算Y值时,写

transformed[i] = x[i] * matrix[1][0] + y[i] * matrix[1][1] + matrix[1][2];

If that doesn't help, then please post some actual numbers, so that we can see which calculations might be causing loss of significance. 如果那没有帮助,请发布一些实际数字,以便我们可以查看哪些计算可能导致重要性下降。 The fact that this works OK for small angles suggests that you are seeing a rounding error, as opposed to an actual incorrect calculation method. 对于较小的角度,此方法可以正常工作的事实表明,您看到的是舍入误差,而不是实际的错误计算方法。

The problem is in the computation of transformed[i] in both cases: for X it should be: 在两种情况下,问题在于transformed[i]的计算:对于X,应该是:

transformed[i] = (x[i] * matrix[0][0]) + (y[i] * matrix[0][1]);

and for Y it should be: 对于Y应该是:

transformed[i] = (x[i] * matrix[1][0]) + (y[i] * matrix[1][1]);

If you include matrix[0][2] and matrix[1][2] this would for a Z component, however those should evaluate to 0 for a rotation around the Z axis ( matrix[2][2] would be 1 although to be complete). 如果包含matrix[0][2]matrix[1][2] ,则表示Z分量,但是对于绕Z轴的旋转,它们的求值应为0(尽管matrix[2][2]为1,完整)。 You probably just forgot to reinitialize them… 您可能只是忘了重新初始化它们……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM