0

As you can see in the screenshot below, there is a large gap between both the circle (a SKShapeNode) and the line (also a SKShapeNode).

Naturally, both of these items should be touching when they collide.

Here is my code:

- (void)createSceneContents
{
    [self setBackgroundColor:[SKColor blackColor]];

    //  Floor
    //
    SKShapeNode *floorShapeNode = [SKShapeNode node];
    CGFloat threeQuartersDown = CGRectGetMidY(self.frame) * 0.5f;
    CGPoint floorFromPoint = CGPointMake(CGRectGetMinX(self.frame), threeQuartersDown);
    CGPoint floorToPoint = CGPointMake(CGRectGetMaxX(self.frame), threeQuartersDown);

    CGMutablePathRef floorLinePath = CGPathCreateMutable();
    CGPathMoveToPoint(floorLinePath, NULL, floorFromPoint.x, floorFromPoint.y);
    CGPathAddLineToPoint(floorLinePath, NULL, floorToPoint.x, floorToPoint.y);
    [floorShapeNode setPath:floorLinePath];

    [floorShapeNode setPhysicsBody:[SKPhysicsBody bodyWithEdgeFromPoint:floorFromPoint toPoint:floorToPoint]];
    [floorShapeNode setStrokeColor:[SKColor whiteColor]];
    [floorShapeNode setLineWidth:0.5f];

    [self addChild:floorShapeNode];

    //  Player
    //
    [self setPlayerNode:[BZPlayerNode node]];
    [self.playerNode setPhysicsBody:[SKPhysicsBody bodyWithCircleOfRadius:(CGRectGetWidth(self.playerNode.frame) / 2.0f)]];
    [self.playerNode.physicsBody setDynamic:YES];
    [self.playerNode setPosition:CGPointMake(CGRectGetMidX(self.frame) - CGRectGetMidX(self.playerNode.frame), CGRectGetMidY(self.frame) - CGRectGetMidX(self.playerNode.frame) + 100.0f)];

    [self addChild:self.playerNode];
}

And the +node method for the SKShapeNode:

+ (instancetype)node
{
    BZPlayerNode *playerNode = [super node];

    if (playerNode) {
        [playerNode setFillColor:[SKColor whiteColor]];
        [playerNode setPath:CGPathCreateWithEllipseInRect(CGRectMake(0.0f, 0.0f, 80.0f, 80.0f), NULL)];
    }

    return playerNode;
}

Does anyone know where I’ve gone wrong?

Thanks

enter image description here

Adam Carter
  • 4,741
  • 5
  • 42
  • 103
  • 2
    bodyWithCircleOfRadius - I believe radius here is not equivalent to the dimensions of the frame, at least when I last tried it in iOS 7.0 the shape radius was always significantly larger in points than what I specified in the initializer. Try setting it to a smaller radius until you get it about right. Debug drawing functions for Sprite Kit physics can help you determine the correct size, there's a repo on github but I can't remember the name. – CodeSmile May 31 '14 at 14:43
  • I tested the value and it was giving the right radius (40) to the method. I changed it to 1 and now it works fine - is this a bug with SKShapeNode? I read that it’s pretty buggy in iOS 7?... – Adam Carter May 31 '14 at 14:47
  • I had the same issue. The texture size was 20/20 points, but the PhysicsNode was a lot greater than the SpriteNode. But I followed your example and with a radius of 1 it seems to work fine. Still, I don't get the logic... – Bogdan Nov 27 '14 at 00:00

0 Answers0