{"id":2242,"date":"2020-06-09T18:39:24","date_gmt":"2020-06-09T10:39:24","guid":{"rendered":"https:\/\/www.clonefactor.com\/wordpress\/?p=2242"},"modified":"2020-06-09T18:39:26","modified_gmt":"2020-06-09T10:39:26","slug":"%e5%a6%82%e4%bd%95%e5%8f%8d%e8%bd%89-gyroscope-quaternion-x-axis-y-axis-%e8%bc%b8%e5%85%a5","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/math\/2242\/","title":{"rendered":"\u5982\u4f55\u53cd\u8f49 Gyroscope (Quaternion) X-axis, Y-axis \u8f38\u5165 ?"},"content":{"rendered":"\n<p>\u5176\u5be6\u6a19\u984c\u61c9\u8a72\u4fee\u6539\u70ba\u5b78\u8853\u4e00\u9ede\u7684\u8aaa\u6cd5, \u4e0d\u904e\u81ea\u5df1\u4e5f\u6703\u5fd8\u8a18\u5c31\u7b97\u4e86.<br \/>\u9019\u554f\u984c\u61c9\u8a72\u7d30\u5206\u70ba\u4e0b\u9762\u5e7e\u500b\u90e8\u5206<\/p>\n<ol>\n<li>\u5728 Local Space \u7684 Quaternion \u4e2d\u628a X-axis \u6216 Y-axis \u53cd\u8f49\u7684\u65b9\u6cd5.<\/li>\n<li>Gyroscope \u662f Right hand \u7684\u8f38\u5165, \u5982\u4f55\u628a Right hand \u8f49\u63db\u70ba Left Hand<\/li>\n<li>Gyroscope \u7684\u65b9\u5411\u662f World space, \u5982\u4f55\u628a World space \u8f49\u63db\u70ba\u904a\u6232\u4e2d\u7684 Local space.<\/li>\n<\/ol>\n\n\n\n<h2>\u53cd\u8f49 Quaternion \u67d0\u500b\u8ef8\u7684\u65b9\u6cd5 (\u9650 Local Space)<\/h2>\n<p>\u65b9\u6cd5\u5176\u5be6\u6709\u9ede\u53d6\u5de7,\u00a0<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">private Quaternion GetPlayerInput(bool isFlipX, bool isFlipY)\n{\n\tVector3 euler = GetBiasDeviceOrientation();\n\tif (isFlipX) euler.x = -euler.x;\n\tif (isFlipY) euler.y = -euler.y;\n\treturn Quaternion.Euler(euler);\n}<\/pre>\n\n\n\n<p>\u53d6\u5de7\u7684\u65b9\u6cd5\u5c31\u662f\u628a Quaternion \u8f49\u70ba Euler Angle, \u5373\u662f\u964d\u7dad, \u7531\u56db\u7dad\u964d\u5230\u4e09\u7dad.<br>\u9650\u5236\u4e5f\u5f88\u660e\u986f\u5c31\u662f <a rel=\"noreferrer noopener\" href=\"https:\/\/www.youtube.com\/watch?v=zc8b2Jo7mno\" target=\"_blank\">Gimbal lock<\/a> \u7684\u9650\u5236, <br>\u4f46\u9664\u6b64\u4ee5\u5916.\u4e26\u6c92\u6709\u751a\u9ebc\u5927\u554f\u984c.<\/p>\n\n\n\n<h2>Right hand \u8f49\u63db\u70ba Left hand<\/h2>\n<p>\u9019\u63a8\u6d89\u53ca Sin \/ Cos \u7b97\u6cd5\u7684\u6771\u897f\u6211\u5c31\u4e0d\u6df1\u7814\u4e86.<br \/>\u7e3d\u4e4b <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Gyroscope.html\" target=\"_blank\" rel=\"noopener noreferrer\">Unity3D tutorial<\/a> \u662f\u9019\u6a23\u6559\u7684\u6211\u4e5f\u6c92\u6709\u751a\u9ebc\u610f\u898b.<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">private Quaternion GyroToUnity(Quaternion q) =&gt; new Quaternion(q.x, q.y, -q.z, -q.w);<\/code><\/p>\n<p>\u00a0<\/p>\n\n\n\n<h2>World Space \u8f49\u70ba Local Space<\/h2>\n<p>\u70ba\u751a\u9ebc\u9700\u8981 World Space \u8f49\u70ba Local Space \u5462?<br \/>\u539f\u56e0\u5728\u65bc Player \u62ff\u8457\u624b\u6a5f\u7684\u6642\u5019, \u4f60\u662f\u4e0d\u77e5\u9053\u4ed6\u600e\u6a23\u62ff\u7684, \u6709\u6c92\u6709\u62ff\u8fd4\u7b49\u7b49\u7684\u610f\u601d.<br \/>\u4ee5\u5716\u7247\u8868\u793a\u5c31\u662f\u4f60\u4ee5\u70ba\u4e16\u754c<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"648\" height=\"435\" src=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/06\/Gun_World_local00.jpg\" alt=\"\" class=\"wp-image-2244\" srcset=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/06\/Gun_World_local00.jpg 648w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/06\/Gun_World_local00-300x201.jpg 300w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/06\/Gun_World_local00-359x241.jpg 359w\" sizes=\"auto, (max-width: 648px) 100vw, 648px\" \/><\/figure>\n\n\n\n<p>\u6211\u5011\u628a \u89d2\u5ea6\u756b\u51fa\u4f86\u5c31\u50cf\u4e0a\u5716\u9019\u6a23, \u5916\u9762\u8f03\u5927\u7684\u8c61\u9650\u662f\u4ee3\u8868\u4e16\u754c, \u88e1\u9762\u5c11\u7684\u8c61\u9650\u662f\u4ee3\u8868\u69cd\u6a21\u578b.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"892\" height=\"431\" src=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/06\/Gun_World_Local01.jpg\" alt=\"\" class=\"wp-image-2245\" srcset=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/06\/Gun_World_Local01.jpg 892w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/06\/Gun_World_Local01-300x145.jpg 300w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/06\/Gun_World_Local01-768x371.jpg 768w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/06\/Gun_World_Local01-359x173.jpg 359w\" sizes=\"auto, (max-width: 892px) 100vw, 892px\" \/><\/figure>\n\n\n\n<p>\u90a3\u9ebc Gryoscope \u53ef\u80fd\u6703\u7d66\u4f60\u9019\u7a2e\u626d\u8f49\u4e86\u7684\u9762\u5411.<br>\u800c\u6211\u5011\u5c31\u8981\u628a\u9019\u7a2e\u626d\u8f49\u4e86\u7684\u9762\u5411\u7cfe\u6b63. \u65b9\u6cd5\u6709\u5169\u500b, \u4f46\u539f\u7406\u662f\u4e00\u81f4\u7684.<br>\u5c31\u662f\u628a Gryoscope \u7684\u5dee\u8ddd\u6d88\u9664(\u5ee2\u8a71).<\/p>\n\n\n\n<p>Low tech \u7684\u8fa6\u6cd5\u662f, <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"131\" height=\"32\" src=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2020\/06\/Gun_World_Local_02.jpg\" alt=\"\" class=\"wp-image-2246\"\/><\/figure>\n\n\n\n<p>\u7528\u5169\u5c64 GameObject, \u5728\u6821\u6b63\u968e\u6bb5\u628a\u5916\u5c64 Gryoscope \u5beb\u5165 Input.gryoscope \u7684\u503c.<br>\u800c\u5167\u5c64\u7684 Gun Model \u5247\u626d\u5230\u4f60\u5e0c\u671b\u5c0d\u9f4a\u7684\u5730\u65b9 (e.g. \u93e1\u982d Camera \u7684\u6b63\u524d\u65b9)<\/p>\n\n\n\n<p>\u6578\u5b78\u4e00\u9ede\u7684\u8fa6\u6cd5\u5c31\u662f\u628a Quaterion \u5c0d\u6d88\u6389.<br>\u50cf\u9019\u6a23<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Quaternion SampleRotation;\nSampleRotation.Inverse() * SampleRotation; \/\/ This will zero out all rotation<\/pre>\n\n\n\n<p>\u50cf\u4e0a\u9762\u7684\u4ee3\u78bc\u4e0d\u8ad6 SampleRotation \u7684\u503c\u662f\u751a\u9ebc\u6700\u7d42\u662f\u6703\u56de\u50b3\u4e00\u500b Quaternion (0,0,1,0) \u7684\u503c. \u5c31\u662f local space \u7684\u521d\u59cb\u503c.<br>\u56e0\u70ba\u53cd\u8f49\u5f8c\u7684\u52d5\u91cf\u5c31\u6703\u5c0d\u6d88\u6389\u5de6\u53f3\/\u4e0a\u4e0b\u7684\u89d2\u5ea6\u503c, \u8b8a\u76f8\u662f\u53d6\u5f97\u4e86\u76f8\u5c0d\u7684 Local space quaternion.<\/p>\n\n\n\n<p>\u6240\u4ee5\u5728 Awake \u6642\u5148\u628a Gyroscope \u7684\u503c\u66ab\u5b58.<br>\u7136\u5f8c\u5728\u9700\u8981\u4f7f\u7528\u6642\u628a Quaternion \u7576\u4f5c\u89d2\u52d5\u91cf\u4f86\u7b97\u7684\u8a71, \u5148 Inverse \u4e00\u4e0b\u5148\u524d\u7684\u52d5\u91cf\u518d\u758a\u52a0\u73fe\u5728 Gyroscope \u7684\u52d5\u91cf<br>\u7576\u6211\u5011\u662f Local space \u7684\u6642\u5019, \u5c31\u53ef\u4ee5\u5957\u7528\u958b\u982d\u7684 Euler angle \u65b9\u5f0f\u5c31\u53ef\u4ee5\u5957\u7528\u4e0b\u53bb.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Quaternion m_WorldSpaceOrientationBias;\nprivate void Awake()\n{\n\tm_WorldSpaceOrientationBias = GyroToUnity(Input.gyro.attitude);\n}\n\nprivate Quaternion GetPlayerInput(bool isFlipX, bool isFlipY)\n{\n\tVector3 euler = GetBiasDeviceOrientation();\n\tif (isFlipX) euler.x = -euler.x;\n\tif (isFlipY) euler.y = -euler.y;\n\treturn Quaternion.Euler(euler);\n}\nprivate Quaternion GetBiasDeviceOrientation()\n{\n\treturn m_WorldSpaceOrientationBias.Inverse() * GyroToUnity(Input.gyro.attitude);\n}\nprivate Quaternion GyroToUnity(Quaternion q) => new Quaternion(q.x, q.y, -q.z, -q.w);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5176\u5be6\u6a19\u984c\u61c9\u8a72\u4fee\u6539\u70ba\u5b78\u8853\u4e00\u9ede\u7684\u8aaa\u6cd5, \u4e0d\u904e\u81ea\u5df1\u4e5f\u6703\u5fd8\u8a18\u5c31\u7b97\u4e86.\u9019\u554f\u984c\u61c9\u8a72\u7d30\u5206\u70ba\u4e0b\u9762\u5e7e\u500b\u90e8\u5206 \u5728 Loc &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78],"tags":[74],"class_list":["post-2242","post","type-post","status-publish","format-standard","hentry","category-math","tag-quaternion"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2242"}],"version-history":[{"count":2,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2242\/revisions"}],"predecessor-version":[{"id":2286,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2242\/revisions\/2286"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}