我猜
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
}
}
}
当然,为了满足你的需要,你必须在这里练习距离和角度。