Qt Quick 3D Physics - 임펠러 예제

트리거 바디와 콜리전 정보를 사용하는 데모입니다.

이 예제는 트리거 바디와 콜리전 정보를 사용하는 방법을 보여줍니다. 이 장면은 녹색 정적 평면, 빨간색 동적 구체, 분홍색 상자 트리거, 파란색 정적 구체로 구성됩니다. 빨간색 구체가 트리거 바디와 겹치면 노란색으로 바뀌고 파란색 구체와 충돌하면 튕겨져 나갑니다.

설정

평소와 같이 PhysicsWorld 을 추가해야 합니다:

PhysicsWorld {
    gravity: Qt.vector3d(0, -490, 0)
    scene: viewport.scene
}

또한 씬 오브젝트를 넣을 View3D 을 추가합니다. 여기에는 시각적 환경에 대한 몇 가지 설정이 있습니다:

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

PerspectiveCamera {
    position: Qt.vector3d(0, 200, 1000)
    clipFar: 2000
    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(500, 500, 1)
        materials: PrincipledMaterial {
            baseColor: "green"
        }
        castsShadows: false
        receivesShadows: true
    }
}

이것이 동적 구체가 정의되는 방식입니다:

DynamicRigidBody {
    id: sphere
    massMode: DynamicRigidBody.CustomDensity
    density: 0.00001
    position: Qt.vector3d(0, 600, 0)
    property bool inArea: false
    sendContactReports: true
    receiveTriggerReports: true

    onEnteredTriggerBody: {
        inArea = true
    }
    onExitedTriggerBody: {
        inArea = false
    }

    collisionShapes: SphereShape {}
    Model {
        source: "#Sphere"
        materials: PrincipledMaterial {
            baseColor: sphere.inArea ? "yellow" : "red"
        }
    }
}

inArea 속성은 구가 상자 트리거 바디와 겹치는 시점을 추적하는 데 사용하는 사용자 지정 속성입니다. 그런 다음 baseColor 속성을 사용하여 구가 상자와 겹치면 구를 노란색으로, 그렇지 않으면 빨간색으로 만듭니다. 구가 접촉 보고에 참여하도록 하려면 sendContactReports 속성을 true 으로 설정해야 합니다. 또한 구가 TriggerBody 에 들어오고 나갈 때 콜백을 받도록 하려면 receiveContactReports 속성도 true 로 설정합니다. 구에 enteredTriggerBodyexitedTriggerBody 신호 메서드를 구현하고 트리거 바디에 들어가거나 나갈 때 inArea 속성을 true 또는 false 로 설정합니다.

이제 트리거 본문을 살펴봅시다:

TriggerBody {
    position: Qt.vector3d(0, 350, 0)
    scale: Qt.vector3d(1, 2, 1)

    collisionShapes: BoxShape {
        id: boxShape
    }
    Model {
        source: "#Cube"
        materials: PrincipledMaterial {
            baseColor: Qt.rgba(1, 0, 1, 0.2)
            alphaMode: PrincipledMaterial.Blend
        }
    }
}

qml 유형은 TriggerBody 으로, 충돌이 비활성 상태라는 점을 제외하면 정적 바디처럼 작동합니다. 대신 구체에서 enteredTriggerBodyexitedTriggerBody 메서드 호출을 트리거합니다.

마지막으로 임펠러를 살펴봅시다:

StaticRigidBody {
    position: Qt.vector3d(0, 0, 0)
    scale: Qt.vector3d(2, 2, 2)
    receiveContactReports: true

    collisionShapes: SphereShape {}

    Model {
        source: "#Sphere"
        materials: PrincipledMaterial {
            baseColor: "blue"
        }
    }

    onBodyContact: (body, positions, impulses, normals) => {
        for (var normal of normals) {
            let velocity = normal.times(-700)
            body.setLinearVelocity(velocity)
        }
    }
}

이것은 정적 바디이며 receiveContactReportstrue 로 설정하여 콜리전 콜백을 활성화합니다. 콜백 bodyContact 은 충돌이 보고될 때마다 호출됩니다. 메서드에서는 setLinearVelocity 을 호출하여 임펠러를 시뮬레이션하기 위해 충돌 법선 벡터의 반대 방향으로 선형 속도를 설정합니다.

파일:

© 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.