代码之家  ›  专栏  ›  技术社区  ›  nullArray

iPhone乒乓碰撞检测

  •  0
  • nullArray  · 技术社区  · 16 年前

    happens in this video . 现在只有当我增加帧率时才会发生这种情况,如果帧率真的很低,就会有点出错,然后“通过”划桨,得到一分。至少在这种情况下,这不是一个明显的bug,游戏还在继续。

    当然,我希望帧率尽可能平滑,所以…,我想解决这个问题。下面是我的代码,用于非常简单的碰撞检测。使用UIImageView。

    if (CGRectIntersectsRect (ball.frame, playerPaddle.frame))
        {
            if (ball.center.y < playerPaddle.center.y) //hits the "top" of the computer paddle
            {
                AudioServicesPlaySystemSound (hitSound);
                ballVelocity.y = -ballVelocity.y; // turn ball around if collide
            }
        }
    
        if (CGRectIntersectsRect (ball.frame, computerPaddle.frame))
        {
            if (ball.center.y > computerPaddle.center.y) //hits the "bottom" of the computer paddle
            {
                AudioServicesPlaySystemSound (hitSound);
                ballVelocity.y = -ballVelocity.y; // turn ball around if collide
            }
        }
    

    5 回复  |  直到 16 年前
        1
  •  4
  •   Mark P Neyer    16 年前

    在没有看到其余代码的情况下,这只是一个猜测,但可能发生的是球的速度不断地来回翻转。击球后,你需要将球移到桨外,或者有一个“最小碰撞间隔时间”

        2
  •  3
  •   Ed Marty    16 年前

    明确地:

    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
    }
    
        3
  •  0
  •   nullArray    16 年前

    需要一个额外的条件。

    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;
            }
        }
    
        4
  •  0
  •   Jeremy Friesner    16 年前

    我认为最简单的处理方法是加入逻辑,这样在与球拍发生碰撞后,球的速度矢量总是指向球拍的中心点。这样,即使球与球拍“再次碰撞”(因为球在下一次循环中飞得太远而无法再次飞出),它也会在第二次“反弹”后继续远离球拍。

        5
  •  0
  •   byachna    16 年前

    这个问题类似于我遇到的一个问题,球被抓在桨内。我解决这个问题的方法是创建一个布尔值。发生冲突时,布尔值设置为true。当球碰到墙时,布尔值设置为false。在检查矩形相交的条件语句内部,我添加了一个检查,以查看碰撞是否设置为false。

    因此,最终将发生的是:

    • 球撞击挡板,布尔值设置为true。
    • 球的速度是相反的
    • 球从墙上反弹,布尔值重置为false
    • 球与另一个桨碰撞,布尔值再次设置为true

    试试这个,让我知道它是如何工作的。

    推荐文章