我正在LIBGDX中开发网球游戏,在此期间我必须检查2位玩家之间的碰撞。当我尝试通过重叠功能检测到球的任何移动时,只要将其放在球员身上,它就会检测到碰撞,但是当我在球的移动过程中检测到它时,它只会检测到两个碰撞中的一个播放器.....
就您的理解而言,当一个球从上到下移动时,当球到达我的游戏的第二名球员时,它没有检测到任何碰撞;通过检测到它的碰撞,该球从第一名球员开始,但在到达第二名时失败。 ...为了检查碰撞,在渲染功能中我添加了一个简单的代码
if(this.getBounds().overlaps(game.white_player.getBounds()))
{
System.out.println("Over Lap.");
}
我已经用矩形包围了球和球员,以检查碰撞。...当我手动将球放在球员上方时,上面编写的代码会检测到碰撞...。但是当我绕球旋转时,我的代码只会检测到它对于第一个玩家,
这是我完整的渲染功能代码。
public void render(float delta) {
game.red_player.update_rec();
game.white_player.update_rec();
if(Gdx.input.isTouched())
{
touchDown(Gdx.input.getX(), Gdx.input.getY(), 1,Input.Buttons.LEFT);
}
if(up==true && down!=true && game.red_player.getBounds().overlaps(this.getBounds()))
{
shape_y+=1;
going_up+=1;
if(going_up > 720)
{
//going_up=200;
//shape_y=200;
System.out.println("yes");
up = false;
down = true;
}
}
if(down == true)
{
shape_y-=1;
going_up-=1;
}
if(shape_y<200)
{
System.out.println("Lallaa");
up=true;
down=false;
//going_up=201;
//sshape_y=201;
}
if(this.getBounds().overlaps(game.white_player.getBounds()) && shape_y==605)
{
System.out.println("Over Lap..");
//up = true;
//down = false;
}
System.out.println(shape_x+" "+shape_y);
game.batch.begin();
game.background.actor.draw(game.batch);
game.red_player.actor.draw(game.batch);
game.white_player.actor.draw(game.batch);
game.batch.end();
stage.draw();
shapes.begin(ShapeType.Filled);
shapes.setColor(1,1,0,0);
shapes.circle(shape_x, shape_y,shape_width, shape_height);
shapes.end();
shapes.begin(ShapeType.Line);
shapes.setColor(0,0,0,0);
shapes.circle(shape_x, shape_y,shape_width+1, shape_height);
shapes.end();
shapes.begin(ShapeType.Line);
shapes.setColor(0,0,0,0);
shapes.rect(game.red_player.position.x,
game.red_player.position.y,game.red_player.width,
game.red_player.height);
shapes.end();
shapes.begin(ShapeType.Line);
shapes.setColor(0,0,0,0);
shapes.rect(game.white_player.position.x, game.white_player.position.y,
game.white_player.width, game.white_player.height);
shapes.end();
shapes.begin(ShapeType.Line);
shapes.setColor(0,0,0,0);
shapes.rect(shape_x-10, shape_y-10, shape_width*2, shape_height/2);
shapes.end();
}
忽略所有未定义的函数...
最佳答案
您可以尝试使用ShapeRenderer类绘制玩家和球的边界。也许您忘了更新边界坐标以匹配玩家或球坐标?
如何使用ShapeRenderer的示例:
shapeRenderer.begin(ShapeType.Line);
shapeRenderer.setColor(Color.CYAN);
shapeRenderer.rect(player.getBounds().x, player.getBounds().y, player.getBounds().width,
player.getBounds().getHeight());
shapeRenderer.end();
如果一切正常,请尝试将冲突代码重写为更简单的内容:
if(ball.bounds.overlaps(anyPlayer.bounds)) {
if(up) {
up = false;
down = true;
System.out.println("Over Lap..");
}
else {
up = true;
down = false;
System.out.println("Over Lap..");
}
}