This commit is contained in:
Lynix
2018-08-08 10:34:32 +02:00
7 changed files with 361 additions and 18 deletions

View File

@@ -56,7 +56,7 @@ namespace Ndk
NazaraAssert(entityWorld, "Entity must have world");
NazaraAssert(entityWorld->HasSystem<PhysicsSystem2D>(), "World must have a physics system");
Nz::PhysWorld2D& physWorld = entityWorld->GetSystem<PhysicsSystem2D>().GetWorld();
Nz::PhysWorld2D& physWorld = entityWorld->GetSystem<PhysicsSystem2D>().GetPhysWorld();
m_staticBody = std::make_unique<Nz::RigidBody2D>(&physWorld, 0.f, m_geom);
m_staticBody->SetUserdata(reinterpret_cast<void*>(static_cast<std::ptrdiff_t>(m_entity->GetId())));

View File

@@ -29,7 +29,7 @@ namespace Ndk
World* entityWorld = m_entity->GetWorld();
NazaraAssert(entityWorld->HasSystem<PhysicsSystem2D>(), "World must have a 2D physics system");
Nz::PhysWorld2D& world = entityWorld->GetSystem<PhysicsSystem2D>().GetWorld();
Nz::PhysWorld2D& world = entityWorld->GetSystem<PhysicsSystem2D>().GetPhysWorld();
Nz::Collider2DRef geom;
if (m_entity->HasComponent<CollisionComponent2D>())

View File

