From e786ddd5837e426b23381233296f65144fafaeb9 Mon Sep 17 00:00:00 2001 From: Francois Bertrand <francois.bertrand87@gmail.com> Date: Mon, 19 Jun 2017 18:06:55 +0200 Subject: [PATCH] Add Accelerometer movement, Changed Packman Camera --- Pacman3D-iOS/GameViewController.swift | 29 +++++++++++++++++++++++++-- Pacman3D-iOS/art.scnassets/Pacman.dae | 8 ++++---- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Pacman3D-iOS/GameViewController.swift b/Pacman3D-iOS/GameViewController.swift index bc8f7fe..0268b79 100644 --- a/Pacman3D-iOS/GameViewController.swift +++ b/Pacman3D-iOS/GameViewController.swift @@ -9,9 +9,14 @@ import UIKit import QuartzCore import SceneKit +import CoreMotion class GameViewController: UIViewController { + var motionManager: CMMotionManager? + + var isRotating: Bool = false + override func viewDidLoad() { super.viewDidLoad() @@ -71,6 +76,26 @@ class GameViewController: UIViewController { // add a tap gesture recognizer let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:))) scnView.addGestureRecognizer(tapGesture) + + motionManager = CMMotionManager() + if (motionManager?.isAccelerometerAvailable)! { + motionManager?.accelerometerUpdateInterval = 0.1 + motionManager?.startAccelerometerUpdates(to: OperationQueue.main, withHandler: { (data, error) in + let rotate = data!.acceleration.y + //print(rotate) + let pacman = scene.rootNode.childNode(withName: "Pacman", recursively: true)! + let direction: Float = rotate < 0 ? 1.0 : -1.0 + if abs(rotate) > 0.3 { + if !self.isRotating { + let action = SCNAction.rotateBy(x: 0, y: CGFloat(direction * Float.pi * 0.5), z: 0, duration: 0.25) + pacman.runAction(action) + self.isRotating = true + } + } else { + self.isRotating = false + } + }) + } } func handleTap(_ gestureRecognize: UIGestureRecognizer) { @@ -109,7 +134,7 @@ class GameViewController: UIViewController { } override var shouldAutorotate: Bool { - return false + return true } override var prefersStatusBarHidden: Bool { @@ -118,7 +143,7 @@ class GameViewController: UIViewController { override var supportedInterfaceOrientations: UIInterfaceOrientationMask { if UIDevice.current.userInterfaceIdiom == .phone { - return .portrait + return .landscape } else { return .all } diff --git a/Pacman3D-iOS/art.scnassets/Pacman.dae b/Pacman3D-iOS/art.scnassets/Pacman.dae index e1a1780..a04bb4a 100755 --- a/Pacman3D-iOS/art.scnassets/Pacman.dae +++ b/Pacman3D-iOS/art.scnassets/Pacman.dae @@ -4,8 +4,8 @@ <contributor> <authoring_tool>SceneKit Collada Exporter v1.0</authoring_tool> </contributor> - <created>2017-06-19T13:19:41Z</created> - <modified>2017-06-19T13:19:41Z</modified> + <created>2017-06-19T15:47:15Z</created> + <modified>2017-06-19T15:47:15Z</modified> <unit meter="1.000000"/> <up_axis>Z_UP</up_axis> </asset> @@ -148,7 +148,7 @@ <library_visual_scenes> <visual_scene id="scene5"> <node id="Sphere" name="Sphere"> - <matrix>1.580022 0 0 0 0 -6.9065e-08 -1.580022 0 0 1.580022 -6.9065e-08 0 0 0 0 1 </matrix> + <matrix>1.580022 0 0 0 0 -6.9065e-08 -1.580022 7.33742e-11 0 1.580022 -6.9065e-08 -0.001678586 0 0 0 1 </matrix> <instance_geometry url="#Sphere-mesh"> <bind_material> <technique_common> @@ -159,7 +159,7 @@ </instance_geometry> </node> <node id="Camera_001" name="Camera_001"> - <matrix>0.009428203 0.3446076 -0.9386549 -5.094206 -0.9999211 0.00324893 -0.008850717 0.0210471 -3.576155e-07 0.9387442 0.3446054 2.326969 0 0 0 1 </matrix> + <matrix>0.00942713 0.2635223 -0.9645737 -6.061088 -0.9999358 0.002483249 -0.009094264 0.01192989 -9.834569e-07 0.9646677 0.2635197 2.681937 0 0 0 1 </matrix> <instance_camera url="#Camera_001-camera"/> </node> </visual_scene> -- GitLab