jl8n
jl8n

Reputation: 53

Problems with gluLookAt() - spinning

To summarize:

I would guess that the spin() functions are the culprits, but the player model spins just fine; it's the "camera" that isn't working properly, so it's probably in my display() code. I have no idea what would be wrong though.

Here's a gif that easily shows what's happening. http://i.imgur.com/eGCuUN7.gifv

Here's the relevant code:

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

Header:

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

Upvotes: 0

Views: 56

Answers (1)

Donnie
Donnie

Reputation: 46923

Your spinLeft and spinRight are incorrect. You just want this:

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

It works correctly when you're walking forward because that's what you did there.

Upvotes: 1

Related Questions