如何使两个物体在QML Box2D中最靠近碰撞点的一侧连接在一起?

时间:2018-11-20 06:09:42

标签: qt qml box2d

我正在处理一个QML Box2D项目,该项目具有许多不同的多边形,这些形式都是具有多边形夹具的Box2D动态实体,它们都可以在一个世界中动态移动。

我的目标是做到这样,以便每当两个不同的物体相互碰撞时,它们都将在最初碰撞的任一侧上接合,方法是面对面地匹配两个侧面,并本质上接合成一个单一的但其余的作为两个独立的实体,以便使用游戏的逻辑对其进行跟踪。

这就像是两个磁铁在太空中漂浮并相互连接,然后使两个磁性面相互吸引并在某种意义上融合在一起。

虽然我在创建实体方面没有任何问题,并且在确定何时发生碰撞以及碰撞时执行各种功能都没有问题,但是我无法让两个对象简单地组合在距离碰撞最近的一侧..

这是到目前为止我一直尝试做的,但没有成功:

Body {
    id: body
     world: physicsWorld

     property var vertices
     bodyType: Body.Dynamic
     target: gamePiece

     function beginContact(other) {
         if (other.getBody() !== wallBody) {
             var newObject = Qt.createQmlObject('import QtQuick 2.9; import Box2D 2.0; DistanceJoint { }', body, "dynamicSnippet1");
             newObject.bodyA = body;
             newObject.bodyB = other.getBody();
             newObject.length = 80;
             DistanceJoint.collideConnected = true;
             body.angularVelocity = 0;
             body.fixedRotation = true;
             console.log("Created Distance Joint " + newObject);
         } else {
             console.log("Skipping Body collision with wall");
         }
     }
     fixtures: Polygon {
         density: 2
         friction: 0.9
         restitution: 0.01
         vertices: body.vertices
         onBeginContact: { body.beginContact(other) }
     }

 }

每个与另一个Body碰撞的对象都将被完全拉进碰撞的Body中,并且侧面完全不匹配。

我将如何确定进行接触的机构的哪些方面以及如何联系它们的最佳方法?

1 个答案:

答案 0 :(得分:1)

我猜WeldJoint更适合,例如:

import QtQuick 2.11
import QtQuick.Window 2.11
import Box2D 2.0

Window {
    visible: true
    width: 800
    height: 600
    title: qsTr("Hello World")
    id: root

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

    Repeater {
        model: [
            { "x": 0, "y": 0, "width": 10, "height": root.height },
            { "x": root.width - 10, "y": 0, "width": 10, "height": root.height },
            { "x": 10, "y": 0, "width": root.width - 20, "height": 10 },
            { "x": 10, "y": root.height - 10, "width": root.width - 20, "height": 10 }
        ]
        delegate: Rectangle {
            id: wall
            x: modelData.x
            y: modelData.y
            width: modelData.width
            height: modelData.height
            color: "lightgreen"
            Body {
                bodyType: Body.Static
                target: wall
                fixtures: Box {
                    width: wall.width
                    height: wall.height
                    friction: 0.5
                    density: 0.5
                }
            }
        }
    }

    Rectangle {
        id: item1
        height: 100
        width: 100
        color: "orange"
        antialiasing: true
        smooth: true
        x: 100
        y: 100
        Body {
            id: itemBody1
            bodyType: Body.Dynamic
            target: item1
            fixtures: Box {
                density: 0.1
                friction: 0.1
                restitution: 1
                width: item1.width
                height: item1.height
                onBeginContact: {
                    var body = other.getBody();
                    if(body === itemBody2)
                    {
                        var newJoint = linkJoint.createObject(root);
                        newJoint.bodyA = itemBody1;
                        newJoint.bodyB = body;
                    }                    
                }
            }
        }
        Component.onCompleted: {
            var x = ((Math.random() * 800) - 400) / 200;
            var y = ((Math.random() * 600) - 300) / 200;
            itemBody1.applyLinearImpulse(Qt.point(x, y), Qt.point(50,50))
        }
    }
    Rectangle {
        id: item2
        height: 100
        width: 100
        color: "lightblue"
        antialiasing: true
        smooth: true
        x: 600
        y: 100
        Body {
            id: itemBody2
            bodyType: Body.Dynamic
            target: item2
            fixtures: Box {
                density: 0.1
                friction: 0.1
                restitution: 1
                width: item1.width
                height: item1.height
            }
        }
        Component.onCompleted: {
            var x = ((Math.random() * 800) - 400) / 200;
            var y = ((Math.random() * 600) - 300) / 200;
            itemBody2.applyLinearImpulse(Qt.point(x, y), Qt.point(50,50))
        }
    }
    Component {
        id: linkJoint
        WeldJoint {
            localAnchorA: Qt.point(50, 50)
            localAnchorB: Qt.point(150, 150)
            collideConnected: true
        }
    }
}

当然,您必须在这里按距离和角度进行游戏才能满足自己的需求。