Java 如何在lwjgl中使用四元数?
我正在用lwjgl做一个3D游戏。我希望能够用四元数旋转具有x、y和z轴的对象(以避免万向节锁定),但它不起作用 这是我的密码:Java 如何在lwjgl中使用四元数?,java,opengl,rotation,lwjgl,quaternions,Java,Opengl,Rotation,Lwjgl,Quaternions,我正在用lwjgl做一个3D游戏。我希望能够用四元数旋转具有x、y和z轴的对象(以避免万向节锁定),但它不起作用 这是我的密码: import org.lwjgl.opengl.GL11; import org.lwjgl.util.vector.*; public class Node{ private double[] pos; private Quaternion rotation; public static float PI180 = (float)Math
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.*;
public class Node{
private double[] pos;
private Quaternion rotation;
public static float PI180 = (float)Math.PI/180;
public Node(){
pos = new double[]{0,0,0};
rotation = new Quaternion();
}
protected void apply(){
GL11.glTranslated(pos[0],pos[1],pos[2]);
float[] axis = getAxisAngle();
GL11.glRotatef(axis[0], axis[1], axis[2], axis[3]);
}
public void translateX(double x){
pos[0]+=x;
}
public void translateY(double y){
pos[1]+=y;
}
public void translateZ(double z){
pos[2]+=z;
}
public void translate(double x, double y, double z){
pos[0]+=x;
pos[1]+=y;
pos[2]+=z;
}
public void rotateX(float x){
Quaternion nrot = fromEuler(x,0,0);
Quaternion.mul(nrot, rotation, rotation);
}
public void rotateY(float y){
Quaternion nrot = fromEuler(0,y,0);
Quaternion.mul(nrot, rotation, rotation);
}
public void rotateZ(float z){
Quaternion nrot = fromEuler(0,0,z);
Quaternion.mul(nrot, rotation, rotation);
}
public void rotate(int x, int y, int z){
Quaternion nrot = fromEuler(x,y,z);
Quaternion.mul(nrot, rotation, rotation);
}
private Quaternion fromEuler(float x, float y, float z){
float pitch = y * PI180 / 2.0f;
float yaw = z * PI180 / 2.0f;
float roll = x * PI180 / 2.0f;
float sinp = (float)Math.sin(pitch);
float siny = (float)Math.sin(yaw);
float sinr = (float)Math.sin(roll);
float cosp = (float)Math.cos(pitch);
float cosy = (float)Math.cos(yaw);
float cosr = (float)Math.cos(roll);
Quaternion q = new Quaternion();
q.x = sinr * cosp * cosy - cosr * sinp * siny;
q.y = cosr * sinp * cosy + sinr * cosp * siny;
q.z = cosr * cosp * siny - sinr * sinp * cosy;
q.w = cosr * cosp * cosy + sinr * sinp * siny;
q.normalise();
return q;
}
private float[] getAxisAngle(){
float scale = (float)Math.sqrt(rotation.x * rotation.x + rotation.y * rotation.y + rotation.z * rotation.z);
return new float[]{(float)Math.acos(rotation.w)*2,rotation.x/scale, rotation.y/scale, rotation.z/scale};
}
}
在我的代码中,3D对象从这个类扩展而来,我在每一帧调用rotateZ(1)
结果视频:
首先,为什么要创建Euler旋转,然后将其转换为四元数?如果要使用四元数,请仅使用它们。其次,请提供一个独立完整的示例,说明如何复制您的问题