Java gluLookAt工作不正常

Java gluLookAt工作不正常,java,opengl,graphics,Java,Opengl,Graphics,我正试着让相机跟随一个物体。这是我的相机类的假人: public void setView(GL2 gl){ double[] myPos = MyObject.getCoords(); double[] myTarget = MyObject.getTarget(); double myRotation = MyObject.getRotation(); double viewX = myPos[0]+(myTarget[0]*0.1); doubl

我正试着让相机跟随一个物体。这是我的
相机
类的假人:

public void setView(GL2 gl){
    double[] myPos = MyObject.getCoords();
    double[] myTarget = MyObject.getTarget();
    double myRotation = MyObject.getRotation(); 

    double viewX = myPos[0]+(myTarget[0]*0.1);
    double viewZ = myPos[2]+(myTarget[1]*0.1);
    double camHeight = Terrain.altitude(myPos[0],myPos[2]) + 0.3;
    GLU glu = new GLU();
    gl.glPushMatrix();
    gl.glMatrixMode(GL2.GL_PROJECTION);  
    gl.glLoadIdentity();    

    gl.glDisable(GL2.GL_LIGHT1);        
    //Look at where the object is looking from behind the object
    glu.gluPerspective(100, aspect, 0.1, 20); 
    System.out.println("Teapot pos" + myPos[0] + myPos[2]);
    System.out.println("Eye pos" + (myPos[0]-myTarget[0]) + (myPos[2]-myTarget[1]));
    glu.gluLookAt(-(myPos[0]-myTarget[0]),-camHeight,-(myPos[2]-myTarget[1]), viewX, camHeight, viewZ, 0, 1, 0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();      
    gl.glPopMatrix();
}
public static void draw(GL2 gl) {

    GLUT glut = new GLUT();

    gl.glPushMatrix();
    //Draw a teapot
    gl.glTranslated(-coords[0],-coords[1], -coords[2]);
    gl.glRotated(-Math.toDegrees(rotation),0,1,0);      
    glut.glutSolidTeapot(0.1);
    gl.glPopMatrix();
}
// move object
public void translate(double displacement) {
    coords[0] += displacement*Math.sin(Math.toRadians(rotation));
    coords[2] += displacement*Math.cos(Math.toRadians(rotation));
    coords[1] = Terrain.altitude(coords[0], coords[2]);
}

// Rotate camera by degrees
// Target is the vector where MyObject is facing
public void rotate(double rot) {
    rotation += rot;
    target[0] = -Math.sin(Math.toDegrees(rotation));
    target[1] = -Math.cos(Math.toDegrees(rotation));
}
下面是我的
MyObject
类中的伪代码:

public void setView(GL2 gl){
    double[] myPos = MyObject.getCoords();
    double[] myTarget = MyObject.getTarget();
    double myRotation = MyObject.getRotation(); 

    double viewX = myPos[0]+(myTarget[0]*0.1);
    double viewZ = myPos[2]+(myTarget[1]*0.1);
    double camHeight = Terrain.altitude(myPos[0],myPos[2]) + 0.3;
    GLU glu = new GLU();
    gl.glPushMatrix();
    gl.glMatrixMode(GL2.GL_PROJECTION);  
    gl.glLoadIdentity();    

    gl.glDisable(GL2.GL_LIGHT1);        
    //Look at where the object is looking from behind the object
    glu.gluPerspective(100, aspect, 0.1, 20); 
    System.out.println("Teapot pos" + myPos[0] + myPos[2]);
    System.out.println("Eye pos" + (myPos[0]-myTarget[0]) + (myPos[2]-myTarget[1]));
    glu.gluLookAt(-(myPos[0]-myTarget[0]),-camHeight,-(myPos[2]-myTarget[1]), viewX, camHeight, viewZ, 0, 1, 0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();      
    gl.glPopMatrix();
}
public static void draw(GL2 gl) {

    GLUT glut = new GLUT();

    gl.glPushMatrix();
    //Draw a teapot
    gl.glTranslated(-coords[0],-coords[1], -coords[2]);
    gl.glRotated(-Math.toDegrees(rotation),0,1,0);      
    glut.glutSolidTeapot(0.1);
    gl.glPopMatrix();
}
// move object
public void translate(double displacement) {
    coords[0] += displacement*Math.sin(Math.toRadians(rotation));
    coords[2] += displacement*Math.cos(Math.toRadians(rotation));
    coords[1] = Terrain.altitude(coords[0], coords[2]);
}

// Rotate camera by degrees
// Target is the vector where MyObject is facing
public void rotate(double rot) {
    rotation += rot;
    target[0] = -Math.sin(Math.toDegrees(rotation));
    target[1] = -Math.cos(Math.toDegrees(rotation));
}
要使
MyObject
移动,在
MyObject
类中的代码如下:

public void setView(GL2 gl){
    double[] myPos = MyObject.getCoords();
    double[] myTarget = MyObject.getTarget();
    double myRotation = MyObject.getRotation(); 

    double viewX = myPos[0]+(myTarget[0]*0.1);
    double viewZ = myPos[2]+(myTarget[1]*0.1);
    double camHeight = Terrain.altitude(myPos[0],myPos[2]) + 0.3;
    GLU glu = new GLU();
    gl.glPushMatrix();
    gl.glMatrixMode(GL2.GL_PROJECTION);  
    gl.glLoadIdentity();    

    gl.glDisable(GL2.GL_LIGHT1);        
    //Look at where the object is looking from behind the object
    glu.gluPerspective(100, aspect, 0.1, 20); 
    System.out.println("Teapot pos" + myPos[0] + myPos[2]);
    System.out.println("Eye pos" + (myPos[0]-myTarget[0]) + (myPos[2]-myTarget[1]));
    glu.gluLookAt(-(myPos[0]-myTarget[0]),-camHeight,-(myPos[2]-myTarget[1]), viewX, camHeight, viewZ, 0, 1, 0);
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();      
    gl.glPopMatrix();
}
public static void draw(GL2 gl) {

    GLUT glut = new GLUT();

    gl.glPushMatrix();
    //Draw a teapot
    gl.glTranslated(-coords[0],-coords[1], -coords[2]);
    gl.glRotated(-Math.toDegrees(rotation),0,1,0);      
    glut.glutSolidTeapot(0.1);
    gl.glPopMatrix();
}
// move object
public void translate(double displacement) {
    coords[0] += displacement*Math.sin(Math.toRadians(rotation));
    coords[2] += displacement*Math.cos(Math.toRadians(rotation));
    coords[1] = Terrain.altitude(coords[0], coords[2]);
}

// Rotate camera by degrees
// Target is the vector where MyObject is facing
public void rotate(double rot) {
    rotation += rot;
    target[0] = -Math.sin(Math.toDegrees(rotation));
    target[1] = -Math.cos(Math.toDegrees(rotation));
}

每当我打印坐标而不旋转时,一切都正常。旋转后,它不能正常工作。由于某些原因,摄影机和对象在不同方向上移动。旋转也有点奇怪,因为有时我看不到茶壶。有人能帮我解决这个问题吗?

Afaik,
Math.sin
取弧度而不是度。