2013-03-27 78 views
1

我試圖在一個塊上創建一個簡單的球反射。我正在使用libgdx框架。LibGDX奇怪的碰撞

我有問題阻止碰撞:

public class World { 

public Ball ball; 
public Bar bar; 
public Block block; 
public BoundingBox boundsBlock,boundsBall; 

public World(){ 
    initialize(); 
} 

public void initialize(){ 
    ball = new Ball(10/2, 6); 
    bar = new Bar(4,1); 
    block = new Block(4,7); 
    boundsBlock = new BoundingBox(new Vector3(block.position.x,block.position.y,0),new Vector3(block.position.x+2,block.position.y+1,0)); 
    boundsBall = new BoundingBox(new Vector3(ball.position.x,ball.position.y,0),new Vector3(ball.position.x+1,ball.position.y+1,0)); 
} 

public void update(float delta){ 
    ball.position.add(ball.direction.x*delta,ball.direction.y * delta,0); 

    //Right Collision 
    if(ball.position.x + 0.25f >9.65f){ 
     ball.position.x = 9.65f - 0.25f; 
     ball.direction.x = -ball.VELOCITY; 
    } 


    //Top Collision 
    if (ball.position.y + 0.25f > 14.65f){ 
     ball.position.y = 14.65f - 0.25f; 
     ball.direction.y = -ball.VELOCITY; 
    } 

    //Left Collision 
    if(ball.position.x - 0.25f < 0.35f){ 
     ball.position.x = 0.35f + 0.25f; 
     ball.direction.x = ball.VELOCITY; 
    } 

    //Bar collision 
    if(ball.position.y < 1+0.1f){ 
     if(ball.position.x > bar.position.x && ball.position.x < bar.position.x + 2){ 
      ball.position.y = 1+ 0.25f; 
      ball.direction.y = ball.VELOCITY; 
     } 

    } 

    //Block Collision 
    if(boundsBlock.contains(boundsBall)){ 

     ball.direction.x = -ball.VELOCITY; 
     ball.direction.y = -ball.VELOCITY; 


    } 

問題開始塊碰撞。它似乎不斷地在碰撞區域「內部」,並且它總是將我的球方向改變爲負值,而不是在球進入障礙邊界時才執行。

歡迎您提出任何建議!

非常感謝您的幫助!

回答

0

問題是你在初始化之後永遠不會更新boundsBall,所以它總是認爲它嵌入在boundsBlock中。