Added World:getFixturesInArea()

This commit is contained in:
Klonan
2022-08-27 17:12:19 +02:00
parent bded15ff2a
commit 03939299db
4 changed files with 68 additions and 3 deletions
+36
View File
@@ -171,6 +171,28 @@ bool World::QueryCallback::ReportFixture(b2Fixture *fixture)
return true;
}
World::CollectCallback::CollectCallback(World *world, lua_State *L)
: world(world)
, L(L)
{
lua_newtable(L);
}
World::CollectCallback::~CollectCallback()
{
}
bool World::CollectCallback::ReportFixture(b2Fixture *f)
{
Fixture* fixture = (Fixture*)world->findObject(f);
if (!fixture)
throw love::Exception("A fixture has escaped Memoizer!");
luax_pushtype(L, fixture);
lua_rawseti(L, -2, i);
i++;
return true;
}
World::RayCastCallback::RayCastCallback(World *world, lua_State *L, int idx)
: world(world)
, L(L)
@@ -556,6 +578,20 @@ int World::queryBoundingBox(lua_State *L)
return 0;
}
int World::getFixturesInArea(lua_State *L)
{
float lx = (float)luaL_checknumber(L, 1);
float ly = (float)luaL_checknumber(L, 2);
float ux = (float)luaL_checknumber(L, 3);
float uy = (float)luaL_checknumber(L, 4);
b2AABB box;
box.lowerBound = Physics::scaleDown(b2Vec2(lx, ly));
box.upperBound = Physics::scaleDown(b2Vec2(ux, uy));
CollectCallback query(this, L);
world->QueryAABB(&query, box);
return 1;
}
int World::rayCast(lua_State *L)
{
float x1 = (float)luaL_checknumber(L, 1);
+19 -1
View File
@@ -102,6 +102,18 @@ public:
int funcidx;
};
class CollectCallback : public b2QueryCallback
{
public:
CollectCallback(World *world, lua_State *L);
~CollectCallback();
virtual bool ReportFixture(b2Fixture *fixture);
private:
World *world;
lua_State *L;
int i = 1;
};
class RayCastCallback : public b2RayCastCallback
{
public:
@@ -270,10 +282,16 @@ public:
b2Body *getGroundBody() const;
/**
* Gets all fixtures that overlap a given bounding box.
* Calls a callback on all fixtures that overlap a given bounding box.
**/
int queryBoundingBox(lua_State *L);
/**
* Gets all fixtures that overlap a given bounding box.
**/
int getFixturesInArea(lua_State *L);
/**
* Raycasts the World for all Fixtures in the path of the ray.
**/
+10
View File
@@ -185,6 +185,15 @@ int w_World_queryBoundingBox(lua_State *L)
return t->queryBoundingBox(L);
}
int w_World_getFixturesInArea(lua_State *L)
{
World *t = luax_checkworld(L, 1);
lua_remove(L, 1);
int ret = 0;
luax_catchexcept(L, [&](){ ret = t->getFixturesInArea(L); });
return ret;
}
int w_World_rayCast(lua_State *L)
{
World *t = luax_checkworld(L, 1);
@@ -228,6 +237,7 @@ static const luaL_Reg w_World_functions[] =
{ "getJoints", w_World_getJoints },
{ "getContacts", w_World_getContacts },
{ "queryBoundingBox", w_World_queryBoundingBox },
{ "getFixturesInArea", w_World_getFixturesInArea },
{ "rayCast", w_World_rayCast },
{ "destroy", w_World_destroy },
{ "isDestroyed", w_World_isDestroyed },