Search code examples
ios7sprite-kitsksceneskphysicsworld

iOS7 SKScene how to make a sprite bounce off the edge of the screen?


I'm building a game with balls bouncing within the iPad's screen. Similar to a Pong game. I see that SKScene's SKPhysicsWorld has gravity property, and also controls how objects collide with each other.

Is there some way I can automatically detect if a sprite's edge has collided with the screen's edge, so it can bounce off that? Or do I need to write my own collision code?


Solution

  • You don't need to write much code to make the ball bounce off the edge of the screen. the physics environment can handle all of the above, you just need to instantiate the sprites in the correct way.

    First, you will have to set the physicsBody of the scene as such:

    self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
    

    In your scene's .h file create two bitmask categories:

    static const uint8_t ballCategory = 1;
    static const uint8_t wallCategory = 2;
    

    And give the scene's physics body a category:

    self.physicsBody.categoryBitMask = wallCategory;
    

    Then create your sprite:

    SKSpriteNode *spriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"ball.png"]; 
    
    spriteNode.name = @"ball";
    
    spriteNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25];
    spriteNode.position = CGPointMake(10.0,10.0); //or anything you want
    spriteNode.physicsBody.dynamic = YES;
    spriteNode.physicsBody.affectedByGravity = NO;
    
    spriteNode.physicsBody.categoryBitMask = ballCategory;
    spriteNode.physicsBody.collisionBitMask = wallCategory;
    
    spriteNode.physicsBody.restitution = 1.0;
    spriteNode.physicsBody.friction = 0.0;
    spriteNode.physicsBody.linearDamping = 0.0;
    spriteNode.physicsBody.angularDamping = 0.0;
    
    [self addChild:spriteNode];
    

    Instead of giving the spriteNode a [SKAction moveTo: duration:], apply an impulse to it's physics body.

    CGVector impulse = CGVectorMake(1.0,1.0);
    [spriteNode.physicsBody applyImpulse:impulse];
    

    And voila! The ball will be bouncing off the walls with nothing to stop it.

    This is what the .h file should look like:

    #import <SpriteKit/SpriteKit.h>
    
    static const uint8_t ballCategory = 1;
    static const uint8_t wallCategory = 2;
    
    @interface BallBounce : SKScene
    
    @end
    

    And this is how the .m file should look like:

    #import "BallBounce.h"
    
    @implementation BallBounce
    
    -(instancetype)initWithSize:(CGSize)size
    {
        if (self = [super initWithSize:size])
        {
            self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
            self.physicsBody.categoryBitMask = wallCategory;
    
            SKSpriteNode *spriteNode = [SKSpriteNode spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(50, 50)];
    
            spriteNode.name = @"ball";
    
            spriteNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25];
            spriteNode.position = CGPointMake(10.0,10.0); //or anything you want
            spriteNode.physicsBody.dynamic = YES;
            spriteNode.physicsBody.affectedByGravity = NO;
    
            spriteNode.physicsBody.categoryBitMask = ballCategory;
            spriteNode.physicsBody.collisionBitMask = wallCategory;
    
            spriteNode.physicsBody.restitution = 1.0;
            spriteNode.physicsBody.friction = 0.0;
            spriteNode.physicsBody.linearDamping = 0.0;
            spriteNode.physicsBody.angularDamping = 0.0;
    
            [self addChild:spriteNode];
    
            CGVector impulse = CGVectorMake(100.0,100.0);
            [spriteNode.physicsBody applyImpulse:impulse];
    
        }
    
        return self;
    }
    
    @end