Qt Quick 3D Physics - Compound Shapes Example

Image non disponible

This example demonstrates how to use more than one collision shape to create complex objects for collision detection. The scene consists of a green static plane and a series of links connected to each other. At the beginning, the simulation is disabled. After some time or when the user press the space key, the simulation will start. An animation will start that causes the leftmost link to move horizontally. Since the leftmost link is purely controlled by animation, the isKinematic property is set to true.

Setup

As usual we need to add our DynamicsWorld:

 
Sélectionnez
DynamicsWorld {
    id: physicsWorld
    running: false
    enableCCD: true
}

We do the usual setup where we have an environment, camera and lights:

 
Sélectionnez
environment: SceneEnvironment {
    antialiasingMode: SceneEnvironment.MSAA
    backgroundMode: SceneEnvironment.Color
    clearColor: "lightblue"
}

focus: true

PerspectiveCamera {
    id: camera
    position: Qt.vector3d(-200, 900, 1300)
    eulerRotation: Qt.vector3d(-10, 0, 0)
    clipFar: 15500
    clipNear: 1
}

DirectionalLight {
    eulerRotation.x: -45
    eulerRotation.y: 45
    castsShadow: true
    brightness: 1.5
    shadowFactor: 15
    shadowFilter: 10
    shadowMapFar: 100
    shadowBias: -0.01
    shadowMapQuality: Light.ShadowMapQualityVeryHigh
}

Physical objects

We have our regular static plane:

 
Sélectionnez
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: DefaultMaterial {
            diffuseColor: "green"
        }
        castsShadows: false
        receivesShadows: true
    }
}

Then we make instances of our links.

 
Sélectionnez
MeshLink {
    isKinematic: true
    position: Qt.vector3d(-6 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(90, 0, 0)
    color: "red"

    SequentialAnimation on x {
        running: physicsWorld.running
        PauseAnimation {
            duration: 7000
        }
        NumberAnimation {
            from: -6 * viewport.ringDistance
            to: -9 * viewport.ringDistance
            duration: 3000
            easing.type: Easing.InOutQuad
        }

        SequentialAnimation {
            NumberAnimation {
                from: -9 * viewport.ringDistance
                to: 3 * viewport.ringDistance
                easing.type: Easing.InOutCubic
                duration: 8000
            }
            NumberAnimation {
                to: -9 * viewport.ringDistance
                from: 3 * viewport.ringDistance
                easing.type: Easing.InOutCubic
                duration: 8000
            }
            loops: Animation.Infinite
        }
    }
}

CapsuleLink {
    position: Qt.vector3d(-5 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(90, 0, 0)
}

MeshLink {
    position: Qt.vector3d(-4 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(90, 0, 0)
}

MeshLink {
    position: Qt.vector3d(-3 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(0, 90, 0)
}

MeshLink {
    position: Qt.vector3d(-2 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(90, 0, 0)
}

MeshLink {
    position: Qt.vector3d(-1 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(0, 90, 0)
}

CapsuleLink {
    position: Qt.vector3d(0, viewport.ringY, 0)
}

MeshLink {
    position: Qt.vector3d(1 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(0, 90, 0)
}

MeshLink {
    position: Qt.vector3d(2 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(90, 0, 0)
}

MeshLink {
    position: Qt.vector3d(3 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(0, 90, 0)
}

MeshLink {
    position: Qt.vector3d(4 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(90, 0, 0)
}

CapsuleLink {
    position: Qt.vector3d(5 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(90, 0, 0)
}

MeshLink {
    isKinematic: true
    position: Qt.vector3d(6 * viewport.ringDistance, viewport.ringY, 0)
    eulerRotation: Qt.vector3d(90, 0, 0)
    color: "red"
}

The first link which is on the left has its isKinematic property set to true so that we can control it via animation. The other links are instanced with some spacing between them.

The interesting part is what is happening inside the Mesh and Capsule Links files. Let's take a look at each one of them.

Mesh Link

The Mesh Link is a Dynamic Rigid Body with a model and a material. The model loads the mesh from a mesh file. We also have a list of collision shapes that together are combined and form a compound shape for collision detection. Each shape is a Convex Mesh shape which loads the mesh from a source file. A convex shape is basically a shape where the line between any two points within the shape is always inside the shape.

If we take a closer look when the debug mode is enabled, this is how the collision shapes form the compound collision shape:

Image non disponible

Capsule Link