ios - 矩形和圆形碰撞检测
<p><p>我正在尝试在矩形和圆形之间进行碰撞检测。我想出了这个方法:</p>
<pre><code>-(BOOL) isCollidingRect:(CCSprite *) spriteOne WithSphere:(CCSprite *) spriteTwo {
float diff = ccpDistance(spriteOne.position, spriteTwo.position);
float obj1Radii = .size.width/2;
float obj2Radii = .size.width/2;
if (diff < obj1Radii + obj2Radii) {
return YES;
} else {
return NO;
}
}
</code></pre>
<p>这就是我检查它的方式:</p>
<pre><code>if ( == true) {
;
}
</code></pre>
<p>这似乎在矩形的一侧正常工作,但它不在它的上方或下方。在顶部和底部,碰撞发生得太早了。</p>
<p> <img src="/image/tEo4A.png" alt="Kinda Like this.."/> </p>
<p>有没有办法可以正确检测到这种碰撞?感谢您的帮助。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以使用 <a href="https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CGGeometry/Reference/reference.html#//apple_ref/c/func/CGRectIntersectsRect" rel="noreferrer noopener nofollow">CGRectIntersectsRect</a>实现这一目标。</p>
<pre><code>-(BOOL) isCollidingRect:(CCSprite *) spriteOne WithSphere:(CCSprite *) spriteTwo {
return CGRectIntersectsRect(,);
}
</code></pre>
<p>它不是像素完美的,但据我所知,在这种情况下没有必要。</p></p>
<p style="font-size: 20px;">关于ios - 矩形和圆形碰撞检测,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20187316/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20187316/
</a>
</p>
页:
[1]