4
\$\begingroup\$

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! enter image description here

// 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;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ What exactly are you looking for here? Are you looking for physics, collision detection, or just stopping the player? Because physics and collision already have an answer on this SE, and stopping the player should be trivial (if (!collides) movePlayer()). \$\endgroup\$ – Charanor Apr 3 '17 at 15:43
  • \$\begingroup\$ Both sir @Charanor . I don't have a solution for collision. I just did the 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 using accelerometer to move my player left right. \$\endgroup\$ – zarawayun Apr 4 '17 at 1:56
  • \$\begingroup\$ You need to be specific about the kind of collision detection you are looking for: rect-based, circle-based, pixel-perfect, etc. Then you need to be specific about the kind of collision resolution you are looking for: do you, for example, need angular results or purely linear results? do you want instant friction or gradual application of friction? etc. etc. \$\endgroup\$ – Engineer May 2 '17 at 11:46
  • \$\begingroup\$ @ArcaneEngineer I just want the player stop when he collide into the ball. I used this code if(Intersector.overlaps(ball, player)){ Gdx.app.log("overlaps", "yes"); if(player.y > 3298 ) player.y = 3298 ; } \$\endgroup\$ – zarawayun May 4 '17 at 2:53
3
+50
\$\begingroup\$

In your collision detection code you can do this to stop your player when he collides with the ball:

// 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
    }
} else {
    //Mobile acceleration
    if (Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) {
        player.x -= Gdx.input.getAccelerometerX();
        player.y += Gdx.input.getAccelerometerY();
    }
}

I've simply moved the movement code inside of the new else { ... } statement. This should completely stop the player if the player collides with the ball. Although this introduces a problem; if we collide with the ball we can never move our player again. To counteract this we have to move the player at least 1px outside of the ball. A very simple and inaccurate solution might look something like this:

if(Intersector.overlaps(ball, player)) {
    tmp.set(player.x, player.y + player.height/2, player.width, player.height/2);

    // The center of the ball
    Vector2 ballCenter = new Vector2(ball.x + ball.radius, ball.y + ball.radius);
    // The center of the cat
    Vector2 catCenter = new Vector2(cat.x + cat.width / 2, cat.y + cat.height / 2);
    // The vector pointing from the ball's center to the cat's center.
    Vector2 catBallVector = new Vector2(ballCenter).sub(catCenter);
    catBallVector.limit(pushDistance); //pushDistance is how far you should push your cat

    cat.setPosition(cat.x + catBallVector.x, cat.y + catBallVector.y);

    ... rest of code ...
} else {
    ... rest of code ...
}

Hopefully I answered your question. If you have any concerns feel free to post a comment!

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.