erincatto / box2d

Box2D is a 2D physics engine for games

Home Page:https://box2d.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bodies that have fixed rotation set can still rotate

simonvanbernem opened this issue · comments

While a body with the e_fixedRotationFlag flag will not gain angular velocity by impulse or force, the angular velocity can still be set directly. This can happen during creation of the body, via b2bodyDef::angularVelocity or by directly calling b2Body::SetAngularVelocity. It can not happen by calling b2Body::SetFixedRotation on a body with angular velocity, because the angular velocity is set to 0 in this method.

Please consider either forcing the angular velocity to 0 in b2Body::SetAngularVelocity and b2World::CreateBody if the body has e_fixedRotationFlag set, or having asserts there.


void b2Body::SetAngularVelocity(float32 omega) {
    // Check if the body has the fixedRotation flag.
    if (IsFixedRotation() && omega != 0.0f) {
        // Optionally issue an assert or warning here.
        assert(false && "Setting non-zero angular velocity for a fixed-rotation body.");
        // You can choose to set the angular velocity to 0 here.
        omega = 0.0f;
    }
    // Set the angular velocity.
    m_angularVelocity = omega;
}