gpt4 book ai didi

java - LibGDX 中的奇怪碰撞

转载 作者:行者123 更新时间:2023-12-01 04:47:04 24 4
gpt4 key购买 nike

我正在尝试在 block 上创建一个简单的球反射。我正在使用 libgdx 框架。

我遇到 block 碰撞问题:

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;


}

问题始于 block 碰撞。看起来它一直在碰撞区域“内部”,并且它始终将我的球方向更改为负方向,而不是仅当球“步入” block 边界时才执行它。

欢迎所有建议!

非常感谢您的帮助!

最佳答案

问题是您在初始化 boundsBall 后永远不会更新它,因此它总是认为它嵌入在 boundsBlock 中。

关于java - LibGDX 中的奇怪碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15650770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com