快速平移节点时无边缘碰撞检测

时间:2014-04-25 22:01:34

标签: ios collision-detection sprite-kit skshapenode

我对这个碰撞问题感到害怕: 当快速平移时,抛光的球会爆破边缘的身体。有人有类似问题吗? 这是解决问题的场景代码。 (在xcode示例中替换MyScene - spritekitgame)

#import "SAMyScene.h"

/* Bitmask for the different entities with physics bodies. */
typedef enum : uint32_t {
    SAColliderTypeBall             = 0x1 << 1
    ,SAColliderTypeEdge            = 0x1 << 3
} SAColliderType;

NSString * const SABallName = @"ballNode";

@interface SAMyScene()
@property (nonatomic, strong) UIPanGestureRecognizer *panGestureRecognizer;
@property (nonatomic, strong) SKShapeNode *ball;
@end
@implementation SAMyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        NSLog(@"Ball categorie bitmask =%d", SAColliderTypeBall);
        NSLog(@"Edges categorie bitmask =%d", SAColliderTypeEdge);

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
        self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        self.physicsBody.categoryBitMask = SAColliderTypeEdge;
        self.physicsBody.usesPreciseCollisionDetection = YES;
        self.ball = [self newBallNode];
        [self addChild:self.ball];
    }
    return self;
}

- (SKShapeNode *)newBallNode {

    SKShapeNode *ball = [[SKShapeNode alloc] init];
    CGMutablePathRef myPath = CGPathCreateMutable();
    CGFloat radius = 60.0;
    CGPathAddArc(myPath, NULL, 0,0, radius, 0, M_PI*2, YES);
    ball.path = myPath;
    ball.fillColor = [SKColor yellowColor];
    ball.name = SABallName;
    ball.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:radius];
    ball.physicsBody.affectedByGravity = NO;
    ball.physicsBody.dynamic = YES;
    ball.physicsBody.categoryBitMask = SAColliderTypeBall;
    ball.physicsBody.contactTestBitMask = SAColliderTypeEdge;
    ball.physicsBody.collisionBitMask = SAColliderTypeEdge;
    ball.physicsBody.usesPreciseCollisionDetection = YES;
    return ball;
}

-(void)update:(CFTimeInterval)currentTime {
    //Called before each frame is rendered
}

- (UIPanGestureRecognizer *)panGestureRecognizer {
    if (!_panGestureRecognizer) {
        _panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
                                                                        action:@selector(handlePanFrom:)];
    }
    return _panGestureRecognizer;
}

- (void)didMoveToView:(SKView *)view {

    if (![[self view].gestureRecognizers containsObject:self.panGestureRecognizer]) {
        [[self view] addGestureRecognizer:self.panGestureRecognizer];
    }
}

- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {

    if (recognizer.state == UIGestureRecognizerStateBegan) {
    } else if (recognizer.state == UIGestureRecognizerStateChanged) {

        CGPoint translation = [recognizer translationInView:recognizer.view];
        translation = CGPointMake(translation.x, - translation.y);
        [self panForTranslation:translation];
        [recognizer setTranslation:CGPointZero inView:recognizer.view];

    } else if (recognizer.state == UIGestureRecognizerStateEnded) {
    }
}

- (void)panForTranslation:(CGPoint)translation {
    CGPoint position = self.ball.position;
    CGPoint newPos = CGPointMake(position.x + translation.x, position.y);
    self.ball.position = newPos;
}

@end

0 个答案:

没有答案