我的转换矩阵运行不正常

时间:2013-11-16 19:05:44

标签: java matrix 2d transformation

所以我的2D转换方法是:

Matrix3f类:

查看init(这里插入转换)方法和mult方法,我的问题可能来自它们。

public class Matrix3f {
public float m[][];

public Matrix3f(){
    m = new float[3][3];
}

public Matrix3f initIdentity(){
    m[0][0] = 1;    m[1][0] = 0;    m[2][0] = 0;
    m[0][1] = 0;    m[1][1] = 1;    m[2][1] = 0;
    m[0][2] = 0;    m[1][2] = 0;    m[2][2] = 1;

    return this;
}

public Matrix3f initTranslation(float x, float y){
    m[0][0] = 1;    m[1][0] = 0;    m[2][0] = x;
    m[0][1] = 0;    m[1][1] = 1;    m[2][1] = y;
    m[0][2] = 0;    m[1][2] = 0;    m[2][2] = 1;

    return this;
}

public Matrix3f initRotation(float a){
    m[0][0] = (float) Math.cos(a);  m[1][0] = (float)Math.sin(a);   m[2][0] = 0;
    m[0][1] = (float) -Math.sin(a); m[1][1] = (float)Math.cos(a);   m[2][1] = 0;
    m[0][2] = 0;                    m[1][2] = 0;                    m[2][2] = 1;

    return this;
}

public Matrix3f initScale(float sx, float sy){
    m[0][0] = sx;   m[1][0] = 0;    m[2][0] = 0;
    m[0][1] = 0;    m[1][1] = sy;   m[2][1] = 0;
    m[0][2] = 0;    m[1][2] = 0;    m[2][2] = 1;

    return this;
}

public Matrix3f mult(Matrix3f r){
    Matrix3f res = new Matrix3f();

    for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                    res.set(i, j, m[i][0] * r.get(0, j) +
                                  m[i][1] * r.get(1, j) +
                                  m[i][2] * r.get(2, j));
            }
    }

    return res;
}

public Vector3f mult(Vector3f r){
    Vector3f res = new Vector3f();
    res.setX(m[0][0] * r.getX() + m[1][0] * r.getX() + m[2][0] * r.getX());
    res.setY(m[0][1] * r.getY() + m[1][1] * r.getY() + m[2][1] * r.getY());
    res.setZ(m[0][2] * r.getZ() + m[1][2] * r.getZ() + m[2][2] * r.getZ());
    return res;
}

public Vector2f mult(Vector2f r){
    Vector3f res = new Vector3f(0,0,0);
    res.setX(m[0][0] * r.getX() + m[1][0] * r.getX() + m[2][0] * r.getX());
    res.setY(m[0][1] * r.getY() + m[1][1] * r.getY() + m[2][1] * r.getY());
    res.setZ(m[0][2] * 1 + m[1][2] * 1 + m[2][2] * 1);
    return new Vector2f(res);
}

public float[][] getM() {
    return m;
}

public void setM(float[][] m) {
    this.m = m;
}

public void set(int x, int y, float value){
    m[x][y] = value;
}

public float get(int x, int y){
    return m[x][y];
}

MainComponent:

public class MainComponent {

public static final int WIDTH = 1600;
public static final int HEIGHT = WIDTH / 16 * 9;

private static Transform transform;
static Vector3f pos = new Vector3f(1,1,1);

    // ignore my numbers that I've used in setTranslation/Scale() they don't mean anything, they just happened to be there.

public static void main(String[] args) {
    transform = new Transform();
    //transform.setRotation((float) Math.toRadians(360));
    transform.setTranslation(9,9);
    transform.setScale(1);
    pos = transform.getTransformation().mult(pos);
    System.out.println(pos.toString());
}

}

变换:

了解getTransformation()

public class Transform {
private Vector2f translation;
private Vector2f scale;
private float rotation;

public Transform(){
    translation = new Vector2f(0,0);
    rotation = 0.0f;
    scale = new Vector2f(1,1);
}

public Matrix3f getTransformation(){
    Matrix3f translationMatrix = new Matrix3f().initTranslation(translation.getX(), translation.getY());
    Matrix3f rotationMatrix = new Matrix3f().initRotation(rotation);
    Matrix3f scaleMatrix = new Matrix3f().initScale(scale.getX(), scale.getY());

    return translationMatrix.mult(rotationMatrix.mult(scaleMatrix));
}

public Vector2f getTranslation() {
    return translation;
}

public void setTranslation(Vector2f translation) {
    this.translation = translation;
}

public void setTranslation(float x, float y){
    this.translation = new Vector2f(x,y);
}

public Vector2f getScale() {
    return scale;
}

public void setScale(Vector2f scale) {
    this.scale = scale;
}

public void setScale(float x, float y){
    this.scale = new Vector2f(x,y);
}

public void setScale(float s){
    this.scale = new Vector2f(s,s);
}

public float getRotation() {
    return rotation;
}

public void setRotation(float rotation) {
    this.rotation = rotation;
}

...我有Vector2f和Vector3f类,我可以在矢量上获取,设置和做数学。

因此,在这个设置中,我在MainComponent中输入了vector3f“pos”(vector3f为了均匀)进行转换;它使用所需的平移,旋转和比例值设置所有矩阵,然后将它们相乘。然后在MainComponent中将结果转换乘以该点以获得结果点。

但这有很多问题:

  • 如果我的观点是0,0并且我将我的翻译设置为任何数字其他而不是0,0,则结果为0,0
  • 如果该点不是整数(有任何小数位),则使用0,0以外的任何其他数字进行平移会产生奇怪的乘数效应(如果pos为0.5f,0.5f且translate设置为3,3则结果是1.5,1.5)。
  • 旋转(浮动)Math.toRadians(360)并没有完全向上舍入到原点,但我认为这是因为pi具有无限(无论多大都可以去)小数位和所有XD

所以问题都集中在翻译矩阵上。我希望我真正找不到它的东西,而不是很容易发现和愚蠢的XD。

0 个答案:

没有答案