1

我试图在一个块上创建一个简单的球反射。我正在使用 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;


    }

问题始于块碰撞。似乎它一直在碰撞区域“内部”,并且它一直将我的球方向改变为负方向,而不是仅在球边界“进入”块边界时执行它。

欢迎所有建议!

非常感谢你的帮助!

4

1 回答 1

0

问题是你在初始化它之后永远不会更新boundsBall,所以它总是认为它嵌入在boundsBlock.

于 2013-03-27T02:59:39.633 回答