Box2D Examples
import QtQuick 2.0
import Felgo 4.0
GameWindow {
id: gameWindow
screenWidth: 960
screenHeight: 640
activeScene: scene
Image {
anchors.fill: parent
source: "background.png"
}
Scene {
id: scene
width: 480
height: 320
PhysicsWorld {
id: world
debugDrawVisible: true
gravity.y: 9.81
}
Repeater {
model: 4
WoodenBox {
x: Math.random() * (scene.width - 100) + 20
y: Math.random() * (scene.height / 3) + 20
rotation: Math.random() * 90
}
}
Wall {
id: ground
height: 20
anchors { left: parent.left; right: parent.right; bottom: parent.bottom }
}
Wall {
id: ceiling
height: 20
anchors { left: parent.left; right: parent.right; top: parent.top }
}
Wall {
id: leftWall
width: 20
anchors { left: parent.left; bottom: ground.top; top: ceiling.bottom }
}
Wall {
id: rightWall
width: 20
anchors { right: parent.right; bottom: ground.top; top: ceiling.bottom }
}
Rectangle {
property real mx: mouseArea.mouseX - scene.width / 2
property real my: mouseArea.mouseY - scene.height / 2
property real len: 2 * Math.sqrt(mx * mx + my * my)
rotation: 180 / Math.PI * Math.atan2(my, mx)
transformOrigin: Item.Center
x: scene.width / 2 - len / 2
y: scene.height / 2
width: len
height: 2
color: "red"
}
Rectangle {
id: rect
property Item target: null
width: target ? target.width : 0
height: target ? target.height : 0
x: target ? target.x : 0
y: target ? target.y : 0
color: "green"
opacity: 0.5
rotation: target ? target.rotation : 0
visible: !!target
transformOrigin: Item.TopLeft
}
RayCast {
id: raycast
maxFraction: 2
onFixtureReported: {
var body = fixture.getBody()
var entity = body.target
rect.target = entity
maxFraction = 0
}
}
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
onClicked: mouse => {
rect.target = null
raycast.maxFraction = 2
var from = Qt.point(mouse.x, mouse.y)
var to = Qt.point(scene.width / 2, scene.height / 2)
console.log("raycast", from, to)
world.rayCast(raycast, from, to)
}
}
}
EntityManager {