Physics2D/PhysWorld2D: Add RegionQuery method

This commit is contained in:
Lynix 2017-03-30 16:32:29 +02:00
parent 6d0314741a
commit 5ec913311f
2 changed files with 19 additions and 1 deletions

View File

@ -49,6 +49,8 @@ namespace Nz
bool RaycastQuery(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<RaycastHit>* hitInfos);
bool RaycastQueryFirst(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, RaycastHit* hitInfo = nullptr);
void RegionQuery(const Nz::Rectf& boundingBox, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<Nz::RigidBody2D*>* bodies);
void RegisterCallbacks(unsigned int collisionId, const Callback& callbacks);
void RegisterCallbacks(unsigned int collisionIdA, unsigned int collisionIdB, const Callback& callbacks);

View File

@ -83,9 +83,11 @@ namespace Nz
bool PhysWorld2D::RaycastQuery(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<RaycastHit>* hitInfos)
{
using ResultType = decltype(hitInfos);
auto callback = [](cpShape* shape, cpVect point, cpVect normal, cpFloat alpha, void* data)
{
std::vector<RaycastHit>* results = reinterpret_cast<std::vector<RaycastHit>*>(data);
ResultType results = static_cast<ResultType>(data);
RaycastHit hitInfo;
hitInfo.fraction = alpha;
@ -133,6 +135,20 @@ namespace Nz
}
}
void PhysWorld2D::RegionQuery(const Nz::Rectf& boundingBox, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<Nz::RigidBody2D*>* bodies)
{
using ResultType = decltype(bodies);
auto callback = [] (cpShape* shape, void* data)
{
ResultType results = static_cast<ResultType>(data);
results->push_back(static_cast<Nz::RigidBody2D*>(cpShapeGetUserData(shape)));
};
cpShapeFilter filter = cpShapeFilterNew(collisionGroup, categoryMask, collisionMask);
cpSpaceBBQuery(m_handle, cpBBNew(boundingBox.x, boundingBox.y + boundingBox.height, boundingBox.x + boundingBox.width, boundingBox.y), filter, callback, bodies);
}
void PhysWorld2D::RegisterCallbacks(unsigned int collisionId, const Callback& callbacks)
{
InitCallbacks(cpSpaceAddWildcardHandler(m_handle, collisionId), callbacks);