如何检测2个UIView之间的碰撞 [英] How to detect Collision between 2 UIViews

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

问题描述

我有一个包含我的主要角色的UIImageView,并且使UIImageView显示为圆形,请参见下面的代码以创建我的角色

I have a UIImageView which contains my main Character, and I have made the UIImageView appear circle see below code for creating my character

copter = [[UIImageView alloc] initWithFrame:CGRectMake(100, 500, 90, 90)];
[copter setContentMode:UIViewContentModeScaleAspectFit];
copter.layer.cornerRadius = roundf(copter.frame.size.width/2.0);
copter.layer.masksToBounds = YES;
[copter startAnimating];
[[self view] addSubview:copter];
[self setBat:copter];

我在角色和游戏中其他物体之间的碰撞遇到麻烦。在矩形而不是圆形上检测到碰撞。我到处都在寻找解决问题的答案,但是没有运气。

I am having trouble with my collision between my character and other objects in the game. The collision is being detected on the rectangle rather than the circle. I have searched everywhere for an answer to fix this but no luck.

这是我正在使用的碰撞代码:

Here is my collision code I am using:

 self.batVelocity += [link duration]*FBDownardBatAccelerationIpad;

 [[self copter] setFrame:CGRectOffset([[self copter] frame], 0, [self batVelocity])];

 UIView *removeBlock = nil;
 for (UIView *block in [self blocks]) {
      [block setFrame:CGRectOffset([block frame], [link duration]*FBSidewaysVelocityIpad, 0)];
       if (CGRectIntersectsRect([[self copter] frame], [block frame])) {
             [self failed];

所以基本上我需要字符的圆边界与矩形对象碰撞,而不是矩形边界

So basically I need the circle bounds of the character to collide with a rectangle object, not the rectangle bounds of which the circle is in. I hope that makes sense.

预先感谢

推荐答案

对,所以首先检查重叠的矩形(已经完成)。当发现重叠时,则需要优化碰撞测试。对于有问题的矩形的每个角,计算从角色中心到有问题的矩形的角的距离。如果从中心到任意一个角的距离小于圆的半径,则发生碰撞。要稍微优化代码,请计算(dx * dx + dy * dy)并将其与半径平方进行比较。这样,您不必计算任何平方根。

Right, so first you check for overlapping rectangles (which you've already done). When you find an overlap, then you need to refine the collision test. For each corner of the offending rectangle, compute the distance from the center of your character to the corner of the offending rectangle. If the distance from the center to any of the corners is less than the radius of the circle, then you have a collision. To optimize the code a little bit, compute (dx * dx + dy * dy) and compare that to the radius squared. That way you don't have to compute any square roots.

在进一步检查时,除了进行角点检查外,还需要进行边缘检查。例如,如果顶部 y 值在圆心上方,而底部 y 值在圆心下方的圆,然后计算矩形左边缘和圆的中心之间的 x 差,如果该距离小于半径,则发生了碰撞。同样,对于矩形的其他三个边缘。

Upon further review, you also need to do an edge check in addition to the corner check. For example, if the top y value is above the center of the circle and the bottom y value is below the center of the circle, then compute the difference in x between the rectangle left edge and the center of the circle, and if that distance is less than the radius, then a collision has occurred. Likewise for the other three edges of the rectangle.

以下是一些用于角落检查的伪代码

Here's some pseudo-code for the corner checking

int dx, dy, radius, radiusSquared;

radiusSquared = radius * radius;
for ( each rectangle that overlaps the player rectangle )
{
    for ( each corner of the rectangle )
    {
        dx = corner.x - center.x;
        dy = corner.y - center.y;
        if ( dx * dx + dy * dy < radiusSquared )
            Collision!!!
    }
}

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

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