C# 我的加速计代码有什么问题?

C# 我的加速计代码有什么问题?,c#,.net,xna,windows-phone,accelerometer,C#,.net,Xna,Windows Phone,Accelerometer,我一直在努力学习XNA,虽然我没有阅读书籍,但我一直依靠MSDN和教程来指导我,现在我可以绘制精灵,改变它们的位置并使用Intersect,我现在正在尝试为Windows Phone 7和8手机提供一个基本的加速计 我有一些拦网和一个球。我想通过上/下/左/右倾斜手机来移动球(或滚动球) 如下图所示,绿点表示球可以移动的区域。虽然深灰色的方块只是边界,如果球碰到它们,它会从中反弹(稍后,我还不关心这部分) 我的问题是,如何让球对倾斜动作做出正确反应 要尝试获得非常基本的倾斜运动,我有:

我一直在努力学习XNA,虽然我没有阅读书籍,但我一直依靠MSDN和教程来指导我,现在我可以绘制精灵,改变它们的位置并使用Intersect,我现在正在尝试为Windows Phone 7和8手机提供一个基本的加速计

我有一些拦网和一个球。我想通过上/下/左/右倾斜手机来移动球(或滚动球)

如下图所示,绿点表示球可以移动的区域。虽然深灰色的方块只是边界,如果球碰到它们,它会从中反弹(稍后,我还不关心这部分)

我的问题是,如何让球对倾斜动作做出正确反应

要尝试获得非常基本的倾斜运动,我有:

    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        Accelerometer accelerometer = new Accelerometer();
        accelerometer.CurrentValueChanged += accelerometer_CurrentValueChanged;
        accelerometer.Start();

        base.Initialize();
    }

    void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e)
    {

            this.squarePosition.X = (float)e.SensorReading.Acceleration.Z * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2.0f;

            if (Window.CurrentOrientation == DisplayOrientation.LandscapeLeft)
            {
                this.squarePosition.X = +(float)e.SensorReading.Acceleration.X * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2;
            }
            else
            {
                this.squarePosition.X = (float)e.SensorReading.Acceleration.X * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2;
            }
        }
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
加速计加速计=新加速计();
加速计.CurrentValueChanged+=加速计\u CurrentValueChanged;
加速度计。开始();
base.Initialize();
}
无效加速计\u CurrentValueChanged(对象发送器、传感器读取事件参数e)
{
这个.squarePosition.X=(float)e.SensorReading.Acceleration.Z*GraphicsDevice.Viewport.Width+GraphicsDevice.Viewport.Width/2.0f;
if(Window.CurrentOrientation==DisplayOrientation.LandscapeLeft)
{
这个.squarePosition.X=+(float)e.SensorReading.Acceleration.X*GraphicsDevice.Viewport.Width+GraphicsDevice.Viewport.Width/2;
}
其他的
{
这个.squarePosition.X=(float)e.SensorReading.Acceleration.X*GraphicsDevice.Viewport.Width+GraphicsDevice.Viewport.Width/2;
}
}
但这绝对令人震惊。这是非常紧张的,就像,物体不断地跳跃,好像它发作了什么,我甚至不确定这是正确的方法


您需要对输入进行某种平滑处理,以消除这种抖动

这里有一个Micrsoft博客,其中包含一些相关信息:


特别是,请看标题为“查看数据平滑”的部分。您的主要问题是直接将加速度与位置联系起来,而不是使用加速度影响速度和速度影响球的位置。你已经做到了,所以球的位置是你倾斜的程度,而不是根据你倾斜的程度来加速

当前,每当加速计读数发生变化时,球的位置将设置为读数。您需要将球的加速度指定给读数,并通过该加速度周期性地增加速度,同时也通过该速度周期性地增加球的位置

XNA可能有一种内置的方法来实现这一点,但我不了解XNA,所以我可以向您展示我如何在没有框架/库帮助的情况下实现这一点:

// (XNA probably has a built-in way to handle acceleration.)
// (Doing it manually like this might be informative, but I doubt it's the best way.)
Vector2 ballPosition; // (when the game starts, set this to where the ball should be)
Vector2 ballVelocity;
Vector2 ballAcceleration;

...

void accelerometer_CurrentValueChanged(object sender, SensorReadingEventArgs<AccelerometerReading> e) {
    var reading = e.SensorReading.Acceleration;
    // (below may need to be -reading.Z, or even another axis)
    ballAcceleration = new Vector2(reading.Z, 0);
}

// (Call this at a consistent rate. I'm sure XNA has a way to do so.)
void AdvanceGameState(TimeSpan period) {
    var t = period.TotalSeconds;
    ballPosition += ballVelocity * t + ballAcceleration * (t*t/2);
    ballVelocity += ballAcceleration * t;

    this.squarePosition = ballPosition;
}
//(XNA可能有一种处理加速的内置方式。)
//(像这样手动操作可能会提供信息,但我怀疑这是最好的方法。)
矢量2球位;//(比赛开始时,将此设置为球应该在的位置)
矢量2球速度;
矢量2加速;
...

无效加速计\u CurrentValueChanged(对象发送器、传感器读取事件参数().

我从来没有为加速度计编写过代码,因此我无法为您提供太多帮助。不过,有件事困扰着我:好吧,加速度计检测到速度的变化。因此,您不应该在每次加速度计值变化时都更新位置:您应该在XNA
update
方法中,根据la改变位置测试加速计的值。这样,如果你倾斜手机,然后停止移动,球将继续平稳移动。或者你可以使用陀螺仪:另外,你将从加速计的Z轴读取的值指定给
squarePosition.X
,然后用从X轴读取的值覆盖它。我继续想两个赋值中的e应该是
squarePosition.Y