2016-11-28 177 views
0

我有一個小迷宮遊戲。它看起來像這樣:http://i.imgur.com/VEy54Lc.pngOpenGL-es通過紋理移動攝像頭

但是我不明白怎麼就不能讓相機通過箱子

搬到這裏是代碼:

public class EulerCameraTest extends GLGame { 

//@Override 
public Screen getStartScreen() { 
    return new EulerCameraScreen(this); 
} 

class EulerCameraScreen extends GLScreen {  
    Texture crateTexture;  
    Vertices3 cube; 
    PointLight light; 
    AmbientLight aLight; 
    EulerCamera camera; 
    Texture buttonTexture; 
    SpriteBatcher batcher; 
    Camera2D guiCamera; 
    TextureRegion buttonRegion; 
    Vector2 touchPos; 
    float lastX = -1; 
    float lastY = -1; 

    public EulerCameraScreen(Game game) { 
     super(game);  

     crateTexture = new Texture(glGame, "crate.png", true); 
     cube = createCube(); 
     light = new PointLight(); 
     light.setPosition(3, 3, -3); 
     aLight = new AmbientLight(); 
     aLight.setColor(128, 128, 128, 255); 
     camera = new EulerCamera(67, glGraphics.getWidth()/(float)glGraphics.getHeight(), 0.1f, 100); 
     camera.getPosition().set(0, 0, 3); 

     buttonTexture = new Texture(glGame, "button.png"); 
     batcher = new SpriteBatcher(glGraphics, 1); 
     guiCamera = new Camera2D(glGraphics, 480, 320); 
     buttonRegion = new TextureRegion(buttonTexture, 0, 0, 64, 64); 
     touchPos = new Vector2(); 
    } 

    private Vertices3 createCube() { 
      float[] vertices = { -0.5f, -0.5f, 0.5f, 0, 1, 0, 0, 1, 
            0.5f, -0.5f, 0.5f, 1, 1, 0, 0, 1, 
            0.5f, 0.5f, 0.5f, 1, 0, 0, 0, 1, 
           -0.5f, 0.5f, 0.5f, 0, 0, 0, 0, 1, 

            0.5f, -0.5f, 0.5f, 0, 1, 1, 0, 0, 
            0.5f, -0.5f, -0.5f, 1, 1, 1, 0, 0, 
            0.5f, 0.5f, -0.5f, 1, 0, 1, 0, 0, 
            0.5f, 0.5f, 0.5f, 0, 0, 1, 0, 0, 

            0.5f, -0.5f, -0.5f, 0, 1, 0, 0, -1, 
           -0.5f, -0.5f, -0.5f, 1, 1, 0, 0, -1, 
           -0.5f, 0.5f, -0.5f, 1, 0, 0, 0, -1, 
            0.5f, 0.5f, -0.5f, 0, 0, 0, 0, -1, 

           -0.5f, -0.5f, -0.5f, 0, 1, -1, 0, 0, 
           -0.5f, -0.5f, 0.5f, 1, 1, -1, 0, 0, 
           -0.5f, 0.5f, 0.5f, 1, 0, -1, 0, 0, 
           -0.5f, 0.5f, -0.5f, 0, 0, -1, 0, 0, 

           -0.5f, 0.5f, 0.5f, 0, 1, 0, 1, 0, 
            0.5f, 0.5f, 0.5f, 1, 1, 0, 1, 0, 
            0.5f, 0.5f, -0.5f, 1, 0, 0, 1, 0, 
           -0.5f, 0.5f, -0.5f, 0, 0, 0, 1, 0, 

           -0.5f, -0.5f, -0.5f, 0, 1, 0, -1, 0, 
            0.5f, -0.5f, -0.5f, 1, 1, 0, -1, 0, 
            0.5f, -0.5f, 0.5f, 1, 0, 0, -1, 0, 
           -0.5f, -0.5f, 0.5f, 0, 0, 0, -1, 0 }; 
      short[] indices = { 0, 1, 2, 2, 3, 0, 
           4, 5, 6, 6, 7, 4, 
           8, 9, 10, 10, 11, 8, 
           12, 13, 14, 14, 15, 12, 
           16, 17, 18, 18, 19, 16, 
           20, 21, 22, 22, 23, 20, 
           24, 25, 26, 26, 27, 24 }; 
      Vertices3 cube = new Vertices3(glGraphics, vertices.length/8, indices.length, false, true, true); 
      cube.setVertices(vertices, 0, vertices.length); 
      cube.setIndices(indices, 0, indices.length); 
      return cube; 
     } 

    //@Override 
    public void resume() { 
     crateTexture.reload(); 
    } 

