From dbbbb7b4e7518a4e56955986e33a3a78c65b5abb Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Wed, 16 Dec 2015 17:20:56 +0100 Subject: [PATCH] Working rotations and translations relative to global and local space for children --- deps | 2 +- resources/Schema/Components/Transform.xml | 2 +- resources/Schema/Entities/EditorTestWorld.xml | 23 +++++++++++- src/Engine/Core/EntityXMLFile.cpp | 4 +- src/Engine/Editor/EditorSystem.cpp | 37 +++++++++++++------ 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/deps b/deps index f20b9cc1..e6399158 160000 --- a/deps +++ b/deps @@ -1 +1 @@ -Subproject commit f20b9cc13bffa39c3b5144bacc5eacd34d43052c +Subproject commit e63991581a268f3eb85f86782d0bdf3687695795 diff --git a/resources/Schema/Components/Transform.xml b/resources/Schema/Components/Transform.xml index 276b89e7..00202aa4 100644 --- a/resources/Schema/Components/Transform.xml +++ b/resources/Schema/Components/Transform.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/resources/Schema/Entities/EditorTestWorld.xml b/resources/Schema/Entities/EditorTestWorld.xml index 6e1c0be5..7fa8e96a 100755 --- a/resources/Schema/Entities/EditorTestWorld.xml +++ b/resources/Schema/Entities/EditorTestWorld.xml @@ -2,7 +2,10 @@ - + + + + @@ -19,12 +22,28 @@ - + + An error + + + + + + + + + An error + + + + + + \ No newline at end of file diff --git a/src/Engine/Core/EntityXMLFile.cpp b/src/Engine/Core/EntityXMLFile.cpp index bf8daceb..c6e00a1b 100644 --- a/src/Engine/Core/EntityXMLFile.cpp +++ b/src/Engine/Core/EntityXMLFile.cpp @@ -431,12 +431,12 @@ float EntityXMLFile::getFloatAttribute(const xercesc::DOMElement* element, const { using namespace xercesc; XSValue::Status status; - XSValue* val = XSValue::getActualValue(element->getAttribute(XSTR(attribute)), xercesc::XSValue::DataType::dt_float, status); + XSValue* val = XSValue::getActualValue(element->getAttribute(XSTR(attribute)), xercesc::XSValue::DataType::dt_double, status); if (val == nullptr) { LOG_ERROR("Element \"%s\" doesn't have an \"%s\" attribute!", XSTR(element->getTagName()), attribute); return 0.f; } else { - return val->fData.fValue.f_float; + return static_cast(val->fData.fValue.f_double); } } diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index e224ca6b..d27a463b 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -111,26 +111,37 @@ bool EditorSystem::OnMouseMove(const Events::MouseMove& e) auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); if (m_WidgetMode == WidgetMode::Translate) { if (m_WidgetSpace == WidgetSpace::Global) { - (glm::vec3&)m_World->GetComponent(m_Selection, "Transform")["Position"] += movement; + EntityID parent = m_World->GetParent(m_Selection); + glm::quat inverseParentOrientation; + if (parent != 0) { + inverseParentOrientation = glm::inverse(RenderQueueFactory::AbsoluteOrientation(m_World, parent)); + } + (glm::vec3&)m_World->GetComponent(m_Selection, "Transform")["Position"] += inverseParentOrientation * movement; } else if (m_WidgetSpace == WidgetSpace::Local) { auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); - (glm::vec3&)selectionTransform["Position"] += glm::quat((glm::vec3&)selectionTransform["Orientation"]) * movement; + (glm::vec3&)selectionTransform["Position"] += glm::quat((glm::vec3)selectionTransform["Orientation"]) * movement; } } else if (m_WidgetMode == WidgetMode::Rotate) { glm::vec3 finalMovement; finalMovement.x = -deltaWorld.y * m_WidgetCurrentAxis.x; finalMovement.y = deltaWorld.x * m_WidgetCurrentAxis.y; finalMovement.z = deltaWorld.y * m_WidgetCurrentAxis.z; - glm::vec3& selectionOrientation = m_World->GetComponent(m_Selection, "Transform")["Orientation"]; if (m_WidgetSpace == WidgetSpace::Global) { - glm::quat currentOrientation = glm::quat(selectionOrientation); + EntityID parent = m_World->GetParent(m_Selection); + glm::quat parentOrientation; + if (parent != 0) { + parentOrientation = RenderQueueFactory::AbsoluteOrientation(m_World, parent); + } + glm::vec3& selectionOrientation = m_World->GetComponent(m_Selection, "Transform")["Orientation"]; + //glm::quat currentOrientation = RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection); + glm::quat currentOrientation = parentOrientation * glm::quat(selectionOrientation); glm::quat deltaOrientation(finalMovement); - selectionOrientation = glm::eulerAngles(deltaOrientation * currentOrientation); // (currentOrientation * deltaOrientation) * glm::vec3(0, 0, -1); + selectionOrientation = glm::eulerAngles(glm::inverse(parentOrientation) * (deltaOrientation * currentOrientation)); } else if (m_WidgetSpace == WidgetSpace::Local) { + glm::vec3& selectionOrientation = m_World->GetComponent(m_Selection, "Transform")["Orientation"]; glm::quat currentOrientation(selectionOrientation); glm::quat deltaOrientation(finalMovement); - selectionOrientation = glm::eulerAngles(currentOrientation * deltaOrientation); // (currentOrientation * deltaOrientation) * glm::vec3(0, 0, -1); - widgetTransform["Orientation"] = selectionOrientation; + selectionOrientation = glm::eulerAngles(currentOrientation * deltaOrientation); } } else if (m_WidgetMode == WidgetMode::Scale) { glm::vec3& scaleX = m_World->GetComponent(m_WidgetX, "Transform")["Scale"]; @@ -189,6 +200,7 @@ bool EditorSystem::OnPicking(const Events::Picking& e) //widgetTransform["Position"] = (glm::vec3)selectionTransform["Position"]; } else { m_Selection = entity; + setWidgetMode(m_WidgetMode); } } else { m_Selection = 0; @@ -222,8 +234,11 @@ void EditorSystem::updateWidget() if (m_Selection != 0) { auto widgetTransform = m_World->GetComponent(m_Widget, "Transform"); - auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); - widgetTransform["Position"] = (glm::vec3)selectionTransform["Position"]; + glm::vec3 selectionPosition = RenderQueueFactory::AbsolutePosition(m_World, m_Selection); + widgetTransform["Position"] = selectionPosition; + if (m_WidgetSpace == WidgetSpace::Local) { + widgetTransform["Orientation"] = glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection)); + } } } @@ -248,7 +263,7 @@ void EditorSystem::setWidgetMode(WidgetMode newMode) if (m_Selection != 0) { if (m_WidgetSpace == WidgetSpace::Local) { auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); - widgetTransform["Orientation"] = (glm::vec3)selectionTransform["Orientation"]; + widgetTransform["Orientation"] = glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection)); } } } else if (newMode == WidgetMode::Scale) { @@ -269,7 +284,7 @@ void EditorSystem::setWidgetMode(WidgetMode newMode) if (m_Selection != 0) { auto selectionTransform = m_World->GetComponent(m_Selection, "Transform"); if (m_WidgetSpace == WidgetSpace::Local) { - widgetTransform["Orientation"] = (glm::vec3)selectionTransform["Orientation"]; + widgetTransform["Orientation"] = glm::eulerAngles(RenderQueueFactory::AbsoluteOrientation(m_World, m_Selection)); } } }