[MetaballsKit] Just replace balls when size changes

This commit is contained in:
Eryn Wells 2017-08-06 08:25:13 -07:00
parent 357db9a248
commit fdcf62db74

View file

@ -43,7 +43,16 @@ public class Field {
let numberOfBallsBeforeFilter = balls.count let numberOfBallsBeforeFilter = balls.count
// Remove balls that fall outside the new bounds. // Remove balls that fall outside the new bounds.
balls = balls.filter { bounds.contains($0.bounds) } // balls = balls.filter { bounds.contains($0.bounds) }
// Scale balls to new position and size.
let scale = Float(size.width / oldValue.width)
balls = balls.map {
let r = $0.radius * scale
let p = randomPoint(forBallWithRadius: r)
let v = Vector(dx: $0.velocity.dx * scale, dy: $0.velocity.dy * scale)
return Ball(radius: r, position: p, velocity: v)
}
// Update Metal state as needed. // Update Metal state as needed.
populateParametersBuffer() populateParametersBuffer()
@ -91,11 +100,7 @@ public class Field {
} }
public func add(ballWithRadius radius: Float) { public func add(ballWithRadius radius: Float) {
let insetBounds = bounds.insetBy(dx: CGFloat(radius), dy: CGFloat(radius)) let position = randomPoint(forBallWithRadius: radius)
let x = Float(UInt32(insetBounds.minX) + arc4random_uniform(UInt32(insetBounds.width)))
let y = Float(UInt32(insetBounds.minY) + arc4random_uniform(UInt32(insetBounds.height)))
let position = Point(x: x, y: y)
let dx = Float(5 - Int(arc4random_uniform(10))) let dx = Float(5 - Int(arc4random_uniform(10)))
let dy = Float(5 - Int(arc4random_uniform(10))) let dy = Float(5 - Int(arc4random_uniform(10)))
@ -114,6 +119,14 @@ public class Field {
balls.removeAll(keepingCapacity: true) balls.removeAll(keepingCapacity: true)
} }
private func randomPoint(forBallWithRadius radius: Float) -> Point {
let insetBounds = bounds.insetBy(dx: CGFloat(radius), dy: CGFloat(radius))
let x = Float(UInt32(insetBounds.minX) + arc4random_uniform(UInt32(insetBounds.width)))
let y = Float(UInt32(insetBounds.minY) + arc4random_uniform(UInt32(insetBounds.height)))
let position = Point(x: x, y: y)
return position
}
// MARK: - Metal Configuration // MARK: - Metal Configuration
private var device: MTLDevice? private var device: MTLDevice?