How can i fix this issue? is it gimbal lock problem?

I have this script to make object follow user cam pos and rot, but the problem is when i turn left or right more than 90 degree my objects starts to be flying around or gone,

//@input SceneObject camPos

// create event to run on every frame update
script.createEvent(“UpdateEvent”).bind(function() {

// Get the position and rotation of camPos
var posToFollow = script.camPos.getTransform().getWorldPosition();
var rotToFollow = script.camPos.getTransform().getWorldRotation();

// Extract the x and z components of the position
var x = posToFollow.x;
var z = posToFollow.z;

var yRot = rotToFollow.y;
var zRot = rotToFollow.z;

// Get the current position and rotation of the object
var pos = script.getSceneObject().getTransform().getWorldPosition();
var rot = script.getSceneObject().getTransform().getWorldRotation();

// Set the x and z components of the object’s position to follow camPos
pos.x = x;
pos.z = z;

rot.y = yRot;
rot.Z = zRot;

script.getSceneObject().getTransform().setWorldPosition(pos);
script.getSceneObject().getTransform().setWorldRotation(rot);

});

I think if you don’t have device motion capability enabled, or plane tracker, the camera won’t be tracked in world space. The device motion will give you rotational orientation, but you won’t be able to move the camera relative to objects. Plane tracking will give you full movement in world space.

Can you show a video of what it is doing? You say “follow cam position and rotation” but what are you trying to do with this? Are you trying to make an object stay on the screen? It sounds like you might want a billboarding behavior.

ah let me show you a video, i want it the object to follow user cam rot and position, but only on Y rotation so when user look up they cant see a thing but only see the object when looking down , i get it to work but when i turn extreme left or right about 90 degree then suddenly the object goes haywire lol

It could be gimbal lock but usually you would see some kind of hyperbolic behavior and not sudden disappearance. You could try printing the rotation value on the screen to see if it’s flipping from negative to positive or something like that.

I feel like Spark implemented quaternions at some point, but I don’t know how to use them to get around gimbal lock. That would be the solution for gimbal lock, though. Quaternions don’t suffer from it.

i see maybe lens studio has something similar, ill try it out, maybe i treat euler angles as quaternions! thannks

Oh, my bad! I thought this was a Spark question :sweat:

Lens studio uses quaternions by default, so you will need to do some conversion or use some quat methods to achieve what you want. The components of a quaternion aren’t the same as the components of a Euler rotation, so your approach won’t do what you want it to. XYZ in a quaternion represents the rotation axis, and W represents the amount of rotation to apply on that axis.

You can see the getWorldRotation method returns a quat: https://docs.snap.com/api/lens-studio/Classes/ScriptObjects#Transform--getWorldRotation

You might be able to simply convert the quat into a euler angle to get the values, but you will also need to create a new quat to assign to the target object
https://docs.snap.com/api/lens-studio/Classes/OtherClasses#quat--toEulerAngles

OMG I FINALLY GOT IT TO WORK!! ill also share the code here(tho my code might not be the best im still learning), thanks so much @josh_beckwith

//@input SceneObject camPos

script.createEvent(“UpdateEvent”).bind(function() {

var camPosRotation = script.camPos.getTransform().getWorldRotation();

var euler = camPosRotation.toEuler();

var yQuat = quat.fromEulerVec(new vec3(0, euler.y, 0));

var posToFollow = script.camPos.getTransform().getWorldPosition();

var x = posToFollow.x;

var z = posToFollow.z;

var pos = script.getSceneObject().getTransform().getWorldPosition();

pos.x = x;

pos.z = z;

script.getSceneObject().getTransform().setWorldPosition(pos);

script.getSceneObject().getTransform().setWorldRotation(yQuat);

});

1 Like