0
\$\begingroup\$

I am using Lens Studio to build an AR effect and trying to use the position of the hand tracker fingertip to make a cube rotate. For example, as I move my finger on the x-axis the cube rotates in the y-axis in the direction of the finger.

I tried the following code but there is probably something I'm missing because it only works correctly on one face of the cube.

var object = script.getSceneObject();
var transform = object.getTransform();
var rotation = transform.getWorldRotation();

var isPinch = false;

function update() {
  var hand = global.handTracking.getHand();

  if (hand === undefined) {
    return;
  }

  detectPinch(hand);

  if (isPinch) {
    rotate(hand);
  }
}

function detectPinch(hand) {
  var thumb = hand.thumb.tip.position;
  var index = hand.indexFinger.tip.position;
  var distance = thumb.distance(index);
  var threshold = 3;

  if (distance < threshold) {
    isPinch = true;
  } else {
    isPinch = false;
  }
}

function rotate(hand) {
  var pos = hand.indexFinger.tip.position;

  var q1 = quat.angleAxis(degToRad(pos.y) * rotSens, vec3.left());
  var q2 = quat.angleAxis(degToRad(pos.x) * rotSens, vec3.up());
  var q3 = q1.multiply(q2);

  transform.setWorldRotation(q3.multiply(rotation));
}

function degToRad(degrees) {
  var pi = Math.PI;
  return degrees * (pi / 180);
}

script.createEvent("UpdateEvent").bind(update);

If I face the cube on the left, right, or backside the rotations are wrong.

I believe this is not a Lens Studio specific problem so similar solutions from other platforms like unity are welcome.

\$\endgroup\$
6
  • \$\begingroup\$ How is rotation defined? \$\endgroup\$
    – DMGregory
    Jan 24 at 23:40
  • \$\begingroup\$ rotation is defined out side the update loop as var rotation = script.getTransform().getWorldRotation(); \$\endgroup\$
    – Harry
    Jan 25 at 5:13
  • \$\begingroup\$ Basically I get the rotation from the object the script is attached to. In this case the cube \$\endgroup\$
    – Harry
    Jan 25 at 5:14
  • \$\begingroup\$ Be sure to edit your question so we can see that code and when it's called. \$\endgroup\$
    – DMGregory
    Jan 25 at 12:21
  • \$\begingroup\$ I have added all code for clarity \$\endgroup\$
    – Harry
    Jan 25 at 16:04

0

You must log in to answer this question.

Browse other questions tagged .