cocos2d使用boundingBox来检查碰撞
我使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为都是圆圈,所以很简单。只需检查两个圆心之间的距离即可。如果它大于圆的半径之和,则精灵不会接触。如果较少,则它们重叠。如果相等,它们只是接触。
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.