LoginSignup
1
4

More than 3 years have passed since last update.

360度、デバイスの向きを測定する時の【Tips】

Posted at

Core MotionのDevice MotionかARKitでデバイスの向きはとれます。

一周は6.28318531ラジアンです。

開始原点が0ラジアン。

デバイスのY軸を中心に回転させた場合、
右回りだとラジアンが加算。
左回りだとラジアンが減算されます。

ただし、半周で+-が逆転します。
右回りの場合、3.14ラジアンまで加算すると、次は-3.14ラジアンになります。

← -3.13 ← -3.14 ← 分水嶺 ← 3.14 ← 3.13

という具合です。

条件式で右回りを判別する場合、加算しているかにくわえて、分水嶺の部分も判別しておく必要があります。

if rotation.y > recentRotation.y || (recentRotation.y > 3 && rotation.y < 0) {
// 現在のラジアンが以前のラジアンより大きい、または、以前のラジアンが3以上で現在のラジアンがマイナス値の場合、右回り
    print("turning right!")
}

分水嶺を越えてしまえば、以降右回りは加算、左回りは減算なのは同じです。


Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
MLBoysチャンネル
Medium

相棒
note

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4