0

I must be missing something really obvious here. My objective: Create a box in the center of the screen, with a bouncing ball staying inside that box. I'm trying to create an edge-based SKSpriteNode for the bounding box and an volume-based SKShapeNode for the internal ball. THe ball spawns in the center of the box, falls through the box, falls off screen, apparently hits something below the bottom of the screen, and bounces back above the bottom edge a few times before finally disappearing. All the while, didBeginContact never fires.

Question1: why is my ball falling through the visible box, yet contacting something below the edge of the screen? Is my physicsbody misaligned from the actual sprite?

Question2: why is didBeginContact not firing? [Edit, ok this was simple. Forgot to set physicsworld.delegate]

Thanks!

Scene.h

#define MAX_WORLD_CELLS_X 10
#define MAX_WORLD_CELLS_Y 10
#define BGTILE_SIZE 100

typedef enum : uint32_t {
    TYPE_SUBSPACE       = 1,
    TYPE_WORLD          = 2,
    TYPE_CHILD          = 4
} ColliderTypes;

@interface helloScene : SKScene
                <SKPhysicsContactDelegate>
{
    SKSpriteNode    *layerSubSpace,*layerWorld;  
}
@end

Scene.m, didBeginContact

-(void) didBeginContact:(SKPhysicsContact *)contact
{
    NSLog(@"Function: GameScene.m:didBeginContact");
}

Scene.m, initWithSize

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
        CGSize layerSize = CGSizeMake(MAX_WORLD_CELLS_X * BGTILE_SIZE *2, MAX_WORLD_CELLS_Y * BGTILE_SIZE *2);
        layerSubSpace = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:layerSize];

        //center subSpace on the screen's center 
        layerSubSpace.position = CGPointMake(CGRectGetMidX(self.frame),         
                                         CGRectGetMidY(self.frame));        

        CGRect subspaceRect = CGRectMake(-layerSubSpace.size.width/2, -layerSubSpace.size.height/2, layerSubSpace.size.width, layerSubSpace.size.height);
        layerSubSpace.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:subspaceRect];
        layerSubSpace.physicsBody.affectedByGravity = NO;
        layerSubSpace.physicsBody.categoryBitMask = TYPE_SUBSPACE;
        layerSubSpace.physicsBody.contactTestBitMask = TYPE_WORLD;
        layerSubSpace.physicsBody.collisionBitMask = TYPE_WORLD;
        [self addChild:layerSubSpace];

        SKShapeNode *ball = [[SKShapeNode alloc] init];
        CGMutablePathRef myPath = CGPathCreateMutable();
        CGPathAddArc(myPath, NULL, 0,0, 60, 0, M_PI*2, YES);
        ball.path = myPath;
        ball.lineWidth = 1.0;
        ball.fillColor = [SKColor blueColor];

        ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:60];
        ball.physicsBody.restitution = 1;
        ball.physicsBody.categoryBitMask = TYPE_WORLD;
        ball.physicsBody.collisionBitMask = TYPE_SUBSPACE;
        ball.physicsBody.contactTestBitMask = TYPE_SUBSPACE;
        [layerSubSpace addChild:ball];
    }

    layerSubSpace.xScale = 0.25;
    layerSubSpace.yScale = 0.25;

    return self;
}

1 Answer 1

1

1) The reason your physics simulation isn't lining up with your visual representation is that you've set the x/yScale properties of layerSubSpace to 0.25. These properties affect your sprites' visual representations but not the underlying physics model. The basic issue is that the size of your ball and box are reduced by a factor of four in each dimension, but the distance that the ball drops isn't reduced at all.

I suspect the solution is simply to reduce the size of everything explicitly by a factor of four, rather than relying on the x/yScale properties. When I reduce BGTILE_SIZE to 25 and the radius of the circle, both in CGPathAddArc and bodyWithCircleOfRadius:, to 15, the ball comes to rest at the bottom of the box on the iPad simulator.

2) As you said, you didn't set self.physicsWorld.contactDelegate = self;

3
  • Thanks, that's a good point. After reducing BGTILE_SIZE, I can indeed fit the physics box within the screen. But, here's the thing, it's still larger than the red box associated with the SKSpriteNode. Since I used the same factors in calculating the size of the sprite and the physics body, I expected them to match in size (and scale together). Why is the physicsbody's box not aligned to the sprite's?
    – Thunk
    Dec 8, 2013 at 7:53
  • @Thunk Sorry, I missed the lines setting xScale and yScale when I copied your code into my test project. I've updated my answer to explain why you're still not seeing everything line up.
    – godel9
    Dec 8, 2013 at 15:56
  • Thanks, that put me on the right path. Scaling the entire scene rather than layerSubSpace produced the desired results. I can change the size of the box (subspace) relative to the screen and the ball remains exactly inside regardless of the zoom.
    – Thunk
    Dec 8, 2013 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.