diff --git a/Editor/src/ComponentsPanel/RigidbodyPanel.h b/Editor/src/ComponentsPanel/RigidbodyPanel.h new file mode 100644 index 00000000..5b4b07b8 --- /dev/null +++ b/Editor/src/ComponentsPanel/RigidbodyPanel.h @@ -0,0 +1,30 @@ +#pragma once +#include "ComponentPanel.h" +#include +#include + +class RigidbodyPanel : ComponentPanel { + +public: + RigidbodyPanel() {} + + void Draw(Nuake::Entity entity) override + { + if (!entity.HasComponent()) + return; + + auto& component = entity.GetComponent(); + BeginComponentTable(RIGIDBODY, Nuake::RigidBodyComponent); + { + { + ImGui::Text("Mass"); + ImGui::TableNextColumn(); + + ImGui::DragFloat("##Mass", &component.Mass, 0.1f, 0.1f); + ImGui::TableNextColumn(); + ComponentTableReset(component.Mass, 0.0f); + } + } + EndComponentTable(); + } +}; \ No newline at end of file diff --git a/Editor/src/Windows/EditorInterface.cpp b/Editor/src/Windows/EditorInterface.cpp index d2ff0fbb..a453a708 100644 --- a/Editor/src/Windows/EditorInterface.cpp +++ b/Editor/src/Windows/EditorInterface.cpp @@ -1406,7 +1406,12 @@ namespace Nuake { if (entityIsSelected && Input::IsKeyPressed(GLFW_KEY_F)) { editorCam->IsMoving = true; - editorCam->TargetPos = Vector3(0, 0, 0); + editorCam->TargetPos = Selection.Entity.GetComponent().GetGlobalPosition(); + } + + if (entityIsSelected && Input::IsKeyPressed(GLFW_KEY_ESCAPE)) + { + Selection = EditorSelection(); } } diff --git a/Editor/src/Windows/EditorSelectionPanel.cpp b/Editor/src/Windows/EditorSelectionPanel.cpp index 0400f5bf..a41a40b6 100644 --- a/Editor/src/Windows/EditorSelectionPanel.cpp +++ b/Editor/src/Windows/EditorSelectionPanel.cpp @@ -14,6 +14,7 @@ EditorSelectionPanel::EditorSelectionPanel() mScriptPanel = ScriptPanel(); mQuakeMapPanel = QuakeMapPanel(); mCameraPanel = CameraPanel(); + mRigidbodyPanel = RigidbodyPanel(); } void EditorSelectionPanel::ResolveFile(Ref file) @@ -96,6 +97,8 @@ void EditorSelectionPanel::DrawEntity(Nuake::Entity entity) mMeshPanel.Draw(entity); mQuakeMapPanel.Draw(entity); mCameraPanel.Draw(entity); + mRigidbodyPanel.Draw(entity); + /* if (Selection.Entity.HasComponent()) { diff --git a/Editor/src/Windows/EditorSelectionPanel.h b/Editor/src/Windows/EditorSelectionPanel.h index 97ff1d4e..b52a8d66 100644 --- a/Editor/src/Windows/EditorSelectionPanel.h +++ b/Editor/src/Windows/EditorSelectionPanel.h @@ -9,6 +9,7 @@ #include "../ComponentsPanel/MeshPanel.h" #include "../ComponentsPanel/QuakeMapPanel.h" #include "../ComponentsPanel/CameraPanel.h" +#include "../ComponentsPanel/RigidbodyPanel.h" #include "../Actions/EditorSelection.h" #include @@ -21,6 +22,7 @@ private: MeshPanel mMeshPanel; QuakeMapPanel mQuakeMapPanel; CameraPanel mCameraPanel; + RigidbodyPanel mRigidbodyPanel; Ref currentFile; Ref selectedResource; diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index 0bdf1077..09eeddd6 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -4,6 +4,8 @@ #include #include +#include + #include #include #include @@ -257,6 +259,40 @@ namespace Nuake void DynamicWorld::AddRigidbody(Ref rb) { + JPH::BodyInterface& bodyInterface = _JoltPhysicsSystem->GetBodyInterface(); + + auto rbShape = rb->GetShape(); + switch (rbShape->GetType()) + { + case RigidbodyShapes::BOX: + { + Box* box = (Box*)rbShape.get(); + const Vector3& boxSize = box->GetSize(); + JPH::BoxShapeSettings boxShapeSettings(JPH::Vec3(boxSize.x, boxSize.y, boxSize.z)); + + // Create the shape + JPH::ShapeSettings::ShapeResult floor_shape_result = boxShapeSettings.Create(); + JPH::ShapeRefC floor_shape = floor_shape_result.Get(); // We don't expect an error here, but you can check floor_shape_result for HasError() / GetError() + + // Create the settings for the body itself. Note that here you can also set other properties like the restitution / friction. + JPH::BodyCreationSettings boxSettings(floor_shape, JPH::Vec3(0.0f, -1.0f, 0.0f), JPH::Quat::sIdentity(), JPH::EMotionType::Static, Layers::NON_MOVING); + + // Create the actual rigid body + JPH::Body* floor = bodyInterface.CreateBody(floor_settings); // Note that if we run out of bodies this can return nullptr + + bodyInterface.AddBody(floor->GetID(), JPH::EActivation::DontActivate); + } + break; + case RigidbodyShapes::SPHERE: + + break; + case RigidbodyShapes::CAPSULE: + + break; + case RigidbodyShapes::MESH: + + break; + } } void DynamicWorld::AddGhostbody(Ref gb) diff --git a/Nuake/src/Core/Physics/PhysicsShapes.cpp b/Nuake/src/Core/Physics/PhysicsShapes.cpp index 82a1dfc7..17250f94 100644 --- a/Nuake/src/Core/Physics/PhysicsShapes.cpp +++ b/Nuake/src/Core/Physics/PhysicsShapes.cpp @@ -10,6 +10,7 @@ namespace Nuake Size = glm::vec3(1); m_Type = BOX; } + // Sphere Box::Box(glm::vec3 size) { Size = size; @@ -21,8 +22,6 @@ namespace Nuake m_Type = BOX; } - - // Sphere Sphere::Sphere(float radius) { Radius = radius; diff --git a/Nuake/src/Core/Physics/Rigibody.h b/Nuake/src/Core/Physics/Rigibody.h index b6df4610..a4f8c093 100644 --- a/Nuake/src/Core/Physics/Rigibody.h +++ b/Nuake/src/Core/Physics/Rigibody.h @@ -3,17 +3,12 @@ #include "../Core/Core.h" #include - namespace Nuake { class Entity; namespace Physics { class RigidBody { private: - bool m_IsDynamic = false; - bool m_IsKinematic = false; - Vector3 m_InitialVel; - Ref m_CollisionShape; public: float m_Mass; @@ -23,23 +18,12 @@ namespace Nuake RigidBody(float mass, glm::vec3 position, Ref shape, glm::vec3 initialVel = glm::vec3(0, 0, 0)); void UpdateTransform(); - Vector3 GetPosition() const; - Vector3 GetRotation() const; void SetEntityID(Entity ent); - void SetKinematic(bool value); - bool IsKinematic() const { return m_IsKinematic; } - bool HasShape() { return m_CollisionShape != nullptr; } void SetShape(Ref shape); Ref GetShape() const { return m_CollisionShape; } - - float GetMass() const { return m_Mass; } - void SetMass(float m); - - void MoveAndSlide(glm::vec3 velocity); }; } - } diff --git a/Nuake/src/Core/Physics/Rigidbody.cpp b/Nuake/src/Core/Physics/Rigidbody.cpp index af49f466..d2da5b69 100644 --- a/Nuake/src/Core/Physics/Rigidbody.cpp +++ b/Nuake/src/Core/Physics/Rigidbody.cpp @@ -15,65 +15,26 @@ namespace Nuake RigidBody::RigidBody(glm::vec3 position, Entity handle) { - Ref shape = CreateRef(); - m_CollisionShape = shape; - - - - m_Mass = 0.0f; - - //rigidbody is dynamic if and only if mass is non zero, otherwise static - m_IsDynamic = (m_Mass != 0.0f); - - - - m_InitialVel = glm::vec3(0, 0, 0); - } RigidBody::RigidBody(float mass, glm::vec3 position, Ref shape, glm::vec3 initialVel) { - m_CollisionShape = shape; - - m_Mass = mass; - - //rigidbody is dynamic if and only if mass is non zero, otherwise static - m_IsDynamic = (m_Mass != 0.0f); - + } void RigidBody::SetShape(Ref shape) { - m_CollisionShape = shape; + } void RigidBody::UpdateTransform() { } - glm::vec3 RigidBody::GetRotation() const { - return { 0, 0, 0 }; - } - void RigidBody::SetEntityID(Entity ent) { } - - void RigidBody::SetKinematic(bool value) - { - - } - - glm::vec3 RigidBody::GetPosition() const { - return { 0, 0, 0 }; - } - - void RigidBody::SetMass(float m) { } - void RigidBody::MoveAndSlide(glm::vec3 velocity) - { - - } } } diff --git a/Nuake/src/Resource/Model.cpp b/Nuake/src/Resource/Model.cpp index ae7e8b04..e4ffbf14 100644 --- a/Nuake/src/Resource/Model.cpp +++ b/Nuake/src/Resource/Model.cpp @@ -53,7 +53,7 @@ namespace Nuake this->IsEmbedded = true; ModelLoader loader; - auto otherModel = loader.LoadModel(FileSystem::Root + j["Path"].dump()); + auto otherModel = loader.LoadModel(j["Path"], false); m_Meshes = otherModel->GetMeshes(); this->Path = j["Path"]; diff --git a/Nuake/src/Resource/ModelLoader.cpp b/Nuake/src/Resource/ModelLoader.cpp index de806c74..90105261 100644 --- a/Nuake/src/Resource/ModelLoader.cpp +++ b/Nuake/src/Resource/ModelLoader.cpp @@ -13,7 +13,7 @@ namespace Nuake ModelLoader::ModelLoader() {} ModelLoader::~ModelLoader() {} - Ref ModelLoader::LoadModel(const std::string& path) + Ref ModelLoader::LoadModel(const std::string& path, bool absolute) { m_Meshes.clear(); Ref model = CreateRef(path); @@ -27,8 +27,9 @@ namespace Nuake aiProcess_FixInfacingNormals | aiProcess_CalcTangentSpace; - modelDir = path + "/../"; - const aiScene* scene = import.ReadFile(path, importFlags); + modelDir = absolute ? path + "/../" : FileSystem::Root + path + "/../"; + const std::string filePath = absolute ? path : FileSystem::Root + path; + const aiScene* scene = import.ReadFile(filePath, importFlags); if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { std::string assimpErrorMsg = std::string(import.GetErrorString()); diff --git a/Nuake/src/Resource/ModelLoader.h b/Nuake/src/Resource/ModelLoader.h index cf886e20..a6127832 100644 --- a/Nuake/src/Resource/ModelLoader.h +++ b/Nuake/src/Resource/ModelLoader.h @@ -20,7 +20,7 @@ namespace Nuake ModelLoader(); ~ModelLoader(); - Ref LoadModel(const std::string& path); + Ref LoadModel(const std::string& path, bool absolute = false); private: std::string modelDir; std::vector> m_Meshes; diff --git a/Nuake/src/Scene/Components/ModelComponent.cpp b/Nuake/src/Scene/Components/ModelComponent.cpp index 12e8f6de..e354fc5e 100644 --- a/Nuake/src/Scene/Components/ModelComponent.cpp +++ b/Nuake/src/Scene/Components/ModelComponent.cpp @@ -15,7 +15,7 @@ namespace Nuake { void ModelComponent::LoadModel() { auto loader = ModelLoader(); - this->ModelResource = loader.LoadModel(FileSystem::Root + ModelPath); + this->ModelResource = loader.LoadModel(ModelPath); } } diff --git a/Nuake/src/Scene/Components/ModelComponent.h b/Nuake/src/Scene/Components/ModelComponent.h index 3b44bfbb..ea1083ca 100644 --- a/Nuake/src/Scene/Components/ModelComponent.h +++ b/Nuake/src/Scene/Components/ModelComponent.h @@ -30,7 +30,7 @@ namespace Nuake bool Deserialize(const std::string str) { BEGIN_DESERIALIZE(); - ModelPath = j["ModelPath"].dump(); + ModelPath = j["ModelPath"]; ModelResource = CreateRef(); if (j.contains("ModelResource")) diff --git a/Nuake/src/Scene/Components/RigidbodyComponent.cpp b/Nuake/src/Scene/Components/RigidbodyComponent.cpp index 96fa61fb..3d50bab5 100644 --- a/Nuake/src/Scene/Components/RigidbodyComponent.cpp +++ b/Nuake/src/Scene/Components/RigidbodyComponent.cpp @@ -6,7 +6,7 @@ #include "src/Rendering/Renderer.h" namespace Nuake { - RigidBodyComponent::RigidBodyComponent() + RigidBodyComponent::RigidBodyComponent() : Mass(0.0f) { //m_Rigidbody = CreateRef(); } @@ -16,40 +16,11 @@ namespace Nuake { return m_Rigidbody; } - void RigidBodyComponent::SetRigidBody(Ref rb) - { - m_Rigidbody = rb; - PhysicsManager::Get()->RegisterBody(rb); - } - - bool RigidBodyComponent::HasRigidBody() const - { - return m_Rigidbody != nullptr; - } - - float RigidBodyComponent::GetMass() { - if (m_Rigidbody) - return m_Rigidbody->GetMass(); - return 0.0f; - } - - void RigidBodyComponent::SetMass(float m) - { - if (!m_Rigidbody) - return; - m_Rigidbody->SetMass(m); - } - - void RigidBodyComponent::SyncTransformComponent(TransformComponent* tc) { if (!m_Rigidbody) return; - glm::vec3 newPosition = m_Rigidbody->GetPosition(); - glm::vec3 newRotation = m_Rigidbody->GetRotation(); - //tc->Translation = newPosition; - //tc->Rotation = newRotation; } void RigidBodyComponent::SyncWithTransform(TransformComponent* tc) diff --git a/Nuake/src/Scene/Components/RigidbodyComponent.h b/Nuake/src/Scene/Components/RigidbodyComponent.h index 25519fdb..7fae80fb 100644 --- a/Nuake/src/Scene/Components/RigidbodyComponent.h +++ b/Nuake/src/Scene/Components/RigidbodyComponent.h @@ -12,23 +12,30 @@ namespace Nuake { class RigidBodyComponent { public: - float mass = 0.0f; + float Mass; Ref m_Rigidbody; - bool IsKinematic = false; RigidBodyComponent(); Ref GetRigidBody() const; - void SetRigidBody(Ref rb); - bool HasRigidBody() const; - - void SetMass(float m); - float GetMass(); - + void SyncTransformComponent(TransformComponent* tc); void SyncWithTransform(TransformComponent* tc); - void DrawShape(TransformComponent* tc); void DrawEditor(); + + json Serialize() + { + BEGIN_SERIALIZE(); + SERIALIZE_VAL_LBL("Mass", Mass); + END_SERIALIZE(); + } + + bool Deserialize(std::string str) + { + BEGIN_DESERIALIZE(); + Mass = j["Mass"]; + return true; + } }; } diff --git a/Nuake/src/Scene/Components/TransformComponent.h b/Nuake/src/Scene/Components/TransformComponent.h index 869b8f8f..146263e2 100644 --- a/Nuake/src/Scene/Components/TransformComponent.h +++ b/Nuake/src/Scene/Components/TransformComponent.h @@ -65,15 +65,14 @@ namespace Nuake float x = j["Rotation"]["x"]; float y = j["Rotation"]["y"]; float z = j["Rotation"]["z"]; + this->Rotation = Quat(w, x, y, z); } - this->Scale = Vector3(j["Scale"]["x"], j["Scale"]["y"], j["Scale"]["z"]); LocalTransform = Matrix4(1); GlobalTransform = Matrix4(1); - Dirty = true; return true; } }; diff --git a/Nuake/src/Scene/EditorCamera.cpp b/Nuake/src/Scene/EditorCamera.cpp index 175c91e5..dec23bc3 100644 --- a/Nuake/src/Scene/EditorCamera.cpp +++ b/Nuake/src/Scene/EditorCamera.cpp @@ -15,7 +15,8 @@ namespace Nuake if (glm::length(Translation - TargetPos) < 3.0f) { IsMoving = false; - Translation = Translation - TargetPos; + Translation = TargetPos - glm::normalize((TargetPos - Translation)) * 3.0f; + Direction = TargetPos - Translation; } } @@ -27,7 +28,14 @@ namespace Nuake if (hover) { + bool previous = controlled; controlled = isPressingMouse; + + if (!previous && controlled) + { + mouseLastX = x; + mouseLastY = y; + } } else { @@ -164,6 +172,9 @@ namespace Nuake Translation += Vector3(Direction) * Input::YScroll; Input::YScroll = 0.0f; } + + mouseLastX = x; + mouseLastY = y; } } diff --git a/Nuake/src/Scene/Entities/Entity.cpp b/Nuake/src/Scene/Entities/Entity.cpp index aa29beb0..c3045b7e 100644 --- a/Nuake/src/Scene/Entities/Entity.cpp +++ b/Nuake/src/Scene/Entities/Entity.cpp @@ -8,13 +8,12 @@ #include "../Components/QuakeMap.h" #include "../Components/WrenScriptComponent.h" #include "../Components/CharacterControllerComponent.h" +#include "../Components/RigidbodyComponent.h" +#include "src/Scene/Components/BSPBrushComponent.h" #include "src/Scene/Components/Components.h" #include -//#include -#include "src/Scene/Components/BSPBrushComponent.h" - namespace Nuake { void Entity::AddChild(Entity ent) @@ -53,6 +52,9 @@ namespace Nuake SERIALIZE_OBJECT_REF_LBL("BSPBrushComponent", GetComponent()); if (HasComponent()) SERIALIZE_OBJECT_REF_LBL("QuakeMapComponent", GetComponent()); + if (HasComponent()) + SERIALIZE_OBJECT_REF_LBL("RigidBodyComponent", GetComponent()); + END_SERIALIZE(); } @@ -74,6 +76,7 @@ namespace Nuake DESERIALIZE_COMPONENT(WrenScriptComponent); DESERIALIZE_COMPONENT(CharacterControllerComponent); DESERIALIZE_COMPONENT(BoxColliderComponent); + DESERIALIZE_COMPONENT(RigidBodyComponent); return true; } diff --git a/Nuake/src/Scene/Scene.cpp b/Nuake/src/Scene/Scene.cpp index 6ef7b7dd..36a1d8ac 100644 --- a/Nuake/src/Scene/Scene.cpp +++ b/Nuake/src/Scene/Scene.cpp @@ -320,7 +320,7 @@ namespace Nuake { BEGIN_SERIALIZE(); SERIALIZE_VAL(Name); SERIALIZE_OBJECT(m_Environement) - std::vector entities = std::vector(); + std::vector entities = std::vector(); for (Entity e : GetAllEntities()) entities.push_back(e.Serialize()); SERIALIZE_VAL_LBL("Entities", entities); diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.cpp b/Nuake/src/Scene/Systems/PhysicsSystem.cpp index 9fb2d111..d6efaab7 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.cpp +++ b/Nuake/src/Scene/Systems/PhysicsSystem.cpp @@ -28,7 +28,7 @@ namespace Nuake if (ent.HasComponent()) { - float mass = rigidbody.mass; + float mass = rigidbody.Mass; BoxColliderComponent& boxComponent = ent.GetComponent(); Ref boxShape = CreateRef(boxComponent.Size);