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.

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