要获取相机面向世界前方的角度(-Z轴),可以使用ARCore的Camera类的getViewMatrix()方法来获取相机的视图矩阵。然后,可以使用OpenGL的Matrix类的invertM()方法来获取相机的旋转矩阵,并从中提取旋转角度。
下面是一个示例代码,展示了如何获取相机面向世界前方的角度(-Z轴):
import android.opengl.Matrix;
// 获取相机面向世界前方的角度
public float getCameraFacingAngle() {
float[] viewMatrix = new float[16];
float[] rotationMatrix = new float[16];
// 获取相机视图矩阵
camera.getViewMatrix(viewMatrix);
// 获取相机旋转矩阵
Matrix.invertM(rotationMatrix, 0, viewMatrix, 0);
// 获取旋转矩阵中的旋转角度
float[] angles = new float[3];
Matrix.decomposeRotationYXZ(rotationMatrix, angles);
// 返回相机面向世界前方的角度(-Z轴)
return angles[2];
}
请注意,上述代码假设您已经初始化了ARCore的相机实例,并且在适当的地方调用了ARCore的update()方法来更新相机的状态。
希望这可以帮助到您!