gluLookAt()问题 - 旋转

时间:2017-05-05 22:01:45

标签: c++ opengl graphics

这是一个可以轻松显示正在发生的事情的GIF。 http://i.imgur.com/eGCuUN7.gifv

总结:

  • 当播放器静止并旋转时,相机在转动180度后跳回原位置
  • 场景"跳跃"在玩家旋转一点然后前进后
  • 在前进和旋转的同时,旋转似乎表现得正常

我猜想spin()函数是罪魁祸首,但玩家模型旋转得很好;这是"相机"它没有正常工作,所以它可能在我的display()代码中。我不知道会出现什么问题。

无论如何,这里是相关的代码:

void Player::spinLeft()
{
    direction += SPIN_SPEED;
    double radians = direction / 180 * 3.141592654;
    xSpeed += cos(radians);
    ySpeed += sin(radians);

    glutPostRedisplay();
}

void Player::spinRight()
{
    direction -= SPIN_SPEED;
    double radians = direction / 180 * 3.141592654;
    xSpeed += cos(radians);
    ySpeed += sin(radians);

    glutPostRedisplay();
}


void Player::moveForward()
{
    double radians = direction / 180 * 3.141592654;
    xSpeed = cos(radians);
    ySpeed = sin(radians);
    double nextX = x + xSpeed*MOVE_SPEED;
    double nextY = y + ySpeed*MOVE_SPEED;

    if (!gMaze.insideWall(nextX, nextY, size))
    {
        x += xSpeed*MOVE_SPEED;
        y += ySpeed*MOVE_SPEED;
    }

    glutPostRedisplay();
}

标题

class Player
{
    public:
        Player();
        void Draw();
        void spinLeft();
        void spinRight();
        void moveForward();
        void moveBack();

        double getX();
        double getY();
        double getZ();
        double getSize();
        double getDirection();
        double getXSpeed();
        double getYSpeed();

    private:
        double direction;  // in degrees
        double x, y, z;  // in cell coords
        double xSpeed, ySpeed;
        double size;
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST);
    glLoadIdentity();

    // I suspect the problem is here
    double x = gPlayer.getX();
    double y = gPlayer.getY();
    double z = gPlayer.getZ();
    double dx = gPlayer.getXSpeed();
    double dy = gPlayer.getYSpeed();
    double at_x = x + dx; 
    double at_y = y + dy;
    double at_z = z;
    gluLookAt(x, y, z,
                   at_x, at_y, at_z,
                   0, 0, 1);

    gMaze.Draw();

    if (gWDown)
        gPlayer.moveForward();
    if (gADown)
        gPlayer.spinLeft();
    if (gSDown)
        gPlayer.moveBack();
    if (gDDown)
        gPlayer.spinRight(); 

    glutSwapBuffers();
}

先谢谢!

1 个答案:

答案 0 :(得分:0)

您的spinLeftspinRight不正确。你只想要这个:

xSpeed = cos(radians);
ySpeed = sin(radians);

当你向前走时它会正常工作,因为那就是你在那里所做的。