Libgdx How to rotate a 3D model on several axes with a phone orientation

I am trying to rotate a 3D model on several axes at the same time using accelerometers. When I do this, I use setToRotation(), but it is only one axis at a time.

For instance:

  ModelInstance modelInstance = instances.first(); 

  //ROLL
  modelInstance.transform.setToRotation(Vector3.Z, phoneAccel.y*9);
  //PITCH
  modelInstance.transform.setToRotation(Vector3.X, phoneAccel.z*9);

The phone is in forced mode. I get an instance of the model I want to rotate.

I installed Vector3 phoneAccelbased on Gdx.input.getAccelerometerX/Y/Z().

In the above example, both lines of code work correctly, but only independently. When I try to use both (one after the other), the first turn (ROLL) is deleted. I would think that two rotation matrices would accumulate, i.e. The Z axis is superimposed on the rotation, and then the rotation is superimposed on the X axis.

Do I need to create my own aggregate rotation matrix and then apply it at the end?

?

+4
2

Matrix4#setToRotation (, ), ( "setTo" ). , rotate. .

modelInstance.transform.setToRotation(Vector3.Z, phoneAccel.y*9);
modelInstance.transform.rotate(Vector3.X, phoneAccel.z*9);

, , ( Z X, ). :

modelInstance.transform.setFromEulerAngles(yaw, pitch, roll);
+8

setToRotation , . , "" Z X.

, :

  • Quaternion @noone said.
  • rotate. , a Vector2, x Z, y - X.
  • setToRotation , phoneAccel.

setToRotation a Vector3 direction a Vector3 face, , . : modelInstance.transform.setToRotation(Vector3.Z, Vector3.Y) inot Z-Direction. Vector3(0, -1, 0) (, ) .

, .

+3

Source: https://habr.com/ru/post/1534826/


All Articles