aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick3d/xr_shared/RotateGadget.qml
blob: b52fe5a75512fa0e942323e8317bcc693834e0cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQuick
import QtQuick3D

pragma ComponentBehavior: Bound

AxisGadget {
    source: "meshes/rotate_mesh.mesh"

    property quaternion originalRot
    property vector3d pressOffset

    property vector3d rotationPlaneNormal
    property vector3d rotationAxis

    function getAngleFromRay(pos: vector3d, dir: vector3d) : real {
        const origin = scenePosition
        const point3D = linePlaneIntersection(origin, rotationPlaneNormal, pos, dir)

        const dragOffset = point3D.minus(origin).normalized()

        const cos = dragOffset.dotProduct(pressOffset)
        const sin = dragOffset.crossProduct(pressOffset).dotProduct(rotationPlaneNormal)

        return -Math.atan2(sin, cos)
    }

    function linePlaneIntersection(planePoint: vector3d, planeNormal: vector3d, linePoint: vector3d, lineDirection: vector3d): vector3d {
        let dot = planeNormal.dotProduct(lineDirection)

        if (dot === 0) {
            console.log("This should not happen")
            return planePoint
        }
        const t =  (planeNormal.dotProduct(planePoint) - planeNormal.dotProduct(linePoint)) / dot
        return linePoint.plus(lineDirection.times(t))
    }

    onPressed: (pos, dir) => {
        originalRot = controlledObject.rotation
        rotationAxis = controlledObject.mapDirectionToScene(axisDirection).normalized()

        const dot = rotationAxis.dotProduct(dir) // protect against rotation gadget being edge on:
        rotationPlaneNormal = Math.abs(dot) < 0.2 ? dir.times(-1) : rotationAxis

        const origin = scenePosition
        pressOffset = linePlaneIntersection(origin, rotationPlaneNormal, pos, dir).minus(origin).normalized()
    }

    onMoved: (pos, dir) => {
        const angle = getAngleFromRay(pos, dir)
        controlledObject.rotation = originalRot.times(Quaternion.fromAxisAndAngle(axisDirection, angle * 180 / Math.PI))
    }
}