    //@Override 
    public void update(float deltaTime) { 
     game.getInput().getTouchEvents(); 
     float x = game.getInput().getTouchX(0); 
     float y = game.getInput().getTouchY(0); 
     guiCamera.touchToWorld(touchPos.set(x, y)); 


     if(game.getInput().isTouchDown(0)) { 
      if(touchPos.x < 64 && touchPos.y < 64) { 
       Vector3 direction = camera.getDirection(); 
       direction.y = 0; 
       camera.getPosition().add(direction.mul(deltaTime)); 
      } else {  
       if(lastX == -1) { 
        lastX = x; 
        lastY = y; 

       } else {        
        camera.rotate((x - lastX)/10, (y - lastY)/10);     
        lastX = x; 
        lastY = y; 
       } 
      } 
     } else { 
      lastX = -1; 
      lastY = -1; 
     } 
    } 

    //@Override 
    public void present(float deltaTime) { 
     GL10 gl = glGraphics.getGL();   
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 
     gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight()); 

     camera.setMatrices(gl); 

     gl.glEnable(GL10.GL_DEPTH_TEST); 
     gl.glEnable(GL10.GL_TEXTURE_2D); 
     gl.glEnable(GL10.GL_LIGHTING); 

     crateTexture.bind(); 
     cube.bind();   
     light.enable(gl, GL10.GL_LIGHT0); 
     aLight.enable(gl); 

     int[] matrix = new int[] 
       {0,0,0,1,0, 
       0,1,1,1,0, 
       0,1,0,0,0 
     }; 
     int step = 0; 
     for(int z = 0; z >= -4/2; z-=2/2) 
     { 
      for(int x = -4/2; x <=4/2; x+=2/2) 
      { 
       if(matrix[step++] == 1) 
        continue; 
       gl.glPushMatrix(); 
       gl.glTranslatef(x, 0, z); 
       cube.draw(GL10.GL_TRIANGLES, 0, 6 * 2 * 3); 
       gl.glPopMatrix(); 
      } 
     } 

     cube.unbind(); 

     gl.glDisable(GL10.GL_LIGHTING); 
     gl.glDisable(GL10.GL_DEPTH_TEST);   

     gl.glEnable(GL10.GL_BLEND); 
     gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); 

     guiCamera.setViewportAndMatrices(); 
     batcher.beginBatch(buttonTexture); 
     batcher.drawSprite(32, 32, 64, 64, buttonRegion); 
     batcher.endBatch(); 

     gl.glDisable(GL10.GL_BLEND); 
     gl.glDisable(GL10.GL_TEXTURE_2D); 
    } 
} 
} 

我相信本()方法是我應該尋找的地方。有沒有人有一個想法如何讓相機不能通過盒子?

回答

0

我相信update(float deltaTime)是你應該看的地方。如果相機不應該通過盒子,那麼在打開盒子時不應該移動它。您需要創建一個良好的系統來控制場景周圍的移動,並可能將某些值轉換爲場景座標系。更一般的你應該將物理引擎與圖形引擎分開。

想想你有一個代表你的場景,具有如數據對象:

int width; // The width of the scene 
int height; // The height of the scene 
bool boxes[width][height]; // The grid of boxes. If the segment is true then a box should be drawn 
float playerPositionX; 
float playerPositionY; 

現在您的Draw方法看起來是這樣的:

for(int x = 0; x < scene.width; x++) 
     { 
      for(int y = 0; y < scene.height; y++) 
      { 
       if(!scene.boxes[y][x]) 
        continue; 
       gl.glPushMatrix(); 
       gl.glTranslatef(x*sceneScale, 0, y*sceneScale); // Scene scale is just some constant to make things as large as you want (a box size most likely in your case). 
       cube.draw(GL10.GL_TRIANGLES, 0, 6 * 2 * 3); 
       gl.glPopMatrix(); 
      } 
     } 

但後來您update方法,你能而只是簡單地打電話:

scene.movePlayer(x, y); 

所以sc烯有一個方法

public void movePlayer(float x, float y) { 
    // first get the current position indices: 
    int previousX = (int)playerPositionX; 
    int previousY = (int)playerPositionY; 

    // And the target position: 
    int targetX = (int)(playerPositionX+x); 
    int targetY = (int)(playerPositionY+y); 

    // Now check if you have collisions 
    if(targetX < 0 || targetX >= width || boxes[targetX][previousY]) { // The previousY is NOT a bug 
     // We hit the wall in X direction so do not move it 
     x = .0f; 
    } 
    if(targetY < 0 || targetY >= height || boxes[previousX][targetY]) { // The previousX is NOT a bug 
     // We hit the wall in Y direction so do not move it 
     y = .0f; 
    } 

    playerPositionX += x; 
    playerPositionY += y; 
} 

現在,所有你需要在update做的是設置根據場景的不同玩家位置攝像機的位置(再申請需要諸如場景比例的任何因素)。