【问题标题】:Problems with gluLookAt() - spinninggluLookAt() 的问题 - 旋转
【发布时间】:2017-05-05 22:01:45
【问题描述】:

这是一个 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();
}

提前致谢!

【问题讨论】:

  • 致投票赞成 MVCE 的人 - 图形代码相当复杂。我不认为它比这更小。
  • 你确实做了很多不必要的数学运算,把你的方向保持在度数而不是弧度上,顺便说一句。

标签: c++ opengl graphics


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2011-11-16
    相关资源
    最近更新 更多