How can I stop my player if he overlap or touch the ball? In my top down game my player is cat and I want to stop his moving or stack in that place of the ball is located if he hit the ball I'm using acceleromter to move the cat left and right.
Here is my code. How can I add collide method just like the image bellow? Thank's and advance!
// 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;
player.x = 300;
player.y = 0;
basketball = new Texture(Gdx.files.internal("ball.png"));
ball = new Circle();
ball.x = Gdx.graphics.getWidth() / 2;
ball.y = Gdx.graphics.getHeight() / 2;
ball.radius = basketball.getWidth() / 2;
In render method
camera.update();
TextureRegion currentFrame = walkAnimation.getKeyFrame(stateTime, true);
spriteBatch.draw(basketball, ball.x-ball.radius, ball.y-ball.radius);
spriteBatch.draw(currentFrame,player.x, player.y);// Draw current frame at (50, 50)
if(player.y < 0)
player.y = 0;
if(player.y > 900 - 80)
player.y = 700 - 80;
// check collision
if(Intersector.overlaps(ball, player)){
tmp.set(player.x, player.y + player.height/2, player.width, player.height/2);
if (Intersector.overlaps(ball, tmp)){
//top half (or both top and bottom) hit
Gdx.app.log("overlaps", "yes");
} else {
//bottom half hit
}
}
//Mobile acceleration
if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) {
player.x -= Gdx.input.getAccelerometerX();
player.y += Gdx.input.getAccelerometerY();
}
if (player.y > Gdx.graphics.getHeight() - 100)
player.y = Gdx.graphics.getHeight() - 100;
if (player.x < 0) {
player.x = 0;
player.x += Gdx.graphics.getDeltaTime() * catSpeed;
}
if (player.x > Gdx.graphics.getHeight() - 250) {
player.x = Gdx.graphics.getHeight() - 250;
}
if (!collides) movePlayer()
). \$\endgroup\$intesector.
overlap and it works fine . What should be the code for stopping or hide the player when he hits the ball? And for the sofa is to hide. I'm usingaccelerometer
to move my player left right. \$\endgroup\$if(Intersector.overlaps(ball, player)){ Gdx.app.log("overlaps", "yes"); if(player.y > 3298 ) player.y = 3298 ; }
\$\endgroup\$