title: Libgdx Tiled map object to box2d body location
body: <p>A <a href="http://www.mapeditor.org/" rel="noreferrer">Tiled</a> map object hat a position x, y in pixels and a rotation in degrees.</p>
  
  <p>I am loading the coordinates and the rotation from the map and trying to assign them to a <a href="https://github.com/libgdx/libgdx/wiki/Box2d" rel="noreferrer">box2d</a> Body. There are a couple of differences between the location models, for example Tiled object rotation is in degrees and box2d body angle is in radians. </p>
  
  <p><strong>How do I convert the location to the BodyDef coordinates x, y and angle so that the body will be created at the correct position?</strong></p>
  
  <hr>
  
  <h2>Background:</h2>
  
  <p>Using the code:</p>
  
  <pre><code> float angle = -rotation * MathUtils.degreesToRadians;
   bodyDef.angle = angle;
   bodyDef.position.set(x, y);
  </code></pre>
  
  <p>Works when the rotation is 0, but the body is not positioned correctly when rotation is different than 0.</p>
  
  <p>I found a couple of hints here:</p>
  
  <p><a href="http://www.tutorialsface.com/2015/12/qu%20...%20dx-solved/" rel="noreferrer">http://www.tutorialsface.com/2015/12/qu ... dx-solved/</a></p>
  
  <p>and here:</p>
  
  <p><a href="https://github.com/libgdx/libgdx/issues/2742" rel="noreferrer">https://github.com/libgdx/libgdx/issues/2742</a></p>
  
  <p>That seem to tackle this exact problem, however neither solution worked for me, the body objects are still positioned wrong after applying those transformations. By positioned wrong I mean that <em>the body is positioned in the area of the map where it should be but slightly off depending on its rotation</em>.</p>
  
  <p>I feel that it should be pretty simple but I do not know how to mediate the differences between Tiled and box2d locations.</p>
  
  <p>For reference these are the two solutions I tried from the links above (after transforming the values x, y, width, height from pixels to world units):</p>
  
  <pre><code>float angle = rotation * MathUtils.degreesToRadians;
  bodyDef.angle = -angle;
  Vector2 correctionPosition = new Vector2(
          height * MathUtils.cosDeg(-rotation - 90),
          height + height * MathUtils.sinDeg(-rotation - 90));
  bodyDef.position.set(x, y).add(correctionPosition);
  </code></pre>
  
  <p>and </p>
  
  <pre><code>    float angle = rotation * MathUtils.degreesToRadians;
  
      bodyDef.angle = -angle;
  
      // Top left corner of object
      Vector2 correctedPosition = new Vector2(x, y + height);
  
      // half of diagonal for rectangular object
      float radius = (float)Math.sqrt(width * width + height * height) / 2.0f;
  
      // Angle at diagonal of rectangular object
      float theta = (float)Math.tanh(height / width) * MathUtils.degreesToRadians;
  
      // Finding new position if rotation was with respect to top-left corner of object.
      // X=x+radius*cos(theta-angle)+(h/2)cos(90+angle)
      // Y=y+radius*sin(theta-angle)-(h/2)sin(90+angle)
      correctedPosition = correctedPosition
              .add(
                      radius * MathUtils.cos(theta - angle),
                      radius * MathUtils.sin(theta - angle))
              .add(
                      ((height / 2) * MathUtils.cos(MathUtils.PI2 + angle)),
                      (-(height / 2) * MathUtils.sin(MathUtils.PI2 + angle)));
  
      bodyDef.position.set(correctedPosition);
  </code></pre>
  
  <p>Any hint would be highly welcomed.</p>