English 中文(简体)
HRW Pong Development, Collision Research
原标题:iOS Pong Development, Collision Detection

I am in a late phase of finishing my first usable iOS app. I am creating a simple Pong game i am using a simple collision detection using CGRectIntersectsRect, but i came up with a problem.

if(CGRectIntersectsRect(mic.frame,plosina_a.frame)) {
        if(mic.center.y < (plosina_a.center.y + plosina_a.frame.size.height)) {
            RychlostMice.y = -RychlostMice.y;
        }
    }
    if(CGRectIntersectsRect(mic.frame,plosina_b.frame)) {
        if(mic.center.y < (plosina_b.center.y + plosina_b.frame.size.height)) {
            RychlostMice.y = -RychlostMice.y;
        }
    }

When i use it like this the ball (mic) sort of gets to the paddles (plosina) and starts moving the other way sort of in the middle of the paddle. My programming teacher managed to fix this problem for one of the paddles (the _b one) by adding the .frame.size.height instead of just .frame which i have used before, but when i did the same thing for the other paddle it didn t work i don t know what s up with that.

而且,它有时还造成另一个问题,即气球在跳板上走,因此,我看一看整个物体的定义,而不仅仅是一个方面?

i 希望你们能够提供帮助。

问题回答

我在这里可以看到三个潜在的问题。

The first is that you are waiting until the ball overlaps the paddle before counting it as a touch. It sounds like you really want to start the ball moving in the other direction when the ball touches the paddle, not when it intersects it. The CGRectIntersectsRect waits until they overlap before returning true. If you make either rectangle one pixel larger with a call to CGRectInset, your test will return true as soon as the ball reaches that paddle--by that time, there will be one pixel overlapping the expand rectangle. The test would look like this:

if(CGRectIntersectsRect(CGRectInset(mic.frame, -1, -1),plosina_a.frame)) {
    if(mic.center.y < (plosina_a.center.y + plosina_a.frame.size.height)) {
        RychlostMice.y = -RychlostMice.y;
    }
}
if(CGRectIntersectsRect(CGRectInset(mic.frame, -1, -1),plosina_b.frame)) {
    if(mic.center.y < (plosina_b.center.y + plosina_b.frame.size.height)) {
        RychlostMice.y = -RychlostMice.y;
    }
}

第二个潜在问题涉及气球的速度。 如果看不到所有法典,我不知道这是否是一个问题。 如果气球能够在某个时间移动不止一个钢子,那么它就很容易重叠,甚至通过一篮子,而没有发现点击。 你可以作许多逻辑上的改动,以照顾到这一点,但最容易的解决办法是,确保球不会在某个时候移动不止一个螺旋。

最后,如果你想把黑板用于两个add子,就会扭转游戏另一边的对比迹象。

我猜测,你的平子是垂直的,这样一篮子就在屏幕上,另一块是底层?

如果你需要垂直地反映其他篮子的逻辑。 现在,你对上层和下层的les子使用同样的逻辑,但对于下层来说,你可能需要像以下那样的东西。

if(CGRectIntersectsRect(mic.frame,plosina_b.frame)) {
    if(mic.center.y > (plosina_b.center.y - plosina_b.frame.size.height)) {
        RychlostMice.y = -RychlostMice.y;
    }
}

• 通知我如何使用“超高”;签署并绕过高,而不是添加。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签