我正在创建一个应用程序,其中水将从底部流向顶部。我使用来自https://github.com/FlexMonkey/ParticleLab的源代码做了很多更改
请在下面的链接上找到视频,这看起来像牛奶,但我正试图让它成为真正的水流。 http://expirebox.com/download/6e3c11ca4fb969df976b51628d1d9c89.html
func resetParticles(edgesOnly: Bool = false, distribution: Distribution = Distribution.Gaussian)
{
func rand() -> Float32
{
return Float(drand48() * -20.005)
}
func rand2() -> Float32
{
return Float(drand48() * 4)
}
let imageWidthDouble = Double(imageWidth/8)
let imageHeightDouble = Double(Float(imageHeight)/1)
let randomSource = GKRandomSource()
let randomWidth: GKRandomDistribution
let randomHeight: GKRandomDistribution
switch distribution
{
case .Gaussian:
randomWidth = GKGaussianDistribution(randomSource: randomSource, lowestValue: 0, highestValue: Int(imageWidthDouble))
randomHeight = GKGaussianDistribution(randomSource: randomSource, lowestValue: 0, highestValue: Int(imageHeightDouble))
case .Uniform:
randomWidth = GKShuffledDistribution(randomSource: randomSource, lowestValue: 0, highestValue: Int(imageWidthDouble))
randomHeight = GKShuffledDistribution(randomSource: randomSource, lowestValue: 0, highestValue: Int(imageHeightDouble))
}
for index in particlesParticleBufferPtr.startIndex ..< particlesParticleBufferPtr.endIndex
{
var positionAX = Float(randomWidth.nextInt())
var positionAY = Float(randomHeight.nextInt())
var positionBX = Float(randomWidth.nextInt())
var positionBY = Float(randomHeight.nextInt())
var positionCX = Float(randomWidth.nextInt())
var positionCY = Float(randomHeight.nextInt())
var positionDX = Float(randomWidth.nextInt())
var positionDY = Float(randomHeight.nextInt())
// print("\(positionAX) \(positionBX) \(positionCX) \(positionDX)")
// print("\(positionAY) \(positionBY) \(positionCY) \(positionDX)")
if edgesOnly
{
let positionRule = Int(arc4random() % 4)
if positionRule == 0
{
positionAX = 0
positionBX = 0
positionCX = 0
positionDX = 0
}
else if positionRule == 1
{
positionAX = Float(imageWidth)
positionBX = Float(imageWidth)
positionCX = Float(imageWidth)
positionDX = Float(imageWidth)
}
else if positionRule == 2
{
positionAY = 0
positionBY = 0
positionCY = 0
positionDY = 0
}
else
{
positionAY = Float(imageHeight)
positionBY = Float(imageHeight)
positionCY = Float(imageHeight)
positionDY = Float(imageHeight)
}
}
let particle = Particle(A: Vector4(x: positionAX + 400 + rand(), y: positionAY, z: -rand2(), w: rand()),
B: Vector4(x: positionBX + 400 + rand(), y: positionBY, z: rand2(), w: rand()),
C: Vector4(x: positionCX + 400 + rand(), y: positionCY, z: -rand2(), w: rand()),
D: Vector4(x: positionDX + 400 + rand(), y: positionDY, z: rand2(), w: rand()))
particlesParticleBufferPtr[index] = particle
}
}