我试图让相机跟随一个物体。这是我Camera
班的假人:
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();
}
这是我MyObject
课程中的虚拟代码:
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();
}
要MyObject
移动,MyObject
类中的代码如下:
// 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));
}
每当我打印坐标而不旋转时,一切似乎都正常。旋转后它无法正常工作。出于某种原因,相机和物体向不同方向移动。旋转也有点奇怪,因为有时我看不到茶壶。任何人都可以帮我解决这个问题吗?