Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to apply impulse to SKSpriteNode physics body

I am new to xcode's sprite kit and I'm an trying to apply an impulse to a SKSpriteNode's physics body. Here is how I create the scene:

self.backgroundColor = [SKColor colorWithRed:0 green:0 blue:0 alpha:1.0];
        self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        self.physicsBody.friction = 0.0f;
        self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
        ballCategory = 1;
        wallCategory = 2;
        self.physicsBody.categoryBitMask = wallCategory;

Here is how I create the player AND give it its impulse:

player = [SKSpriteNode spriteNodeWithImageNamed:filePath];
    player.size = CGSizeMake(100, 100);
    player.position = CGPointMake(150, 250);
    player.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:player.frame];
    player.physicsBody.categoryBitMask = ballCategory;
    player.physicsBody.collisionBitMask = wallCategory;
    player.physicsBody.friction = 0.0f;
    player.physicsBody.restitution = 1.0f;
    player.physicsBody.linearDamping = 0.0f;
    player.physicsBody.allowsRotation = NO;
    [self addChild:player];
        CGVector impulse = CGVectorMake(100,100);
        [player.physicsBody applyImpulse:impulse];

Is there anything obvious that I am missing because I've been following the iOS Developer Library Sprite Kit Programming Guide perfectly? Thanks in advance for the help!

like image 273
Lucas Bullen Avatar asked Jan 16 '14 13:01

Lucas Bullen


1 Answers

You are creating the player's physicsBody as an edge body - these are always static (immovable by forces/impulses). You should change that line to:

player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.frame.size];

This example makes it a dynamic volume based on a rectangle which will be affected by physics forces. You can read up on the types of physics bodies here.

like image 92
Batalia Avatar answered Oct 23 '22 05:10

Batalia