As of late 2021, we are in the process of updating Stencylpedia and fixing up outdated & broken content. Thanks for your patience!

Views

Level

Using the Accelerometer

by Jon (Updated on 2015-10-14)

Contents

• Introduction
• Accelerometer Values
• How to Use the Values
• Challenge: Low Pass Filter

Introduction

Some developers use a device's accelerometer to create games a user can control by tilting the device. One famous example is a "labyrinth" game.

Accelerometer Values

You can access accelerometer data through a trio of blocks under the User Input > Mobile category in the Design Mode Palette.

Aside: The "z" value is largely useless for 2D games. It would be triggered by laying the device flat and spinning it around.

Explanation

The values of each of these range between -1.0 and 1.0 inclusive. Those values represent how much the device is tilted in a given direction.

• An x value of 1 would mean it's tilted all the way to the right.
• An x value of -1 would mean itâ€™s tilted all the way to the left.

The values are flipped for a landscape oriented game. In other words, the values are always relative to portrait orientation and won't automatically adapt for a landscape game. See the example below for details.

Summary

Portrait Orientation

Name Description
X (Positive) Right
X (Negative) Left
Y (Positive) Up
Y (Negative) Down

Landscape Orientation

Name Description
X (Positive) Up
X (Negative) Down
Y (Positive) Left
Y (Negative) Right

Warning: Do not enable auto-rotation for a game that uses the accelerometer. This will cause the values to flip when the device's orientation changes and will create a confusing situation.

How to Use the Values

To replicate a basic tilting motion for a landscape-oriented game, create a simple behavior as shown in the image below. You can adjust the value -70 to another that suits your needs.

Why are x and y flipped? Shouldn't it be the other way around?

As stated in the previous section, the x and y values are flipped for landscape-oriented games.

Why is it -70 rather than 70?

If you input 70, the game will act the opposite of what you'd expect.

Full Explanation: As mentioned above, a positive y-accelerometer value indicates a left tilt. However, since this is opposite of how Stencyl works (right is positive, left is negative), that's why the negative sign has to be there.

Challenge: Low Pass Filter

At times, you may find the accelerometer values to be overly sensitive, leading to jerky motion or motion where none is desired.

To combat this, add some logic to throw out low, absolute values, so that the device has to be tilted beyond some minimum theshold before responding.

Disclaimer: Use comments to provide feedback and point out issues with the article (typo, wrong info, etc.). If you're seeking help for your game, please ask a question on the forums. Thanks!

nur alif ilyasa
write positive is " 70" or "70"?
0 10 years, 4 months ago
HidetoKoudanshi
A typo in the Accelerometer Values Aside: "It would be triggered it would be triggered by laying..."
1 10 years, 4 months ago
Jon
Reworked the article a bit for Stencyl 3.0. Explained the values in much more detail.
0 10 years, 6 months ago
champy
Awesome ,I will apply with my game,
0 10 years, 10 months ago
Vanz
0 10 years, 10 months ago
Jon
Most games don't use the Z-axis - it would be triggered by laying the device flat and spinning it around - not practical for most games.
0 12 years, 7 months ago
malospam
Interesting. Maybe some comments on using the z-axis would be good. Much appreciated.
0 12 years, 9 months ago