diff --git a/Nuake/src/Core/Maths.h b/Nuake/src/Core/Maths.h index 6612d894..c16fba0e 100644 --- a/Nuake/src/Core/Maths.h +++ b/Nuake/src/Core/Maths.h @@ -5,6 +5,7 @@ #include #include #include +#include "src/Vendors/glm/gtx/matrix_decompose.hpp" #include namespace Nuake diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index bb5fa6ae..a83d77b0 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -3,6 +3,7 @@ #include "src/Core/Core.h" #include "src/Core/Logger.h" +#include "src/Core/Maths.h" #include #include "src/Scene/Components/TransformComponent.h" #include "src/Scene/Components/CharacterControllerComponent.h" @@ -259,9 +260,11 @@ namespace Nuake } const auto& startPos = rb->GetPosition(); + const Quat& bodyRotation = rb->GetRotation(); + const auto& joltRotation = JPH::Quat(bodyRotation.x, bodyRotation.y, bodyRotation.z, bodyRotation.w); const auto& joltPos = JPH::Vec3(startPos.x, startPos.y, startPos.z); auto joltShape = GetJoltShape(rb->GetShape()); - JPH::BodyCreationSettings bodySettings(joltShape, joltPos, JPH::Quat::sIdentity(), motionType, Layers::MOVING); + JPH::BodyCreationSettings bodySettings(joltShape, joltPos, joltRotation, motionType, Layers::MOVING); if (mass > 0.0f) { @@ -410,7 +413,7 @@ namespace Nuake // Do 1 collision step per 1 / 60th of a second (round up). int collisionSteps = 1; constexpr float minStepDuration = 1.0f / 90.0f; - constexpr int maxStepCount = 4; + constexpr int maxStepCount = 32; if(ts > minStepDuration) { collisionSteps = static_cast(ts) / minStepDuration; @@ -427,7 +430,7 @@ namespace Nuake for (auto& c : _registeredCharacters) { - c.second->PostSimulation(0.01); + c.second->PostSimulation(0.001); } SyncEntitiesTranforms(); diff --git a/Nuake/src/Core/Physics/Rigibody.h b/Nuake/src/Core/Physics/Rigibody.h index 17030073..14b99b76 100644 --- a/Nuake/src/Core/Physics/Rigibody.h +++ b/Nuake/src/Core/Physics/Rigibody.h @@ -1,31 +1,38 @@ #pragma once #include "PhysicsShapes.h" -#include "../Core/Core.h" -#include "../Scene/Entities/Entity.h" + +#include "src/Core/Core.h" +#include "src/Core/Maths.h" +#include "src/Scene/Entities/Entity.h" #include namespace Nuake { class Entity; - namespace Physics { - class RigidBody { + namespace Physics + { + class RigidBody + { private: Ref _collisionShape; Vector3 _position; + Quat _rotation; Entity _entity; + public: float _mass; Matrix4 _transform; RigidBody(); - RigidBody(glm::vec3 position, Entity handle); - RigidBody(float mass, glm::vec3 position, Matrix4 transform, Ref shape, Entity entity, glm::vec3 initialVel = glm::vec3(0, 0, 0)); + RigidBody(Vector3 position, Entity handle); + RigidBody(float mass, Vector3 position, Quat rotation, Matrix4 transform, Ref shape, Entity entity, glm::vec3 initialVel = glm::vec3(0, 0, 0)); void UpdateTransform(); void SetEntityID(Entity ent); Vector3 GetPosition() const { return _position; } + Quat GetRotation() const { return _rotation; } bool HasShape() { return _collisionShape != nullptr; } void SetShape(Ref shape); Ref GetShape() const { return _collisionShape; } diff --git a/Nuake/src/Core/Physics/Rigidbody.cpp b/Nuake/src/Core/Physics/Rigidbody.cpp index 73583412..aaa93938 100644 --- a/Nuake/src/Core/Physics/Rigidbody.cpp +++ b/Nuake/src/Core/Physics/Rigidbody.cpp @@ -18,12 +18,13 @@ namespace Nuake } - RigidBody::RigidBody(float mass, glm::vec3 position, Matrix4 transform, Ref shape, Entity entity, glm::vec3 initialVel) : + RigidBody::RigidBody(float mass, Vector3 position, Quat rotation, Matrix4 transform, Ref shape, Entity entity, glm::vec3 initialVel) : _position(position), _collisionShape(shape), _mass(mass), _entity(entity), - _transform(transform) + _transform(transform), + _rotation(rotation) { } diff --git a/Nuake/src/Scene/Systems/PhysicsSystem.cpp b/Nuake/src/Scene/Systems/PhysicsSystem.cpp index b7b94ef5..24fc52ec 100644 --- a/Nuake/src/Scene/Systems/PhysicsSystem.cpp +++ b/Nuake/src/Scene/Systems/PhysicsSystem.cpp @@ -141,11 +141,12 @@ namespace Nuake for (const auto& hull : brushComponent.Hulls) { const auto entity = Entity({ e, m_Scene }); - const Vector3 startPosition = transformComponent.GetGlobalPosition(); - const Matrix4 startTransform = transformComponent.GetGlobalTransform(); + const Vector3& startPosition = transformComponent.GetGlobalPosition(); + const Matrix4& startTransform = transformComponent.GetGlobalTransform(); + const Quat& startRotation = transformComponent.GetGlobalRotation(); const auto collisionShape = CreateRef(hull); - auto rigidBody = CreateRef(0.0f, startPosition, startTransform, collisionShape, entity); + auto rigidBody = CreateRef(0.0f, startPosition, startRotation, startTransform, collisionShape, entity); brushComponent.Rigidbody.push_back(rigidBody); PhysicsManager::Get().RegisterBody(rigidBody); @@ -220,8 +221,7 @@ namespace Nuake BoxColliderComponent& boxComponent = ent.GetComponent(); Ref boxShape = CreateRef(boxComponent.Size); - - rigidBody = CreateRef(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalTransform(), boxShape, ent); + rigidBody = CreateRef(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), boxShape, ent); PhysicsManager::Get().RegisterBody(rigidBody); } @@ -232,7 +232,7 @@ namespace Nuake float height = capsuleComponent.Height; auto capsuleShape = CreateRef(radius, height); - rigidBody = CreateRef(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalTransform(), capsuleShape, ent); + rigidBody = CreateRef(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), capsuleShape, ent); PhysicsManager::Get().RegisterBody(rigidBody); } @@ -243,7 +243,7 @@ namespace Nuake const auto& component = ent.GetComponent(); auto shape = CreateRef(component.Radius); - rigidBody = CreateRef(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalTransform(), shape, ent); + rigidBody = CreateRef(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), shape, ent); PhysicsManager::Get().RegisterBody(rigidBody); } @@ -266,7 +266,7 @@ namespace Nuake } Ref mesh = submeshes[subMeshId]; auto shape = CreateRef(mesh); - rigidBody = CreateRef(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalTransform(), shape, ent); + rigidBody = CreateRef(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), shape, ent); PhysicsManager::Get().RegisterBody(rigidBody); } }