c# – 使用方向计算位置的Windows手机
发布时间:2020-12-15 17:22:49 所属栏目:百科 来源:网络整理
导读:我正在使用Motion API,我正试图找出我正在开发的游戏的控制方案. 我想要实现的目的是让设备直接收集到一个位置.这样,向前和向左倾斜电话表示左上方位置,向后右方向表示右下方位置. 照片使其更清晰(红点将是计算的位置). 前进和左前 背部和右边 现在是棘手的
我正在使用Motion API,我正试图找出我正在开发的游戏的控制方案.
我想要实现的目的是让设备直接收集到一个位置.这样,向前和向左倾斜电话表示左上方位置,向后右方向表示右下方位置. 照片使其更清晰(红点将是计算的位置). 前进和左前 背部和右边 现在是棘手的一点.我还必须确保这些值考虑左侧横向和右侧横向设备方向(纵向是默认值,因此不需要进行任何计算). 有人做过这样的事吗? 笔记: >我尝试过使用偏航,俯仰,滚动和四元数读数. 样品: // Get device facing vector public static Vector3 GetState() { lock (lockable) { var down = Vector3.Forward; var direction = Vector3.Transform(down,state); switch (Orientation) { case Orientation.LandscapeLeft: return Vector3.TransformNormal(direction,Matrix.CreateRotationZ(-rightAngle)); case Orientation.LandscapeRight: return Vector3.TransformNormal(direction,Matrix.CreateRotationZ(rightAngle)); } return direction; } } 解决方法
您想使用加速度传感器控制屏幕上的对象.
protected override void Initialize() { ... Accelerometer acc = new Accelerometer(); acc.ReadingChanged += AccReadingChanged; acc.Start(); ... } 这是计算对象位置的方法 void AccReadingChanged(object sender,AccelerometerReadingEventArgs e) { // Y axes is same in both cases this.circlePosition.Y = (float)e.Z * GraphicsDevice.Viewport.Height + GraphicsDevice.Viewport.Height / 2.0f; // X axes needs to be negative when oriented Landscape - Left if (Window.CurrentOrientation == DisplayOrientation.LandscapeLeft) this.circlePosition.X = -(float)e.Y * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2.0f; else this.circlePosition.X = (float)e.Y * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2.0f; } 我在游戏中使用传感器的Z轴作为我的Y,在游戏中使用传感器的Y轴作为我的X.校准将通过从中心减去传感器的Z轴来完成.这样,我们的传感器轴直接对应于屏幕上的位置(百分比). 为此,我们根本不需要X轴传感器…… 这只是快速实施.您会找到传感器的中心,因为此Viewport.Width / 2f不是中心,3次测量的总和和平均值,X传感器轴上的校准,以便您可以在平坦或某种程度的位置上播放/使用应用程序等. 此代码在Windows Phone设备上进行测试! (和工作) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |