Reputation: 67
So I am currently working on a game where if you tilt the device left an object on screen moves left, and if you tilt the device right, the object on screen moves right.
This works fine on all of my devices that I have (Samsung Galaxy S5, Samsung Galaxy 3 Tablet, and a ZTE Straight Talk phone. But I've released a beta version and one of my friends that hava a tablet; Samsung Galaxy 2 10 inch. On it, moving left and right doesn't work, but instead he has to tilt it up and down for it to move left and right. It seems almost like the sensor is rotated. But the game is locked to the "portrait" position. Is it possible that on some devices that the sensor may be rotated or implemented in such a way that it's "up" vector and "left" vectors are changed?
Upvotes: 0
Views: 85
Reputation: 1704
I have faced same problem , you need to get device default orientation before setting the gravity of that object.
public int getDeviceDefaultOrientation() {
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Configuration config = getResources().getConfiguration();
int rotation = windowManager.getDefaultDisplay().getRotation();
if ( ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) &&
config.orientation == Configuration.ORIENTATION_LANDSCAPE)
|| ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) &&
config.orientation == Configuration.ORIENTATION_PORTRAIT)) {
return Configuration.ORIENTATION_LANDSCAPE;
} else {
return Configuration.ORIENTATION_PORTRAIT;
}
}
for example in my case I have used box2d and code is something like this
if (box2dUtils.getWorld() != null) {
int orient=getDeviceDefaultOrientation();
if(orient==1){
box2dUtils.getWorld().setGravity(new Vec2(xaccel,yaccel));
}
else if(orient==2){
box2dUtils.getWorld().setGravity(new Vec2(-yaccel,-xaccel));
}// initially Vec2(xaccel, yaccel);
else{
box2dUtils.getWorld().setGravity(new Vec2(xaccel,yaccel));
}
}
Upvotes: 1