Rotating multiple sprites as one ( around same origin )

前端 未结 2 1970
醉酒成梦
醉酒成梦 2021-01-21 14:59

I have array of sprites forming T shape, and I want to ratate them around the same origin, in my case box2D body origin, like this:

2条回答
  •  無奈伤痛
    2021-01-21 15:00

    As you can see in your 'right' example in the diagram, the position of the sprites depends on the angle of the body, which you are not accounting for.

    b2Vec2 pos = ...; // center of the sprite relative to body (local coords)
    float ang = ...;  // angle of the sprite relative to body (probably zero)
    
    //need to rotate image local center by body angle
    b2Rot rot( body->GetAngle() );
    pos = b2Mul(rot, pos) + body->GetPosition();
    ang += -body->GetAngle();
    
    sprite.setRotation( ang * RADTODEG ); // RADTODEG = 57.295779513082320876f
    sprite.setPosition( PTM * pos.x,  PTM * -pos.y);
    

    Prior to that, I have also done:

    sf::FloatRect rect = sprite.getLocalBounds();
    sprite.setOrigin( 0.5 * rect.width, 0.5 * rect.height );
    

提交回复
热议问题