菜鸟教程小白 发表于 2022-12-13 01:40:17

ios - iPhone 旋转应用于 3d 对象


                                            <p><p>您从 CMAttitude 获得的四元数似乎有缺陷。在 Unity 中,我可以获取 iPhone 的旋转,将其应用于 3d 对象,并且当您旋转 iPhone 时,该对象将旋转。在 Xcode 中似乎有所不同。</p>

<p>要设置快速测试项目,请按以下步骤操作:</p>

<ol>
<li><p>在 Xcode 中,从 <strong>iOS > 游戏模板</strong> 创建一个新项目(这为我们提供了一个 3d 对象进行测试)。</p></li>
<li><p>在 <code>GameViewController.h</code> 中添加 <code>#import <CoreMotion/CoreMotion.h></code> 和 <code>@property (strong, nonatomic) CMMotionManager *motionManager;</code></p></li>
<li><p>在 <code>GameViewController.m</code> 中删除第 46 行的动画 <code>[ship runAction: etc..</code> 并且不允许在第 55 行进行相机控制(没必要)</p></li>
<li><p>在<code>GameViewController.m</code>添加到ViewDidLoad的底部</p>

<pre><code>self.motionManager = [ init];
self.motionManager.deviceMotionUpdateInterval = .1;


   withHandler:^(CMDeviceMotion *motion, NSError *error) {

         CMQuaternion q = motion.attitude.quaternion;
         ship.rotation = SCNVector4Make(q.x, q.y, q.z, q.w);

         if(error){ NSLog(@&#34;%@&#34;, error); }
} ];
</code></pre> </li>
<li><p>为避免麻烦,在项目 > 目标 > <em>项目名称</em>> 部署信息 > 设备方向中,仅允许纵向(或仅锁定 iPhone 的旋转)</p></li>
</ol>

<p>当我将它构建到我的 iPhone 时,3d 对象(飞机)不会跟随手机的旋转。投球类作品。</p>

<p>这是故意的吗?我怎么用错了?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在 3D 空间中有不同的旋转表示。
您正在使用 <code>SCNNode</code> 的 <code>rotation</code> 属性:</p>

<blockquote>
<p>The four-component rotation vector specifies the direction
of the rotation axis in the first three components and the
angle of rotation (in radians) in the fourth.</p>
</blockquote>

<p>但您将设备旋转指定为 <a href="https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation" rel="noreferrer noopener nofollow">quaternion</a> .</p>

<p>将四元数分配给 <a href="https://developer.apple.com/documentation/scenekit/scnnode/1408048-orientation" rel="noreferrer noopener nofollow"><code>orientation</code></a> <code>SCNNode</code> 的属性或使用 <a href="https://developer.apple.com/documentation/coremotion/cmattitude" rel="noreferrer noopener nofollow"><code>CMAttitude</code></a> 的偏航角、俯仰角和滚动角并将这些分配给 <a href="https://developer.apple.com/documentation/scenekit/scnnode/1407980-eulerangles" rel="noreferrer noopener nofollow"><code>eulerAngles</code></a> <code>SCNNode</code>的属性:</p>

<blockquote>
<p>The node’s orientation, expressed as pitch, yaw, and roll angles,
each in radians.</p>
</blockquote></p>
                                   
                                                <p style="font-size: 20px;">关于ios - iPhone 旋转应用于 3d 对象,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44604662/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44604662/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - iPhone 旋转应用于 3d 对象