Find the Best Demos and Examples

Check out this quick tour to find the best demos and examples for you, and to see how the Felgo SDK can help you to develop your next app or game!

Forums

OverviewFelgo 2 Support (Qt 5) › PolygonItem MouseArea

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #17180

    Bas

    hi,

    i need an accurate touch detection in a PolygonItem.

    is this possible somehow?

    or do i have to write the point detection myself?

    Greetz,

    Bas

     

    #17184

    Bas

    found one and was easy to use with the vertices

    #17187

    Günther
    Felgo Team
    #17192

    Bas
    import Felgo 3.0
    import QtQuick 2.0
    
    GameWindow {
        id: gameWindow
    
        activeScene: scene
    
        screenWidth: 960
        screenHeight: 640
    
        Scene {
            id: scene
    
            // the "logical size" - the scene content is auto-scaled to match the GameWindow size
            width: 480*2
            height: 320*2
    
            PolygonItem {
                id: pol
                fill: true
                color:"grey"
                vertices: [
                    Qt.point(20, 20),
                    Qt.point(200,40),
                    Qt.point(80, 20),
                    Qt.point(600, 100),
                    Qt.point(0, 100)
                  ]
    
                MouseArea {
                    anchors.fill: parent
    
                    // when the rectangle that fits the whole scene is pressed, change the background color and the text
                    onPressed: {
                        var mx = mouse.x
                        var my = mouse.y
                        var ins =  inside( Qt.point(mx,my), pol.vertices );
    
                        console.debug("pressed position:", mouse.x, mouse.y, ins)
                    }
                }
            }
    
        }
    
        function inside(point, vs) {
            // ray-casting algorithm based on
            // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
    
            var x = point.x, y = point.y;
    
            var inside = false;
            for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
                var xi = vs[i].x, yi = vs[i].y;
                var xj = vs[j].x, yj = vs[j].y;
    
                var intersect = ((yi > y) != (yj > y))
                    && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
                if (intersect) inside = !inside;
            }
    
            return inside;
        }
    
    }
    

     

    #17193

    Bas

    for anyone who is interested, ok it needs some adjustments, but the start is there

Viewing 5 posts - 1 through 5 (of 5 total)

RSS feed for this thread

You must be logged in to reply to this topic.