finallyfunctional / openvr-driver-example

This repository is an example on how to implement an Open VR driver that sends joystick and/or trackpad input to Open VR.
MIT License
63 stars 22 forks source link

How could we add Rotational & Positional data to HMD or Controller #2

Open apurv1305 opened 3 years ago

apurv1305 commented 3 years ago

Thanks for your explanation of the OpenVR architecture, it's really very helpful.

I was trying to write a sample driver inspired by your explanation to the OpenVR APIs. I am trying to write a driver for a custom VR controller and want to write Rotational and Positional data for the same by reading the serial port.

My Question was how could I write both positional and rotational data for the controller in the driver.

Thank You.

r57zone commented 3 years ago

Hi, to change the rotation of the controller, you need to add the following to the GetPos function: If you are using Euler angles:

inline vr::HmdQuaternion_t EulerAngleToQuaternion(double Yaw, double Pitch, double Roll)
{
    vr::HmdQuaternion_t q;
    // Abbreviations for the various angular functions
    double cy = cos(Yaw * 0.5);
    double sy = sin(Yaw * 0.5);
    double cp = cos(Pitch * 0.5);
    double sp = sin(Pitch * 0.5);
    double cr = cos(Roll * 0.5);
    double sr = sin(Roll * 0.5);

    q.w = cr * cp * cy + sr * sp * sy;
    q.x = sr * cp * cy - cr * sp * sy;
    q.y = cr * sp * cy + sr * cp * sy;
    q.z = cr * cp * sy - sr * sp * cy;

    return q;
}

pose.qRotation = EulerAngleToQuaternion(yourRoll, -yourYaw, yourPitch);

If you are using quaternions, then simply assign the quaternion.

To change the position, change the values (X, Y, Z):

pose.vecPosition[0] = 0.02; 
pose.vecPosition[1] = 0.001; 
pose.vecPosition[2] = 0.001;