我使用Box2D作为物理引擎,使用QtQuick作为可视化工具。
我设置了一个简单的场景,用下落的小矩形和平台来测试碰撞
// the platform block
Body {
id: block
bodyType: Body.Kinematic
width:200
height:20
transformOrigin: Body.Center
fixtures: Box {
anchors.fill: parent
friction: 1
density: 1
}
Rectangle {
anchors.fill: parent
color: "yellow"
border.color: "blue"
}
MouseArea {
anchors.fill: parent
onClicked: {
block.rotation += 20
}
}
}在QML中,我可以设置旋转中心:
transformOrigin: Body.Center默认情况下,transformOrigin是object的左上角,在这种情况下,一切都很好。

这是从Box2D获取坐标并绘制QML对象时的代码的一部分。
//getting coordination and angle of Box2D object
const b2Vec2 position = mBody->GetPosition();
const float32 angle = mBody->GetAngle();
const qreal newX = position.x * scaleRatio;
const qreal newY = -position.y * scaleRatio;
const qreal newRotation = -(angle * 360.0) / (2 * b2_pi);
// paint QML object at received coords
setX(newX);
setY(newY);
setRotation(newRotation);我的问题是-如何在Box2D中设置原点?
发布于 2013-12-29 18:23:09
为了保持代码简单,并且因为qml-box2d是一个未完成的库,transformOrigin设置为TopLeft (Body的默认值)是当前唯一受支持的值。
但是,Box2D主体本身并不需要大小。只有其夹具的大小是相关的。如果在body上设置0大小,那么它的transformOrigin就不再重要了。如果随后将设备置于主体的中心,则主体的旋转将有效地应用于设备的中心。
我已经使用这种方法调整了您的简单场景,如下所示:
// the platform block
Body {
id: block
bodyType: Body.Kinematic
fixtures: Box {
id: boxFixture
anchors.centerIn: parent
width:200
height:20
friction: 1
density: 1
}
Rectangle {
anchors.fill: boxFixture
color: "yellow"
border.color: "blue"
}
MouseArea {
anchors.fill: boxFixture
onClicked: {
block.rotation += 20
}
}
}当然,理想情况下,qml-box2d最终将处理transformOrigin的所有可能值,而不管主体的大小。
https://stackoverflow.com/questions/20810176
复制相似问题