4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

deviceorientationAPIを使う時にalpha,gammaがbeta90を超えた時に値が乱れるのをなおすやつ

Last updated at Posted at 2018-03-31

メモ

  • deviceorientation使って、デバイスの傾きで中のワールドを回転させたいときなどに、スマホを垂直にした時に表示が崩れてしまう時
  • logなどで見てるとalphaやgammaの値が突然飛ぶ時
function getQuaternion(alpha, beta, gamma) {
    var _x = beta ? beta * degtorad : 0; // beta value
    var _y = gamma ? gamma * degtorad : 0; // gamma value
    var _z = alpha ? alpha * degtorad : 0; // alpha value

    var cX = Math.cos(_x / 2);
    var cY = Math.cos(_y / 2);
    var cZ = Math.cos(_z / 2);
    var sX = Math.sin(_x / 2);
    var sY = Math.sin(_y / 2);
    var sZ = Math.sin(_z / 2);

    var w = cX * cY * cZ - sX * sY * sZ;
    var x = sX * cY * cZ - cX * sY * sZ;
    var y = cX * sY * cZ + sX * cY * sZ;
    var z = cX * cY * sZ + sX * sY * cZ;

    return [x, y, z, w];
}

#参考文献
まんまこれ
A.2 Alternate device orientation representations

4
1
1

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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?