Additionally, Joint::bodyA can be set to a body which should be checked for collision detection with the movingBody. For example, you could set it to the ground body, and the Joint::bodyB to a paddle entity of a pong game. That would allow to check collisions between these bodies, as long as Joint::collideConnected is set to true.

The target point initially coincides with the body's anchor point. The maximum force is used to prevent violent reactions when multiple dynamic bodies interact. You can make
this as large as you like. The frequency and damping ratio are used to create a spring/damper effect similar to the distance joint.

Note: Regarding the Box2D doc: Many users have tried to adapt the mouse joint for game play. Users often want to achieve precise positioning and instantaneous response. The mouse joint
doesn't work very well in that context. You may wish to consider using kinematic bodies instead.

Example Usage

The following example demonstrates how to move a body towards the mouse position. In this example, a body can be selected by pressing on it

GameWindow {
Scene {
PhysicsWorld {
id: physicsWorldgravity.y: 9.81
}
// this entity contains a BoxCollider and falls down because of gravityBox {
}
// this component is used to create a MouseJoint dynamically every time a body is selectedComponent {
id: mouseJointMouseJoint {
maxForce: 30000// The damping ratio. 0 = no damping, 1 = critical damping. Default is 0.7dampingRatio: 1// The response speed, default is 5frequencyHz: 2//collideConnected: true // collideConnected is only needed, if 2 bodies are connected with a MouseJoint and those should collide or not - but the typical use case for a MouseJoint is that only 1 body is affected, which should be pulled towards a targetPoint
}
}
MouseArea {
anchors.fill: parent
property BodyselectedBody: null
property MouseJointmouseJointWhileDragging: nullonPressed: {
selectedBody=physicsWorld.bodyAt(Qt.point(mouseX, mouseY));
console.debug("selected body at position", mouseX, mouseY, ":", selectedBody);
if(selectedBody) {
mouseJointWhileDragging=mouseJoint.createObject(physicsWorld)
// set the target point to the current mouse positionmouseJointWhileDragging.target=Qt.point(mouseX, mouseY)
// set the body to move to the currently selected onemouseJointWhileDragging.bodyB=selectedBody
}
}
onPositionChanged: {
if (mouseJointWhileDragging)
mouseJointWhileDragging.target=Qt.point(mouseX, mouseY)
}
onReleased: {
// if the user pressed a body initially, don't create a new box but remove the created MouseJointif(selectedBody) {
selectedBody=nullif (mouseJointWhileDragging)
mouseJointWhileDragging.destroy()
}
}
}
} // end of Scene
} // end of GameWindow