Qt Quick 3D Physics - 簡単な例

簡単な物理シーンのセットアップをデモします。

この例題では、簡単な例題のコードを通して、Quick 3D Physicsの基本的な機能の概要を紹介します。この紹介は、Qt Quick 3Dモジュールに精通していることを前提としていることに注意してください。

セットアップ

サンプル全体はmain.qmlファイルに含まれ、セットアップされています。まず、物理世界を作るオブジェクトを見てみましょう。このノードはPhysicsWorld と呼ばれています:

PhysicsWorld {
    scene: viewport.scene
}

scene プロパティをView3D のシーンに設定します。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 の2種類があります。StaticRigidBody は静的な(動かせない)剛体を含むQMLノードです。ボディを動かすことは技術的には可能ですが、パフォーマンス上のペナルティが生じます。一方、DynamicRigidBody は動くオブジェクトに使われます。DynamicRigidBody にはisKinematic プロパティがあり、これを true に設定すると、剛体は外力の影響を受けなくなり、スクリプトやアニメーションから制御できるようになります。例えば、サッカーボールはキネマティックになりませんが、エレベーターや動くプラットフォームはキネマティックになります。また、サッカーボールのposition プロパティを更新しても、エレベーターはその位置に移動しますが、サッカーボールはその位置に移動しません。

他の物理オブジェクトと相互作用するためには、collisionShapes プロパティを設定する必要があります。collisionShapes プロパティは、1つまたは複数のシェイプを含むことができるリストで、組み合わされた1つの剛体ユニットとして動作します。これらの形状は、互いに対して位置決めしたり回転させたりすることができます。平面、三角メッシュ、ハイトマップシェイプは、ボディがキネマティックである場合にのみ、ダイナミックボディと連動することに注意してください。

シェイプ

私たちのシーンには、4つの物理オブジェクト(平面、箱、ボール、カプセル)があります。1つずつ見ていきましょう。

平面
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 を設定します。 平面は、空間を「上」と「下」に分割します。平面の「下」にあるものはすべて平面に衝突し、平面の上に押し出される。プレーンはYZ平面上にあり、「上」は正のXを指しています。プレーンをレンダリングするために、StaticRigidBody ノードの中にモデルを入れます。これはよくあるパターンで、物理オブジェクトがシーン内で相互作用するときに、モデルも同じように動き、回転するようにします。平面を XY 平面上に配置したいので、eulerRotation プロパティを使って回転させます。モデルも同様に回転させます。

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

DynamicRigidBody ノードを作成し、collisionShapesBoxShape を 1 つ作成します。これは動的なノードなので、ボックスは自由なオブジェクトのように衝突したり動いたりしてシーンと相互作用します。立方体モデルは単位立方体の100倍大きいので、それに応じてスケーリングする必要があります。これはダイナミックボディなので、物理的な重さがあります。density プロパティを設定します。

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

DynamicRigidBody ノードを作り、collisionShapesSphereShape を1つ作ることで、球体を作ります。

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

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

DynamicRigidBody ノードを作り、collisionShapesCapsuleShape を1つ作って、カプセルを作ります。Qt Quick 3D Physics で提供される組み込みの CapsuleGeometry を使用して、カプセルモデルを構築します。

ファイル

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