Design and import your complex, custom SKPhysicsBodys for SpriteKit. SKImport comes with a fancy editor and a loader class for use in your apps. SKImport uses SpriteKit's [SKPhysicsBody bodyWithPolygonFromPath]
and [SKPhysicsBody bodyWithBodies]
to create complex, multiple sub-bodies physic bodies.
Open up the editor and import your sprite image. Design your sprite's bodies and export it.
Add your exported sprite JSON file to Xcode using File > Add files to "Project Name" or the plus symbol at the bottom of the project navigator.
Navigate to your project's directory and install SKImport using Cocoapods.
$ cd /to/your/project
$ touch Podfile
$ open -e Podfile
Add SKImport as a dependancy in your Podfile
platform :ios, '7.0'
pod 'SKImport'
And install
$ pod install
Import your SKImport
in your classes header (.h
) file.
#import <SKPhysicsBody+SKPhysicsBodyImport.h>
Load your sprite and set it as the physicsBody
on your sprite.
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Sprite.png"];
// Find your body file
NSString *bodyFile = [[NSBundle mainBundle] pathForResource: @"sprite" ofType: @"json"];
// Add your physics body
sprite.physicsBody = [SKPhysicsBody bodyWithFile: bodyFile];
Done! Congratulations. You're now using complex physics bodies with SpriteKit. Be sure to take a look at SpriteKit's quirks below and how to use the editor.
Designing sprites is as simple as importing your image and tracing around it. SKImport uses SpriteKit's [SKPhysicsBody bodyWithPolygonFromPath]
. This imposes a limitation of only concave polygons (as opposed to convex polygons) as bodies.
To circumvent this limitation, you simply need to split the body up into concave shapes. SKImport automatically combines multiple bodies into one SKPhysicsBody
on import.
An upcoming feature of the editor is to "decompose" convex polygons into concave polygons.
The editor has a number of shortcuts to make path editing a little bit easier.
Shortcut | Description |
---|---|
⌘ + P | Add a point to the current body. |
⌘ + B | Create a new body. |
(Shift +) Tab | Navigate between points. |
⌘ + [] | Navigate between bodies. |
(Shift +) ←, →, ↓, ↑ | Nudge the point. |
Unfortunately, due to the cripplingly bad state of browser's filesystem APIs, SKImport editor can't present a "Save as.." dialog without setting up a server and using a swf object. To circumvent this and enable SKImport's editor to work standalone, the editor opens a new tab with the content trusting you to CMD/Ctrl + S the contents to the appropriate position. Exported files a repositioned to the anchor point (0.5, 0.5), flipped vertically and scaled to the current scale value.
Setting up Cocoapods is as simple as running:
$ sudo gem install cocoapods
$ pod setup
If you run into any problems, Cocoapods awesome website should be enough to sort you out.
SKImport was designed and implemented by Adrian Cooney. Licensed under MIT.