glulookat() - 旋转问题



总结:

  • 当玩家静止并旋转时,摄像机在进行约180度转弯后跳回原始位置
  • 场景"跳跃"玩家旋转一点之后,然后向前移动
  • 同时向前前进和旋转时,旋转似乎是正确的

我猜想spin()功能是罪魁祸首,但是玩家模型却很好。这是"相机"这无法正常工作,因此可能是在我的display()代码中。我不知道怎么了。

这是一个很容易显示正在发生的事情的GIF。http://i.imgur.com/egcuun7.gifv

这是相关代码:

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();
}

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

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

当您前进时它正常工作,因为那是您在那里所做的。

相关内容

  • 没有找到相关文章

最新更新