Qt Quick 3D物理-叶轮示例

Qt Quick 3D Physics - Impeller Example

Qt Quick 3D物理-叶轮示例

Demonstrates using trigger bodies and collision info.

演示如何使用触发器体和碰撞信息。

This example demonstrates how to use trigger bodies and collision info. The scene consists of a green static plane, a red dynamic sphere, a pink box trigger and a blue static sphere. When the red sphere overlaps the trigger body it will turn yellow and when it collides with the blue sphere it will be repelled away.

此示例演示如何使用触发器实体和碰撞信息。场景由绿色静态平面、红色动态球体、粉色框触发器和蓝色静态球体组成。当红色球体与触发器主体重叠时,它将变为黄色,当它与蓝色球体碰撞时,它会被排斥。

Setup

设置

As usual we need to add our DynamicsWorld:

​像往常一样,我们需要添加DynamicsWorld:

DynamicsWorld {
    gravity: Qt.vector3d(0, -490, 0)
}

We also add a View3D where we put our scene objects. In this we have some settings for the visual environment:

​我们还添加了一个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: 100
}

Physical objects

物体

We have our regular static plane:

我们有常规的静态平面:

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

This is how our dynamic sphere is defined:

这就是定义动态球体:

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

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

The property inArea is a custom property we use to keep track of when the sphere is overlapping the box trigger body. This is then used for the baseColor property to make the sphere yellow when it is overlapping the box and red otherwise. Since we want the sphere to partake in contact reporting the property sendContactReports needs to be set to true.

​inArea属性是一个自定义属性,用于跟踪球体何时与长方体触发器体重叠。然后用于baseColor属性,使球体在与长方体重叠时变为黄色,否则变为红色。由于我们希望球体参与联系人报告,因此需要将属性sendContactReports设置为true。

Now let's look at the trigger body:

现在让我们看看触发器主体:

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
        }
    }

    onBodyEntered: (body) => {
        if (body.hasOwnProperty('inArea'))
            body.inArea = true;
    }
    onBodyExited: (body) => {
        if (body.hasOwnProperty('inArea'))
            body.inArea = false;
    }
}

The qml type is a TriggerBody which acts like a static body except it's collisions are inactive. Instead it will trigger bodyEntered and bodyExited method calls. In this case we check if the triggering body has a inArea property and if so set it to true or false.

​qml类型是一个TriggerBody,它的行为类似于静态体,只是其碰撞处于非活动状态。相反,它将触发bodyEntered和bodyExited方法调用。在这种情况下,我们检查触发主体是否具有inArea属性,如果是,则将其设置为true或false。

Finally, lets look at the impeller:

最后,让我们看看叶轮:

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 force = normal.times(-2000);
            body.applyCentralImpulse(force);
        }
    }
}

This is a static body and we set receiveContactReports to true to enable the collision callbacks. The callback bodyContact gets called whenever a collision is reported. In the method we add a repelling impulse in the opposite direction of the collision.

​这是一个静态主体,我们将receiveContactReports设置为true以启用冲突回调。每当报告碰撞时,就会调用回调bodyContact。在该方法中,我们添加了与碰撞方向相反的排斥冲量。

Files:

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值