0

I'm working with sensor to simulator the Android device moving through 3D model. Currently, I calculate the euler angle (yaw, pitch, roll) and after that, use them to rotate model. Everything works fine but when I tilt the device to vertically, the roll angle suddenly change so the model rotate wrong. Here is the start position of the model: enter image description here And when tilt vertically: enter image description here

So, please let me know that:

  1. Is my code okey?
  2. Why when I tilt the device vertically, the roll angle suddenly change and how to fix it. Thank you!

Here is my code to get the yaw, pitch, roll angle:

fun handleSensorEvent(event: SensorEvent) {
    val eventValues = event.values
    when (event.sensor.type) {
         Sensor.TYPE_ACCELEROMETER -> {
             accels = eventValues.clone()
          }
         Sensor.TYPE_MAGNETIC_FIELD -> {
             mags = eventValues.clone()
         }
    }
}

private fun updateOrientationChange() {
        SensorManager.getRotationMatrix(rotationMatrix, null, accels, mags)

        SensorManager.remapCoordinateSystem(
            rotationMatrix,
            SensorManager.AXIS_X,
            SensorManager.AXIS_Y,
            outR
        )

        SensorManager.getOrientation(outR, orientationAngles)

        _pitch = -Math.toDegrees(orientationAngles[1].toDouble()).toFloat()
        _roll = Math.toDegrees(orientationAngles[2].toDouble()).toFloat()
        _yaw = Math.toDegrees(atan2(outR[4].toDouble(), outR[0].toDouble())).toFloat()
    }

Here is the code I using to rotate model:

fun rotate(roll: Float, pitch: Float, yaw: Float) {
        rotateAngleY = roll
        rotateAngleX = pitch
        rotateAngleZ = yaw
        updateViewMatrix()
    }

private fun updateViewMatrix() {
        Matrix.setLookAtM(viewMatrix, 0, 0f, 0f, translateZ, 0f, 0f, 0f, 0f, 1.0f, 0.0f)
        Matrix.rotateM(viewMatrix, 0, rotateAngleX, 1f, 0f, 0f)
        Matrix.rotateM(viewMatrix, 0, rotateAngleY, 0f, 1f, 0f)
        Matrix.rotateM(viewMatrix, 0, rotateAngleZ, 0f, 0f, 1f)
    }
genpfault
  • 51,148
  • 11
  • 85
  • 139

0 Answers0