Copy rotations from head binding to a joint of a 3d object
I have a 3d object with some joints, and I want to copy the rotations of the User's head (head binding) and apply them to one of the joints of my 3d object. My current code is giving inconsistent results as the user moves around in the scene
// @input SceneObject sourceObject
// @input SceneObject targetObject
var targetTransform;
var sourceTransform;
var rotateTo;
if (script.targetObject) {
targetTransform = script.targetObject.getTransform();
} else {
targetTransform = script.getSceneObject().getTransform();
}
if (script.sourceObject) {
sourceTransform = script.sourceObject.getTransform();
} else {
print ("[CopyRotation] Source object is not set");
}
function onUpdate() {
if (sourceTransform) {
rotateTo = quat.fromEulerVec( new vec3(sourceTransform.getWorldRotation().toEulerAngles().x -90,
sourceTransform.getWorldRotation().toEulerAngles().y,
sourceTransform.getWorldRotation().toEulerAngles().z))
targetTransform.setWorldRotation(rotateTo);
}
}
var updateEvent = script.createEvent("UpdateEvent");
updateEvent.bind(onUpdate);
If all you want is to copy the rotation, then this script will do the trick. When you add the script component to your scene, change it from "On Awake" to run on every frame update so that it is continuously updating the rotation. Or you can put the code inside an UpdateEvent.
Thanks! This would work great, but I need to get the X,Y,Z values separately as an offset value needs to be added to the X rotation. This also doesn't solve my issue of extra rotation happening as the user rotates the camera (looks around). It's as if the camera's rotations are being added to the rotations of the head binding, but none of my assets are children of the camera.
Have you tried using local rotation instead of world rotation?
The head rotation needs to be in relation to something, and generally it is in relation to the camera. If I remain stationary but move the camera, my head rotation is going to change because it is measured relative to the camera. It sounds to me like this is what is happening with your lens.
You might be able to solve this issue by incorporating device tracking to keep track of the camera's rotations and then do the head rotation relative to that. After adding a device tracking component you can access the information via the scripting API as well. I've never done this personally nor do I know what the math would be like, but that's where I would look next.
Local Rotations was key haha thanks!