传感器与运动学与动态夹具的接触

Sensor contact with kinematic vs dynamic fixtures

我正在尝试通过向静止物体添加传感器来检测移动物体。 box1 是静止的,有一个大的圆形传感器,box2 是运动的,通过设置其线速度移动。

import QtQuick 2.6
import QtQuick.Window 2.2
import Box2D 2.0 as Box2D

Window {
    id: window
    width: 640
    height: 480
    visible: true

    Box2D.World {
        id: physicsWorld
        gravity: Qt.point(0, 0)
    }

    Item {
        id: box1
        x: window.width / 2 - width / 2
        y: window.height / 2 - height / 2
        width: 32
        height: 32

        property int sensorRadius: 128

        Box2D.Body {
            id: boxBody
            target: box1

            fixtures: [
                Box2D.Box {
                    width: box1.width
                    height: box1.height
                },
                Box2D.Circle {
                    x: box1.width / 2 - box1.sensorRadius
                    y: box1.height / 2 - box1.sensorRadius
                    objectName: boxBody.objectName + "CircleSensor"
                    radius: box1.sensorRadius
                    sensor: true

                    onBeginContact: touchIndicator.border.color = "red"
                    onEndContact: touchIndicator.border.color = "transparent"
                }
            ]
        }

        Rectangle {
            id: touchIndicator
            anchors.centerIn: parent
            width: box1.sensorRadius * 2
            height: box1.sensorRadius * 2
            color: "transparent"
            border.color: "transparent"
        }
    }

    Item {
        id: box2
        x: 100
        y: 160
        width: 32
        height: 32
        focus: true

        Keys.onSpacePressed: box2Body.linearVelocity = Qt.point(3, 0)

        Box2D.Body {
            id: box2Body
            world: physicsWorld
            target: box2
            bodyType: Box2D.Body.Kinematic

            Box2D.Box {
                width: box2.width
                height: box2.height
            }
        }
    }

    Box2D.DebugDraw {
        id: debugDraw
        world: physicsWorld
        anchors.fill: parent
        opacity: 0.75
    }
}

这是结果:

传感器未检测到移动物体。

如果我让移动物体动态:

bodyType: Box2D.Body.Dynamic

然后检测到它:

正确的做法是什么?

请记住:

  1. 我想保持运动简单,因为我通过力吸取精确运动(因此我目前使用 linearVelocity 的方法并想使用 Kinematic)。
  2. 我没有 want/need 任何可以实际碰撞和反弹的东西,我只需要传感器和光线投射。

运动体具有无限质量,因此不能参与碰撞检测。要检测碰撞,其中一个物体必须是 Dynamicqml-box2d 插件中的默认 bodyTypeStatic。所以你的场景包含 2 个物体 - KinematicStatic 并且传感器没有检测到接触。将 boxBody.bodyType 更改为 Body.Dynamic 即可解决问题。