我一直在使用 LibGDX 框架用 Java 开发这款打砖 block 游戏。这是我的第一个java游戏。我对其他语言有一点经验,但遇到了一些麻烦。我已经设置了碰撞检测并且它在大多数情况下都有效。球有时会弹向错误的方向,有时会撞到不应撞到的方 block 。我已经进行了一些搜索,但很难将其转化为我的游戏。
我的代码真的很糟糕,因为球只以 45 度角移动。不太现实,这将是我修复此问题后的下一步。
public void checkBrickCollision()
{
for (int i=0;i<level.brickCount;i++) {
if (level.bricks[i].GetVisible() == true) {
if (level.bricks[i].getRect().overlaps(ball.getRect()))
{
int xd = (int) Math.abs( (ball.ballRect.x + ball.ballRect.width - level.bricks[i].brickRect.x - level.bricks[i].getRect().width) /2 );
int yd = (int) Math.abs( (ball.ballRect.y + ball.ballRect.height - level.bricks[i].brickRect.y - level.bricks[i].getRect().height) /2 );
if (xd > yd)
{
// Collision on top or bottom, reverse y velocity
ball.ballSpeedY = -ball.ballSpeedY;
Score score = new Score(level.bricks[i].getScore(),(int)level.bricks[i].brickRect.x,(int)level.bricks[i].brickRect.y);
scoreList.add(score);
level.bricks[i].Destroy();
System.out.println("Top/Bottom");
return;
}
if (yd > xd)
{
// Collision on left or right, reverse x velocity
ball.ballSpeedX = -ball.ballSpeedX;
Score score = new Score(level.bricks[i].getScore(),(int)level.bricks[i].brickRect.x,(int)level.bricks[i].brickRect.y);
scoreList.add(score);
level.bricks[i].Destroy();
System.out.println("Sides");
return;
}
if (xd == yd)
{
// Collision on corners, reverse both
ball.ballSpeedX = -ball.ballSpeedX;
ball.ballSpeedY = -ball.ballSpeedY;
Score score = new Score(level.bricks[i].getScore(),(int)level.bricks[i].brickRect.x,(int)level.bricks[i].brickRect.y);
scoreList.add(score);
level.bricks[i].Destroy();
System.out.println("Corners");
return;
}
}
}
}
}
最佳答案
我建议你将你的 level.bricks[] 数组放在 ArrayList 列表中,这样你就可以便宜地从列表中删除()和/或销毁()它们苍蝇,避免在 level.bricks 数组中的每次迭代中检查空值(除非您的设置不介意数组中的空值)。另外,它将使您不必在每个渲染周期期间检查所有砖 block 的每次迭代的数组大小...
List<Brick> brickList = new ArrayList<Brick>();
for (brick: level.bricks) {
brickList.add( new Brick(brick) );
}
//-------or-------
//if the brick object is a Sprite
for (brick: level.bricks) {
brickList.add(brick);
}
我认为错误的砖 block 被检测为“击中”的问题与尝试补偿球纹理和边界为矩形有关。我建议使用 com.badlogic.gdx.math.Circle 类来定义球的边界。以下是 com.badlogic.gdx.math.Intersector 的脏自定义碰撞包装器,它应该与您上面尝试的操作一起工作。它假设球的视觉像素延伸到纹理的边缘,并且球纹理是方形的:
public class Collider {
private String bounceType = "";
private boolean vertical = false;
private boolean horizontal = false;
// Segment Vertices of the Rectangle
private Vector2 leftStart = new Vector2();
private Vector2 leftEnd = new Vector2();
private Vector2 topStart = new Vector2();
private Vector2 topEnd = new Vector2();
private Vector2 rightStart = new Vector2();
private Vector2 rightEnd = new Vector2();
private Vector2 bottomStart = new Vector2();
private Vector2 bottomEnd = new Vector2();
private Vector2 center = new Vector2();
private Circle ballBounds = new Circle();
// Pointers passed once during construction
private Ball ball;
private List<Brick> brickList;
private List<Score> scoreList;
/**
* Constructor requires that ball and brickList pointers are given.
* <p>
* Runs the updateBallBounds() method after assigning the parameters
*
*@param ball points to the ball to be used for the collision calculations
*@param brickList points to a list of brick objects to check against
*@param scoreList points to a list of score objects to track the score
*/
public Collider(Ball ball, List<Brick> brickList, List<Score> scoreList) {
this.ball = ball;
this.brickList = brickList;
updateBallBounds(this.ball, this.ballBounds);
}
/**
* Sets the position and radius of the bounding circle
* for the given ball with a rectangular shape in order
* to prepare it for bounds checking.
*
* @param ball The ball object to
* @param bounds The circle object that will store the bounds information
*/
private void updateBallBounds(Ball ball, Circle bounds) {
bounds.set( (ball.ballRect.x + (ball.ballRect.width/2)), //Center x pos
(ball.ballRect.y + (ball.ballRect.height/2)), //Center y pos
(ball.ballRect.width / 2) ); //Radius of ball
}
/**
* Builds the start and end Vectors for each of the segments
* of the provided rectangle. Also builds the center Vector for
* the ball.
* <p>
* Used to prepare for finding which segments of the rectangle the
* ball is intersecting
*
* @param brickRect The rectangle to process the line segments from
*/
private setVectors(Rectangle brickRect) {
leftStart.set(brickRect.x, brickRect.y);
leftEnd.set(brickRect.x, brickRect.height);
topStart.set(brickRect.x, brickRect.height);
topEnd.set(brickRect.width, brickRect.height);
rightStart.set(Poyline( brickRect.width, brickRect.y);
rightEnd .set(brickRect.width, brickRect.height);
bottomStart.set(brickRect.x, brickRect.y);
bottomEnd.set(brickRect.width, brickRect.y);
center.set(ballBounds.x, ballBounds.y);
}
/**
* Finds bricks in the list that the ball is currently
* colliding with.
* <p>
* For every rectangle that the ball is currently colliding with,
* the method calls the setVectors() method to prepare the start-end
* vertices for the processCollision() method.
* <p>
* WARNING: this may not handle multiple collision very well. It
* should work, but you most likely will not give very good results
* on multiple brick hit detected. You should think about including
* a break statement after the first collision is detected and let
* the Collider find an additional collision during the next render()
* call.
*/
public void detectCollisions() {
updateBallBounds(ball, ballBounds);
for (brick: brickList) {
if( Intersector.overlaps(ballBounds, brick.brickRect) ) {
setVectors(brick.brickRect);
processCollision(brick, brick.brickRect);
//break;
}
}
}
/**
* Detects how to handle the collision based on the segments being hit.
*
*@param brick the brick found by detectCollision() method. will be
* destroyed after collision is processed
*/
public void processCollision(Brick brick) {
if ( Intersector.intersectSegmentCircle( topStart, topEnd,
center * center,
ballBounds.radius ) ||
Intersector.intersectSegmentCircle( bottomStart, bottomEnd,
center * center,
ballBounds.radius )) {
vertical = true;
}
if ( Intersector.intersectSegmentCircle( leftStart, leftEnd,
center * center,
ballBounds.radius ) ||
Intersector.intersectSegmentCircle( rightStart, rightEnd,
center * center,
ballBounds.radius ) ) {
horizontal = true;
}
// The following could return the value to a calling entity.
// Then the game logic of what to do here would be decoupled.
if (vertical && horizontal) {
bounceType = "CORNER"
} else if (vertical && !horizontal) {
bounceType = "VERTICAL"
} else if () {
bounceType = "HORIZONTAL"
} else {
// The game blows up...
}
switch (bounceType) {
case "VERTICAL":
ball.ballSpeedY = -ball.ballSpeedY;
break;
case "HORIZONTAL":
ball.ballSpeedX = -ball.ballSpeedX;
break;
case "CORNER":
ball.ballSpeedY = -ball.ballSpeedY;
ball.ballSpeedX = -ball.ballSpeedX;
break;
default: // Try not to blow up the game...
break;
}
Score score = new Score(brick.getScore(), brick.x, brick.y)
scoreList.add(score);
brickList.remove(brick);
brick.destroy();
}
}
我确信这里有错误,它没有经过测试。除了前面的假设之外,它不会阻止砖 block 在数组中渲染(除非 destroy() 处理这个问题,并且希望不是通过在渲染时查找空值...)。
基本结构可以做得更好......
- 用一些 except,尝试,捕捉东西。
- 您可以添加更多类型化构造函数和方法来覆盖更多形状。
- 空间分区可以添加一个方法来映射静态边界 build 。然后你的动态对象可以记录它们的分区 当前占用且只查询对方的碰撞结果 记录在这些分区列表中的对象。
- 我认为这个类的实现应该解耦并处理 其他地方。
- 这还可以让您创建返回/触发的碰撞类或事件。
希望这有帮助。
<小时/>我还建议您研究一下 Box2D。您的要求似乎很简单,可以轻松学习其使用方法。 This page可以向您展示如何配置它并与 LibGDX 一起使用。这将使您能够快速在物体上实现 Material 属性、球旋转、速度变化、角度反弹......以及自动魔法的各种优点,这将为您的大脑节省一些周期。物理引擎会为您完成所有数学计算。您只需初始化并执行它,监听事件(例如碰撞)。
祝您游戏顺利。
关于java - 我该如何修复这个碰撞检测 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16410184/