Added trigger rigid bodies

This commit is contained in:
Antoine Pilote
2024-03-16 16:37:06 -04:00
parent 16e12ff37d
commit a5ca094c5f
3 changed files with 35 additions and 10 deletions

View File

@@ -170,12 +170,21 @@ namespace Nuake
//std::cout << "Contact validate callback" << std::endl;
// Allows you to ignore a contact before it is created (using layers to not make objects collide is cheaper!)
return JPH::ValidateResult::AcceptAllContactsForThisBodyPair;
return JPH::ValidateResult::AcceptAllContactsForThisBodyPair;
}
virtual void OnContactAdded(const JPH::Body& inBody1, const JPH::Body& inBody2, const JPH::ContactManifold& inManifold, JPH::ContactSettings& ioSettings) override
{
//std::cout << "A contact was added" << std::endl;
auto entId1 = static_cast<int>(inBody1.GetUserData());
Entity entity1 = Engine::GetCurrentScene()->GetEntityByID(entId1);
auto entId2 = static_cast<int>(inBody2.GetUserData());
Entity entity2 = Engine::GetCurrentScene()->GetEntityByID(entId2);
const std::string entity1Name = entity1.GetComponent<NameComponent>().Name;
const std::string entity2Name = entity2.GetComponent<NameComponent>().Name;
Logger::Log("Collision detected between " + entity1Name + " and " + entity2Name);
}
virtual void OnContactPersisted(const JPH::Body& inBody1, const JPH::Body& inBody2, const JPH::ContactManifold& inManifold, JPH::ContactSettings& ioSettings) override
@@ -314,8 +323,9 @@ namespace Nuake
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, joltRotation, motionType, layer);
JPH::BodyCreationSettings bodySettings(joltShape, joltPos, joltRotation, motionType, layer);
bodySettings.mIsSensor = rb->IsTrigger();
bodySettings.mAllowedDOFs = (JPH::EAllowedDOFs::All);
if (rb->GetLockXAxis())
@@ -372,7 +382,6 @@ namespace Nuake
settings->mPenetrationRecoverySpeed = 1.0f;
settings->mPredictiveContactDistance = 0.01f;
settings->mShape = GetJoltShape(cc->Shape);
auto joltPosition = JPH::Vec3(cc->Position.x, cc->Position.y, cc->Position.z);
const Quat& bodyRotation = cc->Rotation;

View File

@@ -20,6 +20,7 @@ namespace Nuake
Quat _rotation;
Entity _entity;
bool _isTrigger = false;
bool m_LockXAxis = false;
bool m_LockYAxis = false;
bool m_LockZAxis = false;
@@ -33,13 +34,16 @@ namespace Nuake
void UpdateTransform();
void SetIsTrigger(bool isTrigger) { _isTrigger = isTrigger; }
bool IsTrigger() const { return _isTrigger; }
bool GetLockXAxis() const { return m_LockXAxis; }
bool GetLockYAxis() const { return m_LockYAxis; }
bool GetLockZAxis() const { return m_LockZAxis; }
void setLockXAxis(bool lock) { m_LockXAxis = lock; }
void setLockYAxis(bool lock) { m_LockYAxis = lock; }
void setLockZAxis(bool lock) { m_LockZAxis = lock; }
void SetLockXAxis(bool lock) { m_LockXAxis = lock; }
void SetLockYAxis(bool lock) { m_LockYAxis = lock; }
void SetLockZAxis(bool lock) { m_LockZAxis = lock; }
void SetEntityID(Entity ent);
Vector3 GetPosition() const { return _position; }

View File

@@ -219,6 +219,8 @@ namespace Nuake
Entity ent = Entity({ e, m_Scene });
Ref<Physics::RigidBody> rigidBody;
Ref<Physics::PhysicShape> shape;
bool isTrigger = false;
if (rigidBodyComponent.GetRigidBody())
{
continue;
@@ -227,6 +229,7 @@ namespace Nuake
if (ent.HasComponent<BoxColliderComponent>())
{
BoxColliderComponent& boxComponent = ent.GetComponent<BoxColliderComponent>();
isTrigger = boxComponent.IsTrigger;
shape = CreateRef<Physics::Box>(boxComponent.Size);
}
@@ -235,6 +238,7 @@ namespace Nuake
auto& capsuleComponent = ent.GetComponent<CapsuleColliderComponent>();
float radius = capsuleComponent.Radius;
float height = capsuleComponent.Height;
isTrigger = capsuleComponent.IsTrigger;
shape = CreateRef<Physics::Capsule>(radius, height);
}
@@ -243,12 +247,14 @@ namespace Nuake
auto& cylinderComponent = ent.GetComponent<CylinderColliderComponent>();
float radius = cylinderComponent.Radius;
float height = cylinderComponent.Height;
isTrigger = cylinderComponent.IsTrigger;
shape = CreateRef<Physics::Cylinder>(radius, height);
}
if (ent.HasComponent<SphereColliderComponent>())
{
const auto& component = ent.GetComponent<SphereColliderComponent>();
isTrigger = component.IsTrigger;
shape = CreateRef<Physics::Sphere>(component.Radius);
}
@@ -262,6 +268,8 @@ namespace Nuake
const auto& modelComponent = ent.GetComponent<ModelComponent>();
const auto& component = ent.GetComponent<MeshColliderComponent>();
isTrigger = component.IsTrigger;
if (modelComponent.ModelResource)
{
uint32_t subMeshId = component.SubMesh;
@@ -282,10 +290,14 @@ namespace Nuake
}
rigidBody = CreateRef<Physics::RigidBody>(rigidBodyComponent.Mass, transform.GetGlobalPosition(), transform.GetGlobalRotation(), transform.GetGlobalTransform(), shape, ent);
rigidBody->setLockXAxis(rigidBodyComponent.LockX);
rigidBody->setLockYAxis(rigidBodyComponent.LockY);
rigidBody->setLockZAxis(rigidBodyComponent.LockZ);
rigidBody->SetLockXAxis(rigidBodyComponent.LockX);
rigidBody->SetLockYAxis(rigidBodyComponent.LockY);
rigidBody->SetLockZAxis(rigidBodyComponent.LockZ);
rigidBody->SetIsTrigger(isTrigger);
PhysicsManager::Get().RegisterBody(rigidBody);
rigidBodyComponent.Rigidbody = rigidBody;
}
}