这是一个可以轻松显示正在发生的事情的GIF。 http://i.imgur.com/eGCuUN7.gifv
总结:
我猜想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();
}
先谢谢!
答案 0 :(得分:0)
您的spinLeft
和spinRight
不正确。你只想要这个:
xSpeed = cos(radians);
ySpeed = sin(radians);
当你向前走时它会正常工作,因为那就是你在那里所做的。