Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. class ViewController: UIViewController {
  2. var oldX: CGFloat = 0.0
  3. var oldY: CGFloat = 0.0
  4.  
  5. override func viewDidLoad() {
  6. let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
  7. }
  8.  
  9. // 行列で回転 (片軸でしか回せなかった)
  10. @objc func handlePan(recognizer: UIPanGestureRecognizer) {
  11. let translation = recognizer.translation(in: view)
  12.  
  13. // デバイスのサイズを2 * piにマップ
  14. let x = translation.x / view.frame.width * .pi * 2
  15.  
  16. // 一つ前の値と合わせて更新する量を決める
  17. let newX = oldX + x
  18. shipNode?.rotation = SCNVector4(0, 1, 0, newX)
  19. recognizer.setTranslation(CGPoint.zero, in: view)
  20. oldX = newX
  21. }
  22.  
  23. // オイラー角で回転
  24. @objc func handlePan(recognizer: UIPanGestureRecognizer) {
  25. let translation = recognizer.translation(in: view)
  26.  
  27. let x = translation.x / view.frame.width * .pi * 2
  28. let y = translation.y / view.frame.width * .pi * 2
  29.  
  30. let newX = oldX + x
  31. let newY = oldY + y
  32.  
  33. shipNode?.eulerAngles.y = Float(newX)
  34. shipNode?.eulerAngles.x = Float(newY)
  35.  
  36. recognizer.setTranslation(CGPoint.zero, in: view)
  37.  
  38. oldX = newX
  39. oldY = newY
  40. }
  41.  
  42. // Quaternionで回転
  43. // 古い値の保持は必要ない
  44. @objc func handlePan(recognizer: UIPanGestureRecognizer) {
  45. let translation = recognizer.translation(in: view)
  46.  
  47. let x = translation.x / view.frame.width * .pi * 2
  48. let y = translation.y / view.frame.width * .pi * 2
  49.  
  50. let qy = simd_quatf(angle: Float(x), axis: float3(0, 1, 0))
  51. let qx = simd_quatf(angle: Float(y), axis: float3(1, 0, 0))
  52. shipNode?.simdLocalRotate(by: qy)
  53. shipNode?.simdLocalRotate(by: qx)
  54.  
  55. recognizer.setTranslation(CGPoint.zero, in: view)
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement