Removed euler, only quaternion and dirty flag

This commit is contained in:
a.pilote
2022-07-14 17:45:06 -04:00
parent fb6080d734
commit 249e26dcd5
15 changed files with 203 additions and 66 deletions

View File

@@ -10,6 +10,7 @@ public:
void Draw(Nuake::Entity entity) override
{
using namespace Nuake;
Nuake::TransformComponent& component = entity.GetComponent<Nuake::TransformComponent>();
BeginComponentTable(TRANSFORM, Nuake::TransformComponent);
{
@@ -17,30 +18,63 @@ public:
ImGui::Text("Translation");
ImGui::TableNextColumn();
ImGuiHelper::DrawVec3("Translation", &component.Translation);
Vector3 position = component.GetLocalPosition();
ImGuiHelper::DrawVec3("Translation", &position);
if (position != component.GetLocalPosition())
component.SetLocalPosition(position);
ImGui::TableNextColumn();
ComponentTableReset(component.Translation, Nuake::Vector3(0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1, 1, 1, 0));
std::string resetLabel = std::string(ICON_FA_UNDO) + "##ResetTranslation";
if (ImGui::Button(resetLabel.c_str()))
component.SetLocalPosition(Vector3(0, 0, 0));
ImGui::PopStyleColor();
}
ImGui::TableNextColumn();
{
ImGui::Text("Rotation");
ImGui::TableNextColumn();
//ImGuiHelper::DrawVec3("Rotation", &component.Rotation);
Vector3 eulerAngles = glm::eulerAngles(component.GetLocalRotation());
ImGuiHelper::DrawVec3("Rotation", &eulerAngles);
Vector3 eulerLocal = glm::eulerAngles(component.GetLocalRotation());
if (eulerLocal != eulerAngles)
{
Quat rotation = QuatFromEuler(eulerAngles.x, eulerAngles.y, eulerAngles.z);
//component.SetLocalRotation(rotation);
}
ImGui::TableNextColumn();
//ComponentTableReset(component.Rotation, Nuake::Vector3(0, 0, 0));
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1, 1, 1, 0));
std::string resetLabel = std::string(ICON_FA_UNDO) + "##ResetRotation";
if (ImGui::Button(resetLabel.c_str()))
{
component.SetLocalRotation(Quat(1, 0, 0, 0));
}
ImGui::PopStyleColor();
}
ImGui::TableNextColumn();
{
ImGui::Text("Scale");
ImGui::TableNextColumn();
ImGuiHelper::DrawVec3("Scale", &component.Scale);
Vector3 localScale = component.GetLocalScale();
ImGuiHelper::DrawVec3("Scale", &localScale);
if (localScale != component.GetLocalScale())
component.SetLocalScale(localScale);
ImGui::TableNextColumn();
ComponentTableReset(component.Scale, Nuake::Vector3(1, 1, 1));
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1, 1, 1, 0));
std::string resetLabel = std::string(ICON_FA_UNDO) + "##ResetScale";
if (ImGui::Button(resetLabel.c_str()))
component.SetLocalScale(Vector3(1, 1, 1));
ImGui::PopStyleColor();
}
}
EndComponentTable();

View File

