2012-07-19 163 views
1

我嘗試檢測兩個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

回答

1

可以使用的Box2D,使其檢測所有的碰撞爲你

1

在兩種方式可以做到。

  1. 將box2D正文用於您的精靈。示例:CLICK HERE
  2. 使用CGMutablePathRef,並使用CGPathContainsPoint()而不是CGRectIntersectsRect。 例子:CLICK HERE
0

這是可能的!嘗試使用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 } 

祝你好運!