[HELP] hid_read snapping back to original position after joystick release

Post questions, comments and feedback to our 3Dconnexion Windows Development Team.

Moderator: Moderators

Post Reply
sunil.george
Posts: 4
Joined: Thu Oct 02, 2025 5:13 am

[HELP] hid_read snapping back to original position after joystick release

Post by sunil.george »

Hi All,
I was trying to integrate Spacemouse pro with our image viewer application using hidapi. I get the XYZ positions using hid_read. The problem I'm facing is:- when i shift the image to left/right, it moves. But when I leave the joystick, it returns back to original position. When I checked the XYZ value, it seems I still keep getting the values after the joystick release.

I use the following code:-

unsigned char* buf = new unsigned char[13]; // Typical report size

int res = m_pHidApiWrapper->Read(m_pHidDevice, buf, sizeof(unsigned char) * 13);
if (res > 0) {
// Example: buf[0] == report ID
if (buf[0] == 0x01) { // Translation
m_posX = (((short)buf[2]) << 8) | (unsigned char)buf[1];
m_posY = (((short)buf[4]) << 8) | (unsigned char)buf[3];
m_posZ = (((short)buf[6]) << 8) | (unsigned char)buf[5];
}
else if (buf[0] == 0x02) { // Rotation
m_posRX = (((short)buf[2]) << 8) | (unsigned char)buf[1];
m_posRY = (((short)buf[4]) << 8) | (unsigned char)buf[3];
m_posRZ = (((short)buf[6]) << 8) | (unsigned char)buf[5];
}

Help needed urgently!!
jwick
Moderator
Moderator
Posts: 3455
Joined: Wed Dec 20, 2006 2:25 pm
Location: USA
Contact:

Re: [HELP] hid_read snapping back to original position after joystick release

Post by jwick »

The device is a spring. It reports its position/orientation (Tx, Ty, Tz, Rx, Ry, Rz) away from the spring's physical rest position. When you release pressure on the cap, it returns to its rest position (0,0,0, 0,0,0). Generally, we treat the displacement as a velocity vector, not a position vector.

If you hold the cap still for "too long" away from its physical rest position, it will recenter itself at that position. When you then release the cap, it will return to its physical center position and think (for a while) that you are pushing/twisting it to that position. Eventually it will, once again, recenter itself at that position. I can show you how to disable this feature if required.

Most recent devices deliver all 6 axes in the Translation packet (reportID 1).
sunil.george
Posts: 4
Joined: Thu Oct 02, 2025 5:13 am

Re: [HELP] hid_read snapping back to original position after joystick release

Post by sunil.george »

Hi jwick,

Is it possible to configure the displacement as a position vector?
jwick
Moderator
Moderator
Posts: 3455
Joined: Wed Dec 20, 2006 2:25 pm
Location: USA
Contact:

Re: [HELP] hid_read snapping back to original position after joystick release

Post by jwick »

Using HID, no. You need to calculate the position yourself. Only you know how the values should be used.

Be careful how you accumulate/calculate the axis values over time.
You can simply sum the translation vectors, but you can not accumulate orientation like that.
If you have a 2D viewer, you will probably be ignoring rotations.
sunil.george
Posts: 4
Joined: Thu Oct 02, 2025 5:13 am

Re: [HELP] hid_read snapping back to original position after joystick release

Post by sunil.george »

Hi jwick,

How about DirectInput? Is it possible to get position vector?
We tried this, but observed inconsistent behavior across different devices. Some provided position and whereas some gave displacement.
Can you confirm this?
jwick
Moderator
Moderator
Posts: 3455
Joined: Wed Dec 20, 2006 2:25 pm
Location: USA
Contact:

Re: [HELP] hid_read snapping back to original position after joystick release

Post by jwick »

I have also found DirectInput to be inconsistent. I'm not sure how much Microsoft is supporting it anymore.
I found it easier to use code.
Post Reply