Qt Quick 3D Physics - 간단한 예제

간단한 물리 장면을 설정하는 방법을 보여줍니다.

이 예제에서는 간단한 예제 코드를 통해 기본 퀵 3D 피직스 기능에 대한 개요를 소개합니다. 이 소개에서는 Qt Quick 3D 모듈에 익숙하다는 가정 하에 설명합니다.

설정

전체 예제는 main.qml 파일에 포함되어 있으며 설정되어 있습니다. 먼저 물리적 세계를 생성하는 객체를 살펴봅시다. 이 노드의 이름은 PhysicsWorld 입니다:

PhysicsWorld {
    scene: viewport.scene
}

scene 프로퍼티를 View3D 의 장면으로 설정합니다. scene 프로퍼티는 시뮬레이션이 시뮬레이션할 물리 노드를 찾을 위치를 정의합니다. 씬 노드 외부에 배치된 모든 물리 노드는 시뮬레이션에 포함되지 않습니다.

Scene

씬을 살펴보겠습니다.

View3D {
    id: viewport
    anchors.fill: parent

    environment: SceneEnvironment {
        clearColor: "#d6dbdf"
        backgroundMode: SceneEnvironment.Color
    }

    PerspectiveCamera {
        position: Qt.vector3d(-200, 100, 500)
        eulerRotation: Qt.vector3d(-20, -20, 0)
        clipFar: 5000
        clipNear: 1
    }

    DirectionalLight {
        eulerRotation.x: -45
        eulerRotation.y: 45
        castsShadow: true
        brightness: 1
        shadowFactor: 50
    }

    StaticRigidBody {
        position: Qt.vector3d(0, -100, 0)
        eulerRotation: Qt.vector3d(-90, 0, 0)
        collisionShapes: PlaneShape {}
        Model {
            source: "#Rectangle"
            scale: Qt.vector3d(10, 10, 1)
            materials: DefaultMaterial {
                diffuseColor: "green"
            }
            castsShadows: false
            receivesShadows: true
        }
    }

    DynamicRigidBody {
        position: Qt.vector3d(-100, 100, 0)
        collisionShapes: BoxShape {
            id: boxShape
        }
        Model {
            source: "#Cube"
            materials: PrincipledMaterial {
                baseColor: "yellow"
            }
        }
    }

    DynamicRigidBody {
        position: Qt.vector3d(0, 100, 0)
        collisionShapes: SphereShape {
            id: sphereShape
        }
        Model {
            source: "#Sphere"
            materials: PrincipledMaterial {
                baseColor: "blue"
            }
        }
    }

    DynamicRigidBody {
        position: Qt.vector3d(75, 200, 0)
        collisionShapes: CapsuleShape {
            id: capsuleShape
        }

        Model {
            geometry: CapsuleGeometry {}
            materials: PrincipledMaterial {
                baseColor: "red"
            }
        }
    }
}

이 씬은 물리적 오브젝트를 그리기 위한 View3D 입니다. 여기에는 렌더링을 위한 PerspectiveCameraDirectionalLight 노드가 포함되어 있지만, 더 중요한 것은 StaticRigidBody 및 3개의 DynamicRigidBody 노드가 포함되어 있다는 것입니다. 물리학에서 강체는 변형이 불가능하고 밀도가 균일한 단단한 물체입니다. StaticRigidBodyDynamicRigidBody 의 두 가지 유형의 리지드 바디를 사용할 수 있습니다. StaticRigidBody 은 정적(움직이지 않는) 리지드 바디를 포함하는 QML 노드입니다. 기술적으로 바디를 움직일 수는 있지만 성능 저하가 발생합니다. 반면 DynamicRigidBody 은 움직일 수 있는 객체에 사용됩니다. DynamicRigidBody 에는 isKinematic 속성이 있으며, 이 속성이 true로 설정되면 리지드 바디는 외부 힘의 영향을 받지 않으며 스크립트 및 애니메이션에서 제어할 수 있습니다. 예를 들어 축구공은 키네마틱이 아니지만 엘리베이터나 움직이는 플랫폼은 일반적으로 키네마틱이 됩니다. 즉, 축구공의 position 프로퍼티를 업데이트하면 엘리베이터는 해당 위치로 이동하지만 축구공은 그 위치로 이동하지 않습니다.

