Accelarometer for Windows Phone
Monkey Targets Forums/XNA/Accelarometer for Windows Phone
| ||
[Updated] Added a version for OS 7.0 which is currently used by Monkey. I've managed to implement the Accelerometer for Windows Phone. You need to do the following: ------------------- Windows Phone OS 7.0 (Current Monkey) ------------------- In program.cs (targets\xna\MonkeyGame\MonkeyGame) add the following: #if WINDOWS_PHONE using Microsoft.Devices.Sensors; #endif You will also need to add a reference to Microsoft.Devices.Sensors (folder C:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone) In mojo.xna.cs (modules\mojo\native) add the following: Line 850: public float accelX = 0; public float accelY = 0; public float accelZ = 0; Line 854: #if WINDOWS_PHONE public bool gamepadFound=true; public PlayerIndex gamepadIndex=PlayerIndex.One; public Accelerometer accel = new Accelerometer(); <-- this line #else public bool gamepadFound; public PlayerIndex gamepadIndex; #endif Line 895: (insert the constructor) #if WINDOWS_PHONE public gxtkInput(){ //Initiaise Accelerometer? if (accel.State != SensorState.NotSupported){ accel.ReadingChanged += OnAccelerometerReadingChanged; accel.Start(); } } private void OnAccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs e){ accelX = (float)e.X; accelY = (float)e.Y; accelZ = (float)e.Z; } #endif Line 1119: public virtual float AccelX() { return accelX; } public virtual float AccelY(){ { return accelY; } public virtual float AccelZ(){ { return accelZ; } ------------------- Windows Phone OS 7.1 (Mango) ------------------- In program.cs (targets\xna\MonkeyGame\MonkeyGame) add the following: #if WINDOWS_PHONE using Microsoft.Devices.Sensors; #endif You will also need to add a reference to Microsoft.Devices.Sensors (folder C:\Program Files\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone71) In mojo.xna.cs (modules\mojo\native) add the following: Line 853: #if WINDOWS_PHONE public bool gamepadFound=true; public PlayerIndex gamepadIndex=PlayerIndex.One; public Accelerometer accel = new Accelerometer(); <-- this line #else public bool gamepadFound; public PlayerIndex gamepadIndex; #endif Line 891: (insert the constructor) #if WINDOWS_PHONE public gxtkInput() { //Initialise sensor? if(accel.State != SensorState.NotSupported) accel.Start(); } #endif Replace the following methods: public virtual float AccelX() { #if WINDOWS_PHONE return accel.CurrentValue.Acceleration.X; //present on windows phone #else return 0; //no accelerator support on WINDOWS/XBOX #endif } public virtual float AccelY(){ #if WINDOWS_PHONE return accel.CurrentValue.Acceleration.Y; //present on windows phone #else return 0; //no accelerator support on WINDOWS/XBOX #endif } public virtual float AccelZ(){ #if WINDOWS_PHONE return accel.CurrentValue.Acceleration.Z; //present on windows phone #else return 0; //no accelerator support on WINDOWS/XBOX #endif } Test with the AccelTest module in Mark's folder in Bananas. |
| ||
Hi, Not working here...I'm getting the compile error: 'Microsoft.Devices.Sensors.Accelerometer' does not contain a definition for 'CurrentValue'. Inspecting the namespace in visual studio also seems to suggest there is no 'CurrentValue' property/field. It can find the Accelerometer class OK, so I assume I have the correct project references set up and a valid using statement. Perhaps it's a version issue? Is there a more recent version of the devices component? |
| ||
I badly need this so I will give it a test and post a feedback soon. |
| ||
Mark, I'll email you my project. All I've installed is the latest version of XNA Installer in January 2012. It may be that you have an older version maybe?? There was a ReadingChanged event which has been depreciated (some example code I found listed this - so perhaps CurrentValue is new). This advises the available properties: http://msdn.microsoft.com/en-us/library/microsoft.devices.sensors.accelerometer(v=vs.92).aspx |
| ||
Hi, Ok, nearly there - I had to 'upgrade windows phone projects' to upgrade the project to 7.1. Meaning it's no longer 7.0 compatible...? Now, I apparently need to install ZUNE before I can run the build on the simulator. |
| ||
Microsoft were/are very keen on getting developers to upgrade and release apps for the 7.1 Mango tools. I'd recommend moving to 7.1 as it opens up other features such as Compass and Gyroscope. It also importantly provides fast resume for apps. |
| ||
Added some code for the current Monkey XNA which uses 7.0 (infact can be updated to 7.1 also). I've investigated reading from the Compass and Gyrospope (both 7.1) - will be easy enough. One thought I had was maybe these need Start and Stop methods so they are available as required. |