我试图在一个块上创建一个简单的球反射。我正在使用libgdx框架。
我遇到Block Collision的问题:
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 :(得分:0)
问题是您在初始化后不会更新boundsBall
,因此它始终认为它已嵌入boundsBlock
。