iPhone Pong碰撞检测 [英] iPhone Pong Collision Detection

查看:160
本文介绍了iPhone Pong碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,如果球恰好击中了球拍,球会被锁在里面而无法释放。有时会在播放器拨片上发生这种情况,但是由于您可以控制它,因此您所要做的就是移动它并使其逃脱。计算机完美地跟随球,因此一旦进入球内就永远无法逃脱,从而导致在此视频中。现在,只有当我提高帧率时,才会发生这种情况,如果帧率确实很低,则有点错误,并通过了桨叶,从而获得了积分。至少在这种情况下,它不是一个明显的错误,游戏仍然可以继续玩。

I have an issue where if the ball hits the paddle just right, it gets locked inside and can't be released. It happens sometimes on the player paddle, but since you have control of it, all you have to to do is move it and it escapes. The computer follows the ball perfectly, so once it gets inside it never can escape, leading to what happens in this video. Now this only happens when I increase the framerate, if it's really low, it kinda, errors out and "goes through" the paddle resulting in a point gained. At least in this case, it's not a noticeable bug and the game continues to play.

自然,我希望帧速率尽可能平滑……整理一下。这是我的非常简单的碰撞检测代码。使用UIImageViews。

Naturally I want the framerate to be as smooth as possible so..., like to sort this out. Here's my code for the very simplistic collision detection. Using UIImageViews.

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
        }
    }

可以帮助。

推荐答案

除了上述答案外,您的球绝对不能真正进入桨板。具体来说,如果球在球拍内,则应在切换其y速度的同时将其位置重置为位于球拍外。

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.

特别是:

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
}

这篇关于iPhone Pong碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