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.