我正在使用 Android 3.1 设备中陀螺仪传感器的方向值旋转矩形。
我必须非常快速地旋转我的设备才能获得 1.0 或更高的值。
这是代码
final float currentRotVector[] = { 1, 0, 0, 0 };
if (timestamp != 0)
{
final float dT = (event.timestamp - timestamp) * NS2S;
// Axis of the rotation sample, not normalized yet.
// Calculate the angular speed of the sample
float omegaMagnitude = (float) Math.sqrt(X * X + Y * Y + Z * Z);
// Normalize the rotation vector if it's big enough to get the axis
if (omegaMagnitude > EPSILON)
{
X /= omegaMagnitude;
Y /= omegaMagnitude;
Z /= omegaMagnitude;
}
// Integrate around this axis with the angular speed by the timestep
// in order to get a delta rotation from this sample over the timestep
// We will convert this axis-angle representation of the delta rotation
// into a quaternion before turning it into the rotation matrix.
float thetaOverTwo = dT * omegaMagnitude / 2.0f;
float sinThetaOverTwo = (float) Math.sin(thetaOverTwo);
float cosThetaOverTwo = (float) Math.cos(thetaOverTwo);
deltaRotationVector[0] = cosThetaOverTwo;
deltaRotationVector[1] = sinThetaOverTwo * X;
deltaRotationVector[2] = sinThetaOverTwo * Y;
deltaRotationVector[3] = sinThetaOverTwo * Z;
/* quaternion multiplication
Reference: http://www.cprogramming.com/tutorial/3d/quaternions.html
*/
currentRotVector[0] = deltaRotationVector[0] * currentRotVector[0] -
deltaRotationVector[1] * currentRotVector[1] -
deltaRotationVector[2] * currentRotVector[2] -
deltaRotationVector[3] * currentRotVector[3];
currentRotVector[1] = deltaRotationVector[0] * currentRotVector[1] +
deltaRotationVector[1] * currentRotVector[0] +
deltaRotationVector[2] * currentRotVector[3] -
deltaRotationVector[3] * currentRotVector[2];
currentRotVector[2] = deltaRotationVector[0] * currentRotVector[2] -
deltaRotationVector[1] * currentRotVector[3] +
deltaRotationVector[2] * currentRotVector[0] +
deltaRotationVector[3] * currentRotVector[1];
currentRotVector[3] = deltaRotationVector[0] * currentRotVector[3] +
deltaRotationVector[1] * currentRotVector[2] -
deltaRotationVector[2] * currentRotVector[1] +
deltaRotationVector[3] * currentRotVector[0];
final float rad2deg = (float) (180.0f / Math.PI);
RotAngle = currentRotVector[0] * rad2deg;
axisX = currentRotVector[1];
axisY = currentRotVector[2];
axisZ = currentRotVector[3];
Log.i("Sensor Orientation GyroScope", "axisX: " + axisX + //
" axisY: " + axisY + //
" axisZ: " + axisZ + //
" RotAngle: " + RotAngle);
}
时间戳 = event.timestamp;
我得到了一些输出,例如axisX:0.69363713 axisY:0.18359372 axisZ:0.0228636 RotAngle:36.7191并且由于轴值的原因,当设备放在桌子上时,输出矩形看起来有所调整。
上面的代码有问题吗?