Hi I’d like to get the Y rotation of an object in 0 - 360 (or -180 to 180) but it only goes from -90 to 90 then the X and Z rotation jump from 0 to 180 or -180. This can also be seen in the editor.
To get the global Y rotation (rotation around the world Y axis) is pretty straightforward. It’s just an atan2 of the forward vector:
    var _transformedForward = new pc.Vec3();
    pc.util.getYaw = function (quat) {
        var transformedForward = _transformedForward;
        quat.transformVector(pc.Vec3.FORWARD, transformedForward);
        return Math.atan2(-transformedForward.x, -transformedForward.z) * pc.math.RAD_TO_DEG;    
    };
If you need to get the local Y rotation, that is a little trickier because you have to take the parent and hierarchy into consideration.
Sorry I’m new to coding in PlayCanvas…
This was my old code:
var e = this.rotationSource.getEulerAngles();
yaw = e.y;
I was wondering how to use your code to get the degrees for the rotationSource entity… thanks!
    var _transformedForward = new pc.Vec3();
    SomeScript.prototype.getYaw = function (quat) {
        var transformedForward = _transformedForward;
        quat.transformVector(pc.Vec3.FORWARD, transformedForward);
        return Math.atan2(-transformedForward.x, -transformedForward.z) * pc.math.RAD_TO_DEG;    
    };
    SomeScript.prototype.someFunction = function() {
        var yaw = this.getYaw(this.rotationSource.getRotation());
        // Some other code
}
            Thanks for your solution. It’s the only one I’ve found to rotate an object around it’s y-axis correcly and get the right Euler Angle. I’m using this calculated yaw angle now to create a quaternion rotation like this:
var rotQuat = pc.Quat();
rotQuat.setFromEulerAngles(0.0 , yaw, 0.0);
The code for setFromEulerAngles is:
/**
 * @function
 * @name pc.Quat#setFromEulerAngles
 * @description Sets a quaternion from Euler angles specified in XYZ order.
 * @param {Number} ex Angle to rotate around X axis in degrees.
 * @param {Number} ey Angle to rotate around Y axis in degrees.
 * @param {Number} ez Angle to rotate around Z axis in degrees.
 * @returns {pc.Quat} Self for chaining.
 * @example
 * var q = new pc.Quat();
 * q.setFromEulerAngles(45, 90, 180);
 */
function setFromEulerAngles(ex, ey, ez) {
    var sx, cx, sy, cy, sz, cz, halfToRad;
    halfToRad = 0.5 * pc.math.DEG_TO_RAD;
    ex *= halfToRad;
    ey *= halfToRad;
    ez *= halfToRad;
    sx = Math.sin(ex);
    cx = Math.cos(ex);
    sy = Math.sin(ey);
    cy = Math.cos(ey);
    sz = Math.sin(ez);
    cz = Math.cos(ez);
    this.x = sx * cy * cz - cx * sy * sz;
    this.y = cx * sy * cz + sx * cy * sz;
    this.z = cx * cy * sz - sx * sy * cz;
    this.w = cx * cy * cz + sx * sy * sz;
    return this;
}
I’ve found it in the forum somewhere, but it works!
I have to admit, that I don’t get it but thanks again for your code (I even don’t get the problem, cause i’ve never had this problem in other game engines before).