Cocos2d - 旋转Sprite的碰撞检测

问题描述:

我尝试检测两个Sprite之间的碰撞。Cocos2d - 旋转Sprite的碰撞检测

if(CGRectIntersectsRect([SpriteA BoundingBox], [SpriteB boundingBox])) 

但是,当我在旋转任何精灵比碰撞检测是不完美.. 我知道使用像素的完美碰撞,但我没有关于它的想法。 请任何人帮助我如何检测碰撞,给我任何代码块,如果有的话。

+0

[Cocos2d,旋转(透明)精灵碰撞]的可能重复(http://stackoverflow.com/questions/10887128/cocos2d-rotated-transparent-sprite-collision) – Hailei 2012-07-19 05:51:03

可以使用的Box2D,使其检测所有的碰撞为你

在两种方式可以做到。

  1. 将box2D正文用于您的精灵。示例:CLICK HERE
  2. 使用CGMutablePathRef,并使用CGPathContainsPoint()而不是CGRectIntersectsRect。 例子:CLICK HERE

您也可以参考,在Ray Wenderlich Tutorial任何2个Box2D的机构之间检测到碰撞。

这是可能的!尝试使用CGPath。 我有同样的问题。我本教程解决:http://bobueland.com/cocos2d/2011/the-magic-of-cgpaths/

的旋转路径试试这个方法,它旋转路径轮boudingBox中心:您检测碰撞简单与

-(CGPathRef) rotateCGPath:(CGPathRef)path corner:(CGFloat)radians 
{ 
    CGRect bounds = CGPathGetBoundingBox(path); 
    CGPoint center = CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds)); 
    CGAffineTransform transf = CGAffineTransformIdentity; 
    transf = CGAffineTransformTranslate(transf, center.x, center.y); 
    transf = CGAffineTransformRotate(transf, -radians); 
    transf = CGAffineTransformTranslate(transf, -center.x, -center.y); 
    return CGPathCreateCopyByTransformingPath(path, &transf); 
} 

在此之后:

if (CGPathContainsPoint(Collisionpath, NULL, collisionPoint, NO)) 
{ //is inside the path } 

祝你好运!