Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ViewController: UIViewController {
- var oldX: CGFloat = 0.0
- var oldY: CGFloat = 0.0
- override func viewDidLoad() {
- let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
- }
- // 行列で回転 (片軸でしか回せなかった)
- @objc func handlePan(recognizer: UIPanGestureRecognizer) {
- let translation = recognizer.translation(in: view)
- // デバイスのサイズを2 * piにマップ
- let x = translation.x / view.frame.width * .pi * 2
- // 一つ前の値と合わせて更新する量を決める
- let newX = oldX + x
- shipNode?.rotation = SCNVector4(0, 1, 0, newX)
- recognizer.setTranslation(CGPoint.zero, in: view)
- oldX = newX
- }
- // オイラー角で回転
- @objc func handlePan(recognizer: UIPanGestureRecognizer) {
- let translation = recognizer.translation(in: view)
- let x = translation.x / view.frame.width * .pi * 2
- let y = translation.y / view.frame.width * .pi * 2
- let newX = oldX + x
- let newY = oldY + y
- shipNode?.eulerAngles.y = Float(newX)
- shipNode?.eulerAngles.x = Float(newY)
- recognizer.setTranslation(CGPoint.zero, in: view)
- oldX = newX
- oldY = newY
- }
- // Quaternionで回転
- // 古い値の保持は必要ない
- @objc func handlePan(recognizer: UIPanGestureRecognizer) {
- let translation = recognizer.translation(in: view)
- let x = translation.x / view.frame.width * .pi * 2
- let y = translation.y / view.frame.width * .pi * 2
- let qy = simd_quatf(angle: Float(x), axis: float3(0, 1, 0))
- let qx = simd_quatf(angle: Float(y), axis: float3(1, 0, 0))
- shipNode?.simdLocalRotate(by: qy)
- shipNode?.simdLocalRotate(by: qx)
- recognizer.setTranslation(CGPoint.zero, in: view)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement