I'm working in Eclipse, using the Libgdx game framework to develop an Android application, where I'm attempting to move a 2D cursor based on the general movements of the Android device. As I've read from countless sources already, this is a much harder task than it sounds, due to the wide margin of error that appears during calculation.
What I've done so far is pretty primitive; pass across a composite "linear acceleration sensor" from Android, use the acceleration values to calculate velocity, use velocity to calculate displacement, add that onto my cursor's x,y coordinates, and lastly make sure the cursor does not exceed the screen's dimensions. I don't think it really needs a code example, but here's a watered down version of the core code;
//class attributes
Vector2 accel, prevAccel;
Vector2 velocity;
Vector2 cursorPos;
IMyLinearAccelerationSensor las;
//update code
accel = new Vector2(las.getAccelXY); //returns a Vector2 with the x and y acceleration values
velocity.x = (accel.x + prevAccel.x) /2 * deltaTime;
velocity.y = (accel.y + prevAccel.y) /2 * deltaTime;
cursorPos.x += tempAccel.x * deltaTime;
cursorPos.y += tempAccel.y * deltaTime;
prevAccel = accel; //update the "previous acceleration"
if (cursorPos.x < 0)
cursorPos.x = 0;
if (cursorPos.y < 0)
cursorPos.y = 0; //etc.
The main issues I'm encountering are drift, and the fact that the sensor appears to be more sensitive to movements in the positive directions of x, and y (I think). In other words, the cursor accelerates/ drifts off, gets stuck in the top left corner of the screen, and the respective velocities for x and y never really fall back to 0. Even when moving the device in the opposite direction, the velocity only decreases slightly.
Given all of this information, I just wanted to ask some questions, and for some advice. Firstly, is this is normal behavior, or are there any glaring problems with my code? If not, are there any steps that can be taken with my current setup, to resolve these problems with drift, and remove some margin of error? e.g. applying different filters to remove noise, applying a resistive force to the velocity, etc. I don't need the movement of the cursor to follow the device's movements too accurately; so long as it moves when the device moves, and stops generally when the device stops, I don't mind if there's some small drifting, or lag.
I was considering to at some point implement more interfaces to get the raw data from the Android hardware sensors (compass, gyroscope, and accelerometer), to generate more accurate results, but even if I do go ahead with this, would this improve much? Knowing that this is a complex problem, I'd rather not spend too much time on it without good reason. (sensor fusion from scratch definitely seems hard...)
Lastly, I'm sorry about the lengthy post, and also sorry if this is considered a repeat question. I've done research, and have seen plenty of other people with similar issues, but I'm not quite sure how their solutions might be applied to my own problem. Given my limited knowledge/ experience, I'd appreciate any helpful insight that can be offered.