Access the accelerometer on Windows Phone 8

Get help using Construct 2

Post » Tue Mar 19, 2013 2:58 pm

I'm having trouble getting the accelerometer to work. All the touch expressions return 0 and the infinite jumping sample project doesn't even work.

Is there are way to get it to work? Or is this simply a feature not yet implemented in Construct 2?
B
6
S
1
Posts: 5
Reputation: 960

Post » Tue Mar 19, 2013 3:16 pm

It does support accelerometer (but not the 'with G' expressions) and inclinometer in Windows 8 apps, but only if the device has support for them.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Tue Mar 19, 2013 3:53 pm

@Ashley I thought Windows Phone 8 doesn't support accelerometer?

@Panacea if you post a request in my Windows Phone plugin thread I can take a look into it (won't be for a couple of weeks I'm afraid as I'm out the country).
Moderator
B
72
S
13
G
11
Posts: 900
Reputation: 11,783

Post » Tue Mar 19, 2013 5:23 pm

The IE engine doesn't support the standardised motion or orientation events yet, so we added support in the Touch plugin to use the Windows 8-app specific libraries to detect motion and orientation. However that will only work if the device has the necessary hardware.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,580

Post » Tue Mar 19, 2013 6:29 pm

Well, my device is a Lumia 820 so my assumption is that it does support orientation. The accelerationXYZ expressions in the touch object return 0. How do I access this "inclinometer"?
B
6
S
1
Posts: 5
Reputation: 960

Post » Fri Mar 29, 2013 9:23 am

It does not work for me too. Just tried the sample jump project, the phone just vibrate when I tilt it.
B
12
S
4
G
1
Posts: 104
Reputation: 1,512

Post » Tue May 14, 2013 1:38 pm

Hi all, I had the same problem and I found a workaround to access accelerometer data. Unfortunately Microsoft doesn't support standard HTML5 deviceorientation and devicemotion events, so you have to deal with deep code changes in your visual studio project.

The following code snippets are simply a workaround to pass accelerometer data from C# code to the webbrowser control in an extremely unelegant way. You may notice some performance drop and you'll have to deal with trigonometry to obtain correct values from sensors.

I needed only touch.beta values on a landscape app, if you are familiar with Visual Studio and Trigonometry you can mod this code to obtain also alpha and gamma values.

1) Export your game as a Windows Phone 8 project without minifying the script.

2) Open Mainpage.Xaml in VS2012 and add the following line before the end of the tag phone:WebBrowser
ScriptNotify="Browser_ScriptNotify"

3) Open Mainpage.Xaml.cs and search for this.
   public void OnAppDeactivated()
        {
            Browser.InvokeScript("eval", "if (window.C2WP8Notify) C2WP8Notify('deactivated');");
        }

Add this snippet under these lines of code.

private void Browser_ScriptNotify(object sender, NotifyEventArgs e)
        {
            if (e.Value == "startAccelerometer")
            {
               if (accelerometer == null)
               {
                    accelerometer = new Microsoft.Devices.Sensors.Accelerometer { TimeBetweenUpdates = TimeSpan.FromMilliseconds(100) };
                    accelerometer.CurrentValueChanged += (o, args) => Dispatcher.BeginInvoke(() =>
                    {
                        var x = args.SensorReading.Acceleration.X;
                        var y = args.SensorReading.Acceleration.Y;
                        var accbeta = (Math.Atan2(-x, y) * 180.0 / Math.PI) - 90;

                        Browser.InvokeScript("eval", string.Format("accelerometerCallback({0})", accbeta));
                    });
                    accelerometer.Start();
               }
            }
        }

4) Open index.html and replace tag <body> with <body onload="onLoad()">

5) Open c2runtime.js and add on top these lines

var wp8_beta = {};
function onLoad() {
      window.external.notify("startAccelerometer");
      }
function accelerometerCallback(accbeta) {
      wp8_beta = accbeta;
      }

6) Find on c2runtime.js the line

return this.orient_beta;

and replace with

return wp8_beta;

Now you'll have touch.beta values correctly passed from wp8 to your game.
Since I suck at trigonometry I hope someone will mod the code to obtain also alpha and gamma values.

B
11
S
3
Posts: 224
Reputation: 2,028

Post » Thu Mar 19, 2015 5:05 pm

And how does it now works in VS 2013 with universial +8.1 apps?
B
10
S
4
Posts: 5
Reputation: 966


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 19 guests