cocos2d使用boundingBox来检查碰撞

发布于 2024-10-24 01:37:05 字数 1206 浏览 0 评论 0原文

我使用boundingBox来检查碰撞,这对于触摸来说很好,但对于2个精灵的碰撞来说根本不行,(它们都是圆形的,但它们的边界框是矩形的)

所以有没有更好的方法?(如果你修改我的,那就太好了下面的代码:

-(bool) isFishCollidingWithRect:(CGRect) rect
{
     FishEntity* fish;
     CCARRAY_FOREACH(fishes, fish)
     {
        if (fish.visible && CGRectIntersectsRect([fish boundingBox], rect)) {
             [fish gotHit];
             return YES;
         }
      }
      return NO;
}

我认为有一个很好的方法来获得一个较小的矩形,它位于原始矩形和顶点刚好接触圆的矩形之间,尺寸很容易获得,但坐标非常困难,因为矩形。可能有旋转,我的数学

-(bool) isBirdCollidingWithSprite:(CCSprite*) sprite
 {
     BirdEntity* bird;
     CCARRAY_FOREACH(birdsAlone, bird)
     {
         float distance = sqrt((bird.anchorPoint.x - sprite.anchorPoint.x) * (bird.anchorPoint.x - sprite.anchorPoint.x) + (bird.anchorPoint.y - sprite.anchorPoint.y) * (bird.anchorPoint.y - sprite.anchorPoint.y)); 
         if (bird.visible && (bird.contentSize.width / 2 + sprite.contentSize.width / 2) >= distance) {
             if ([bird inBubble] == NO) {
                 [bird bubbled];
             }
             return YES;
         }
     }
     return NO;
 }

很糟糕,鱼总是会被击中,我相信 contentSize 是批处理节点中的整个纹理,我应该使用什么?

i use boundingBox to check collision, it's fine for touches, but not at all for collision of 2 sprites, (they are all circular but their bounding box is rect)

so is there any better ways?(it would be great if you modify my code below:

-(bool) isFishCollidingWithRect:(CGRect) rect
{
     FishEntity* fish;
     CCARRAY_FOREACH(fishes, fish)
     {
        if (fish.visible && CGRectIntersectsRect([fish boundingBox], rect)) {
             [fish gotHit];
             return YES;
         }
      }
      return NO;
}

i think there is a good way to get a smaller rect, which is in between of the original rect and a rect whose vertices just touch the circle. the size is easy to get, but the coordinate is very hard since the rect may have rotation, my math sucks

-(bool) isBirdCollidingWithSprite:(CCSprite*) sprite
 {
     BirdEntity* bird;
     CCARRAY_FOREACH(birdsAlone, bird)
     {
         float distance = sqrt((bird.anchorPoint.x - sprite.anchorPoint.x) * (bird.anchorPoint.x - sprite.anchorPoint.x) + (bird.anchorPoint.y - sprite.anchorPoint.y) * (bird.anchorPoint.y - sprite.anchorPoint.y)); 
         if (bird.visible && (bird.contentSize.width / 2 + sprite.contentSize.width / 2) >= distance) {
             if ([bird inBubble] == NO) {
                 [bird bubbled];
             }
             return YES;
         }
     }
     return NO;
 }

well, the fishes will always got hit, i believe the contentSize is the whole texture in the batch node. what should i use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇柔作态 2024-10-31 01:37:05

因为都是圆圈,所以很简单。只需检查两个圆心之间的距离即可。如果它大于圆的半径之和,则精灵不会接触。如果较少,则它们重叠。如果相等,它们只是接触。

Since they're all circles, it's pretty simple. Just check the distance between the centers of the two circles. If it's greater than the sum of the radii of the circles, the sprites aren't touching. If it's less, they're overlapping. If it's equal, they're just touching.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文