@@ -122,8 +122,8 @@ namespace Nuake {
if (ImGuizmo::IsUsing())
{
tc.LocalTransform = transform;
tc.GlobalTransform = transform;
tc.SetLocalTransform(transform);
tc.SetGlobalTransform(transform);
//Entity currentParent = Selection.Entity;
//if (parent.HasParent)
@@ -233,7 +233,7 @@ namespace Nuake {
if (ImGui::Selectable("Focus camera"))
{
Ref<EditorCamera> editorCam = Engine::GetCurrentScene()->m_EditorCamera;
editorCam->Translation = entity.GetComponent<TransformComponent>().Translation;
editorCam->Translation = entity.GetComponent<TransformComponent>().GetGlobalPosition();
Vector3 camDirection = entity.GetComponent<CameraComponent>().CameraInstance->GetDirection();
editorCam->SetDirection(camDirection);
}

13
Nuake/Maths.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "src/Core/Maths.h"
namespace Nuake
{
Quat QuatFromEuler(float x, float y, float z)
{
// Taken from: https://gamedev.stackexchange.com/questions/13436/glm-euler-angles-to-quaternion
Quat QuatAroundX = Quat(x, Vector3(1.0, 0.0, 0.0));
Quat QuatAroundY = Quat(y, Vector3(0.0, 1.0, 0.0));
Quat QuatAroundZ = Quat(z, Vector3(0.0, 0.0, 1.0));
return QuatAroundX * QuatAroundY * QuatAroundZ;
}
}

View File

@@ -16,4 +16,6 @@ namespace Nuake
using Color = glm::vec4;
using Matrix4 = glm::mat4;
using Matrix3 = glm::mat3;
Quat QuatFromEuler(float x, float y, float z);
}

View File

@@ -8,8 +8,8 @@ namespace Nuake
{
TransformComponent tc = TransformComponent();
tc.Translation = glm::vec3(from.x(), from.y(), from.z());
tc.Scale = glm::vec3(1.0f);
//tc.Translation = glm::vec3(from.x(), from.y(), from.z());
//tc.Scale = glm::vec3(1.0f);
//tc.Rotation = glm::vec3(0.0f);
glBegin(GL_LINES);

View File

@@ -156,7 +156,7 @@ namespace Nuake
int idx = m_Lights.size();
Vector3 direction = light.GetDirection();
Vector3 pos = transform.GlobalTranslation;
Vector3 pos = transform.GetGlobalPosition();
//light.m_Framebuffer->GetTexture(GL_DEPTH_ATTACHMENT)->Bind(17);

View File

@@ -36,7 +36,7 @@ namespace Nuake {
btVector3 pos = CharacterController->m_motionTransform.getOrigin();
glm::vec3 finalPos = glm::vec3(pos.x(), pos.y(), pos.z());
tc.Translation = finalPos;
tc.SetLocalPosition(finalPos);
}
};
}

View File

@@ -48,7 +48,7 @@ namespace Nuake {
glm::vec3 newPosition = m_Rigidbody->GetPosition();
glm::vec3 newRotation = m_Rigidbody->GetRotation();
tc->Translation = newPosition;
//tc->Translation = newPosition;
//tc->Rotation = newRotation;
}
@@ -59,7 +59,7 @@ namespace Nuake {
btTransform newTransform;
newTransform.setIdentity();
newTransform.setOrigin(btVector3(tc->Translation.x, tc->Translation.t, tc->Translation.z));
//newTransform.setOrigin(btVector3(tc->Translation.x, tc->Translation.t, tc->Translation.z));
btQuaternion quat;
//quat.setEulerZYX(tc->Rotation.x, tc->Rotation.y, tc->Rotation.z);

View File

@@ -18,13 +18,67 @@ namespace Nuake
GlobalTransform = Matrix4(1);
}
void TransformComponent::SetRotation(float x, float y, float z)
void TransformComponent::SetLocalRotation(const Quat& quat)
{
Quat QuatAroundX = Quat(1.0, 0.0, 0.0, x);
Quat QuatAroundY = Quat(0.0, 1.0, 0.0, y);
Quat QuatAroundZ = Quat(0.0, 0.0, 1.0, z);
Quat finalOrientation = QuatAroundX * QuatAroundY * QuatAroundZ;
Rotation = finalOrientation;
Rotation = quat;
Dirty = true;
}
Quat TransformComponent::GetLocalRotation() const
{
return Rotation;
}
Quat TransformComponent::GetGlobalRotation() const
{
return GlobalRotation;
}
void TransformComponent::SetGlobalRotation(const Quat& rotation)
{
GlobalRotation = rotation;
}
Vector3 TransformComponent::GetLocalPosition() const
{
return Translation;
}
void TransformComponent::SetLocalPosition(const Vector3& position)
{
Translation = position;
Dirty = true;
}
Vector3 TransformComponent::GetGlobalPosition() const
{
return GlobalTranslation;
}
void TransformComponent::SetGlobalPosition(const Vector3& position)
{
GlobalTranslation = position;
}
Vector3 TransformComponent::GetLocalScale() const
{
return Scale;
}
void TransformComponent::SetLocalScale(const Vector3& scale)
{
Scale = scale;
Dirty = true;
}
Vector3 TransformComponent::GetGlobalScale() const
{
return GlobalScale;
}
void TransformComponent::SetGlobalScale(const Vector3& scale)
{
GlobalScale = scale;
}
Matrix4 TransformComponent::GetGlobalTransform() const
@@ -32,8 +86,18 @@ namespace Nuake
return GlobalTransform;
}
void TransformComponent::SetGlobalTransform(const Matrix4& transform)
{
GlobalTransform = transform;
}
Matrix4 TransformComponent::GetLocalTransform() const
{
return LocalTransform;
}
void TransformComponent::SetLocalTransform(const Matrix4& transform)
{
LocalTransform = transform;
}
}

View File

@@ -5,9 +5,7 @@
namespace Nuake
{
class TransformComponent {
public:
bool Dirty = true;
private:
Vector3 Translation;
Quat Rotation;
Vector3 Scale;
@@ -18,20 +16,40 @@ namespace Nuake
Matrix4 LocalTransform;
Matrix4 GlobalTransform;
public:
bool Dirty = true;
TransformComponent();
Matrix4 GetGlobalTransform() const;
Matrix4 GetLocalTransform() const;
void SetGlobalTransform(const Matrix4& transform);
void SetRotation(float x, float y, float z);
Matrix4 GetLocalTransform() const;
void SetLocalTransform(const Matrix4& transform);
void SetLocalRotation(const Quat& quat);
Quat GetLocalRotation() const;
void SetGlobalRotation(const Quat& quat);
Quat GetGlobalRotation() const;
Vector3 GetLocalPosition() const;
void SetLocalPosition(const Vector3& position);
Vector3 GetGlobalPosition() const;
void SetGlobalPosition(const Vector3& position);
void SetLocalScale(const Vector3& scale);
Vector3 GetLocalScale() const;
void SetGlobalScale(const Vector3& scale);
Vector3 GetGlobalScale() const;
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL_LBL("Type", "TransformComponent");
SERIALIZE_VEC3(Translation);
//SERIALIZE_VEC3(Rotation);
SERIALIZE_VEC4(Rotation);
SERIALIZE_VEC3(Scale);
END_SERIALIZE();
}
@@ -40,8 +58,12 @@ namespace Nuake
{
BEGIN_DESERIALIZE();
this->Translation = Vector3(j["Translation"]["x"], j["Translation"]["y"], j["Translation"]["z"]);
//this->Rotation = Vector3(j["Rotation"]["x"], j["Rotation"]["y"], j["Rotation"]["z"]);
if(j.contains("Rotation"))
this->Rotation = Quat(j["Rotation"]["w"], j["Rotation"]["x"], j["Rotation"]["y"], j["Rotation"]["z"]);
this->Scale = Vector3(j["Scale"]["x"], j["Scale"]["y"], j["Scale"]["z"]);
LocalTransform = Matrix4(1);
GlobalTransform = Matrix4(1);
return true;
}
};

View File

@@ -135,7 +135,7 @@ namespace Nuake {
{
auto [transform, camera, parent] = view.get<TransformComponent, CameraComponent, ParentComponent>(e);
cam = camera.CameraInstance;
cam->Translation = transform.GlobalTranslation;
cam->Translation = transform.GetGlobalPosition();
break;
}
}

View File

@@ -33,7 +33,7 @@ namespace Nuake
BoxColliderComponent& boxComponent = ent.GetComponent<BoxColliderComponent>();
Ref<Physics::Box> boxShape = CreateRef<Physics::Box>(boxComponent.Size);
Ref<Physics::RigidBody> btRigidbody = CreateRef<Physics::RigidBody>(mass, transform.Translation, boxShape);
Ref<Physics::RigidBody> btRigidbody = CreateRef<Physics::RigidBody>(mass, transform.GetGlobalPosition(), boxShape);
rigidbody.m_Rigidbody = btRigidbody;
@@ -49,7 +49,7 @@ namespace Nuake
{
auto [transform, cc] = ccview.get<TransformComponent, CharacterControllerComponent>(e);
cc.CharacterController = CreateRef<Physics::CharacterController>(cc.Height, cc.Radius, cc.Mass, transform.Translation);
cc.CharacterController = CreateRef<Physics::CharacterController>(cc.Height, cc.Radius, cc.Mass, transform.GetLocalPosition());
Entity ent = Entity({ e, m_Scene });
cc.CharacterController->SetEntity(ent);
@@ -67,7 +67,7 @@ namespace Nuake
for (auto m : brush.Meshes)
{
Ref<Physics::MeshShape> meshShape = CreateRef<Physics::MeshShape>(m);
Ref<Physics::RigidBody> btRigidbody = CreateRef<Physics::RigidBody>(0.0f, transform.GlobalTranslation, meshShape);
Ref<Physics::RigidBody> btRigidbody = CreateRef<Physics::RigidBody>(0.0f, transform.GetGlobalPosition(), meshShape);
btRigidbody->SetEntityID(Entity{ e, m_Scene });
brush.Rigidbody.push_back(btRigidbody);
PhysicsManager::Get()->RegisterBody(btRigidbody);
@@ -80,7 +80,7 @@ namespace Nuake
auto [transform, brush, trigger] = bspTriggerView.get<TransformComponent, BSPBrushComponent, TriggerZone>(e);
Ref<Physics::MeshShape> meshShape = CreateRef<Physics::MeshShape>(brush.Meshes[0]);
Ref<GhostObject> ghostBody = CreateRef<GhostObject>(transform.GlobalTranslation, meshShape);
Ref<GhostObject> ghostBody = CreateRef<GhostObject>(transform.GetGlobalPosition(), meshShape);
trigger.GhostObject = ghostBody;
PhysicsManager::Get()->RegisterGhostBody(ghostBody);
@@ -100,8 +100,8 @@ namespace Nuake
for (auto& r : brush.Rigidbody)
{
r->m_Transform->setOrigin(btVector3(transform.GlobalTranslation.x, transform.GlobalTranslation.y, transform.GlobalTranslation.z));
r->UpdateTransform(*r->m_Transform);
//r->m_Transform->setOrigin(btVector3(transform.GlobalTranslation.x, transform.GlobalTranslation.y, transform.GlobalTranslation.z));
//r->UpdateTransform(*r->m_Transform);
}
if (!brush.IsFunc)

View File

@@ -51,9 +51,9 @@ namespace Nuake {
parent.AddChild(brushEntity);
transformComponent.Translation = Vector3(brush->center.y * (1.0f / 64),
transformComponent.SetGlobalPosition(Vector3(brush->center.y * (1.0f / 64),
brush->center.z * ScaleFactor * (1.0f / 64),
brush->center.x * ScaleFactor * (1.0f / 64));
brush->center.x * ScaleFactor * (1.0f / 64)));
BSPBrushComponent& bsp = brushEntity.AddComponent<BSPBrushComponent>();
bsp.IsSolid = false;
@@ -113,10 +113,10 @@ namespace Nuake {
parent.AddChild(brushEntity);
transformComponent.Translation = Vector3(
transformComponent.SetGlobalPosition(Vector3(
brush->center.y * ScaleFactor * (1.0f / 64),
brush->center.z * ScaleFactor * (1.0f / 64),
brush->center.x * ScaleFactor * (1.0f / 64));
brush->center.x * ScaleFactor * (1.0f / 64)));
int index_offset = 0;
int lastTextureID = -1;
@@ -247,10 +247,10 @@ namespace Nuake {
parent.AddChild(brushEntity);
transformComponent.Translation = Vector3(
transformComponent.SetLocalPosition(Vector3(
brush->center.y * ScaleFactor * (1.0f / 64),
brush->center.z * ScaleFactor * (1.0f / 64),
brush->center.x * ScaleFactor * (1.0f / 64));
brush->center.x * ScaleFactor * (1.0f / 64)));
int index_offset = 0;
int lastTextureID = -1;
@@ -422,7 +422,7 @@ namespace Nuake {
float z = String::ToFloat(splits[0]);
Vector3 position = Vector3(x, y, z) * ScaleFactor * (1.0f / 64.0f);
newEntity.GetComponent<TransformComponent>().Translation = position;
newEntity.GetComponent<TransformComponent>().SetLocalPosition(position);
}
if (key == "classname")

View File

@@ -52,11 +52,13 @@ namespace Nuake {
if (transform.Dirty)
{
Matrix4 localTransform = Matrix4(1);
localTransform = glm::translate(transform.LocalTransform, transform.Translation);
localTransform = localTransform * glm::toMat4(transform.Rotation);
localTransform = glm::scale(transform.LocalTransform, transform.Scale);
localTransform = glm::translate(localTransform, transform.GetLocalPosition());
localTransform = localTransform * glm::toMat4(transform.GetLocalRotation());
localTransform = glm::scale(localTransform, transform.GetLocalScale());
transform.LocalTransform = localTransform;
transform.SetLocalTransform(localTransform);
transform.Dirty = false;
}
}
@@ -69,36 +71,35 @@ namespace Nuake {
if (!parent.HasParent)
{
// If no parents, then globalTransform is local transform.
transform.GlobalTransform = transform.LocalTransform;
transform.SetGlobalTransform(transform.GetLocalTransform());
continue;
}
Entity currentParent = Entity((entt::entity)e, m_Scene);
Matrix4 globalTransform = transform.LocalTransform;
Vector3 globalPosition = transform.Translation;
Quat globalOrientation = transform.Rotation;
Vector3 globalScale = transform.Scale;
Matrix4 globalTransform = transform.GetLocalTransform();
Vector3 globalPosition = transform.GetLocalPosition();
Quat globalOrientation = transform.GetLocalRotation();
Vector3 globalScale = transform.GetLocalScale();
ParentComponent parentComponent = currentParent.GetComponent<ParentComponent>();
while (parentComponent.HasParent)
{
TransformComponent& transformComponent = parentComponent.Parent.GetComponent<TransformComponent>();
globalTransform = transformComponent.LocalTransform * globalTransform;
globalTransform = transformComponent.GetLocalTransform() * globalTransform;
globalPosition += transformComponent.Translation;
globalOrientation *= transformComponent.Rotation;
globalScale *= transformComponent.Scale;
globalPosition += transformComponent.GetLocalPosition();
globalOrientation *= transformComponent.GetLocalRotation();
globalScale *= transformComponent.GetLocalScale();
parentComponent = parentComponent.Parent.GetComponent<ParentComponent>();
}
transform.GlobalTranslation = globalPosition;
transform.GlobalRotation = globalOrientation;
transform.GlobalScale = globalScale;
transform.GlobalTransform = globalTransform;
transform.SetGlobalPosition(globalPosition);
transform.SetGlobalRotation(globalOrientation);
transform.SetGlobalScale(globalScale);
transform.SetGlobalTransform(globalTransform);
}
}
}

View File

@@ -217,9 +217,10 @@ namespace Nuake {
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& transform = ent.GetComponent<TransformComponent>();
// set the slots
wrenSetSlotDouble(vm, 1, transform.Translation.x);
wrenSetSlotDouble(vm, 2, transform.Translation.y);
wrenSetSlotDouble(vm, 3, transform.Translation.z);
Vector3 position = transform.GetLocalPosition();
wrenSetSlotDouble(vm, 1, position.x);
wrenSetSlotDouble(vm, 2, position.y);
wrenSetSlotDouble(vm, 3, position.z);
// Fill the list
wrenSetSlotNewList(vm, 0);
@@ -255,7 +256,7 @@ namespace Nuake {
float y = wrenGetSlotDouble(vm, 3);
float z = wrenGetSlotDouble(vm, 4);
transform.SetRotation(x, y, z);
transform.SetLocalRotation(QuatFromEuler(x, y, z));
}
static void SetTranslation(WrenVM* vm)
@@ -268,7 +269,7 @@ namespace Nuake {
float y = wrenGetSlotDouble(vm, 3);
float z = wrenGetSlotDouble(vm, 4);
transform.Translation = Vector3(x, y, z);
transform.SetGlobalPosition(Vector3(x, y, z));
}
static void TriggerGetOverlappingBodyCount(WrenVM* vm)