How to convert a quaternion turn into a steering wheel movement?

I create a game in which you use a rotary remote control (he knows only its rotation) to control the car. I had a problem converting Quaternion (this is the controller output) to steering wheel rotation. This is what is closest to the working form, all the things I tried (transform.localRotation is turning the steering wheel):

 void Update() { transform.localRotation = GvrController.Orientation; transform.localRotation = new Quaternion(0.0f, 0.0f, transform.localRotation.z, transform.localRotation.w); } 

This is obviously not a good solution and does not work so well. There is a very simple visualization of what I'm trying to do: enter image description here By default, the orientation of the controller is facing forward, as in the figure.

Basically, the whole problem is how to skip the entire axis other than the one responsible for the rotation (axis 1 in the figure) and apply it to the steering wheel. Do you know how I can do this?

EDIT: Since it is difficult to explain the problem without proper visualization, I took photos of my controller and drew an axis on them.

This is the default orientation of the controller:

enter image description here

And this is how it is held with the axis marked on it:

enter image description here

+5
source share
1 answer

Use Quaternion.Euler and Quaternion.eulerAngles . Note that the order for the Euler function is z, x, and y, unlike Vector3. EulerAngles returns the angle in degrees (whereas rotation.x returns the number of quaternions for this axis). The Euler function expects an angle, so you want something like:

 Quaternion rot = GvrController.Orientation; transform.localRotation = Quaternion.Euler(rot.eulerAngles.z, 0, 0); 

Depending on how the axes of your controller are configured, you may need to experiment with different orientations if it is not y-up z-forward, for example.

 transform.localRotation = Quaternion.Euler(rot.eulerAngles.x, 0, 0); 

or

 transform.localRotation = Quaternion.Euler(0, 0, rot.eulerAngles.z); 

etc. It should soon become clear which system it uses.

In addition, if the steering wheel is not a parent, use rotation instead of local rotation.

+2
source

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


All Articles