# How can i rotate an object based on the finger position?

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.

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