Andrej
Andrej

Reputation: 61

THREE.js - Quaternion Offset / Calibration

I'm working with a sensor to control the rotation of a hand model in THREE. I want to be able to apply an offset (on button click) to the hand model to adjust for any initial sensor position discrepancies.

I'm using quaternions and i'm not entirely clear on how to do this.

It seems that the approach should be to get the current position, get the difference between the current position and the home position [0,0,0,1], and apply this offset to the live sensor values.

I've tried this so far:

// on button click 
quaternionOffset.copy(quaternion.multiplyQuaternions(quaternion.inverse(), startPosition))
// on update
quaternion.multiply(quaternionOffset)

start
initial sensor position (example)

start
home position (fingers pointing forward)

Thank you for the help

Upvotes: 1

Views: 686

Answers (2)

ckundo
ckundo

Reputation: 1569

let baseQuaternion = new THREE.Quaternion();
let diff;

function changeHandler(value) {
  if (!diff) {
    diff = baseQuaternion.multiply(value.inverse());
  }
  let calibrated = new THREE.Quaternion();
  calibrated.multiplyQuaternions(diff, value);
  object.setRotationFromQuaternion(calibrated);
}

Inspired by https://stackoverflow.com/a/22167097/1162838

Upvotes: 0

neeh
neeh

Reputation: 3025

I think your approach is correct, just make sure you don't accidentally apply the home quaternion inside the input quaternion :

var inputQuaternion = new THREE.Quaternion();
var homeQuaternion = new THREE.Quaternion();

document.addEventListener('click', function () {
  // set the current rotation as home
  homeQuaternion.copy(inputQuaternion).inverse();
});

function updateFromSensor(quaternion) {
  inputQuaternion.copy(quaternion);
  object.quaternion.multiplyQuaternions(inputQuaternion, homeQuaternion);
}

Upvotes: 1

Related Questions