Rough implementation of the metaballs simulation in MetaballsKit
This commit is contained in:
parent
2099749071
commit
440ed1b3f3
6 changed files with 427 additions and 0 deletions
26
MetaballsKit/Info.plist
Normal file
26
MetaballsKit/Info.plist
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>en</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>$(CURRENT_PROJECT_VERSION)</string>
|
||||
<key>NSHumanReadableCopyright</key>
|
||||
<string>Copyright © 2017 Eryn Wells. All rights reserved.</string>
|
||||
<key>NSPrincipalClass</key>
|
||||
<string></string>
|
||||
</dict>
|
||||
</plist>
|
78
MetaballsKit/Metaballs.swift
Normal file
78
MetaballsKit/Metaballs.swift
Normal file
|
@ -0,0 +1,78 @@
|
|||
//
|
||||
// Metaballs.swift
|
||||
// Metaballs
|
||||
//
|
||||
// Created by Eryn Wells on 7/30/17.
|
||||
// Copyright © 2017 Eryn Wells. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public struct Ball {
|
||||
let radius: CGFloat
|
||||
var position = CGPoint()
|
||||
var velocity = CGVector()
|
||||
|
||||
internal var bounds: CGRect {
|
||||
let diameter = radius * 2
|
||||
return CGRect(x: position.x - radius, y: position.y - radius, width: diameter, height: diameter)
|
||||
}
|
||||
|
||||
init(radius r: CGFloat) {
|
||||
radius = r
|
||||
}
|
||||
|
||||
internal mutating func update() {
|
||||
position.x += velocity.dx
|
||||
position.y += velocity.dy
|
||||
}
|
||||
}
|
||||
|
||||
public struct Field {
|
||||
var size: CGSize
|
||||
private(set) var balls = [Ball]()
|
||||
|
||||
internal var bounds: CGRect {
|
||||
return CGRect(origin: CGPoint(), size: size)
|
||||
}
|
||||
|
||||
init(size s: CGSize) {
|
||||
size = s
|
||||
}
|
||||
|
||||
public func update() {
|
||||
let selfBounds = bounds
|
||||
for var ball in balls {
|
||||
// Update position of ball.
|
||||
ball.update()
|
||||
|
||||
if !selfBounds.contains(ball.position) {
|
||||
// Degenerate case. If the ball finds itself outside the bounds of the field, plop it back in the center.
|
||||
ball.position = CGPoint(x: selfBounds.midX, y: selfBounds.midY)
|
||||
} else {
|
||||
// Do collision detection with walls.
|
||||
let ballBounds = ball.bounds
|
||||
if !selfBounds.contains(ballBounds) {
|
||||
if ballBounds.minX < selfBounds.minX || ballBounds.maxX > selfBounds.maxX {
|
||||
ball.velocity.dx *= -1
|
||||
}
|
||||
if ballBounds.minY < selfBounds.minY || ballBounds.maxY > selfBounds.maxY {
|
||||
ball.velocity.dy *= -1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func sample(at point: CGPoint) throws -> CGFloat {
|
||||
return 0.0
|
||||
}
|
||||
|
||||
public mutating func add(ball: Ball) throws {
|
||||
guard bounds.contains(ball.bounds) else {
|
||||
/// TODO: Throw an error.
|
||||
return
|
||||
}
|
||||
balls.append(ball)
|
||||
}
|
||||
}
|
19
MetaballsKit/MetaballsKit.h
Normal file
19
MetaballsKit/MetaballsKit.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
//
|
||||
// MetaballsKit.h
|
||||
// MetaballsKit
|
||||
//
|
||||
// Created by Eryn Wells on 7/30/17.
|
||||
// Copyright © 2017 Eryn Wells. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
//! Project version number for MetaballsKit.
|
||||
FOUNDATION_EXPORT double MetaballsKitVersionNumber;
|
||||
|
||||
//! Project version string for MetaballsKit.
|
||||
FOUNDATION_EXPORT const unsigned char MetaballsKitVersionString[];
|
||||
|
||||
// In this header, you should import all the public headers of your framework using statements like #import <MetaballsKit/PublicHeader.h>
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue