Skip to content

Instantly share code, notes, and snippets.

@krooked
Last active February 14, 2017 15:13
Show Gist options
  • Save krooked/01886e7f002eaf36162ffcc3a863cbee to your computer and use it in GitHub Desktop.
Save krooked/01886e7f002eaf36162ffcc3a863cbee to your computer and use it in GitHub Desktop.
Euler movement test
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
// Helper functions
func * (point: CGPoint, scalar: CGFloat) -> CGPoint {
return CGPoint(x: point.x * scalar, y: point.y * scalar)
}
func + (left: CGPoint, right: CGPoint) -> CGPoint {
return CGPoint(x: left.x + right.x, y: left.y + right.y)
}
func / (point: CGPoint, scalar: CGFloat) -> CGPoint {
return CGPoint(x: point.x / scalar, y: point.y / scalar)
}
// Variables
var timerCount = 0
let maxTimeCount = 100
let framerate = 1.0
var position = CGPoint(x: 0, y: 0)
var velocity = CGPoint(x: 0, y: 0)
var acceleration = CGPoint(x: 10, y: 0) // Force
let mass = 1
// Update function
func update(deltaTime: Double) {
position = position + velocity * CGFloat(deltaTime)
velocity = velocity + (acceleration / CGFloat(mass) * CGFloat(deltaTime))
}
// Run update function
while (timerCount < maxTimeCount) {
timerCount += 1
update(deltaTime: 1.0.divided(by: framerate))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment