How to rotate an object by genericTweenValue

I have an object that I want to rotate on the y-axis by 90 degrees every time I tap on the screen, and I would like to use the easing functions from the tween manager using genericTweenValue. In the code below I try to do that but the angles seem to be wrong.

Expected outcome: Object rotates by an angle of 90 degrees on each tap.
Actual outcome: Makes multiple rotations on each tap, that are not 90 degrees.

global.turn = function() {
    var updateEvent = script.createEvent("UpdateEvent");

    var transform = object.getTransform();
    var rotation = transform.getLocalRotation();

    tweenManager.setStartValue(object, "y_rot_", rotation.y);
    tweenManager.setEndValue(object, "y_rot_", rotation.y + 90);

    updateEvent.bind(function(eventData) {
        var rotY = tweenManager.getGenericTweenValue(object, "y_rot_");
        var rotationToApply = quat.angleAxis(rotY, vec3.up());
        var oldRotation = object.getTransform().getLocalRotation();
        var newRotation = rotationToApply.multiply(oldRotation);
        transform.setLocalRotation(newRotation);
    });

    tweenManager.startTween(object, "y_rot_", function() {
        script.removeEvent(updateEvent);
    });
}

var touchEvent = script.createEvent("TapEvent");
touchEvent.bind(function(eventData) {global.turn()});

Answers

  • I figured it out by putting rotationToApply.multiply(rotation) in transform.setLocalRotation

    global.turn = function() {
        var updateEvent = script.createEvent("UpdateEvent");
    
        var transform = object.getTransform();
        var rotation = transform.getLocalRotation();
    
        tweenManager.setStartValue(object, "y_rot_", rotation.y);
        tweenManager.setEndValue(object, "y_rot_", rotation.y + 90);
    
        updateEvent.bind(function(eventData) {
            var rotY = tweenManager.getGenericTweenValue(object, "y_rot_");
            var rotationToApply = quat.angleAxis(degToRad(rotY), vec3.up());
            transform.setLocalRotation(rotationToApply.multiply(rotation));
        });
    
        tweenManager.startTween(object, "y_rot_", function() {
            script.removeEvent(updateEvent);
        });
    }
    
    var touchEvent = script.createEvent("TapEvent");
    touchEvent.bind(function(eventData) {global.turn()});