1
\$\begingroup\$

I made complex dynamic body that rotates around other body that is static. It works perfectly, however I got messed up with sprite position on that body. Here's what I have now enter image description here

and here's what I need to have

enter image description here

I will add my code

private Body createSide(float x, float y, Sprite tmpSprite, boolean sharp){
      BodyDef bDef = new BodyDef();
      bDef.type = BodyDef.BodyType.DynamicBody;

    PolygonShape shape = new PolygonShape();
    if(!sharp){
        shape.setAsBox(1.8f / PPM, 0.2f / PPM);
    } else {
        shape.setAsBox(0.2f / PPM, 1.8f / PPM);
    }

    FixtureDef fdef = new FixtureDef();
    fdef.shape = shape;

    bDef.position.set(x, y);
    Body body = world.createBody(bDef).createFixture(fdef).getBody();
    body.setAngularVelocity(-1f);
    tmpSprite.setPosition(bDef.position.x * 32, bDef.position.y * 32);
    return body;
}

private void createBody(int y, final int V_WIDTH){
        staticBody = createStaticBody(y, V_WIDTH);
        Body rightSide = createSide(5.5f, 15, sprites[0], true);
        Body leftSide = createSide(9.5f, 15, sprites[1], true);
        Body top = createSide(7.5f, 17, sprites[2], false);
        Body bottom = createSide(7.5f, 13, sprites[3], false);

        bodies[0] = rightSide;
        bodies[1] = leftSide;
        bodies[2] = top;
        bodies[3] = bottom;

...
...
...
    }

and here's my draw method

@Override
    public void draw(SpriteBatch sb) {
        for (int i=0; i<bodies.length; i++){
            sprites[i].setRotation(bodies[i].getAngle() * MathUtils.radiansToDegrees);
            sprites[i].draw(sb);
        }
    }
\$\endgroup\$
2
\$\begingroup\$

Ok, after 4 hours of searching finally I found the mistake. Problem was with the sprite origin. Here's the line that was necessary (staticBody is the body around other bodies will rotate)

tmpSprite.setOrigin(staticBody.getPosition().x / 32 + tmpSprite.getWidth() / 2,
                staticBody.getPosition().y / 32 + tmpSprite.getHeight() / 2);

And here's full code

private Body createSide(float x, float y, Sprite tmpSprite, boolean sharp){
        BodyDef bDef = new BodyDef();
        bDef.type = BodyDef.BodyType.DynamicBody;

        PolygonShape shape = new PolygonShape();
        if(!sharp){
            shape.setAsBox(1.8f / PPM, 0.2f / PPM);
        } else {
            shape.setAsBox(0.2f / PPM, 1.8f / PPM);
        }

        FixtureDef fdef = new FixtureDef();
        fdef.shape = shape;

        bDef.position.set(x, y);
        Body body = world.createBody(bDef).createFixture(fdef).getBody();
        body.setAngularVelocity(1f);
        //body.setFixedRotation(true);
        tmpSprite.setX(body.getPosition().x * 32 - tmpSprite.getWidth());
        tmpSprite.setY(body.getPosition().y * 32 - tmpSprite.getHeight());
        tmpSprite.setOrigin(staticBody.getPosition().x / 32 + tmpSprite.getWidth() / 2,
                staticBody.getPosition().y / 32 + tmpSprite.getHeight() / 2);
        return body;
    }

    @Override
    public void draw(SpriteBatch sb) {
        for (int i=0; i<sprites.length; i++){
            float xball = 32 * (bodies[i].getPosition().x) - sprites[i].getWidth()/2;
            float yball = 32 * (bodies[i].getPosition().y) - sprites[i].getHeight()/2;
            sprites[i].setRotation(bodies[i].getAngle() * MathUtils.radiansToDegrees);
            sprites[i].setPosition(xball, yball);
            sprites[i].draw(sb);
        }
    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.