Skip to content

Instantly share code, notes, and snippets.

@veeneck
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save veeneck/6c65a168e058863619a5 to your computer and use it in GitHub Desktop.

Select an option

Save veeneck/6c65a168e058863619a5 to your computer and use it in GitHub Desktop.
Wheel around center point
// This would be the public method callled to start a wheel
func wheelToAngle(angle:CGFloat) {
self.wheelAngle = angle
self.wheeling = true
}
// This would run each game loop to perform the wheel
func wheel() {
// Hard code the speed of the turn. Each frame, move 0.003 radians until we reach target.
let radian = self.formation!.heading - 0.003
// If we have wheeled past our target angle, stop wheeling. Process is complete.
if(radian <= self.wheelAngle) {
radian = self.wheelAngle
self.wheeling = false
}
// Only do math functions once
let cosRadian = cos(radian)
let sinRadian = sin(radian)
// Loop over each unit, and caluclate the target coordinates given the 0.003 rotation.
for (index, unit) in enumerate(self.units) {
let vector = self.formation!.getVectorAtPosition(index)
let newX = vector.dx * cosRadian - vector.dy * sinRadian
let newY = vector.dx * sinRadian + vector.dy * cosRadian
unit.moveToPoint(CGPoint(x:newX, y:newY), {})
}
// Update formation to reflect our new heading
self.formation!.heading = radian
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment