Ошибка преобразования мира в локальную позицию - PullRequest
0 голосов
/ 31 марта 2020

Для пользовательского движка у меня проблемы с окружающим миром из-за локальной позиции преобразования детей. У меня нет никаких проблем, когда есть какая-либо шкала или какая-либо позиция на родителях, но как только происходит вращение на родителе (если только значение топора, кажется, работает нормально), позиция быстро увеличивается (x, y, z ).

Вот как я преобразовываю локальное преобразование в мир:

void TransformsManager::SetComponentFromWorldTransform(
    const EntityIndex entityIndex,
    const math::Transform& worldTransform)
{
    //If the entity has no parent, the worldTransform == localTransform
    if (parents_[entityIndex] == kNoParent) {
        SetComponent(entityIndex, worldTransform);
    } else {
        const auto p = worldTransform.GetLocalPosition();
        const auto localPos = (GetWorldToLocalMatrix(parents_[entityIndex]) * math::Vec4(p.x, p.y, p.z, 1)).To3();

        //Works fine
        const auto localScale = GetLocalScaleFromWorldScale(
            entityIndex,
            worldTransform.GetLocalScale());

        //Works fine
        const auto localRotation = GetLocalRotationFromWorldRotation(
            entityIndex,
            worldTransform.GetLocalRotation());

        SetComponent(entityIndex, math::Transform(localPos, localRotation, localScale));
    }
}

Функция ton возвращает мой мир в локальную матрицу

math::Matrix4 TransformsManager::GetWorldToLocalMatrix(const EntityIndex entityIndex)
{
    if ((dirtyFlags_[entityIndex] & math::TransformDirtyFlagStatus::IS_WORLD_DIRTY) == math::TransformDirtyFlagStatus::IS_WORLD_DIRTY) {
        worldToLocalMatrices_[entityIndex] = math::Matrix4::GetInverse(GetLocalToWorldMatrix(entityIndex));

        dirtyFlags_[entityIndex] &= ~math::TransformDirtyFlagStatus::
            IS_WORLD_DIRTY;
    }
    return worldToLocalMatrices_[entityIndex];
}

Функция Получить матрицу мира

math::Matrix4 TransformsManager::GetLocalToWorldMatrix(const EntityIndex entityIndex)
{
    if ((dirtyFlags_[entityIndex] & math::TransformDirtyFlagStatus::IS_LOCAL_DIRTY) ==
        math::TransformDirtyFlagStatus::IS_LOCAL_DIRTY) {
        if (parents_[entityIndex] == kNoParent) {
            localToWorldMatrices_[entityIndex] = CalculateLocalToParentMatrix(entityIndex);
        } else {
            localToWorldMatrices_[entityIndex] =
                GetLocalToWorldMatrix(parents_[entityIndex]) * CalculateLocalToParentMatrix(entityIndex);
        }
        dirtyFlags_[entityIndex] &= ~math::TransformDirtyFlagStatus::IS_LOCAL_DIRTY;
    }
    return localToWorldMatrices_[entityIndex];
}

И функцию для вычисления окончательной матрицы

math::Matrix4 TransformsManager::CalculateLocalToParentMatrix(
    const EntityIndex entityIndex) const
{
    const auto transform = transforms_[entityIndex];

    auto parentMatrix = math::Matrix4::Identity();
    parentMatrix = math::Matrix4::Translate(
        parentMatrix,
        transform.GetLocalPosition());

    const auto localRotation = transform.GetLocalRotation();

    parentMatrix = math::Matrix4::Rotate(
        parentMatrix,
        localRotation.x,
        math::Vec3(1, 0, 0));

    parentMatrix = math::Matrix4::Rotate(
        parentMatrix,
        localRotation.y,
        math::Vec3(0, 1, 0));

    parentMatrix = math::Matrix4::Rotate(
        parentMatrix,
        localRotation.z,
        math::Vec3(0, 0, 1));

    parentMatrix = math::Matrix4::Scale(
        parentMatrix,
        transform.GetLocalScale());

    return parentMatrix;
}

1 Ответ

0 голосов
/ 31 марта 2020

Исправлено, у меня была ошибка с GetInverse ()

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...