Raycast.qml Example File

examples/physics/box2d-examples/qml/raycast/Raycast.qml
 import QtQuick 2.0
 import Felgo 3.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
     } // end of PhysicsWorld

     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 }
     }

     //show the ray that is being cast as a rectangle
     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"
     }

     //highlight the object which is hit by the ray
     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 //cast ray twice the distance from start to end point
       onFixtureReported: {
         //fixture.getBody() returns the body object, body.target returns the entity which contains the body
         var body = fixture.getBody()
         var entity = body.target

         rect.target = entity
         maxFraction = 0 // cancel current raycast, report no more objects
       }
     }

     MouseArea {
       id: mouseArea
       anchors.fill: parent
       hoverEnabled: true

       onClicked: {
         rect.target = null

         raycast.maxFraction = 2

         //cast a ray from the mouse position through the center of the scene
         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)
       }
     }
   } // end of Scene

   EntityManager {
     id: entityManager
   }
 }

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support