同理添加旋转变化 要为四维矩阵类添加相应的变化公式
public Matrix4f initRotation(float x, float y, float z)
{
Matrix4f rx = new Matrix4f();
Matrix4f ry = new Matrix4f();
Matrix4f rz = new Matrix4f();
x = (float)Math.toRadians(x);
y = (float)Math.toRadians(y);
z = (float)Math.toRadians(z);
rz.m[0][0] = (float)Math.cos(z);rz.m[0][1] = -(float)Math.sin(z);rz.m[0][2] = 0; rz.m[0][3] = 0;
rz.m[1][0] = (float)Math.sin(z);rz.m[1][1] = (float)Math.cos(z);rz.m[1][2] = 0; rz.m[1][3] = 0;
rz.m[2][0] = 0; rz.m[2][1] = 0; rz.m[2][2] = 1; rz.m[2][3] = 0;
rz.m[3][0] = 0; rz.m[3][1] = 0; rz.m[3][2] = 0; rz.m[3][3] = 1;
rx.m[0][0] = 1; rx.m[0][1] = 0; rx.m[0][2] = 0; rx.m[0][3] = 0;
rx.m[1][0] = 0; rx.m[1][1] = (float)Math.cos(x);rx.m[1][2] = -(float)Math.sin(x);rx.m[1][3] = 0;
rx.m[2][0] = 0; rx.m[2][1] = (float)Math.sin(x);rx.m[2][2] = (float)Math.cos(x);rx.m[2][3] = 0;
rx.m[3][0] = 0; rx.m[3][1] = 0; rx.m[3][2] = 0; rx.m[3][3] = 1;
ry.m[0][0] = (float)Math.cos(y);ry.m[0][1] = 0; ry.m[0][2] = -(float)Math.sin(y);ry.m[0][3] = 0;
ry.m[1][0] = 0; ry.m[1][1] = 1; ry.m[1][2] = 0; ry.m[1][3] = 0;
ry.m[2][0] = (float)Math.sin(y);ry.m[2][1] = 0; ry.m[2][2] = (float)Math.cos(y);ry.m[2][3] = 0;
ry.m[3][0] = 0; ry.m[3][1] = 0; ry.m[3][2] = 0; ry.m[3][3] = 1;
m = rz.mul(ry.mul(rx)).getM();
return this;
}
然后修改Transform类
public class Transform {
/*
* 平移
* */
private Vector3f translation;
/*
* 旋转
* */
private Vector3f rotation;
/*
* 构造函数
* */
public Transform() {
translation = new Vector3f(0,0,0);
rotation = new Vector3f(0,0,0);
}
/*
* 获得变换矩阵
* */
public Matrix4f getTransformation()
{
Matrix4f translationMatrix = new Matrix4f().initTranslation(translation.getX(), translation.getY(), translation.getZ());
Matrix4f rotationMatrix = new Matrix4f().initRotation(rotation.getX(), rotation.getY(), rotation.getZ());
return translationMatrix.mul(rotationMatrix);
}
public Vector3f getTranslation() {
return translation;
}
public void setTranslation(Vector3f translation) {
this.translation = translation;
}
public void setTranslation(float x, float y, float z)
{
this.translation = new Vector3f(x, y, z);
}
public Vector3f getRotation() {
return rotation;
}
public void setRotation(Vector3f rotation) {
this.rotation = rotation;
}
public void setRotation(float x, float y, float z)
{
this.rotation = new Vector3f(x, y, z);
}
}
再Game Update周期里 去加上旋转变化即可
/*
* 均匀化 物理变化
* */
transform.setTranslation((float)Math.sin(temp), 0, 0);
transform.setRotation(0, 0, (float)Math.sin(temp) * 180);
shader.setUniform("transform", transform.getTransformation());
效果: