LibGDX矩形和圆形之间的交集

时间:2017-03-31 10:35:57

标签: java libgdx collision-detection intersection

在我自上而下的游戏中,我使用accelerometer来移动我的角色,现在我正在研究矩形和圆圈之间的碰撞检测猫和球,但我的代码不起作用我希望猫在他击球时停下来,当我将手机向左右移动时再次制作动画。猫是我的球员,球是障碍。

这是我的代码

// Load the sprite sheet as a texture
    cat = new Texture(Gdx.files.internal("cat.png"));
    catsprite = new Sprite(cat);
    player = new Rectangle();
    player.width = 20;
    player.height = 80;
    playerX = 300;
    playerY = 0;

    basketball = new Texture(Gdx.files.internal("equip/Basketball.png"));
    ball = new Circle();
    ball.x = Gdx.graphics.getWidth() / 2;
    ball.y = Gdx.graphics.getHeight() / 2;
    ball.radius = basketball.getWidth() / 2;

在渲染方法

spriteBatch.draw(basketball, ball.x-ball.radius, ball.y-ball.radius);
spriteBatch.draw(currentFrame,playerX, playerY);// Draw current frame at (50, 50)  

    if(playerY < 0) playerY = 0;
    if(playerY > 700 - 80) playerY = 700 - 80;
    // check collision
    if (Intersector.overlaps(ball, player))
        Gdx.app.log("overlaps", "yes");

        //Accelerometer
        if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) {
            playerX -= Gdx.input.getAccelerometerX();
            playerY += Gdx.input.getAccelerometerY();
        }
    if (playerY > Gdx.graphics.getHeight() - 100) {
        playerY = Gdx.graphics.getHeight() - 100;
    }
        if (playerX < 0) {
            playerX = 0;
            playerX += Gdx.graphics.getDeltaTime() * catSpeed;
        }
        if (playerX > Gdx.graphics.getHeight() - 250) {
            playerX = Gdx.graphics.getHeight() - 250;
        } 

感谢&#39; s并在这里推进完整源代码:) GameScreen

0 个答案:

没有答案