Created
August 27, 2014 22:53
-
-
Save tado/8d923f934ee000a85ab9 to your computer and use it in GitHub Desktop.
Swift + Playground physics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SpriteKit | |
import XCPlayground | |
let width:CGFloat = 400 | |
let height:CGFloat = 400 | |
let canvasWidth: UInt32 = UInt32(width) | |
let canvasHeight: UInt32 = UInt32(height) | |
let view:SKView = SKView(frame:CGRectMake(0, 0, width, height)) | |
let scene:SKScene = SKScene(size:CGSizeMake(width, height)) | |
scene.backgroundColor = SKColor.blackColor() | |
view.presentScene(scene) | |
scene.physicsWorld.gravity = CGVectorMake(0, -1) | |
for var i = 0; i < 100; i++ { | |
let radius:CGFloat = 6 | |
let shape = SKShapeNode(circleOfRadius:radius) | |
shape.fillColor = SKColor.blueColor() | |
shape.position = CGPoint( | |
x: CGFloat(arc4random_uniform(canvasWidth)), | |
y: CGFloat(arc4random_uniform(canvasHeight))) | |
scene.addChild(shape) | |
shape.physicsBody = SKPhysicsBody(circleOfRadius:radius) | |
shape.physicsBody.friction = 0.01 | |
shape.physicsBody.restitution = 0.99 | |
shape.physicsBody.mass = 1.0 | |
} | |
scene.physicsBody = SKPhysicsBody( | |
edgeLoopFromRect:CGRect(x:0, y:0, width:width, height:height)) | |
XCPShowView("my view", view) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!!