Windows phone using orientation to calculate position

I am using the Motion API, and I am trying to figure out the game control scheme that I am currently developing.

What I'm trying to achieve is linking a device to a connection directly to a position. Thus, tilting the phone forward and left represents the upper left position, and back to the right will be the lower right position.

Photos to make it clearer (the red dot will be the calculated position).

Tilt left top
Forward and left

Tilt right bottom
Back and right

Now for the difficult bit. I also need to make sure that the values ​​take into account the orientation of the left landscape and the right landscape device (default portrait, so no calculations are needed for it).

Has anyone done something like this?

Notes:

  • I tried using yaw, pitch, roll, and quaternion.

  • I just realized that the behavior I'm talking about would be much higher than the level.

Example:

// 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; } } 
+4
source share
1 answer

You want to monitor an object on the screen using an acceleration sensor.

 protected override void Initialize() { ... Accelerometer acc = new Accelerometer(); acc.ReadingChanged += AccReadingChanged; acc.Start(); ... } 

This is a method that calculates the position of an object.

 void AccReadingChanged(object sender, AccelerometerReadingEventArgs e) { // Y axes is same in both cases this.circlePosition.Y = (float)eZ * 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)eY * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2.0f; else this.circlePosition.X = (float)eY * GraphicsDevice.Viewport.Width + GraphicsDevice.Viewport.Width / 2.0f; } 

I use the Z axis of the sensor as my Y in the game and the Y axis of the sensor as my X in the game. Calibration will be performed by subtracting the Z axis of the sensor from the center. Thus, our sensor axes directly correspond to the position (in percent) on the screen.

For this, we don’t need the X axis of the sensor at all ...

This is just a quick implementation. You will find the center for the sensor, since this Viewport.Width / 2f not the central, total and average of the three dimensions, calibration along the X-axis, so you can play / use the application in a flat or some position, etc.

This code has been tested on a Windows Phone device! (and working)

+2
source

Source: https://habr.com/ru/post/1389189/


All Articles