title: Align a rotated Sprite with a DinamicBody libgdx
body: <p>I've a sprite (rectangular) that rotates around its central point. I used:</p>
  
  <pre><code>setOriginCenter();
  setRotation(angleDegrees);
  </code></pre>
  
  <p>That works fine.</p>
  
  <p>I defined a DynamicBody (box2d) using a CircleShape.</p>
  
  <p>In the update method of the sprite, I want to center the sprite (rotated sprite) with the center of the DynamicBody.
  I tried several method but I can't get a solution:</p>
  
  <ol>
  <li><p>This doesn't work</p>
  
  <pre><code>Rectangle rect = myRotatedSprite.getBoundingRectangle();
  myRotatedSprite.setPosition(b2body.getPosition().x - rect.getWidth() / 2, b2body.getPosition().y - rect.getHeight() / 2);
  </code></pre></li>
  <li><p>This doesn't work</p>
  
  <pre><code>myRotatedSprite.setPosition(b2body.getPosition().x - myRotatedSprite.getWidth() / 2, b2body.getPosition().y - myRotatedSprite.getHeight() / 2);
  </code></pre></li>
  <li><p>This doesn't work</p>
  
  <pre><code>float rot = myRotatedSprite.getRotation();
  myRotatedSprite.setRotation(0);
  Rectangle rect = getBoundingRectangle();
  myRotatedSprite.setPosition(b2body.getPosition().x - rect.width / 2, 
  b2body.getPosition().y - rect.height / 2);
  myRotatedSprite.setRotation(rot);
  myRotatedSprite.getBoundingRectangle();
  </code></pre></li>
  </ol>
  
  <p>What I am doing wrong ? It seems to me that <code>getBoundingRectangle()</code> is not correct when the sprite is rotated...</p>
  
  <p>Here is my full code:</p>
  
  <pre><code>public class HeroBullet extends Weapon {
  private static final String TAG = HeroBullet.class.getName();
  
  private float stateTimer;
  private Animation heroBulletAnimation;
  private Vector2 tmp; // Temp GC friendly vector
  
  public HeroBullet(PlayScreen screen, float x, float y, float width, float height, float circleShapeRadius, float angle, Animation animation) {
      super(screen, x, y, circleShapeRadius &gt; 0 ? circleShapeRadius : Constants.HEROBULLET_CIRCLESHAPE_RADIUS_METERS);
  
      setOriginCenter();
  
      width = width &gt; 0 ? width : Constants.HEROBULLET_WIDTH_METERS;
      height = height &gt; 0 ? height : Constants.HEROBULLET_HEIGHT_METERS;
  
      setBounds(getX(), getY(), width, height);
  
      velocity = new Vector2(Constants.HEROBULLET_VELOCITY_X, Constants.HEROBULLET_VELOCITY_Y);
      if (angle &gt; 0) {
          velocity.rotate(angle);
          setRotation(angle);
      }
      if (animation != null) {
          heroBulletAnimation = animation;
      } else {
          heroBulletAnimation = Assets.instance.heroBullet.heroBulletAnimation;
      }
  
      stateTimer = 0;
      currentState = State.SHOT;
  
      AudioManager.instance.play(Assets.instance.sounds.heroShoot, 0.2f, MathUtils.random(1.0f, 1.1f));
  
      tmp = new Vector2();
  }
  
  @Override
  protected void defineWeapon() {
      BodyDef bdef = new BodyDef();
      bdef.position.set(getX(), getY()); // In b2box the origin is at the center of the body
      bdef.type = BodyDef.BodyType.DynamicBody;
      b2body = world.createBody(bdef);
  
      FixtureDef fdef = new FixtureDef();
          CircleShape shape = new CircleShape();
      shape.setRadius(circleShapeRadius);
      fdef.filter.categoryBits = Constants.HERO_WEAPON_BIT; // Depicts what this fixture is
      fdef.filter.maskBits = Constants.BORDERS_BIT |
              Constants.OBSTACLE_BIT |
              Constants.POWERBOX_BIT |
              Constants.FINAL_ENEMY_LEVEL_ONE_BIT |
              Constants.ENEMY_BIT; // Depicts what this Fixture can collide with
  
      fdef.shape = shape;
      b2body.createFixture(fdef).setUserData(this);
  }
  
  @Override
  public void update(float dt) {
      switch (currentState) {
          case SHOT:
              stateShot(dt);
              break;
          case ONTARGET:
              stateOnTarget();
              break;
          case FINISHED:
              break;
          default:
              break;
      }
      super.checkBoundaries();
  }
  
  private void stateShot(float dt) {
      b2body.setLinearVelocity(velocity);
  
      /* ***********
      Here I want to center the sprite (rotated sprite) with the center of the DynamicBody, but I don't know how
      ******** */
  
      setRegion((TextureRegion) heroBulletAnimation.getKeyFrame(stateTimer, true));
      stateTimer += dt;
  }
  
  private void stateOnTarget() {
      world.destroyBody(b2body);
      currentState = State.FINISHED;
  }
  
  @Override
  public void renderDebug(ShapeRenderer shapeRenderer) {
      shapeRenderer.rect(getBoundingRectangle().x, getBoundingRectangle().y, getBoundingRectangle().width, getBoundingRectangle().height);
  }
  
  @Override
  public void onTarget() {
      currentState = State.ONTARGET;
  }
  
  public void draw(Batch batch) {
      if (currentState == State.SHOT) {
          super.draw(batch);
      }
  }
  </code></pre>
  
  <p>}</p>