다른 물리적 객체와 상호 작용하려면 collisionShapes 프로퍼티를 설정해야 합니다. collisionShapes 프로퍼티는 하나 이상의 도형을 포함할 수 있는 목록으로, 하나의 리지드 유닛을 결합한 것처럼 작동합니다. 이러한 도형을 서로에 대해 배치하고 회전할 수 있습니다. 평면, 삼각형 메쉬 및 하이트맵 셰이프는 동적 바디가 키네마틱인 경우에만 작동합니다.

셰이프

우리 씬에는 네 가지 물리적 오브젝트(평면, 상자, 공, 캡슐)가 포함되어 있습니다. 한 번에 하나씩 살펴보겠습니다.

평면
StaticRigidBody {
    position: Qt.vector3d(0, -100, 0)
    eulerRotation: Qt.vector3d(-90, 0, 0)
    collisionShapes: PlaneShape {}
    Model {
        source: "#Rectangle"
        scale: Qt.vector3d(10, 10, 1)
        materials: DefaultMaterial {
            diffuseColor: "green"
        }
        castsShadows: false
        receivesShadows: true
    }
}

StaticRigidBody 노드를 만들어 평면을 만듭니다. 이 노드에서 collisionShapes 속성에 PlaneShape 을 포함하도록 설정합니다. 평면은 공간을 "위"와 "아래"로 나눕니다. 평면의 "아래"에 있는 모든 것은 평면과 충돌하여 평면 위로 밀려납니다. 평면은 양수 X를 가리키는 "위"가 YZ 평면에 놓이고 StaticRigidBody 노드 안에 모델을 배치하여 평면을 렌더링합니다. 이는 장면에서 실제 물체가 상호작용할 때 모델도 같은 방식으로 움직이고 회전하도록 하는 일반적인 패턴입니다. 평면이 XY 평면에 놓이기를 원하므로 eulerRotation 속성을 사용하여 평면을 회전합니다. 모델도 비슷한 방식으로 회전합니다.

Box
DynamicRigidBody {
    position: Qt.vector3d(-100, 100, 0)
    collisionShapes: BoxShape {
        id: boxShape
    }
    Model {
        source: "#Cube"
        materials: PrincipledMaterial {
            baseColor: "yellow"
        }
    }
}

DynamicRigidBody 노드와 collisionShapes 에 단일 BoxShape 을 만들어 구를 만듭니다. 이 노드는 동적 노드이므로 상자는 자유 객체처럼 충돌하고 움직이며 장면과 상호 작용합니다. 큐브 모델은 단위 큐브보다 100배 더 크기 때문에 그에 맞게 크기를 조정해야 합니다. 동적 바디이므로 물리적 무게가 있으므로 density 프로퍼티를 설정합니다.

구체
DynamicRigidBody {
    position: Qt.vector3d(0, 100, 0)
    collisionShapes: SphereShape {
        id: sphereShape
    }
    Model {
        source: "#Sphere"
        materials: PrincipledMaterial {
            baseColor: "blue"
        }
    }
}

collisionShapes 에서 DynamicRigidBody 노드와 단일 SphereShape 을 만들어 구를 만듭니다.

캡슐
DynamicRigidBody {
    position: Qt.vector3d(75, 200, 0)
    collisionShapes: CapsuleShape {
        id: capsuleShape
    }

    Model {
        geometry: CapsuleGeometry {}
        materials: PrincipledMaterial {
            baseColor: "red"
        }
    }
}

DynamicRigidBody 노드와 collisionShapes 에 단일 CapsuleShape 을 만들어 캡슐을 만듭니다. Qt Quick 3D Physics 에서 제공하는 내장 캡슐 지오메트리를 사용하여 캡슐 모델을 빌드합니다.

파일을 사용합니다:

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.