Note that there are some explanatory texts on larger screens.

plurals
  1. POBox2D center of rotation
    text
    copied!<p>I use Box2D as phisics engine and QtQuick as visualizer.</p> <p>I setted up simple scene with falling small rectangles and platform to test collisions</p> <pre><code>// the platform block Body { id: block bodyType: Body.Kinematic width:200 height:20 transformOrigin: Body.Center fixtures: Box { anchors.fill: parent friction: 1 density: 1 } Rectangle { anchors.fill: parent color: "yellow" border.color: "blue" } MouseArea { anchors.fill: parent onClicked: { block.rotation += 20 } } } </code></pre> <p>In QML I can set center of rotation:</p> <pre><code>transformOrigin: Body.Center </code></pre> <p>By default transformOrigin is top-left corner of object and in this case everything paintet fine. But when i move origin to center in QML it is going wrong as decribed at attached image </p> <p><img src="https://i.stack.imgur.com/oRy6B.png" alt="http://www.ruslan.pw/box2d.png"></p> <p>This is part of code when it get coords from Box2D and paint QML object</p> <pre><code>//getting coordination and angle of Box2D object const b2Vec2 position = mBody-&gt;GetPosition(); const float32 angle = mBody-&gt;GetAngle(); const qreal newX = position.x * scaleRatio; const qreal newY = -position.y * scaleRatio; const qreal newRotation = -(angle * 360.0) / (2 * b2_pi); // paint QML object at received coords setX(newX); setY(newY); setRotation(newRotation); </code></pre> <p>The problem is than Box2D rotates object with origin in top-left corner but QML object painted with origin in his center. So QML object is not syncronized with Box2D and falling rectangle painter in wrong place. I maked printscreen but actually box of Box2D is not visible, I added it to understand the problem.</p> <p>And my question - how can I set origin point in Box2D?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload