0

这是我的问题:我正在用 android 制作台球游戏,我想让相机围绕桌子的中心自由旋转。问题是,当我停止移动时,glulookat 似乎会自行重置,因为我只能一遍又一遍地看到相同的东西。如果有人知道如何解决这个问题或另一种方法来做我想做的事情,我将不胜感激

这是我的渲染器类:

public void onDrawFrame(GL10 gl) {


    // Redraw background color
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    // Set GL_MODELVIEW transformation mode
    gl.glMatrixMode(GL10.GL_MODELVIEW);

    gl.glLoadIdentity();   // reset the matrix to its default state



    // Screen position to angle conversion
    theta = (float) ((360.0/screenHeight)*mMoveY*3.0);    //3.0 rotations possible
    phi = (float) ((360.0/screenWidth)*mMoveX*3.0);

    // Spherical to Cartesian conversion.  
    // Degrees to radians conversion factor 0.0174532
    eyeX = (float) (r * Math.sin(theta/**0.0174532*/) * Math.sin(phi/**0.0174532*/));
    eyeY = (float) (r * Math.cos(theta/**0.0174532*/));
    eyeZ = (float) (r * Math.sin(theta/**0.0174532*/) * Math.cos(phi/**0.0174532*/));

    // Reduce theta slightly to obtain another point on the same longitude line on the sphere.

    eyeXtemp = (float) (r * Math.sin(theta/**0.0174532*/-dt) * Math.sin(phi/**0.0174532*/));
    eyeYtemp = (float) (r * Math.cos(theta/**0.0174532*/-dt));
    eyeZtemp = (float) (r * Math.sin(theta/**0.0174532*/-dt) * Math.cos(phi/**0.0174532*/));

    // Connect these two points to obtain the camera's up vector.
    upX=eyeXtemp-eyeX;
    upY=eyeYtemp-eyeY;
    upZ=eyeZtemp-eyeZ;

    // Set the view point
    GLU.gluLookAt(gl,       eyeX, eyeY, eyeZ,       0,0,0,      upX, upY, upZ);

这是我的活动类中的 onTouch 方法:

 public boolean onTouchEvent(MotionEvent e) {

    float x = e.getX();
    float y = e.getY();

    switch (e.getAction()) {
    case MotionEvent.ACTION_MOVE:

        float dx = x - mPreviousX;
        float dy = y - mPreviousY;

        // reverse direction of rotation above the mid-line
        /*if (y > getHeight() / 2) {
            dx = dx * -1 ;
        }

        // reverse direction of rotation to left of the mid-line
        if (x < getWidth() / 2) {
            dy = dy * -1 ;
        }*/

        mRenderer.mMoveX = dx * TOUCH_SCALE_FACTOR/100;
        mRenderer.mMoveY = dy * TOUCH_SCALE_FACTOR/100;

        requestRender();
    }

    mPreviousX = x;
    mPreviousY = y;
    return true;
} 
4

1 回答 1

0

当您进行数学运算时,您正在使用mMoveXand mMoveY。看起来您应该将它们添加到其他更持久的 x/y 变量中。

就像是:

mCamX += mMoveX;
mCamY += mMoveY;
theta = (float) ((360.0/screenHeight)*mCamY*3.0);    //3.0 rotations possible
phi = (float) ((360.0/screenWidth)*mCamX*3.0);
于 2012-06-22T04:12:55.737 回答