0

私はこの衝突の問題にびっくりしています: パンされたボールは、速くパンされるとエッジループ ボディから抜け出します。誰かが同様の問題を抱えていましたか?この問題を説明するシーン コードを次に示します。(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
4

0 に答える 0