# Use rotation vector from Aruco in Unity3d

Hi, I am using OpenCV, Aruco and Unity3d for an augmented reality application. I would like to spawn objects on detected aruco markers, and the position is fine, but I cannot get the rotation right. Aruco gives a vector with 3 elements for the rotation, and as far as I found out it is an axis-angle representation with the angle being the module of the vector. I tried to get the rotation matrix with Rodrigues and use this approach to get to the Quaternion for Unity3d, but the values are almost always 0, sometimes they jump to random angles.

Then I saw that Unity has a Quaternion.AngleAxis function that I tried to use like this:

float theta = (float)(Math.Sqrt(m.x*m.x + m.y*m.y + m.z*m.z)*180/Math.PI);
Vector3 axis = new Vector3 (m.x, -m.y, m.z);            //multiply m.y by -1 since in Unity y-axis points upward
Quaternion rot = Quaternion.AngleAxis (theta, axis);


The values seem to be fine now, and the rotation looks good as long as the marker is only rotated along one axis. For example if I rotate the marker to the left and then tilt it forwards, in unity the marker does not tilt forwards but brings it right side to the front (I do not know how to describe this any better).

Has anybody tried this? Any idea on how to convert the values? I guess that somehow the axes are different, but I cannot figure out what to change.

edit retag close merge delete

Sort by » oldest newest most voted

Inverting x and z instead of y did the trick. So now I am using:

float theta = (float)(Math.Sqrt(m.x*m.x + m.y*m.y + m.z*m.z)*180/Math.PI);
Vector3 axis = new Vector3 (-m.x, m.y, -m.z);
Quaternion rot = Quaternion.AngleAxis (theta, axis);


m.x, m.y and m.z are the values of the rvec returned by Arucos estimatePoseSingleMarkers.

Now everything rotates fine!

more

I tried doing the same as above, but the in my case even if both camera and marker are still, the orientation of detected marker keeps on juggling. I get the following rvecs from opencv . Can you tell how do I stop juggling and make the rotation of detected object still , if both camera and marker are still.

3.1076 -0.0546688 -0.579278

3.1178 -0.0546684 -0.56534

3.11035 -0.0524637 -0.563036

3.09086 -0.0504446 -0.553181

3.09476 -0.0359067 -0.267254

3.1261 -0.054629 -0.570334

3.21824 -0.0465225 -0.385657

3.10909 -0.0539971 -0.560765

3.12176 -0.0560612 -0.5593

3.10504 -0.0530066 -0.56954

3.11173 -0.0539191 -0.56983

3.12181 -0.0573736 -0.56769

3.10272 -0.055436 -0.556921

Official site

GitHub

Wiki

Documentation