Skip to content
Snippets Groups Projects
Commit 1979b4c5 authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

Try Device Motion

parent b1b35689
Branches master
Tags v1.0.0
No related merge requests found
...@@ -82,38 +82,66 @@ class GameViewController: UIViewController, SKSceneDelegate { ...@@ -82,38 +82,66 @@ class GameViewController: UIViewController, SKSceneDelegate {
scnView.addGestureRecognizer(tapGesture) scnView.addGestureRecognizer(tapGesture)
motionManager = CMMotionManager() motionManager = CMMotionManager()
if (motionManager?.isAccelerometerAvailable)! { // if (motionManager?.isAccelerometerAvailable)! {
motionManager?.accelerometerUpdateInterval = 0.1 // motionManager?.accelerometerUpdateInterval = 0.1
motionManager?.startAccelerometerUpdates(to: OperationQueue.main, withHandler: { (data, error) in // motionManager?.startAccelerometerUpdates(to: OperationQueue.main, withHandler: { (data, error) in
let rotate = data!.acceleration.y // let rotate = data!.acceleration.y
//print(rotate) // //print(rotate)
let pacman = self.scene.rootNode.childNode(withName: "Pacman", recursively: true)! // let pacman = self.scene.rootNode.childNode(withName: "Pacman", recursively: true)!
let direction: Float = rotate < 0 ? 1.0 : -1.0 // let direction: Float = rotate < 0 ? 1.0 : -1.0
if abs(rotate) > 0.3 { // if abs(rotate) > 0.3 {
if !self.isRotating { // if !self.isRotating {
let action = SCNAction.rotateBy(x: 0, y: CGFloat(direction * Float.pi * 0.5), z: 0, duration: 0.25) // let action = SCNAction.rotateBy(x: 0, y: CGFloat(direction * Float.pi * 0.5), z: 0, duration: 0.25)
pacman.runAction(action) // pacman.runAction(action)
self.isRotating = true // self.isRotating = true
//
var directionVal = self.direction.rawValue + Int(direction) // var directionVal = self.direction.rawValue + Int(direction)
if directionVal == 5 { // if directionVal == 5 {
directionVal = 1 // directionVal = 1
} // }
if directionVal == 0 { // if directionVal == 0 {
directionVal = 4 // directionVal = 4
} // }
self.direction = Player.Direction(rawValue: directionVal)! // self.direction = Player.Direction(rawValue: directionVal)!
} // }
} else { // } else {
self.isRotating = false // self.isRotating = false
// }
// })
// }
if (motionManager?.isDeviceMotionAvailable)! {
motionManager?.deviceMotionUpdateInterval = 0.1
motionManager?.startDeviceMotionUpdates(to: OperationQueue.main, withHandler: { (data, error) in
if let data = data {
self.handleRotation(data: data)
// if data.userAcceleration.z < -0.1 {
// let pacman = self.scene.rootNode.childNode(withName: "Pacman", recursively: true)!
//
// pacman.position.x += -Float(data.userAcceleration.z) * pacman.rotation.x * 200000.0
// pacman.position.z += -Float(data.userAcceleration.z) * pacman.rotation.z * 200000.0
//
// print(pacman.position)
// }
} }
}) })
} }
if let view = self.view as? SCNView { if let view = self.view as? SCNView {
view.overlaySKScene = createOverlay() view.overlaySKScene = createOverlay()
} }
} }
private var lastRotation: Float = 0
func handleRotation(data: CMDeviceMotion) {
let pacman = self.scene.rootNode.childNode(withName: "Pacman", recursively: true)!
if abs(lastRotation - Float(data.attitude.yaw)) > 0.02 {
pacman.eulerAngles.y = Float(data.attitude.yaw)
lastRotation = Float(data.attitude.yaw)
print(pacman.eulerAngles.y)
}
}
func createOverlay() -> SKScene { func createOverlay() -> SKScene {
let scene = SKScene(size: self.view.frame.size) let scene = SKScene(size: self.view.frame.size)
let node = SKSpriteNode(imageNamed: "run.png") let node = SKSpriteNode(imageNamed: "run.png")
...@@ -146,7 +174,7 @@ class GameViewController: UIViewController, SKSceneDelegate { ...@@ -146,7 +174,7 @@ class GameViewController: UIViewController, SKSceneDelegate {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone { if UIDevice.current.userInterfaceIdiom == .phone {
return .landscape return .portrait
} else { } else {
return .all return .all
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment