From ba4283b4dc81178710b8270874847fa0ba24d3ab Mon Sep 17 00:00:00 2001 From: Simon Tippe Date: Thu, 15 Feb 2024 18:58:49 +0100 Subject: [PATCH] Smaller changes --- src/editor/ui/panels/ViewportPanel.cpp | 35 ++++--- src/editor/ui/panels/ViewportPanel.h | 3 + src/editor/ui/windows/SceneWindow.cpp | 8 ++ src/engine/physics/Player.cpp | 98 +++++++++++++++++++ src/engine/physics/Player.h | 35 ++++++- .../scene/components/RigidBodyComponent.h | 2 +- 6 files changed, 166 insertions(+), 15 deletions(-) diff --git a/src/editor/ui/panels/ViewportPanel.cpp b/src/editor/ui/panels/ViewportPanel.cpp index 017c8142c..73e6fb4eb 100644 --- a/src/editor/ui/panels/ViewportPanel.cpp +++ b/src/editor/ui/panels/ViewportPanel.cpp @@ -31,19 +31,17 @@ namespace Atlas::Editor::UI { ImGui::Begin(GetNameID()); - isFocused = ImGui::IsWindowFocused(); - - if (drawMenuBarFunc) { - ImGui::BeginChild("Viewport area"); - - isFocused |= ImGui::IsWindowFocused(); - - drawMenuBarFunc(); - - ImGui::EndChild(); + // Workaround for offsetted Gizmo without resize after a restart + // Seems like some ImGuizmo or ImGui config isn't properly updated + if (firstFrame) { + auto size = ImGui::GetWindowPos(); + ImGui::SetWindowPos(ImVec2(size.x + 1.0f, size.y)); + firstFrame = false; } - ImGui::BeginChild("Viewport area"); + isFocused = ImGui::IsWindowFocused(); + + ImGui::BeginChild("Viewport area", ImVec2(0.0f, 0.0f)); isFocused |= ImGui::IsWindowFocused(); @@ -92,13 +90,24 @@ namespace Atlas::Editor::UI { ImGui::Image(set, region); } - ImGui::SetCursorPos(ImVec2(0.0f, windowPos.y)); - if (drawOverlayFunc) drawOverlayFunc(); ImGui::EndChild(); + ImGui::SetCursorPos(ImVec2(0.0f, 0.0f)); + + if (drawMenuBarFunc) { + ImGui::BeginChild("Viewport area"); + + isFocused |= ImGui::IsWindowFocused(); + + drawMenuBarFunc(); + + ImGui::EndChild(); + } + + ImGui::End(); } diff --git a/src/editor/ui/panels/ViewportPanel.h b/src/editor/ui/panels/ViewportPanel.h index bfd37331d..c1dc92dd3 100644 --- a/src/editor/ui/panels/ViewportPanel.h +++ b/src/editor/ui/panels/ViewportPanel.h @@ -26,6 +26,9 @@ namespace Atlas::Editor::UI { std::function drawMenuBarFunc; std::function drawOverlayFunc; + private: + bool firstFrame = true; + }; } diff --git a/src/editor/ui/windows/SceneWindow.cpp b/src/editor/ui/windows/SceneWindow.cpp index 0b270d9dd..caea6a02f 100644 --- a/src/editor/ui/windows/SceneWindow.cpp +++ b/src/editor/ui/windows/SceneWindow.cpp @@ -291,6 +291,14 @@ namespace Atlas::Editor::UI { const auto& cameraComponent = entity.GetComponent(); viewportPanel.primitiveBatchWrapper.RenderLineFrustum(cameraComponent.frustum, vec3(1.0f, 0.0f, 1.0f)); } + if (entity.HasComponent()) { + const auto& lightComponent = entity.GetComponent(); + if (lightComponent.shadow) { + for (const auto& component : lightComponent.shadow->views) + viewportPanel.primitiveBatchWrapper.RenderLineFrustum( + Volume::Frustum(component.frustumMatrix), vec3(1.0f, 0.0f, 0.0f)); + } + } } diff --git a/src/engine/physics/Player.cpp b/src/engine/physics/Player.cpp index a950c3317..3957b4769 100644 --- a/src/engine/physics/Player.cpp +++ b/src/engine/physics/Player.cpp @@ -1,5 +1,7 @@ #include "Player.h" #include "MathConversion.h" +#include "PhysicsWorld.h" +#include "PhysicsManager.h" namespace Atlas::Physics { @@ -13,10 +15,106 @@ namespace Atlas::Physics { settings.mMass = mass; settings.mMaxStrength = maxStrength; + settings.mPredictiveContactDistance = predictiveContactDistance; + settings.mCharacterPadding = padding; + + settings.mShapeOffset = VecToJPHVec(shapeOffset); settings.mShape = shape->ref; return settings; } + void Player::SetPosition(vec3 position) { + + AE_ASSERT(world != nullptr && "Physics world is invalid"); + character->SetPosition(Physics::VecToJPHVec(position)); + + } + + mat4 Player::GetMatrix() const { + + AE_ASSERT(world != nullptr && "Physics world is invalid"); + return Physics::JPHMatToMat(character->GetWorldTransform()); + + } + + void Player::SetLinearVelocity(vec3 velocity) { + + AE_ASSERT(world != nullptr && "Physics world is invalid"); + character->SetLinearVelocity(Physics::VecToJPHVec(velocity)); + + } + + vec3 Player::GetLinearVelocity() const { + + AE_ASSERT(world != nullptr && "Physics world is invalid"); + return Physics::JPHVecToVec(character->GetLinearVelocity()); + + } + + vec3 Player::GetGroundVelocity() const { + + AE_ASSERT(world != nullptr && "Physics world is invalid"); + + character->UpdateGroundVelocity(); + return Physics::JPHVecToVec(character->GetGroundVelocity()); + + } + + bool Player::IsOnGround() const { + + AE_ASSERT(world != nullptr && "Physics world is invalid"); + return character->GetGroundState() == JPH::CharacterVirtual::EGroundState::OnGround; + + } + + void Player::SetUp(vec3 up) { + + AE_ASSERT(world != nullptr && "Physics world is invalid"); + character->SetUp(Physics::VecToJPHVec(up)); + + } + + vec3 Player::GetUp() const { + + AE_ASSERT(world != nullptr && "Physics world is invalid"); + return Physics::JPHVecToVec(character->GetUp()); + + } + + void Player::SetShape(const Ref& shape) { + + AE_ASSERT(world != nullptr && "Physics world is invalid"); + + auto system = world->system; + + const auto& physicsSettings = system->GetPhysicsSettings(); + const auto& broadPhaseLayerFilter = system->GetDefaultBroadPhaseLayerFilter(Layers::MOVABLE); + const auto& defaultLayerFilter = system->GetDefaultLayerFilter(Physics::Layers::MOVABLE); + const auto& tempAllocator = Physics::PhysicsManager::tempAllocator; + + character->SetShape(shape->ref, physicsSettings.mPenetrationSlop * 1.5f, broadPhaseLayerFilter, + defaultLayerFilter, {}, {}, *tempAllocator); + + } + + void Player::Update(float deltaTime) { + + AE_ASSERT(world != nullptr && "Physics world is invalid"); + + auto system = world->system; + + const auto& physicsSettings = system->GetPhysicsSettings(); + const auto& broadPhaseLayerFilter = system->GetDefaultBroadPhaseLayerFilter(Layers::MOVABLE); + const auto& defaultLayerFilter = system->GetDefaultLayerFilter(Physics::Layers::MOVABLE); + const auto& tempAllocator = Physics::PhysicsManager::tempAllocator; + + auto gravityVector = -GetUp() * glm::length(world->GetGravity()); + + character->Update(deltaTime, VecToJPHVec(gravityVector), broadPhaseLayerFilter, + defaultLayerFilter, {}, {}, *tempAllocator); + + } + } \ No newline at end of file diff --git a/src/engine/physics/Player.h b/src/engine/physics/Player.h index a7c317906..3bdf81870 100644 --- a/src/engine/physics/Player.h +++ b/src/engine/physics/Player.h @@ -29,9 +29,42 @@ namespace Atlas::Physics { class Player { public: - Player(const PlayerCreationSettings& creationSettings, Ref& physicsWorld); + Player(const PlayerCreationSettings& creationSettings, const Ref& physicsWorld); + bool IsValid() const { return world != nullptr; } + void SetPosition(vec3 position); + + vec3 GetPosition() const; + + void SetRotation(quat rotation); + + quat GetRotation() const; + + mat4 GetMatrix() const; + + void SetLinearVelocity(vec3 velocity); + + vec3 GetLinearVelocity() const; + + vec3 GetGroundVelocity() const; + + bool IsOnGround() const; + + void SetUp(vec3 up); + + vec3 GetUp() const; + + void SetShape(const Ref& shape); + + virtual void Update(float deltaTime); + + Ref playerCreationSettings = nullptr; + + protected: + Ref character = nullptr; + + PhysicsWorld* world = nullptr; }; diff --git a/src/engine/scene/components/RigidBodyComponent.h b/src/engine/scene/components/RigidBodyComponent.h index 2bb755b46..4b109d85f 100644 --- a/src/engine/scene/components/RigidBodyComponent.h +++ b/src/engine/scene/components/RigidBodyComponent.h @@ -21,7 +21,7 @@ namespace Atlas { explicit RigidBodyComponent(const Physics::BodyCreationSettings& bodyCreationSettings) : layer(bodyCreationSettings.objectLayer), bodyCreationSettings(CreateRef(bodyCreationSettings)) {} - Physics::BodyCreationSettings GetBodyCreationSettings() override; + virtual Physics::BodyCreationSettings GetBodyCreationSettings() override; Physics::ObjectLayer layer = Physics::Layers::STATIC; Ref bodyCreationSettings = nullptr;