iPhone Pong Collision Detection

人盡茶涼 提交于 2019-12-04 23:21:58

In addition to the answers above, your ball should really never be allowed to actually enter the paddle. Specifically, if the ball is within the paddle, you should reset its position to just outside of the paddle at the same time you switch its y-velocity.

Specifically:

if (CGRectIntersectsRect (ball.frame, playerPaddle.frame))
{
    AudioServicesPlaySystemSound (hitSound);
    CGRect frame = ball.frame;
    frame.origin.y = playerPaddle.frame.origin.y - frame.size.height;
    ball.frame = frame;
    ballVelocity.y = -ballVelocity.y; // turn ball around if collide
}

if (CGRectIntersectsRect (ball.frame, computerPaddle.frame))
{               
    AudioServicesPlaySystemSound (hitSound);
    CGRect frame = ball.frame;
    frame.origin.y = CGRectGetMaxY(computerPaddle.frame);
    ball.frame = frame;
    ballVelocity.y = -ballVelocity.y; // turn ball around if collide
}

Without seeing the rest of your code, this is just a guess, but what is probably happening is the ball's velocity continually gets flipped back and forth. You need to move the ball outside the paddle after it hits, or have a "minimum time between collisions"

Needed an extra condition.

if (ballVelocity.y > 0 && CGRectIntersectsRect (ball.frame, playerPaddle.frame))
    {
        if (ball.center.y < playerPaddle.center.y) //player
        {
            AudioServicesPlaySystemSound (volleyFileID);
            ballVelocity.y = -ballVelocity.y;
        }
    }

    if (ballVelocity.y < 0 && CGRectIntersectsRect (ball.frame, computerPaddle.frame)) //computer
    {
        if (ball.center.y > computerPaddle.center.y)
        {
            AudioServicesPlaySystemSound (volley2FileID);
            ballVelocity.y = -ballVelocity.y;
        }
    }

I think the easiest way to handle it is to add in logic so that after a collision with the paddle, the ball's velocity vector is forced to be always pointing away from the center point of the paddle. That way, even if the ball "collides again" with the paddle (because it went too far inside to get out again on the next tick of the event loop), it will just continue heading away from the paddle after the second "bounce".

This problem was similar to an issue I was having with the ball being caught inside of the paddle. What I did to solve this was create a boolean. When there is a collision, the boolean is set to true. When the ball hits a wall, the boolean is set to false. Inside of the conditional statement where we check for Rectangle Intersection, I add a check to see if collision is set to false.

So, ultimately what will happen is this:

  • The ball hits paddle and boolean is set to true.
  • The velocity is reversed on the ball
  • When the collision detection tries to execute, it doesn't because the boolean is true
  • The ball bounces off a wall and the boolean is reset to false
  • The ball collides with the other paddle and the boolean is set to true again

Try this and let me know how it works.

-Brandon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!