微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 使用方向计算位置的Windows手机

我正在使用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设备上进行测试! (和工作)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