From c826bbde99337c1cd6cdd45dd4db890dee679230 Mon Sep 17 00:00:00 2001 From: antopilo Date: Tue, 14 Mar 2023 22:39:36 -0400 Subject: [PATCH] Decompose of jolt matrix to get new transform --- Nuake/src/Core/Physics/DynamicWorld.cpp | 47 +++++++++---------------- 1 file changed, 16 insertions(+), 31 deletions(-) diff --git a/Nuake/src/Core/Physics/DynamicWorld.cpp b/Nuake/src/Core/Physics/DynamicWorld.cpp index e6c1fb53..a6177d46 100644 --- a/Nuake/src/Core/Physics/DynamicWorld.cpp +++ b/Nuake/src/Core/Physics/DynamicWorld.cpp @@ -6,6 +6,8 @@ #include +#include "src/Vendors/glm/gtx/matrix_decompose.hpp" + #include #include #include @@ -18,6 +20,7 @@ #include #include #include +#include #include #include @@ -188,7 +191,7 @@ namespace Nuake std::cout << "A body went to sleep" << std::endl; } }; - JPH::BodyID sphere_id; + BPLayerInterfaceImpl JoltBroadphaseLayerInterface = BPLayerInterfaceImpl(); namespace Physics { @@ -218,33 +221,6 @@ namespace Nuake // variant of this. We're going to use the locking version (even though we're not planning to access bodies from multiple threads) _JoltBodyInterface = &_JoltPhysicsSystem->GetBodyInterface(); - // Next we can create a rigid body to serve as the floor, we make a large box - // Create the settings for the collision volume (the shape). - // Note that for simple shapes (like boxes) you can also directly construct a BoxShape. - JPH::BoxShapeSettings floor_shape_settings(JPH::Vec3(100.0f, 1.0f, 100.0f)); - - // Create the shape - JPH::ShapeSettings::ShapeResult floor_shape_result = floor_shape_settings.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 floor_settings(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 = _JoltBodyInterface->CreateBody(floor_settings); // Note that if we run out of bodies this can return nullptr - - _JoltBodyInterface->AddBody(floor->GetID(), JPH::EActivation::DontActivate); - - JPH::BodyCreationSettings sphere_settings(new JPH::SphereShape(0.5f), JPH::Vec3(0.0, 2.0, 0.0), JPH::Quat::sIdentity(), JPH::EMotionType::Dynamic, Layers::MOVING); - sphere_id = _JoltBodyInterface->CreateAndAddBody(sphere_settings, JPH::EActivation::Activate); - - // Now you can interact with the dynamic body, in this case we're going to give it a velocity. - // (note that if we had used CreateBody then we could have set the velocity straight on the body before adding it to the physics system) - _JoltBodyInterface->SetLinearVelocity(sphere_id, JPH::Vec3(0.0f, -5.0f, 0.0f)); - - // We simulate the physics world in discrete time steps. 60 Hz is a good rate to update the physics system. - const float cDeltaTime = 1.0f / 60.0f; - // Optional step: Before starting the physics simulation you can optimize the broad phase. This improves collision detection performance (it's pointless here because we only have 2 bodies). // You should definitely not call this every frame or when e.g. streaming in a new level section as it is an expensive operation. // Instead insert all new objects in batches instead of 1 at a time to keep the broad phase efficient. @@ -359,7 +335,8 @@ namespace Nuake } const auto& startPos = rb->GetPosition(); - JPH::BodyCreationSettings bodySettings(shapeResult.Get(), JPH::Vec3(startPos.x, startPos.y, startPos.z), JPH::Quat::sIdentity(), motionType, Layers::MOVING); + const auto& joltPos = JPH::Vec3(startPos.x, startPos.y, startPos.z); + JPH::BodyCreationSettings bodySettings(shapeResult.Get(), joltPos, JPH::Quat::sIdentity(), motionType, Layers::MOVING); if (mass > 0.0f) { @@ -417,14 +394,22 @@ namespace Nuake joltTransform(0, 3), joltTransform(1, 3), joltTransform(2, 3), joltTransform(3, 3) ); + Vector3 scale = Vector3(); + Quat rotation = Quat(); + Vector3 pos = Vector3(); + Vector3 skew = Vector3(); + Vector4 pesp = Vector4(); + glm::decompose(transform, scale, rotation, pos, skew, pesp); + uint32_t entId = bodyInterface.GetUserData(bodyId); Entity entity = Engine::GetCurrentScene()->GetEntityByID(entId); const std::string& name = entity.GetComponent().Name; TransformComponent& transformComponent = entity.GetComponent(); transformComponent.GlobalTransform = transform; - transformComponent.SetGlobalPosition(Vector3(position.GetX(), position.GetY(), position.GetZ())); - transformComponent.SetGlobalTransform(transform); + //transformComponent.SetLocalPosition(pos); + //transformComponent.SetLocalRotation(rotation); + //transformComponent.SetLocalScale(scale); transformComponent.SetLocalTransform(transform); } // If you take larger steps than 1 / 60th of a second you need to do multiple collision steps in order to keep the simulation stable. Do 1 collision step per 1 / 60th of a second (round up).