Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Change vertices in box2d polygon
Qt 6.11 is out! See what's new in the release blog

Change vertices in box2d polygon

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt
      • Unsolved