@@ -4,6 +4,7 @@
#include <NDK/Systems/PhysicsSystem2D.hpp>
#include <Nazara/Physics2D/RigidBody2D.hpp>
#include <NDK/World.hpp>
#include <NDK/Components/CollisionComponent2D.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/PhysicsComponent2D.hpp>
@@ -33,9 +34,108 @@ namespace Ndk
void PhysicsSystem2D::CreatePhysWorld() const
{
NazaraAssert(!m_world, "Physics world should not be created twice");
NazaraAssert(!m_physWorld, "Physics world should not be created twice");
m_world = std::make_unique<Nz::PhysWorld2D>();
m_physWorld = std::make_unique<Nz::PhysWorld2D>();
}
void PhysicsSystem2D::DebugDraw(const DebugDrawOptions& options, bool drawShapes, bool drawConstraints, bool drawCollisions)
{
Nz::PhysWorld2D::DebugDrawOptions worldOptions{ options.constraintColor, options.collisionPointColor, options.shapeOutlineColor };
if (options.colorCallback)
{
worldOptions.colorCallback = [&options, this](Nz::RigidBody2D& body, std::size_t shapeIndex, void* userdata)
{
return options.colorCallback(GetEntityFromBody(body), shapeIndex, userdata);
};
}
worldOptions.circleCallback = options.circleCallback;
worldOptions.dotCallback = options.dotCallback;
worldOptions.polygonCallback = options.polygonCallback;
worldOptions.segmentCallback = options.segmentCallback;
worldOptions.thickSegmentCallback = options.thickSegmentCallback;
worldOptions.userdata = options.userdata;
m_physWorld->DebugDraw(worldOptions, drawShapes, drawConstraints, drawCollisions);
}
const EntityHandle& PhysicsSystem2D::GetEntityFromBody(const Nz::RigidBody2D& body) const
{
auto entityId = static_cast<EntityId>(reinterpret_cast<std::uintptr_t>(body.GetUserdata()));
auto& world = GetWorld();
NazaraAssert(world.IsEntityIdValid(entityId), "All Bodies of this world must be part of the physics world by using PhysicsComponent");
return world.GetEntity(entityId);
}
bool PhysicsSystem2D::NearestBodyQuery(const Nz::Vector2f& from, float maxDistance, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, EntityHandle* nearestBody)
{
Nz::RigidBody2D* body;
bool res = m_physWorld->NearestBodyQuery(from, maxDistance, collisionGroup, categoryMask, collisionMask, &body);
(*nearestBody) = GetEntityFromBody(*body);
return res;
}
bool PhysicsSystem2D::NearestBodyQuery(const Nz::Vector2f& from, float maxDistance, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, NearestQueryResult* result)
{
Nz::PhysWorld2D::NearestQueryResult queryResult;
bool res = m_physWorld->NearestBodyQuery(from, maxDistance, collisionGroup, categoryMask, collisionMask, &queryResult);
result->nearestBody = GetEntityFromBody(*queryResult.nearestBody);
result->closestPoint = std::move(queryResult.closestPoint);
result->fraction = std::move(queryResult.fraction);
result->distance = queryResult.distance;
return res;
}
bool PhysicsSystem2D::RaycastQuery(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<RaycastHit>* hitInfos)
{
std::vector<Nz::PhysWorld2D::RaycastHit> queryResult;
bool res = m_physWorld->RaycastQuery(from, to, radius, collisionGroup, categoryMask, collisionMask, &queryResult);
for (auto& hitResult : queryResult)
{
hitInfos->push_back({
GetEntityFromBody(*hitResult.nearestBody),
std::move(hitResult.hitPos),
std::move(hitResult.hitNormal),
hitResult.fraction
});
}
return res;
}
bool PhysicsSystem2D::RaycastQueryFirst(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, RaycastHit* hitInfo)
{
Nz::PhysWorld2D::RaycastHit queryResult;
bool res = m_physWorld->RaycastQueryFirst(from, to, radius, collisionGroup, categoryMask, collisionMask, &queryResult);
hitInfo->body = GetEntityFromBody(*queryResult.nearestBody);
hitInfo->hitPos = std::move(queryResult.hitPos);
hitInfo->hitNormal = std::move(queryResult.hitNormal);
hitInfo->fraction = queryResult.fraction;
return res;
}
void PhysicsSystem2D::RegionQuery(const Nz::Rectf& boundingBox, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<EntityHandle>* bodies)
{
std::vector<Nz::RigidBody2D*> queryResult;
m_physWorld->RegionQuery(boundingBox, collisionGroup, categoryMask, collisionMask, &queryResult);
for (auto& body : queryResult)
{
bodies->emplace_back(GetEntityFromBody(*body));
}
}
/*!
@@ -58,7 +158,7 @@ namespace Ndk
auto& entities = (entity->HasComponent<PhysicsComponent2D>()) ? m_dynamicObjects : m_staticObjects;
entities.Insert(entity);
if (!m_world)
if (!m_physWorld)
CreatePhysWorld();
}
@@ -70,10 +170,10 @@ namespace Ndk
void PhysicsSystem2D::OnUpdate(float elapsedTime)
{
if (!m_world)
if (!m_physWorld)
return;
m_world->Step(elapsedTime);
m_physWorld->Step(elapsedTime);
for (const Ndk::EntityHandle& entity : m_dynamicObjects)
{
@@ -124,5 +224,81 @@ namespace Ndk
}
}
void PhysicsSystem2D::RegisterCallbacks(unsigned int collisionId, const Callback& callbacks)
{
Nz::PhysWorld2D::Callback worldCallbacks;
if (callbacks.endCallback)
{
worldCallbacks.endCallback = [&callbacks, this](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
{
callbacks.endCallback(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
};
}
if (callbacks.preSolveCallback)
{
worldCallbacks.preSolveCallback = [&callbacks, this](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
{
return callbacks.preSolveCallback(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
};
}
if (callbacks.postSolveCallback)
{
worldCallbacks.postSolveCallback = [&callbacks, this](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
{
callbacks.preSolveCallback(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
};
}
if (callbacks.startCallback)
{
worldCallbacks.startCallback = [&callbacks, this](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
{
return callbacks.startCallback(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
};
}
worldCallbacks.userdata = callbacks.userdata;
m_physWorld->RegisterCallbacks(collisionId, worldCallbacks);
}
void PhysicsSystem2D::RegisterCallbacks(unsigned int collisionIdA, unsigned int collisionIdB, const Callback& callbacks)
{
Nz::PhysWorld2D::Callback worldCallbacks{};
if (callbacks.endCallback)
{
worldCallbacks.endCallback = [&callbacks, this](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
{
callbacks.endCallback(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
};
}
if (callbacks.preSolveCallback)
{
worldCallbacks.preSolveCallback = [&callbacks, this](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
{
return callbacks.preSolveCallback(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
};
}
if (callbacks.postSolveCallback)
{
worldCallbacks.postSolveCallback = [&callbacks, this](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
{
callbacks.preSolveCallback(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
};
}
if (callbacks.startCallback)
{
worldCallbacks.startCallback = [&callbacks, this](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
{
return callbacks.startCallback(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
};
}
worldCallbacks.userdata = callbacks.userdata;
m_physWorld->RegisterCallbacks(collisionIdA, collisionIdB, worldCallbacks);
}
SystemIndex PhysicsSystem2D::systemIndex;
}