所以我目前正在做一个游戏,如果你倾斜设备,屏幕上的一个物体向左移动,如果你向右倾斜,屏幕上的物体会向右移动。
这在我所有的设备(三星Galaxy S5、三星Galaxy 3平板电脑和中兴直讲手机)上都能正常工作。但我已经发布了测试版,我的朋友之一也有平板电脑;三星Galaxy 2 10英寸。在上面,左右移动不起作用,但他必须上下倾斜才能左右移动。好像传感器是旋转的。但游戏锁定在“肖像”位置。在某些设备上,传感器是否可能被旋转或实现,从而改变它的“向上”矢量和“左”向量?
发布于 2015-07-09 05:25:50
我也遇到过同样的问题,在设置物体的重力之前,你需要得到设备的默认方向。
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;
}
}
例如,在我的例子中,我使用了box2d,代码如下所示
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));
}
}
https://stackoverflow.com/questions/31308699
复制相似问题