0

In my top down game I'm using accelerometer to move my character and now I'm studying collision detection between rectangle and circle a cat and ball but my codes doesn't work.I want the cat to stop when he hits the ball and also animate again when I move my phone to the left and right.The cat is my player and ball is obstacle.

Here is my code

// 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;

In render method

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;
        } 

Thank's and advance here is the full source code :) GameScreen

  • 2
    You say "my codes doesn't work", how does it not work? Is your PC catching fire? Is the game playing Nickleback on collision? We can't see what you are seeing. Also, why are you using playerX and playerY instead of player.x and player.y? – Jim Mar 31 '17 at 12:12
  • Thank you Sir @IronMonkey I used player.x and player.y and now it's say yes when the cat is overlap in the ball :) It works now but how can I stop the player when he overlap the ball? –  Apr 03 '17 at 01:48

0 Answers0