0

So I'm creating a game for android that uses the roll of the device to set the position of the main character. The position is updated everytime the .onSensorChanged(event) method gets run. The problem is, even when I run my app and set my phone on the tabletop, the values change significantly (about two degrees). The sensitivity of the data collected also doesn't seem to be very precise, as difference in angle seem to be in increments of about 0.4 degrees every change. Using

Log.e(TAG, "roll: " + event.values[2]); 

Sequential degree output looks like this:

roll: 2.265
roll: 2.265
roll: 2.265
roll: 1.843
roll: 2.265
roll: 2.265
roll: 2.75
roll: 2.265

I've also implemented this algorithm to limit my character's movement in the screen, but it's not enough and severely limits the speed and responsiveness of the character's movement with regards to the current roll data (I would like the character's placement to be as close to 1-1 with regards to roll as possible).

public void onSensorChanged(SensorEvent event)
{
    if (event.sensor.getType() == Sensor.TYPE_ORIENTATION)
    {
    float newX = -(int)event.values[2] * CCDirector.sharedDirector().displaySize().width/10.0f + 
          CCDirector.sharedDirector().displaySize().width/2.0f;

            sam.updatePosition(newX);

    Log.e(TAG, "roll: " + event.values[2]);
     }
}

public void updatePosition(float newX)
{
    float winSizeX = CCDirector.sharedDirector().displaySize().width;
    float contentWidth = this.sprite.getContentSize().width;

    //tries to eliminate jumpy behaviour by the character by limiting his speed
    double tolerance = 0.01;
    if (Math.abs(newX - this.sprite.getPosition().x) > winSizeX * tolerance) 
        newX = this.sprite.getPosition().x - ((this.sprite.getPosition().x - newX) * 0.1f);

    this.sprite.setPosition(newX, this.sprite.getPosition().y);
}

I'm wondering if this is normal or if it's the phone I'm testing it on (Sony Xperia Play.)

Thanks for any input.

Undo
  • 25,519
  • 37
  • 106
  • 129
mknutso2
  • 47
  • 8

1 Answers1

0

How about moving average to smooth your data on the fly?

It will lag a little but it would be unnoticeable.

On the other hand, I would not use Euler angles (such as roll).

Community
  • 1
  • 1
Ali
  • 56,466
  • 29
  • 168
  • 265
  • I had already incorporated this position movement limiting algorithm (now included in the question) but there was too much lag. I'm not experiencing the problems from your link. I don't expect anyone to be playing the game with the device at extreme orientation and thus don't have the issue of erratic angle behavior. – mknutso2 Mar 04 '12 at 21:02