2013-10-10 47 views
1

我想旋轉位於我的OpenGL-World的(X,Y,Z)=(0,0,0)的模型(地形)。 +X是東,-Z是北,+Y是高度。查看正在尋找-Y,因爲設備歐拉角是(0°,0°,0°)而設備躺在桌子上指向北方。爲OpenGL ES2模型旋轉切換設備軸旋轉矩陣

的問題

我需要切換我的設備的軸來測量角度device -y更改爲device z

目前圍繞device x-axis工程輪換(結果在World X-axis左右輪換)。

但左右旋轉device y導致周圍World Y而不是World -Z和旋轉的旋轉圍繞device z以旋轉周圍World Z代替World Y

現在我已經沒有想法如何解決這個問題了。任何人都可以給我一個提示嗎?

(OpenGL)       (Device) 

^+Y-axis      ^+z-axis         
    *        *         
    *        *         
    *        * ^+y-axis (North)    
    *        * *        
    *        * *         
    *        * *         
    ************> + X-axis   ************> +x-axis      
    *                 
*                  
v +Z-axis (Minus North)  

我試過到目前爲止:

  • 使用從歐拉角SensorManager.getOrientation開關角工作正常,但我進入萬向節鎖定密齒90度到。所以我正在尋找另一個解決方案(SensorManager旋轉矩陣或四元數)。
  • SensorManager.remapCoordinateSystem幾乎在每一個可能的星座 - >沒有幫助
  • 更改的cols /從SensorManager.getRotationMatrix我旋轉矩陣的行 - >沒有幫助

回答

1

好吧,我已經找到了一個非常簡單的解決方案使用四元數。 軸在最後切換。

參見:

現在我仍然不知道如何啓用以上90度槳距角(在肖像模式) 。有任何想法嗎?

活動:

private SensorManager sensorManager; 
private Sensor rotationSensor; 

public void onCreate(Bundle savedInstanceState) { 
    ... 
    rotationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR); 
    ... 
} 

// Register/unregister SensorManager Listener at onResume()/onPause() ! 

public void onSensorChanged(SensorEvent event) { 

    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) { 
    // Set rotation vector 
    MyRenderer.setOrientationVector(event.values); 
    } 

} 

MyRenderer:

float[] orientationVector = new float[3]; 

public void setOrientationVector (float[] vector) { 

    // Rotation vector to quaternion 
    float[] quat = new float[4]; 
    SensorManager.getQuaternionFromVector(quat, vector); 
    // Switch quaternion from [w,x,y,z] to [x,y,z,w] 
    float[] switchedQuat = new float[] {quat[1], quat[2], quat[3], quat[0]}; 

    // Quaternion to rotation matrix 
    float[] rotationMatrix = new float[16]; 
    SensorManager.getRotationMatrixFromVector(rotationMatrix, switchedQuat); 

    // Rotation matrix to orientation vector 
    SensorManager.getOrientation(rotationMatrix, orientationVector); 

} 

public void onDrawFrame(GL10 unused) { 
    ... 
    // Rotate model matrix (note the axes beeing switched!) 
    Matrix.rotateM(modelMatrix, 0, 
    (float) (orientationVector[1] * 180/Math.PI), 1, 0, 0); 

    Matrix.rotateM(modelMatrix, 0, 
    (float) (orientationVector[0] * 180/Math.PI), 0, 1, 0); 

    Matrix.rotateM(modelMatrix, 0, 
    (float) (orientationVector[2] * 180/Math.PI), 0, 0, 1); 
    ... 
}