Click here to Skip to main content
Click here to Skip to main content
Go to top

Working with Accelerometer Windows Phone c#

, 5 Aug 2014
Rate this:
Please Sign up or sign in to vote.
Link between Accelerometer and composant in xaml

Introduction

Accelerometer is used mostly on Games , then we can move objects with just moving our device and it's so easy to do that.

 

Using the code

Fisrt of all we create an instance of Accelerometer :

Accelerometer a = Accelerometer();

Then we precise the TimeBetweenUpdates, means that how many time you want to update the change of accelerometer. For Example : 

 a.TimeBetweenUpdates = TimeSpan.FromMilliseconds(10);

Then you define the Event CurrentValueChanged that will be called after any update to inform us the new x,y,z axes then we start.

a.CurrentValueChanged += new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(a_CurrentValueChanged);

a.Start();

so the responsible method that execute after update is a_CurrentValueChanged : 

 void a_CurrentValueChanged(Object sender, SensorReadingEventArgs<Microsoft.Devices.Sensors.AccelerometerReading> sr)
        {
            Dispatcher.BeginInvoke(() => UpdateUI(sr.SensorReading));
        }
        double x, y, z;
        void UpdateUI(Microsoft.Devices.Sensors.AccelerometerReading ar)
        {

            Vector3 xyz = ar.Acceleration;
           
            x += xyz.X;  
            y += xyz.Y;
            z += xyz.Z;


After we get the three demension of accelerometer , we can now bind it to an object.

we create for example an Ellipse : 

<Ellipse Width="50" Height="50" Fill="Blue" Name="cercle" />

on the code behind we link the Margin of the Ellipse by the Vector xyz : 

 cercle.Margin = new Thickness(x * 15, -y * 15, -x * 15, y * 15);

You can modify the Speed of the Object by multiplying  coordinates.

History

I thing it become so clear to work with accelerometer , you can now create your First Game !

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Anis Derbel
Software Developer (Junior) Microsoft Student Partners
Tunisia Tunisia
I study Software Engineering , 23 years old , I'm motivated with all Technologies of Microsoft.
Since I have been in the Community of Microsoft as Microsoft Student Partners, I developped many apps on the platform Windows and Phone. Now , it's time to share what I learn here and I'am ready to help Everyone.
You can contact me at any time (anisderbel@outlook.com)
Group type: Organisation

9 members

Follow on   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 6 Aug 2014
Article Copyright 2014 by Anis Derbel
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid