Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert quaternion rotation to steering wheel motion?

I'm creating a game where you use rotational remote(it knows only its rotation) to steer a car. I have a problem with converting Quaternion(this is the output of the controller) to steering wheel rotation.This is what is the closest to working form all the things that I have tried(transform.localRotation is a rotation of the steering wheel):

void Update() {

    transform.localRotation = GvrController.Orientation;

    transform.localRotation = new Quaternion(0.0f, 0.0f, transform.localRotation.z, transform.localRotation.w);

}

This obviously isn't a good solution and works not so well. There is a very simple visualization of what I'm trying to do: enter image description here Default orientation of the controller is facing forward as on the picture.

Basically the whole problem is how to skip all the axis other than the one responsible for rotating(axis 1 on the picture) and apply it to the steering wheel. Do you know how can I do this?

EDIT: Because it's hard to explain the problem without proper visualization i took photos of my controller and drew axis on them.

This is the default orientation of the controller:

enter image description here

And this is how it's held with axis marked on it:

enter image description here

like image 883
Wojtek Wencel Avatar asked Oct 30 '22 13:10

Wojtek Wencel


1 Answers

Use Quaternion.Euler and Quaternion.eulerAngles. Note the order for the Euler function is z, x then y, unlike Vector3. EulerAngles returns an angle in degrees (whereas rotation.x returns the quaternion amount for that 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 set up you may need to experiment with other orientations if it's not y-up z-forward e.g.

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

or

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

and so on. It should soon become clear which system it uses.

Also, if the steering wheel is not parented to anything use rotation rather than localRotation.

like image 171
Absinthe Avatar answered Nov 15 '22 04:11

Absinthe