Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Change vertices in box2d polygon



  • hi, everybody.
    i'm writing game, using qml & box2d. there's file containing box2d polygon and image:
    @
    import QtQuick 1.0
    import Box2D 1.0

    Body {
    id: block
    fixedRotation: false
    sleepingAllowed: false
    bodyType: Body.Static

    property variant __x: []
    property variant __y: []
    property alias image: image.source
    
    function setBlock(x,y)
    {
        __x = x;
        __y = y;
        polygon.vertices = [
            Qt.point(__x[0],__y[0]),
            Qt.point(__x[1],__y[1]),
            Qt.point(__x[2],__y[2]),
            Qt.point(__x[3],__y[3]),
        ];
    }
    
    fixtures: Polygon {
        id: polygon
        anchors.fill: parent
        density: 0;
        friction: 10;
        restitution: 1;
        vertices: [
            Qt.point(__x[0],__y[0]),
            Qt.point(__x[1],__y[1]),
            Qt.point(__x[2],__y[2]),
            Qt.point(__x[3],__y[3]),
        ]
    }
    
    Image {
        id: image
        x: __x[2]
        y: __y[1]
        width: __x[0] - __x[2]
        height: __y[0] - __y[1]
        fillMode: Image.Tile
        source: "../../img/block_.png"
    }
    
    Behavior on x { SmoothedAnimation { velocity: 300 } }
    Behavior on y { SmoothedAnimation { velocity: 300 } }
    

    }
    @

    i create it dymanically from javascript like this:
    @
    var dynamicObject = block.createObject(world,{"__x": var_1,"__y":var_2});
    @
    If i try to change vertices, using setBlock() method polygon's coordinates stay the same, only image changes.
    Does somebody know what to do?



  • I've found that it's possible to recreate object with new vertices:
    @
    blockArray[i].destroy();
    var block = Qt.createComponent("Block.qml");
    if (block.status == Component.Ready) {
    var dynamicObject = block.createObject(world,{"__x": x,"__y": y});
    blockArray[i] = dynamicObject;
    }
    @
    But it's not the solution of problem, it's just the way to make it work


Log in to reply