[Metaballs] Randomly position and give velocity to balls when they are added

Add 10 balls to the field on start!
This commit is contained in:
Eryn Wells 2017-08-06 07:48:26 -07:00
parent 8dcd0910f1
commit 79236d805b
2 changed files with 15 additions and 6 deletions

View file

@ -40,7 +40,10 @@ class ViewController: NSViewController, RendererDelegate {
override func viewWillAppear() { override func viewWillAppear() {
super.viewWillAppear() super.viewWillAppear()
renderer.mtkView(metalView, drawableSizeWillChange: metalView.drawableSize) renderer.mtkView(metalView, drawableSizeWillChange: metalView.drawableSize)
field.add(ballWithRadius: 10.0) for _ in 1...10 {
let r = Float(20 + arc4random_uniform(50))
field.add(ballWithRadius: r)
}
} }
private func newErrorView() -> NSView { private func newErrorView() -> NSView {

View file

@ -87,15 +87,21 @@ public class Field {
} }
} }
} }
populateBallBuffer()
} }
public func add(ballWithRadius radius: Float) { public func add(ballWithRadius radius: Float) {
let insetBounds = bounds.insetBy(dx: CGFloat(radius), dy: CGFloat(radius)) let insetBounds = bounds.insetBy(dx: CGFloat(radius), dy: CGFloat(radius))
// let x = CGFloat(UInt32(insetBounds.minX) + arc4random_uniform(UInt32(insetBounds.width)))
// let y = CGFloat(UInt32(insetBounds.minY) + arc4random_uniform(UInt32(insetBounds.height))) let x = Float(UInt32(insetBounds.minX) + arc4random_uniform(UInt32(insetBounds.width)))
let position = Point(x: Float(insetBounds.midX), y: Float(insetBounds.midY)) let y = Float(UInt32(insetBounds.minY) + arc4random_uniform(UInt32(insetBounds.height)))
// TODO: Randomly generate velocity too. let position = Point(x: x, y: y)
let ball = Ball(radius: radius, position: position, velocity: Vector())
let dx = Float(5 - Int(arc4random_uniform(10)))
let dy = Float(5 - Int(arc4random_uniform(10)))
let velocity = Vector(dx: dx, dy: dy)
let ball = Ball(radius: radius, position: position, velocity: velocity)
balls.append(ball) balls.append(ball)
NSLog("Added ball \(ball); fieldSize=\(size)") NSLog("Added ball \(ball); fieldSize=\(size)")