简体   繁体   中英

Box2D c++ rotate around point

However, I also want to have the sprite's rotations reflect the rotations of the b2bodies. But, after I rotate the sprites, the offset I use to center them correctly with the b2body positions is no longer accurate.

void CRoom::RotateGun(const SDL_Event &event)
{
    glm::vec2 mousePosition = sdl::GetMousePosition(event.motion);
    glm::vec2 delta = mousePosition - GUN_POSITION;
    float angle = atan2f(delta.y, delta.x);
    m_gun->GetBody()->SetTransform(m_gun->GetPosition(), angle);
}

image1

Use the matrix multiply.

Using the normal conventions, the transform is

 move(-width/2, -height/2) * rotate(angle) * move(width/2 height/2) *move(x, y)

Sometimes you will have to reverse the order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM