[英]Difficulties with Windows Phone Accelerometer
I want to use the accelerometer to move a ball in a Windows Phone game. 我想使用加速度计在Windows Phone游戏中移动球。 But the ball moves not correct when I tilt the Windows Phone device. 但是当我倾斜Windows Phone设备时,球的移动不正确。
For example, if I tilt the device to the left, the ball isn't moving to the left, instead the ball moves very strange. 例如,如果我将设备向左倾斜,则球不会向左移动,而是球移动得很奇怪。 I don't know what I could change in my code so that the ball moves correctly. 我不知道我可以在代码中进行哪些更改以使球正确移动。
What should I change so that the ball moves correctly if I tilt the Windows Phone device? 如果倾斜Windows Phone设备,应如何更改以使球正确移动?
UPDATE: I changed my code but the ball doesn't move in the right direction. 更新:我更改了代码,但是球没有朝正确的方向移动。 You can download my project here: http://www.file-upload.net/download-8439759/WindowsPhoneGame22.rar.html 您可以在这里下载我的项目: http : //www.file-upload.net/download-8439759/WindowsPhoneGame22.rar.html
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Motion motion;
Texture2D Ball;
Vector2 BallPos = new Vector2(400, 300);
Vector3 accelReading = new Vector3();
Vector3 speed = new Vector3();
void motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
{
UpdateUI(e.SensorReading);
}
private void UpdateUI(MotionReading e)
{
accelReading.X = e.DeviceAcceleration.X;
accelReading.Y = e.DeviceAcceleration.Y;
accelReading.Z = e.DeviceAcceleration.Z;
accelReading.Normalize();
Vector3 currentAccelerometerState = accelReading;
if (currentAccelerometerState.X != 0)
speed += new Vector3(currentAccelerometerState.X, 0, 0);
if (currentAccelerometerState.Z != 0)
speed += new Vector3(0, 0, -currentAccelerometerState.Z);
if (speed.Length() > 2)
{
speed.Normalize();
speed *= 2;
}
BallPos.X += speed.X;
BallPos.Y += speed.Y;
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
TargetElapsedTime = TimeSpan.FromTicks(333333);
InactiveSleepTime = TimeSpan.FromSeconds(1);
graphics.IsFullScreen = true;
}
protected override void Initialize()
{
if (Motion.IsSupported)
{
motion = new Motion();
motion.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
motion.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<MotionReading>>(motion_CurrentValueChanged);
motion.Start();
}
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Ball = Content.Load<Texture2D>("ballbig");
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(Ball, BallPos, null, Color.White, rotation, new Vector2(Ball.Width/2,Ball.Height/2), 1f, SpriteEffects.None,1);
spriteBatch.End();
base.Draw(gameTime);
}
}
I did a pretty similar game on Windows8, and I use a method like this to handle the accelerometer. 我在Windows8上做了一个非常类似的游戏,并且我使用类似的方法来处理加速度计。 I hope you can find something that can help you. 希望您能找到可以帮助您的东西。
private Accelerometer _accelerometer;
public void Initialize()
{
if (_accelerometer != null)
{
_accelerometer = Accelerometer.GetDefault();
_accelerometer.ReadingChanged += _accelerometer_ReadingChanged;
}
}
private void _accelerometer_ReadingChanged(Accelerometer sender, AccelerometerReadingChangedEventArgs args)
{
AccelerometerReading reading = _accelerometer.GetCurrentReading();
Vector3 stateValue = new Vector3();
stateValue.Y = -1;
stateValue.X = (float)reading.AccelerationX;
stateValue.Z = (float)reading.AccelerationY;
stateValue.Normalize();
Vector3 currentAccelerometerState = stateValue;
Vector3 Rotation = Vector3.Zero;
if (currentAccelerometerState.X != 0)
marble.speed += new Vector3(currentAccelerometerState.X, 0, 0);
if (currentAccelerometerState.Z != 0)
marble.speed += new Vector3(0, 0, -currentAccelerometerState.Z);
if (marble.speed.Length() > 10)
{
marble.speed.Normalize();
marble.speed *= 10;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.