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