Skip to content

Instantly share code, notes, and snippets.

@tado
Created August 27, 2014 22:53
Show Gist options
  • Save tado/8d923f934ee000a85ab9 to your computer and use it in GitHub Desktop.
Save tado/8d923f934ee000a85ab9 to your computer and use it in GitHub Desktop.
Swift + Playground physics
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)
Copy link

ghost commented Oct 9, 2016

Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment