Skip to content

Commit

Permalink
Added all boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveBarnegren committed Jun 28, 2020
1 parent b6ec5e9 commit 8e28d62
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
10 changes: 8 additions & 2 deletions PhysicsDemo/Physics System/PhysicsSimulation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ class PhysicsSimulation {

switch boundary.orientation {
case .minX:
fatalError()
if ball.minX < boundary.value {
ball.position.x = boundary.value + ball.radius
ball.velocity.x = abs(ball.velocity.x) * boundary.elasticity * ball.elasticity
}
case .maxX:
if ball.maxX > boundary.value {
ball.position.x = boundary.value - ball.radius
Expand All @@ -141,7 +144,10 @@ class PhysicsSimulation {
ball.velocity.y = abs(ball.velocity.y) * boundary.elasticity * ball.elasticity
}
case .maxY:
fatalError()
if ball.maxY > boundary.value {
ball.position.y = boundary.value - ball.radius
ball.velocity.y = -abs(ball.velocity.y) * boundary.elasticity * ball.elasticity
}
}
}

Expand Down
36 changes: 27 additions & 9 deletions PhysicsDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,8 @@ class ViewController: NSViewController {
simulation = PhysicsSimulation()
simulation.gravity = -10

// Add bottom Boundary
let bottomBoundary = Boundary(orientation: .minY)
bottomBoundary.value = 20
simulation.add(boundary: bottomBoundary)

// Add right Boundary
let rightBoundary = Boundary(orientation: .maxX)
rightBoundary.value = simulationSize.width - 20
simulation.add(boundary: rightBoundary)
// Add boundaries
addSimulationBoundaries()

// Setup input handlers
ballPlacementHandlers = [InputHandlerPlaceBall(), InputHandlerFlingBall()]
Expand All @@ -66,6 +59,31 @@ class ViewController: NSViewController {
pushInputHandler(baseInputHandler)
}

func addSimulationBoundaries() {

let inset = 20.0

// Bottom
let bottomBoundary = Boundary(orientation: .minY)
bottomBoundary.value = inset
simulation.add(boundary: bottomBoundary)

// Right
let rightBoundary = Boundary(orientation: .maxX)
rightBoundary.value = simulationSize.width - inset
simulation.add(boundary: rightBoundary)

// Top
let topBoundary = Boundary(orientation: .maxY)
topBoundary.value = simulationSize.height - inset
simulation.add(boundary: topBoundary)

// Left
let leftBoundary = Boundary(orientation: .minX)
leftBoundary.value = inset
simulation.add(boundary: leftBoundary)
}

override func viewDidAppear() {
super.viewDidAppear()

Expand Down

0 comments on commit 8e28d62

Please sign in to comment.