[SOLVED] Issue with rotation by vector

Hello,
I want to rotate entity based on wall normal. For most of the entities my code works fine. I have tv entity that rotates to wrong direction and I’m not sure why.
I have this code:

const from = this.cameraEntity.getPosition().clone();
const to = this.cameraEntity.camera.screenToWorld(screenPos.x, screenPos.y, this.cameraEntity.camera.farClip);

 const result = this.app.systems.rigidbody.raycastFirstByTag(from, to, "wall");

  if (result) {
this.entity.setPosition(result. Point);
this.entity.setRotation(this.vec3LookRotation(result.normal, pc.Vec3.UP)
}


// vec3 rotation function
vec3LookRotation = function (forward, up) {
    forward = forward.normalize();

    var vector = forward.normalize();
    var vector2 = this.vec.copy(pc.Vec3.ZERO).cross(up, vector).normalize();
    var vector3 = this.vec2.copy(pc.Vec3.ZERO).cross(vector, vector2);
    var m00 = vector2.x;
    var m01 = vector2.y;
    var m02 = vector2.z;
    var m10 = vector3.x;
    var m11 = vector3.y;
    var m12 = vector3.z;
    var m20 = vector.x;
    var m21 = vector.y;
    var m22 = vector.z;

    var num8 = (m00 + m11) + m22;
    var quaternion = this.quat;
    if (num8 > 0.0) {
        var num = Math.sqrt(num8 + 1.0);
        quaternion.w = num * 0.5;
        num = 0.5 / num;
        quaternion.x = (m12 - m21) * num;
        quaternion.y = (m20 - m02) * num;
        quaternion.z = (m01 - m10) * num;
        return quaternion;
    }
    if ((m00 >= m11) && (m00 >= m22)) {
        var num7 = Math.sqrt(((1.0 + m00) - m11) - m22);
        var num4 = 0.5 / num7;
        quaternion.x = 0.5 * num7;
        quaternion.y = (m01 + m10) * num4;
        quaternion.z = (m02 + m20) * num4;
        quaternion.w = (m12 - m21) * num4;
        return quaternion;
    }
    if (m11 > m22) {
        var num6 = Math.sqrt(((1.0 + m11) - m00) - m22);
        var num3 = 0.5 / num6;
        quaternion.x = (m10 + m01) * num3;
        quaternion.y = 0.5 * num6;
        quaternion.z = (m21 + m12) * num3;
        quaternion.w = (m20 - m02) * num3;
        return quaternion;
    }
    var num5 = Math.sqrt(((1.0 + m22) - m00) - m11);
    var num2 = 0.5 / num5;
    quaternion.x = (m20 + m02) * num2;
    quaternion.y = (m21 + m12) * num2;
    quaternion.z = 0.5 * num5;
    quaternion.w = (m01 - m10) * num2;
    return quaternion;
};

function setMat4Forward(mat4, forward, up) {
    var x = this.x;
    var y = this.y;
    var z = this.z;

    // Inverse the forward direction as +z is pointing backwards due to the coordinate system
    z.copy(forward).scale(-1);
    y.copy(up).normalize();
    x.cross(y, z).normalize();
    y.cross(z, x);

    var r = mat4.data;

    r[0] = x.x;
    r[1] = x.y;
    r[2] = x.z;
    r[3] = 0;
    r[4] = y.x;
    r[5] = y.y;
    r[6] = y.z;
    r[7] = 0;
    r[8] = z.x;
    r[9] = z.y;
    r[10] = z.z;
    r[11] = 0;
    r[15] = 1;

    return mat4;
}

Video Record Example

Hi @Lukas_31,

Since you’re using the forward axis of the television, could you confirm in the editor or in the 3D modeling program that the model is pointing in the right direction? In the Editor, the blue arrow should be facing perpendicular to the sides of the television.

2 Likes

Hey thanks! The forward vector was the issue. I fixed it in blender now it works.