Box2D Examples
import QtQuick 2.0
import Felgo 4.0
GameWindow {
activeScene: scene
Component {
id: heavyBox
EntityBase {
property alias body: collider.body
transformOrigin: Item.TopLeft
width: 40
height: 40
BoxCollider {
id: collider
density: 0.005
friction: 0.3
restitution: 0.2
}
Rectangle {
anchors.fill: parent
smooth: true
color: "brown"
}
}
}
Component {
id: lightBox
EntityBase {
property alias body: collider.body
transformOrigin: Item.TopLeft
width: 40
height: 40
BoxCollider {
id: collider
density: 0.002
friction: 0.3
restitution: 0.2
}
Rectangle {
anchors.fill: parent
color: "red"
}
}
}
Component {
id: extraJoint
DistanceJoint {
frequencyHz: 20
dampingRatio: 0.5
collideConnected: false
}
}
Timer {
running: true
repeat: false
interval: 2000
onTriggered: {
var ball1Id = entityManager.createEntityFromComponentWithProperties(heavyBox,
{ x: 400, y: 50})
var ball1 = entityManager.getEntityById(ball1Id)
var ball2Id = entityManager.createEntityFromComponentWithProperties(lightBox,
{x: 150, y: 50})
var ball2 = entityManager.getEntityById(ball2Id)
var joint = extraJoint.createObject(world,
{
"bodyA": ball1.body,
"bodyB": ball2.body,
"collideConnected": true
})
}
}
Scene {
id: scene
PhysicsWorld {
id: world
gravity.y: 9.81
}
Wall {
id: ground
height: 20
anchors { left: parent.left; right: parent.right; top: parent.bottom }
}
Wall {
id: ceiling
height: 20
anchors { left: parent.left; right: parent.right; bottom: parent.top }
}
Wall {
id: leftWall
width: 20
anchors { right: parent.left; bottom: ground.top; top: ceiling.bottom }
}
Wall {
id: rightWall
width: 20
anchors { left: parent.right; bottom: ground.top; top: ceiling.bottom }
}
Ball {
id: ball
x: 100
y: 100
rotation: 0
radius: 20
}
Square {
id: square
x: 220
y: 180
rotation: 0
width: 40
height: 40
}
DistanceJoint {
id: joint
frequencyHz: 15
dampingRatio: 0.5
collideConnected: true
bodyA: ball.body
bodyB: square.body
localAnchorA: Qt.point(20, 0)
localAnchorB: Qt.point(20, 40)
}
MouseArea {
anchors.fill: parent
onClicked: {
ball.body.applyLinearImpulse(
Qt.point(10 * (mouseX - ball.x),
10 * (mouseY - ball.y)),
ball.body.getWorldCenter())
}
}
}
EntityManager {
id: entityManager