April 25, 2012

longWorm longWorm
Lab Rat
7 posts

Change vertices in box2d polygon

 

hi, everybody.
i’m writing game, using qml & box2d. there’s file containing box2d polygon and image:

  1. import QtQuick 1.0
  2. import Box2D 1.0
  3.  
  4. Body {
  5.     id: block
  6.     fixedRotation: false
  7.     sleepingAllowed: false
  8.     bodyType: Body.Static
  9.  
  10.     property variant __x: []
  11.     property variant __y: []
  12.     property alias image: image.source
  13.  
  14.     function setBlock(x,y)
  15.     {
  16.         __x = x;
  17.         __y = y;
  18.         polygon.vertices = [
  19.             Qt.point(__x[0],__y[0]),
  20.             Qt.point(__x[1],__y[1]),
  21.             Qt.point(__x[2],__y[2]),
  22.             Qt.point(__x[3],__y[3]),
  23.         ];
  24.     }
  25.  
  26.     fixtures: Polygon {
  27.         id: polygon
  28.         anchors.fill: parent
  29.         density: 0;
  30.         friction: 10;
  31.         restitution: 1;
  32.         vertices: [
  33.             Qt.point(__x[0],__y[0]),
  34.             Qt.point(__x[1],__y[1]),
  35.             Qt.point(__x[2],__y[2]),
  36.             Qt.point(__x[3],__y[3]),
  37.         ]
  38.     }
  39.  
  40.     Image {
  41.         id: image
  42.         x: __x[2]
  43.         y: __y[1]
  44.         width: __x[0] - __x[2]
  45.         height: __y[0] - __y[1]
  46.         fillMode: Image.Tile
  47.         source: "../../img/block_.png"
  48.     }
  49.  
  50.     Behavior on x { SmoothedAnimation { velocity: 300 } }
  51.     Behavior on y { SmoothedAnimation { velocity: 300 } }
  52. }

i create it dymanically from javascript like this:

  1. 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

April 26, 2012

longWorm longWorm
Lab Rat
7 posts

I’ve found that it’s possible to recreate object with new vertices:

  1.         blockArray[i].destroy();
  2.         var block = Qt.createComponent("Block.qml");
  3.         if (block.status == Component.Ready) {
  4.             var dynamicObject = block.createObject(world,{"__x": x,"__y": y});
  5.             blockArray[i] = dynamicObject;
  6.         }

But it’s not the solution of problem, it’s just the way to make it work

 
  ‹‹ Difference between normal mouse click and touch      How to make QML WebView Element use Qt::openUrlExternally for all ’new windows’? ››

You must log in to post a reply. Not a member yet? Register here!