Overlap()函数在LIBGDX中不起作用

时间:2013-11-26 22:59:10

标签: java android eclipse libgdx

我正在LIBGDX开发一个网球比赛,我必须检查两名球员之间的碰撞。当我尝试通过重叠功能检测到它与球的任何运动,只需将它放在球员上它确实检测到碰撞,但是当我在球的运动过程中这样做时,它只检测到两个中的一个碰撞玩家..... 为了你的理解只是Image一个球从上到下移动,当球到达我的游戏的第二个玩家时,它没有检测到任何碰撞,Ball通过检测到它的碰撞从第一个玩家开始,但是当到达第二个玩家时失败。 ...为了检查碰撞,在渲染功能中我添加了一个简单的代码

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();

}

忽略所有未定义的功能......

1 个答案:

答案 0 :(得分:1)

你可以尝试使用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..");
    }
}