Added ability for triggers to move according to parent

This commit is contained in:
Antoine Pilote
2024-04-18 20:08:04 -04:00
parent 07e080f6a9
commit 89846a6885
10 changed files with 75 additions and 16 deletions

View File

@@ -144,8 +144,8 @@ namespace Nuake
virtual void OnContactAdded(const JPH::Body& inBody1, const JPH::Body& inBody2, const JPH::ContactManifold& inManifold, JPH::ContactSettings& ioSettings) override
{
int entity1 = static_cast<int>(inBody1.GetUserData());
int entity2 = static_cast<int>(inBody2.GetUserData());
uint32_t entity1 = static_cast<uint32_t>(inBody1.GetUserData());
uint32_t entity2 = static_cast<uint32_t>(inBody2.GetUserData());
JPH::Vec3 joltNormal = inManifold.mWorldSpaceNormal;
Vector3 normal = Vector3(joltNormal.GetX(), joltNormal.GetY(), joltNormal.GetZ());
@@ -301,6 +301,7 @@ namespace Nuake
layer = Layers::MOVING;
}
const std::string name = rb->GetEntity().GetComponent<NameComponent>().Name;
if (rb->IsTrigger())
{
layer = Layers::SENSORS;
@@ -353,7 +354,7 @@ namespace Nuake
Logger::Log("Entity with ID 0 detected. Name: " + rb->GetEntity().GetComponent<NameComponent>().Name, "DEBUG");
}
int entityId = rb->GetEntity().GetID();
int entityId = rb->GetEntity().GetHandle();
if (entityId == 0)
{
@@ -430,6 +431,31 @@ namespace Nuake
return false;
}
void DynamicWorld::SetBodyPosition(const Entity& entity, const Vector3& position, const Quat& rotation)
{
const auto& bodyInterface = _JoltPhysicsSystem->GetBodyInterface();
for (const auto& body : _registeredBodies)
{
auto bodyId = static_cast<JPH::BodyID>(body);
auto bodyEntityId = bodyInterface.GetUserData(bodyId);
if (bodyEntityId == entity.GetHandle())
{
JPH::Vec3 currentPosition;
JPH::Quat currentRotation;
_JoltBodyInterface->GetPositionAndRotation(bodyId, currentPosition, currentRotation);
JPH::Vec3 newPosition = { position.x, position.y, position.z };
JPH::Quat newRotation = { rotation.x, rotation.y, rotation.z, rotation.w };
if (newPosition != currentPosition || currentRotation != newRotation)
{
std::string name = entity.GetComponent<NameComponent>().Name;
_JoltBodyInterface->SetPositionAndRotation(bodyId, newPosition, newRotation, JPH::EActivation::DontActivate);
}
}
}
}
void DynamicWorld::SetCharacterControllerPosition(const Entity & entity, const Vector3 & position)
{
const uint32_t entityHandle = entity.GetHandle();
@@ -486,6 +512,11 @@ namespace Nuake
auto bodyId = static_cast<JPH::BodyID>(body);
if (auto entId = static_cast<int>(bodyInterface.GetUserData(bodyId)); entId != 0)
{
if (bodyInterface.GetObjectLayer(bodyId) == Layers::SENSORS)
{
continue;
}
JPH::Vec3 position = bodyInterface.GetCenterOfMassPosition(bodyId);
JPH::Vec3 velocity = bodyInterface.GetLinearVelocity(bodyId);
JPH::Mat44 joltTransform = bodyInterface.GetWorldTransform(bodyId);
@@ -505,7 +536,7 @@ namespace Nuake
Vector4 pesp = Vector4();
glm::decompose(transform, scale, rotation, pos, skew, pesp);
Entity entity = Engine::GetCurrentScene()->GetEntityByID(entId);
Entity entity = { (entt::entity)entId, Engine::GetCurrentScene().get() };
auto& transformComponent = entity.GetComponent<TransformComponent>();
transformComponent.SetLocalPosition(pos);
transformComponent.SetLocalRotation(Quat(bodyRotation.GetW(), bodyRotation.GetX(), bodyRotation.GetY(), bodyRotation.GetZ()));
@@ -724,7 +755,7 @@ namespace Nuake
{
auto bodyId = static_cast<JPH::BodyID>(body);
auto entityId = bodyInterface.GetUserData(bodyId);
if (entityId == entity.GetID())
if (entityId == entity.GetHandle())
{
bodyInterface.AddForce(bodyId, JPH::Vec3(force.x, force.y, force.z));
return;
@@ -744,7 +775,7 @@ namespace Nuake
{
Box* box = (Box*)shape.get();
const Vector3& boxSize = box->GetSize();
JPH::BoxShapeSettings shapeSettings(JPH::Vec3(boxSize.x, boxSize.y, boxSize.z));
JPH::BoxShapeSettings shapeSettings(JPH::Vec3(boxSize.x, boxSize.y, boxSize.z), 0.01f);
result = shapeSettings.Create();
}
break;

View File

@@ -75,6 +75,8 @@ namespace Nuake
bool IsCharacterGrounded(const Entity& entity);
void SetCharacterControllerPosition(const Entity& entity, const Vector3& position);
void SetBodyPosition(const Entity& entity, const Vector3& position, const Quat& rotation);
// 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);

View File

@@ -27,6 +27,11 @@ namespace Nuake
m_World->AddCharacterController(cc);
}
void PhysicsManager::SetBodyTransform(const Entity& entity, const Vector3& position, const Quat& rotation)
{
m_World->SetBodyPosition(entity, position, rotation);
}
void PhysicsManager::SetCharacterControllerPosition(const Entity& entity, const Vector3& position)
{
m_World->SetCharacterControllerPosition(entity, position);

View File

@@ -56,6 +56,7 @@ namespace Nuake
void RegisterGhostBody(Ref<GhostObject> rb);
void RegisterCharacterController(Ref<Physics::CharacterController> c);
void SetBodyTransform(const Entity& entity, const Vector3& position, const Quat& rotation);
void SetCharacterControllerPosition(const Entity& entity, const Vector3& position);
};
}

View File

@@ -26,7 +26,6 @@ namespace Nuake
bool m_LockZAxis = false;
public:
float _mass;
Matrix4 _transform;
RigidBody();
RigidBody(Vector3 position, Entity handle);

View File

@@ -25,7 +25,6 @@ namespace Nuake
_collisionShape(shape),
_mass(mass),
_entity(entity),
_transform(transform),
_rotation(rotation)
{

View File

@@ -75,7 +75,32 @@ namespace Nuake
return;
InitializeRigidbodies();
auto view = m_Scene->m_Registry.view<TransformComponent, RigidBodyComponent>();
for (auto e : view)
{
auto [transform, rigidBodyComponent] = view.get<TransformComponent, RigidBodyComponent>(e);
Entity ent = Entity({ e, m_Scene });
bool isTrigger = false;
if (!rigidBodyComponent.GetRigidBody())
{
continue;
}
if (rigidBodyComponent.Rigidbody->IsTrigger())
{
auto globaltransform = transform.GetGlobalTransform();
Vector3 translation;
glm::quat rotation;
Vector3 scale;
Vector3 skew;
Vector4 perspective;
glm::decompose(globaltransform, scale, rotation, translation, skew, perspective);
PhysicsManager::Get().SetBodyTransform(ent, globaltransform[3], glm::normalize(transform.GetGlobalRotation()));
}
}
}
void PhysicsSystem::Exit()
@@ -155,7 +180,7 @@ namespace Nuake
Logger::Log("Cannot use mesh collider without model component", "physics", WARNING);
}
auto meshColliderComponent = meshColliderView.get<MeshColliderComponent>(e);
auto& meshColliderComponent = meshColliderView.get<MeshColliderComponent>(e);
const auto& modelComponent = entity.GetComponent<ModelComponent>();
if (modelComponent.ModelResource)

View File

@@ -177,8 +177,8 @@ namespace Nuake
const auto& collisions = physicsManager.GetCollisions();
for (const auto& col : collisions)
{
Entity entity1 = m_Scene->GetEntityByID(col.Entity1);
Entity entity2 = m_Scene->GetEntityByID(col.Entity2);
Entity entity1 = { (entt::entity)col.Entity1, m_Scene };
Entity entity2 = { (entt::entity)col.Entity2, m_Scene };
if (entity1.IsValid() && scriptingEngineNet.HasEntityScriptInstance(entity1))
{

View File

@@ -150,9 +150,6 @@ namespace Nuake
for (auto& type : m_GameAssembly.GetTypes())
{
Logger::Log(std::string("Detected type: ") + std::string(type->GetFullName()), ".net");
Logger::Log(std::string("Detected base type: ") + std::string(type->GetBaseType().GetFullName()), ".net");
const std::string baseTypeName = std::string(type->GetBaseType().GetFullName());
if (baseTypeName == "Nuake.Net.Entity")
{

View File

@@ -21,7 +21,7 @@ namespace Nuake {
private:
const std::string m_Scope = "Nuake.Net";
const std::string m_EngineAssemblyName = "NuakeNet.dll";
const std::string m_NetDirectory = "bin/Debug/net8.0";
const std::string m_NetDirectory = ".net";
const std::string m_ContextName = "NuakeEngineContext";
bool m_IsInitialized = false;