diff --git a/Nuake/src/Physics/DynamicWorld.cpp b/Nuake/src/Physics/DynamicWorld.cpp index 527c9c18..c1308fad 100644 --- a/Nuake/src/Physics/DynamicWorld.cpp +++ b/Nuake/src/Physics/DynamicWorld.cpp @@ -430,6 +430,16 @@ namespace Nuake return false; } + void DynamicWorld::SetCharacterControllerPosition(const Entity & entity, const Vector3 & position) + { + const uint32_t entityHandle = entity.GetHandle(); + if (_registeredCharacters.find(entityHandle) != _registeredCharacters.end()) + { + auto& characterController = _registeredCharacters[entityHandle].Character; + characterController->SetPosition({ position.x, position.y, position.z }); + } + } + std::vector DynamicWorld::Raycast(const Vector3& from, const Vector3& to) { // Create jolt ray @@ -474,37 +484,27 @@ namespace Nuake for (const auto& body : _registeredBodies) { auto bodyId = static_cast(body); - JPH::Vec3 position = bodyInterface.GetCenterOfMassPosition(bodyId); - JPH::Vec3 velocity = bodyInterface.GetLinearVelocity(bodyId); - JPH::Mat44 joltTransform = bodyInterface.GetWorldTransform(bodyId); - const auto bodyRotation = bodyInterface.GetRotation(bodyId); - - Matrix4 transform = glm::mat4( - joltTransform(0, 0), joltTransform(1, 0), joltTransform(2, 0), joltTransform(3, 0), - joltTransform(0, 1), joltTransform(1, 1), joltTransform(2, 1), joltTransform(3, 1), - joltTransform(0, 2), joltTransform(1, 2), joltTransform(2, 2), joltTransform(3, 2), - 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); - - auto entId = static_cast(bodyInterface.GetUserData(bodyId)); - if (entId == 1337) - { - Entity entity = Engine::GetCurrentScene()->GetEntity("Gizmo"); - auto& transformComponent = entity.GetComponent(); - transformComponent.SetLocalPosition(pos); - transformComponent.SetLocalRotation(Quat(bodyRotation.GetW(), bodyRotation.GetX(), bodyRotation.GetY(), bodyRotation.GetZ())); - transformComponent.SetLocalTransform(transform); - transformComponent.Dirty = true; - } - if (entId != 0) + if (auto entId = static_cast(bodyInterface.GetUserData(bodyId)); entId != 0) { + JPH::Vec3 position = bodyInterface.GetCenterOfMassPosition(bodyId); + JPH::Vec3 velocity = bodyInterface.GetLinearVelocity(bodyId); + JPH::Mat44 joltTransform = bodyInterface.GetWorldTransform(bodyId); + const auto bodyRotation = bodyInterface.GetRotation(bodyId); + + Matrix4 transform = glm::mat4( + joltTransform(0, 0), joltTransform(1, 0), joltTransform(2, 0), joltTransform(3, 0), + joltTransform(0, 1), joltTransform(1, 1), joltTransform(2, 1), joltTransform(3, 1), + joltTransform(0, 2), joltTransform(1, 2), joltTransform(2, 2), joltTransform(3, 2), + 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); + Entity entity = Engine::GetCurrentScene()->GetEntityByID(entId); auto& transformComponent = entity.GetComponent(); transformComponent.SetLocalPosition(pos); @@ -517,10 +517,6 @@ namespace Nuake void DynamicWorld::SyncCharactersTransforms() { - // TODO(ANTO): Finish this to connect updated jolt transforms back to the entity. - // The problem was that I dont know yet how to go from jolt body ptr to the entity - // Combinations of find and iterators etc. I do not have the brain power rn zzz. - // const auto& bodyInterface = _JoltPhysicsSystem->GetBodyInterface(); for (const auto& e : _registeredCharacters) { Entity entity { (entt::entity)e.first, Engine::GetCurrentScene().get()}; diff --git a/Nuake/src/Physics/DynamicWorld.h b/Nuake/src/Physics/DynamicWorld.h index 97555abb..5b7f9b4b 100644 --- a/Nuake/src/Physics/DynamicWorld.h +++ b/Nuake/src/Physics/DynamicWorld.h @@ -72,6 +72,8 @@ namespace Nuake void AddGhostbody(Ref gb); void AddCharacterController(Ref cc); bool IsCharacterGrounded(const Entity& entity); + void SetCharacterControllerPosition(const Entity& entity, const Vector3& position); + // This is going to be ugly. TODO: Find a better way that passing itself as a parameter void MoveAndSlideCharacterController(const Entity& entity, const Vector3& velocity); void AddForceToRigidBody(Entity& entity, const Vector3& force); diff --git a/Nuake/src/Physics/PhysicsManager.cpp b/Nuake/src/Physics/PhysicsManager.cpp index 06120b7e..d48b0b98 100644 --- a/Nuake/src/Physics/PhysicsManager.cpp +++ b/Nuake/src/Physics/PhysicsManager.cpp @@ -27,6 +27,11 @@ namespace Nuake m_World->AddCharacterController(cc); } + void PhysicsManager::SetCharacterControllerPosition(const Entity& entity, const Vector3& position) + { + m_World->SetCharacterControllerPosition(entity, position); + } + void PhysicsManager::Step(Timestep ts) { m_World->StepSimulation(ts); diff --git a/Nuake/src/Physics/PhysicsManager.h b/Nuake/src/Physics/PhysicsManager.h index 9c310b12..418f60ce 100644 --- a/Nuake/src/Physics/PhysicsManager.h +++ b/Nuake/src/Physics/PhysicsManager.h @@ -55,5 +55,7 @@ namespace Nuake void RegisterBody(Ref rb); void RegisterGhostBody(Ref rb); void RegisterCharacterController(Ref c); + + void SetCharacterControllerPosition(const Entity& entity, const Vector3& position); }; } diff --git a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp index 2e3a7fe8..f8320002 100644 --- a/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp +++ b/Nuake/src/Scripting/NetModules/SceneNetAPI.cpp @@ -112,6 +112,11 @@ namespace Nuake { { auto& component = entity.GetComponent(); component.SetLocalPosition({ x, y, z }); + + if (entity.HasComponent()) + { + PhysicsManager::Get().SetCharacterControllerPosition(entity, { x, y, z }); + } } } diff --git a/NuakeNet/src/Entity.cs b/NuakeNet/src/Entity.cs index 12f13ffa..4118778e 100644 --- a/NuakeNet/src/Entity.cs +++ b/NuakeNet/src/Entity.cs @@ -52,15 +52,14 @@ namespace Nuake.Net public virtual void OnFixedUpdate(float dt) { } public virtual void OnDestroy() { } - public virtual void OnCollision(int entity1, int entity2) + public virtual void OnCollision(Entity entity1, Entity entity2) { - Engine.Log("penis"); } // Physics public void OnCollisionInternal(int entity1, int entity2) { - //OnCollision(new Entity { ECSHandle = entity1 }, new Entity { ECSHandle = entity2 }); + OnCollision(new Entity { ECSHandle = entity1 }, new Entity { ECSHandle = entity2 }); } protected static Dictionary MappingTypeEnum = new Dictionary() diff --git a/NuakeNet/src/Scene.cs b/NuakeNet/src/Scene.cs index 1010f094..65edaf11 100644 --- a/NuakeNet/src/Scene.cs +++ b/NuakeNet/src/Scene.cs @@ -21,6 +21,7 @@ namespace Nuake.Net { ECSHandle = handle }; + return entity; } }