openvr icon indicating copy to clipboard operation
openvr copied to clipboard

Quaternion format

Open kkocs opened this issue 9 years ago • 5 comments

In the virtual DriverPose_t GetPose() method i want to set a DriverPose_t variable's pose.qRotation.w, pose.qRotation.x, pose.qRotation.y, pose.qRotation.z but i am not sure how the quaternion which is expected by the driver is built (the interval which the angle of the quaternion is defined).

Can anyone please give a little help?

kkocs avatar May 06 '17 10:05 kkocs

Can't get to your point... Do you want to ask what's the definition of pose.qRotation?

LoSealL avatar May 06 '17 11:05 LoSealL

Yes, i would like to know what each element of the quaternion means. For example what is the relation with this quaternion and the one returned by the Android's Sensor.TYPE_ROTATION_VECTOR. (Link: https://developer.android.com/reference/android/hardware/SensorEvent.html#values )

kkocs avatar May 06 '17 12:05 kkocs

Hi @kkocs . As for quaternion and rotation, you can refer Quaternion. So it's easy to transfer a quaternion to a rotate matrix or an euler angle. The equation can be found at

Where in wiki the (s, x, y, z) stands for (w, x, y, z) in openvr. And plz note that opengl treat the first index of the array as w while direct3d treats the last index of the array as w

LoSealL avatar May 06 '17 13:05 LoSealL

I use GLM to provide such rich interfaces in my project.

HelenXR avatar May 27 '17 09:05 HelenXR

void Chellovr_engineDlg::GetRotation(float mat[3][3],Vector4& rot)
{
	float		q[4];
	float		trace;
	float		s;
	float		t;
	int     	i;
	int			j;
	int			k;
	static int 	next[ 3 ] = { 1, 2, 0 };
	trace = mat[ 0 ][ 0 ] + mat[ 1 ][ 1 ] + mat[ 2 ][ 2 ];
	if ( trace > 0.0f ) 
	{
		t = trace + 1.0f;
		s = 0.5f / sqrtf(t);
		q[3] = s * t;
		q[0] = ( mat[ 2 ][ 1 ] - mat[ 1 ][ 2 ] ) * s;
		q[1] = ( mat[ 0 ][ 2 ] - mat[ 2 ][ 0 ] ) * s;
		q[2] = ( mat[ 1 ][ 0 ] - mat[ 0 ][ 1 ] ) * s;
	} 
	else 
	{
		i = 0;
		if ( mat[ 1 ][ 1 ] > mat[ 0 ][ 0 ] ) {
			i = 1;
		}
		if ( mat[ 2 ][ 2 ] > mat[ i ][ i ] ) {
			i = 2;
		}
		j = next[ i ];
		k = next[ j ];
		t = ( mat[ i ][ i ] - ( mat[ j ][ j ] + mat[ k ][ k ] ) ) + 1.0f;
		s = 0.5f / sqrtf(t);
		q[i] = s * t;
		q[3] = ( mat[ k ][ j ] - mat[ j ][ k ] ) * s;
		q[j] = ( mat[ j ][ i ] + mat[ i ][ j ] ) * s;
		q[k] = ( mat[ k ][ i ] + mat[ i ][ k ] ) * s;
	}

	//四元素转轴角
	double ax(0),ay(0),az(0),aw(0);
	aw = acos(q[3])*2;
	double temp = sin(aw/2);
	if (temp!=0)
	{
		ax = q[0]/temp;
		ay = q[1]/temp;
		az = q[2]/temp;
	}

	rot.x = ax;  //ax---------------
	rot.y = ay;   // ay
	rot.z = az;
	rot.w = aw;	  	
}
void Chellovr_engineDlg::GetDeviceInfo(int nDevice,Vector3 &pos,Vector4 &rot)
{
	float cx[3][3];
	double m[4][4];	

	//steamvr matrix
	const vr::HmdMatrix34_t matrixTemp_ = m_rTrackedDevicePose[nDevice].mDeviceToAbsoluteTracking;

	//Matrix4矩阵,主要是不清楚用哪个矩阵转换
	Matrix4 mat= m_rmat4DevicePose[nDevice];
	const float* pmat = mat.get();
	const float matrixTemp[3][4]={
		pmat[0],pmat[1],pmat[2], pmat[3], 
		pmat[4],pmat[5],pmat[6], pmat[7],
		pmat[8],pmat[9],pmat[10],pmat[11]
	};
	
	{  
		cx[0][0] =  matrixTemp[0][0];
		cx[0][1] =  matrixTemp[0][1];
		cx[0][2] = matrixTemp[0][2];

		cx[1][0] =  matrixTemp[1][0];
		cx[1][1] =  matrixTemp[1][1];
		cx[1][2] = matrixTemp[1][2];

		cx[2][0] = matrixTemp[2][0];
		cx[2][1] = matrixTemp[2][1];
		cx[2][2] = matrixTemp[2][2];
		GetRotation(cx,rot);

		float fBeilv = 1;
		pos.x = fBeilv*pmat[12];
		pos.y = fBeilv*pmat[13];
		pos.z = fBeilv*pmat[14];
	}			 	

	if (_isnan(rot.x))
	{
		rot.x=0;
	}

	if (_isnan(rot.y))
	{
		rot.y=0;
	}

	if (_isnan(rot.z))
	{
		rot.z=0;
	}

	if (_isnan(rot.w))
	{
		rot.w=0;
	}
}

qywako avatar Jun 02 '17 00:06 qywako