LookAtTransformThe LookAtTransform item implements a transformation that causes objects to face the camera. More... Inherits QtObject LookAtTransform instantiates the C++ class QGraphicsLookAtTransform This type was introduced in Qt 4.8. Detailed DescriptionSometimes it can be useful to make an object face towards another object, wherever it might be located. This is useful for objects like cameras, lights, arrows, faces etc. Another common use is called "billboarding", where a quad is always oriented to face the camera. In QML, this can be achieved as follows: Camera3D camera Item3D { mesh: Mesh { source: "pane.obj" } position: Qt.vector3d(2, 0, -20) transform: LookAtTransform { worldPosition - camera.worldPosition } effect: Effect { texture: "picture.jpg" } } Because the lookAt transformation will override many other transformations ont the matrix, it will usually be the last element in the transform list (transformations are applied to the matrix in reverse order of their appearance in transform): Item3D { mesh: Mesh { source: "pane.obj" } position: Qt.vector3d(2, 0, -20) transform: [ Scale3D { scale: 0.5 }, Rotation3D { angle: 30 }, LookAtTransform { worldPosition - camera.worldPosition } ] effect: Effect { texture: "picture.jpg" } } Typically, orientation as well as facing is important. For example, a face that does not remain basically upright will look very odd indeed as it tracks a subject. The lookAt transform will always rotate first around the local y axis, and secondarily around the local x axis, around an origin of (0,0,0). If rotation around a different axis or origin is desired, place the Item3D to be rotated inside a new parent Item3D. Apply rotations the and translations to the original Item3D such that the desired "front" matches the new parent Item3d's positive z direction and the left to right matches the parent's x-axis, and then apply the LookAt transform to the new parent item. Item3D { position: Qt.vector3d(0,0,4) transform: LookAt { subject: subjectPenguin } Item3D { id: lookAwayMonkey mesh: Mesh { source: "meshes/monkey.3ds" } transform: Rotation3D { axis: Qt.vector3d(0,1,0); angle: 180 } } } By default the LookAt transform will cause the object to face directly at the subject no matter how the world co-ordinate system is rotated. Sometimes it is useful to limit the lookAt to only one axis of rotation - for example, a tank with a turret and barrel that each have only one degree of freedom. This is achieved by using the preserveUpVector property: Pane { position: Qt.vector3d(2, 0, -20) transform: LookAtTransform { preserveUpVector: true } effect: Effect { texture: "picture.jpg" } } |