Repository: FlaxEngine/FlaxEngine
File: Source/Engine/Physics/Actors/RigidBody.cpp (C++)

Code before:
void RigidBody::ClosestPoint(const Vector3& position, Vector3& result) const
{
    Vector3 tmp;
    Real minDistanceSqr = MAX_Real;
    result = Vector3::Maximum;
    for (int32 i = 0; i < Children.Count(); i++)
    {
        const auto collider = dynamic_cast<Collider*>(Children[i]);
        if (collider && collider->GetAttachedRigidBody() == this)
        {
            collider->ClosestPoint(position, tmp);
            const Real dstSqr = Vector3::DistanceSquared(position, tmp);
            if (dstSqr < minDistanceSqr)
            {
                minDistanceSqr = dstSqr;
                result = tmp;
            }
        }
    }
}

void RigidBody::AddMovement(const Vector3& translation, const Quaternion& rotation)
{
    // filter rotation according to constraints
    Quaternion allowedRotation;
    if (static_cast<unsigned int>(GetConstraints()) & static_cast<unsigned int>(RigidbodyConstraints::LockRotation))
        allowedRotation = Quaternion::Identity;
    else
    {
        Float3 euler = rotation.GetEuler();
        const auto constraints = static_cast<unsigned int>(GetConstraints());
        if (constraints & static_cast<unsigned int>(RigidbodyConstraints::LockRotationX))
            euler.X = 0;
        if (constraints & static_cast<unsigned int>(RigidbodyConstraints::LockRotationY))
            euler.Y = 0;
        if (constraints & static_cast<unsigned int>(RigidbodyConstraints::LockRotationZ))
            euler.Z = 0;
        allowedRotation = Quaternion::Euler(euler);
    }

    // filter translation according to the constraints
    auto allowedTranslation = translation;
    if (static_cast<unsigned int>(GetConstraints()) & static_cast<unsigned int>(RigidbodyConstraints::LockPosition))
        allowedTranslation = Vector3::Zero;
    else
    {
        const auto constraints = static_cast<unsigned int>(GetConstraints());
        if (constraints & static_cast<unsigned int>(RigidbodyConstraints::LockPositionX))
            allowedTranslation.X = 0;
        if (constraints & static_cast<unsigned int>(RigidbodyConstraints::LockPositionY))
            allowedTranslation.Y = 0;

Review comment (suggestion):
Use `EnumHasAllFlags(GetConstraints(). RigidbodyConstraints::LockRotation)` for readability (the same for translation).

Code after:
void RigidBody::ClosestPoint(const Vector3& position, Vector3& result) const
{
    Vector3 tmp;
    Real minDistanceSqr = MAX_Real;
    result = Vector3::Maximum;
    for (int32 i = 0; i < Children.Count(); i++)
    {
        const auto collider = dynamic_cast<Collider*>(Children[i]);
        if (collider && collider->GetAttachedRigidBody() == this)
        {
            collider->ClosestPoint(position, tmp);
            const Real dstSqr = Vector3::DistanceSquared(position, tmp);
            if (dstSqr < minDistanceSqr)
            {
                minDistanceSqr = dstSqr;
                result = tmp;
            }
        }
    }
}

void RigidBody::AddMovement(const Vector3& translation, const Quaternion& rotation)
{
    // filter rotation according to constraints
    Quaternion allowedRotation;
    if (EnumHasAllFlags(GetConstraints(), RigidbodyConstraints::LockRotation))
        allowedRotation = Quaternion::Identity;
    else
    {
        Float3 euler = rotation.GetEuler();
        if (EnumHasAnyFlags(GetConstraints(), RigidbodyConstraints::LockRotationX))
            euler.X = 0;
        if (EnumHasAnyFlags(GetConstraints(), RigidbodyConstraints::LockRotationY))
            euler.Y = 0;
        if (EnumHasAnyFlags(GetConstraints(), RigidbodyConstraints::LockRotationZ))
            euler.Z = 0;
        allowedRotation = Quaternion::Euler(euler);
    }

    // filter translation according to the constraints
    auto allowedTranslation = translation;
    if (EnumHasAllFlags(GetConstraints(), RigidbodyConstraints::LockPosition))
        allowedTranslation = Vector3::Zero;
    else
    {
        if (EnumHasAnyFlags(GetConstraints(), RigidbodyConstraints::LockPositionX))
            allowedTranslation.X = 0;
        if (EnumHasAnyFlags(GetConstraints(), RigidbodyConstraints::LockPositionY))
            allowedTranslation.Y = 0;
        if (EnumHasAnyFlags(GetConstraints(), RigidbodyConstraints::LockPositionZ))
            allowedTranslation.Z = 0;