12,744

Views

Beginner

Level

7

Comments

Using the Accelerometer

by Jon (Updated on 2014-01-30)


Contents

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

 

Introduction

Some games use an accelerometer to create games a user can control by tilting the device. One famous example is the Labyrinth game.

Coming from Stencyl 2.2 and below? You no longer need to enable the accelerometer to use it.

 

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 it would be triggered by laying the device flat and spinning it around.

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.

For example, an x value of 1 would mean the user is tilting it 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.

In summary...

Portrait Orientation

  • X (Positive) = Right
  • X (Negative) = Left
  • Y (Positive) = Up
  • Y (Negative) = Down

Landscape Orientation

  • 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 and generally create a confusing situation.


How to Use the Values

To replicate a basic tilting motion on 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 because the device doesn't "know" that a game is oriented in landscape.

 

Why is it negative 70 rather than positive 70?

If you input positive 70, the game will act the opposite of what you'd expect. The reason is because the y-value of the accelerometer (which we are using to set the x-speed) is positive when tilted left and negative when tilted right.

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: All articles are geared towards Stencyl 3.0 and above. 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!

7 Comments

nur alif ilyasa
write positive is " 70" or "70"?
0 5 months ago
HidetoKoudanshi
A typo in the Accelerometer Values Aside: "It would be triggered it would be triggered by laying..."
1 5 months, 1 week ago
Jon
Reworked the article a bit for Stencyl 3.0. Explained the values in much more detail.
0 7 months ago
champy
Awesome ,I will apply with my game,
0 11 months, 1 week ago
Vanz
when using this accelerometer feature, physics bounce does not seemed to work. Anybody knows about this?
0 11 months, 2 weeks 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 2 years, 7 months ago
malospam
Interesting. Maybe some comments on using the z-axis would be good. Much appreciated.
0 2 years, 9 months ago

Sign In to Comment