2

我正在使用 c++、OpenGL、SOIL 图形库和 Microsoft Visual c++ 2010 Express。我添加了一个视频来演示这个问题
http://www.youtube.com/watch?v=7F7YDN0zbc4&feature=youtu.be

这是我目前的问题;

我们的舞台或环境是 3D[x][y][z]。当我们在 x 方向移动并且在 y 方向移动时不会出现问题(我们不允许在 y 方向移动步数,我们可以跳跃)。但是一旦我们到达 z=70,相机就会转过来。因此,如果在 x 和 y 为 1 时向 z=70 移动,让我们考虑我们在 z 方向上的朝向 +,但是一旦我们达到 70,我们的相机会转身,但我们的朝向仍然是 +。我知道这一点,因为我有一个 cout << 告诉我方向和坐标系位置。

cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos()  << ", " << player.getZPos() << ", " << player.getFacing() << "\n";

我不确定我需要在这里输入什么样的代码,所以我应该在这里输入什么样的函数。

//这是我们的起始位置

void demo(){
     gameState = 1;
     player.setPos(4,1,15);
     player.setFacing(3);
     setPos();
     cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing();
}

//这就是我们设置位置的方式

void setPos(){
     currentXPos = player.getXPos()+(player.getXWidth()/2);
     currentYPos = player.getYPos()+(player.getYHeight());
     currentZPos = player.getZPos()+(player.getZDepth()/2); 
     currentYFace = currentYPos; 
     if(player.getFacing() == 0){
         currentXFace = MAPWIDTH;
         currentZFace = currentZPos;
     }else if(player.getFacing() == 2){
         currentXFace = 0;
         currentZFace = currentZPos;
     }else if(player.getFacing() == 3){
         currentXFace = currentXPos;
         currentZFace = MAPDEPTH;
     }else{
         currentXFace = currentXPos;
         currentZFace = 0;
     }
}

//这是我们的显示函数

 void /*GraphicsEngine::*/display(){
      glClearColor (0.0, 0.0, 0.0, 1.0);
       glLoadIdentity ();
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glShadeModel(GL_FLAT);
       glClearDepth(1.0);
      glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      setPos();
      gluLookAt(currentXPos,currentYPos,currentZPos,currentXFace,currentYFace,currentZFace,0,1,0);
      generateMap();
      graphicsFloor();
      int i = 0;
      if(projectiles.size()>0){
         for(i= 0; i< projectiles.size(); i++){
             arrow(projectiles.operator[](i).getXPos(),projectiles.operator[](i).getYPos(),projectiles.operator[](i).getZPos(),0.5,BLOCKWIDTH);
         }
         projectileMotion();
         player.advance();
         glutPostRedisplay();
      }else if(player.advance()){
          glutPostRedisplay();
      }
      glFlush();
      glutSwapBuffers();
 }

//这是我们的重塑功能

void reshape(int w, int h){
        glViewport(0, 0, (GLsizei) w, (GLsizei) h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 200.0);
        glMatrixMode(GL_MODELVIEW);
            glTranslatef(0.0, 0.0, -3.6);
        glLoadIdentity();
}

//这是我们控制用户使用键盘的方式,我不会发布所有其他情况,因为它们是空白的

 void userAction(unsigned int userInput){
int i;
switch(userInput){
    case 1:
        player.turnLeft();
        glutPostRedisplay();
        break;
    case 2:
        player.turnRight();
        //apply graphics
        glutPostRedisplay();
        break;
    case 3:
        if(player.getFacing() == 0){
            player.setPos(player.getXPos()-1,player.getYPos(),player.getZPos());
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }else if(player.getFacing() ==3){
            player.setPos(player.getXPos(),player.getYPos(),player.getZPos()-1);
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }else if(player.getFacing() == 2){
            player.setPos(player.getXPos()+1,player.getYPos(),player.getZPos());
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }else{
            player.setPos(player.getXPos(),player.getYPos(),player.getZPos()+1);
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }
        //apply graphics
        glutPostRedisplay();
        break;
    case 4:
        if(player.getFacing() == 0){
            player.setPos(player.getXPos()+1,player.getYPos(),player.getZPos());
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }else if(player.getFacing() ==3){
            player.setPos(player.getXPos(),player.getYPos(),player.getZPos()+1);
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }else if(player.getFacing() == 2){
            player.setPos(player.getXPos()-1,player.getYPos(),player.getZPos());
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }else{
            player.setPos(player.getXPos(),player.getYPos(),player.getZPos()-1);
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }
        //apply graphics
        glutPostRedisplay();
        break;
    case 5:
        if(player.getFacing() == 0){
            player.setPos(player.getXPos(),player.getYPos(),player.getZPos()-1);
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }else if(player.getFacing() == 2){
            player.setPos(player.getXPos(),player.getYPos(),player.getZPos()+1);
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }else if(player.getFacing() == 3){
            player.setPos(player.getXPos()+1,player.getYPos(),player.getZPos());
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }else{
            player.setPos(player.getXPos()-1,player.getYPos(),player.getZPos());
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }
        glutPostRedisplay();
        break;
    case 6:
        if(player.getFacing() == 0){
            player.setPos(player.getXPos(),player.getYPos(),player.getZPos()+1);
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }else if(player.getFacing() == 2){
            player.setPos(player.getXPos(),player.getYPos(),player.getZPos()-1);
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " <<player.getFacing() << "\n";
        }else if(player.getFacing() == 1){
            player.setPos(player.getXPos()+1,player.getYPos(),player.getZPos());
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }else{
            player.setPos(player.getXPos()-1,player.getYPos(),player.getZPos());
            cout << "The player is at position: " << player.getXPos() << ", " << player.getYPos() << ", " << player.getZPos() << ", " << player.getFacing() << "\n";
        }
        glutPostRedisplay();
        break;
 }

//这就是我们设置人脸的方式

 void PlayerCharacter::setFacing(unsigned short newFace){
      facing = newFace % 4;
 }

//这是位置

bool PlayerCharacter::setPos(int x, int y, int z){
positionData pTemp;
pTemp.x = x;
pTemp.y = y;
pTemp.z = z;
pTemp.id = p.id;
pTemp.w = p.w;
pTemp.h = p.h;
pTemp.d = p.d;
pTemp.type = p.type;
 if(/*phys->*/openBlocks(pTemp) && /*phys->*/validPosition(pTemp)==0){
      p.x=x;
      p.y=y;
      p.z=z;
      /*phys->*/updatePlacements(p);
      return true;
 }else{
      return false;
 }
}
4

1 回答 1

0

currentZPos = player.getZPos(); 这样做可以解决问题

于 2013-03-31T21:57:12.817 回答