mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
love.physics: move internal box2d->love object map to World instances, and clean up some physics object code. Resolves issue #1465.
This commit is contained in:
@@ -1,50 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2019 LOVE Development Team
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#include "Memoizer.h"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace love
|
||||
{
|
||||
|
||||
static std::unordered_map<void *, void *> objectMap;
|
||||
|
||||
void Memoizer::add(void *key, void *val)
|
||||
{
|
||||
objectMap[key] = val;
|
||||
}
|
||||
|
||||
void Memoizer::remove(void *key)
|
||||
{
|
||||
objectMap.erase(key);
|
||||
}
|
||||
|
||||
void *Memoizer::find(void *key)
|
||||
{
|
||||
auto it = objectMap.find(key);
|
||||
|
||||
if (it != objectMap.end())
|
||||
return it->second;
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // love
|
||||
@@ -1,39 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2006-2019 LOVE Development Team
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
**/
|
||||
|
||||
#ifndef LOVE_MEMOIZER_H
|
||||
#define LOVE_MEMOIZER_H
|
||||
|
||||
namespace love
|
||||
{
|
||||
|
||||
class Memoizer
|
||||
{
|
||||
public:
|
||||
|
||||
static void add(void *key, void *val);
|
||||
static void remove(void *key);
|
||||
static void *find(void *key);
|
||||
|
||||
}; // Memoizer
|
||||
|
||||
} // love
|
||||
|
||||
#endif // LOVE_MEMOIZER_H
|
||||
@@ -21,7 +21,6 @@
|
||||
#include "Body.h"
|
||||
|
||||
#include "common/math.h"
|
||||
#include "common/Memoizer.h"
|
||||
|
||||
#include "Shape.h"
|
||||
#include "Fixture.h"
|
||||
@@ -51,18 +50,7 @@ Body::Body(World *world, b2Vec2 p, Body::Type type)
|
||||
// Box2D body holds a reference to the love Body.
|
||||
this->retain();
|
||||
this->setType(type);
|
||||
Memoizer::add(body, this);
|
||||
}
|
||||
|
||||
Body::Body(b2Body *b)
|
||||
: body(b)
|
||||
, udata(nullptr)
|
||||
{
|
||||
udata = (bodyudata *) b->GetUserData();
|
||||
world = (World *) Memoizer::find(b->GetWorld());
|
||||
// Box2D body holds a reference to the love Body.
|
||||
this->retain();
|
||||
Memoizer::add(body, this);
|
||||
world->registerObject(body, this);
|
||||
}
|
||||
|
||||
Body::~Body()
|
||||
@@ -452,7 +440,7 @@ int Body::getFixtures(lua_State *L) const
|
||||
{
|
||||
if (!f)
|
||||
break;
|
||||
Fixture *fixture = (Fixture *)Memoizer::find(f);
|
||||
Fixture *fixture = (Fixture *)world->findObject(f);
|
||||
if (!fixture)
|
||||
throw love::Exception("A fixture has escaped Memoizer!");
|
||||
luax_pushtype(L, fixture);
|
||||
@@ -474,7 +462,7 @@ int Body::getJoints(lua_State *L) const
|
||||
if (!je)
|
||||
break;
|
||||
|
||||
Joint *joint = (Joint *) Memoizer::find(je->joint);
|
||||
Joint *joint = (Joint *) world->findObject(je->joint);
|
||||
if (!joint)
|
||||
throw love::Exception("A joint has escaped Memoizer!");
|
||||
|
||||
@@ -497,9 +485,9 @@ int Body::getContacts(lua_State *L) const
|
||||
if (!ce)
|
||||
break;
|
||||
|
||||
Contact *contact = (Contact *) Memoizer::find(ce->contact);
|
||||
Contact *contact = (Contact *) world->findObject(ce->contact);
|
||||
if (!contact)
|
||||
contact = new Contact(ce->contact);
|
||||
contact = new Contact(world, ce->contact);
|
||||
else
|
||||
contact->retain();
|
||||
|
||||
@@ -523,7 +511,7 @@ void Body::destroy()
|
||||
}
|
||||
|
||||
world->world->DestroyBody(body);
|
||||
Memoizer::remove(body);
|
||||
world->unregisterObject(body);
|
||||
body = NULL;
|
||||
|
||||
// Remove userdata reference to avoid it sticking around after GC
|
||||
|
||||
@@ -77,11 +77,6 @@ public:
|
||||
**/
|
||||
Body(World *world, b2Vec2 p, Type type);
|
||||
|
||||
/**
|
||||
* Create a Body from an extant b2Body.
|
||||
**/
|
||||
Body(b2Body *b);
|
||||
|
||||
virtual ~Body();
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
#include "World.h"
|
||||
#include "Physics.h"
|
||||
|
||||
#include "common/Memoizer.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace physics
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
#include "World.h"
|
||||
#include "Physics.h"
|
||||
|
||||
#include "common/Memoizer.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace physics
|
||||
|
||||
@@ -22,8 +22,6 @@
|
||||
#include "World.h"
|
||||
#include "Physics.h"
|
||||
|
||||
#include "common/Memoizer.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace physics
|
||||
@@ -33,10 +31,11 @@ namespace box2d
|
||||
|
||||
love::Type Contact::type("Contact", &Object::type);
|
||||
|
||||
Contact::Contact(b2Contact *contact)
|
||||
Contact::Contact(World *world, b2Contact *contact)
|
||||
: contact(contact)
|
||||
, world(world)
|
||||
{
|
||||
Memoizer::add(contact, this);
|
||||
world->registerObject(contact, this);
|
||||
}
|
||||
|
||||
Contact::~Contact()
|
||||
@@ -48,14 +47,14 @@ void Contact::invalidate()
|
||||
{
|
||||
if (contact != NULL)
|
||||
{
|
||||
Memoizer::remove(contact);
|
||||
world->unregisterObject(contact);
|
||||
contact = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool Contact::isValid()
|
||||
{
|
||||
return contact != NULL ? true : false;
|
||||
return contact != NULL;
|
||||
}
|
||||
|
||||
int Contact::getPositions(lua_State *L)
|
||||
@@ -146,8 +145,8 @@ void Contact::getChildren(int &childA, int &childB)
|
||||
|
||||
void Contact::getFixtures(Fixture *&fixtureA, Fixture *&fixtureB)
|
||||
{
|
||||
fixtureA = (Fixture *) Memoizer::find(contact->GetFixtureA());
|
||||
fixtureB = (Fixture *) Memoizer::find(contact->GetFixtureB());
|
||||
fixtureA = (Fixture *) world->findObject(contact->GetFixtureA());
|
||||
fixtureB = (Fixture *) world->findObject(contact->GetFixtureB());
|
||||
|
||||
if (!fixtureA || !fixtureB)
|
||||
throw love::Exception("A fixture has escaped Memoizer!");
|
||||
|
||||
@@ -57,7 +57,7 @@ public:
|
||||
* data pointed to.
|
||||
* @param contact Pointer to the Box2D contact.
|
||||
**/
|
||||
Contact(b2Contact *contact);
|
||||
Contact(World *world, b2Contact *contact);
|
||||
|
||||
virtual ~Contact();
|
||||
|
||||
@@ -159,6 +159,8 @@ private:
|
||||
|
||||
// The Box2D contact.
|
||||
b2Contact *contact;
|
||||
|
||||
World *world;
|
||||
};
|
||||
|
||||
} // box2d
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
#include "World.h"
|
||||
#include "Physics.h"
|
||||
|
||||
#include "common/Memoizer.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace physics
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
#include "World.h"
|
||||
#include "Physics.h"
|
||||
|
||||
#include "common/Memoizer.h"
|
||||
|
||||
// STD
|
||||
#include <bitset>
|
||||
|
||||
@@ -51,18 +49,7 @@ Fixture::Fixture(Body *body, Shape *shape, float density)
|
||||
def.density = density;
|
||||
fixture = body->body->CreateFixture(&def);
|
||||
this->retain();
|
||||
Memoizer::add(fixture, this);
|
||||
}
|
||||
|
||||
Fixture::Fixture(b2Fixture *f)
|
||||
: fixture(f)
|
||||
{
|
||||
udata = (fixtureudata *)f->GetUserData();
|
||||
body = (Body *)Memoizer::find(f->GetBody());
|
||||
if (!body)
|
||||
body = new Body(f->GetBody());
|
||||
this->retain();
|
||||
Memoizer::add(fixture, this);
|
||||
body->world->registerObject(fixture, this);
|
||||
}
|
||||
|
||||
Fixture::~Fixture()
|
||||
@@ -361,7 +348,7 @@ void Fixture::destroy(bool implicit)
|
||||
|
||||
if (!implicit && fixture != nullptr)
|
||||
body->body->DestroyFixture(fixture);
|
||||
Memoizer::remove(fixture);
|
||||
body->world->unregisterObject(fixture);
|
||||
fixture = nullptr;
|
||||
|
||||
// Remove userdata reference to avoid it sticking around after GC
|
||||
|
||||
@@ -38,6 +38,8 @@ namespace physics
|
||||
namespace box2d
|
||||
{
|
||||
|
||||
class World;
|
||||
|
||||
/**
|
||||
* This struct is stored in a void pointer
|
||||
* in the Box2D Fixture class. For now, all we
|
||||
@@ -68,11 +70,6 @@ public:
|
||||
**/
|
||||
Fixture(Body *body, Shape *shape, float density);
|
||||
|
||||
/**
|
||||
* Creates a Fixture.
|
||||
**/
|
||||
Fixture(b2Fixture *f);
|
||||
|
||||
virtual ~Fixture();
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
// Module
|
||||
#include "Body.h"
|
||||
#include "World.h"
|
||||
#include "common/Memoizer.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
@@ -69,7 +68,7 @@ Joint *GearJoint::getJointA() const
|
||||
if (b2joint == nullptr)
|
||||
return nullptr;
|
||||
|
||||
Joint *j = (Joint *) Memoizer::find(b2joint);
|
||||
Joint *j = (Joint *) world->findObject(b2joint);
|
||||
if (j == nullptr)
|
||||
throw love::Exception("A joint has escaped Memoizer!");
|
||||
|
||||
@@ -82,7 +81,7 @@ Joint *GearJoint::getJointB() const
|
||||
if (b2joint == nullptr)
|
||||
return nullptr;
|
||||
|
||||
Joint *j = (Joint *) Memoizer::find(b2joint);
|
||||
Joint *j = (Joint *) world->findObject(b2joint);
|
||||
if (j == nullptr)
|
||||
throw love::Exception("A joint has escaped Memoizer!");
|
||||
|
||||
|
||||
@@ -23,9 +23,6 @@
|
||||
// STD
|
||||
#include <bitset>
|
||||
|
||||
// LOVE
|
||||
#include "common/Memoizer.h"
|
||||
|
||||
// Module
|
||||
#include "Body.h"
|
||||
#include "World.h"
|
||||
@@ -107,7 +104,7 @@ Body *Joint::getBodyA() const
|
||||
if (b2body == nullptr)
|
||||
return nullptr;
|
||||
|
||||
Body *body = (Body *) Memoizer::find(b2body);
|
||||
Body *body = (Body *) world->findObject(b2body);
|
||||
if (body == nullptr)
|
||||
throw love::Exception("A body has escaped Memoizer!");
|
||||
|
||||
@@ -120,7 +117,7 @@ Body *Joint::getBodyB() const
|
||||
if (b2body == nullptr)
|
||||
return nullptr;
|
||||
|
||||
Body *body = (Body *) Memoizer::find(b2body);
|
||||
Body *body = (Body *) world->findObject(b2body);
|
||||
if (body == nullptr)
|
||||
throw love::Exception("A body has escaped Memoizer!");
|
||||
|
||||
@@ -129,7 +126,7 @@ Body *Joint::getBodyB() const
|
||||
|
||||
bool Joint::isValid() const
|
||||
{
|
||||
return joint != 0;
|
||||
return joint != nullptr;
|
||||
}
|
||||
|
||||
int Joint::getAnchors(lua_State *L)
|
||||
@@ -159,7 +156,7 @@ b2Joint *Joint::createJoint(b2JointDef *def)
|
||||
{
|
||||
def->userData = udata;
|
||||
joint = world->world->CreateJoint(def);
|
||||
Memoizer::add(joint, this);
|
||||
world->registerObject(joint, this);
|
||||
// Box2D joint has a reference to this love Joint.
|
||||
this->retain();
|
||||
return joint;
|
||||
@@ -175,9 +172,9 @@ void Joint::destroyJoint(bool implicit)
|
||||
return;
|
||||
}
|
||||
|
||||
if (!implicit && joint != 0)
|
||||
if (!implicit && joint != nullptr)
|
||||
world->world->DestroyJoint(joint);
|
||||
Memoizer::remove(joint);
|
||||
world->unregisterObject(joint);
|
||||
joint = NULL;
|
||||
|
||||
// Remove userdata reference to avoid it sticking around after GC
|
||||
|
||||
@@ -153,7 +153,7 @@ private:
|
||||
// The Box2D joint object.
|
||||
b2Joint *joint;
|
||||
|
||||
};
|
||||
}; // Joint
|
||||
|
||||
} // box2d
|
||||
} // physics
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
#include "World.h"
|
||||
#include "Physics.h"
|
||||
|
||||
#include "common/Memoizer.h"
|
||||
|
||||
namespace love
|
||||
{
|
||||
namespace physics
|
||||
|
||||
@@ -25,8 +25,6 @@
|
||||
#include "World.h"
|
||||
#include "Physics.h"
|
||||
|
||||
#include "common/Memoizer.h"
|
||||
|
||||
// STD
|
||||
#include <bitset>
|
||||
|
||||
@@ -47,17 +45,12 @@ Shape::Shape(b2Shape *shape, bool own)
|
||||
: shape(shape)
|
||||
, own(own)
|
||||
{
|
||||
if (own)
|
||||
Memoizer::add(shape, this);
|
||||
}
|
||||
|
||||
Shape::~Shape()
|
||||
{
|
||||
if (shape && own)
|
||||
{
|
||||
Memoizer::remove(shape);
|
||||
delete shape;
|
||||
}
|
||||
shape = nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include "Shape.h"
|
||||
#include "Contact.h"
|
||||
#include "Physics.h"
|
||||
#include "common/Memoizer.h"
|
||||
#include "common/Reference.h"
|
||||
|
||||
namespace love
|
||||
@@ -36,9 +35,10 @@ namespace box2d
|
||||
|
||||
love::Type World::type("World", &Object::type);
|
||||
|
||||
World::ContactCallback::ContactCallback()
|
||||
World::ContactCallback::ContactCallback(World *world)
|
||||
: ref(nullptr)
|
||||
, L(nullptr)
|
||||
, world(world)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ void World::ContactCallback::process(b2Contact *contact, const b2ContactImpulse
|
||||
|
||||
// Push first fixture.
|
||||
{
|
||||
Fixture *a = (Fixture *)Memoizer::find(contact->GetFixtureA());
|
||||
Fixture *a = (Fixture *)world->findObject(contact->GetFixtureA());
|
||||
if (a != nullptr)
|
||||
luax_pushtype(L, a);
|
||||
else
|
||||
@@ -66,16 +66,16 @@ void World::ContactCallback::process(b2Contact *contact, const b2ContactImpulse
|
||||
|
||||
// Push second fixture.
|
||||
{
|
||||
Fixture *b = (Fixture *)Memoizer::find(contact->GetFixtureB());
|
||||
Fixture *b = (Fixture *)world->findObject(contact->GetFixtureB());
|
||||
if (b != nullptr)
|
||||
luax_pushtype(L, b);
|
||||
else
|
||||
throw love::Exception("A fixture has escaped Memoizer!");
|
||||
}
|
||||
|
||||
Contact *cobj = (Contact *)Memoizer::find(contact);
|
||||
Contact *cobj = (Contact *)world->findObject(contact);
|
||||
if (!cobj)
|
||||
cobj = new Contact(contact);
|
||||
cobj = new Contact(world, contact);
|
||||
else
|
||||
cobj->retain();
|
||||
|
||||
@@ -138,8 +138,9 @@ bool World::ContactFilter::process(Fixture *a, Fixture *b)
|
||||
return true;
|
||||
}
|
||||
|
||||
World::QueryCallback::QueryCallback(lua_State *L, int idx)
|
||||
: L(L)
|
||||
World::QueryCallback::QueryCallback(World *world, lua_State *L, int idx)
|
||||
: world(world)
|
||||
, L(L)
|
||||
, funcidx(idx)
|
||||
{
|
||||
luaL_checktype(L, funcidx, LUA_TFUNCTION);
|
||||
@@ -154,7 +155,7 @@ bool World::QueryCallback::ReportFixture(b2Fixture *fixture)
|
||||
if (L != nullptr)
|
||||
{
|
||||
lua_pushvalue(L, funcidx);
|
||||
Fixture *f = (Fixture *)Memoizer::find(fixture);
|
||||
Fixture *f = (Fixture *)world->findObject(fixture);
|
||||
if (!f)
|
||||
throw love::Exception("A fixture has escaped Memoizer!");
|
||||
luax_pushtype(L, f);
|
||||
@@ -167,8 +168,9 @@ bool World::QueryCallback::ReportFixture(b2Fixture *fixture)
|
||||
return true;
|
||||
}
|
||||
|
||||
World::RayCastCallback::RayCastCallback(lua_State *L, int idx)
|
||||
: L(L)
|
||||
World::RayCastCallback::RayCastCallback(World *world, lua_State *L, int idx)
|
||||
: world(world)
|
||||
, L(L)
|
||||
, funcidx(idx)
|
||||
{
|
||||
luaL_checktype(L, funcidx, LUA_TFUNCTION);
|
||||
@@ -183,7 +185,7 @@ float32 World::RayCastCallback::ReportFixture(b2Fixture *fixture, const b2Vec2 &
|
||||
if (L != nullptr)
|
||||
{
|
||||
lua_pushvalue(L, funcidx);
|
||||
Fixture *f = (Fixture *)Memoizer::find(fixture);
|
||||
Fixture *f = (Fixture *)world->findObject(fixture);
|
||||
if (!f)
|
||||
throw love::Exception("A fixture has escaped Memoizer!");
|
||||
luax_pushtype(L, f);
|
||||
@@ -206,14 +208,14 @@ float32 World::RayCastCallback::ReportFixture(b2Fixture *fixture, const b2Vec2 &
|
||||
|
||||
void World::SayGoodbye(b2Fixture *fixture)
|
||||
{
|
||||
Fixture *f = (Fixture *)Memoizer::find(fixture);
|
||||
Fixture *f = (Fixture *)findObject(fixture);
|
||||
// Hint implicit destruction with true.
|
||||
if (f) f->destroy(true);
|
||||
}
|
||||
|
||||
void World::SayGoodbye(b2Joint *joint)
|
||||
{
|
||||
Joint *j = (Joint *)Memoizer::find(joint);
|
||||
Joint *j = (Joint *)findObject(joint);
|
||||
// Hint implicit destruction with true.
|
||||
if (j) j->destroyJoint(true);
|
||||
}
|
||||
@@ -221,6 +223,10 @@ void World::SayGoodbye(b2Joint *joint)
|
||||
World::World()
|
||||
: world(nullptr)
|
||||
, destructWorld(false)
|
||||
, begin(this)
|
||||
, end(this)
|
||||
, presolve(this)
|
||||
, postsolve(this)
|
||||
{
|
||||
world = new b2World(b2Vec2(0,0));
|
||||
world->SetAllowSleeping(true);
|
||||
@@ -229,12 +235,16 @@ World::World()
|
||||
world->SetDestructionListener(this);
|
||||
b2BodyDef def;
|
||||
groundBody = world->CreateBody(&def);
|
||||
Memoizer::add(world, this);
|
||||
registerObject(world, this);
|
||||
}
|
||||
|
||||
World::World(b2Vec2 gravity, bool sleep)
|
||||
: world(nullptr)
|
||||
, destructWorld(false)
|
||||
, begin(this)
|
||||
, end(this)
|
||||
, presolve(this)
|
||||
, postsolve(this)
|
||||
{
|
||||
world = new b2World(Physics::scaleDown(gravity));
|
||||
world->SetAllowSleeping(sleep);
|
||||
@@ -243,7 +253,7 @@ World::World(b2Vec2 gravity, bool sleep)
|
||||
world->SetDestructionListener(this);
|
||||
b2BodyDef def;
|
||||
groundBody = world->CreateBody(&def);
|
||||
Memoizer::add(world, this);
|
||||
registerObject(world, this);
|
||||
}
|
||||
|
||||
World::~World()
|
||||
@@ -297,8 +307,8 @@ void World::EndContact(b2Contact *contact)
|
||||
end.process(contact);
|
||||
|
||||
// Letting the Contact know that the b2Contact will be destroyed any second.
|
||||
Contact *c = (Contact *)Memoizer::find(contact);
|
||||
if (c != NULL)
|
||||
Contact *c = (Contact *)findObject(contact);
|
||||
if (c != nullptr)
|
||||
c->invalidate();
|
||||
}
|
||||
|
||||
@@ -316,8 +326,8 @@ void World::PostSolve(b2Contact *contact, const b2ContactImpulse *impulse)
|
||||
bool World::ShouldCollide(b2Fixture *fixtureA, b2Fixture *fixtureB)
|
||||
{
|
||||
// Fixtures should be memoized, if we created them
|
||||
Fixture *a = (Fixture *)Memoizer::find(fixtureA);
|
||||
Fixture *b = (Fixture *)Memoizer::find(fixtureB);
|
||||
Fixture *a = (Fixture *)findObject(fixtureA);
|
||||
Fixture *b = (Fixture *)findObject(fixtureB);
|
||||
if (!a || !b)
|
||||
throw love::Exception("A fixture has escaped Memoizer!");
|
||||
return filter.process(a, b);
|
||||
@@ -472,7 +482,7 @@ int World::getBodies(lua_State *L) const
|
||||
break;
|
||||
if (b == groundBody)
|
||||
continue;
|
||||
Body *body = (Body *)Memoizer::find(b);
|
||||
Body *body = (Body *)findObject(b);
|
||||
if (!body)
|
||||
throw love::Exception("A body has escaped Memoizer!");
|
||||
luax_pushtype(L, body);
|
||||
@@ -491,7 +501,7 @@ int World::getJoints(lua_State *L) const
|
||||
do
|
||||
{
|
||||
if (!j) break;
|
||||
Joint *joint = (Joint *)Memoizer::find(j);
|
||||
Joint *joint = (Joint *)findObject(j);
|
||||
if (!joint) throw love::Exception("A joint has escaped Memoizer!");
|
||||
luax_pushtype(L, joint);
|
||||
lua_rawseti(L, -2, i);
|
||||
@@ -501,7 +511,7 @@ int World::getJoints(lua_State *L) const
|
||||
return 1;
|
||||
}
|
||||
|
||||
int World::getContacts(lua_State *L) const
|
||||
int World::getContacts(lua_State *L)
|
||||
{
|
||||
lua_newtable(L);
|
||||
b2Contact *c = world->GetContactList();
|
||||
@@ -509,9 +519,9 @@ int World::getContacts(lua_State *L) const
|
||||
do
|
||||
{
|
||||
if (!c) break;
|
||||
Contact *contact = (Contact *)Memoizer::find(c);
|
||||
Contact *contact = (Contact *)findObject(c);
|
||||
if (!contact)
|
||||
contact = new Contact(c);
|
||||
contact = new Contact(this, c);
|
||||
else
|
||||
contact->retain();
|
||||
luax_pushtype(L, contact);
|
||||
@@ -538,7 +548,7 @@ int World::queryBoundingBox(lua_State *L)
|
||||
box.lowerBound = Physics::scaleDown(b2Vec2(lx, ly));
|
||||
box.upperBound = Physics::scaleDown(b2Vec2(ux, uy));
|
||||
luaL_checktype(L, 5, LUA_TFUNCTION);
|
||||
QueryCallback query(L, 5);
|
||||
QueryCallback query(this, L, 5);
|
||||
world->QueryAABB(&query, box);
|
||||
return 0;
|
||||
}
|
||||
@@ -552,7 +562,7 @@ int World::rayCast(lua_State *L)
|
||||
b2Vec2 v1 = Physics::scaleDown(b2Vec2(x1, y1));
|
||||
b2Vec2 v2 = Physics::scaleDown(b2Vec2(x2, y2));
|
||||
luaL_checktype(L, 5, LUA_TFUNCTION);
|
||||
RayCastCallback raycast(L, 5);
|
||||
RayCastCallback raycast(this, L, 5);
|
||||
world->RayCast(&raycast, v1, v2);
|
||||
return 0;
|
||||
}
|
||||
@@ -586,19 +596,38 @@ void World::destroy()
|
||||
b = b->GetNext();
|
||||
if (t == groundBody)
|
||||
continue;
|
||||
Body *body = (Body *)Memoizer::find(t);
|
||||
Body *body = (Body *)findObject(t);
|
||||
if (!body)
|
||||
throw love::Exception("A body has escaped Memoizer!");
|
||||
body->destroy();
|
||||
}
|
||||
|
||||
world->DestroyBody(groundBody);
|
||||
Memoizer::remove(world);
|
||||
unregisterObject(world);
|
||||
|
||||
delete world;
|
||||
world = nullptr;
|
||||
}
|
||||
|
||||
void World::registerObject(void *b2object, love::Object *object)
|
||||
{
|
||||
box2dObjectMap[b2object] = object;
|
||||
}
|
||||
|
||||
void World::unregisterObject(void *b2object)
|
||||
{
|
||||
box2dObjectMap.erase(b2object);
|
||||
}
|
||||
|
||||
love::Object *World::findObject(void *b2object) const
|
||||
{
|
||||
auto it = box2dObjectMap.find(b2object);
|
||||
if (it != box2dObjectMap.end())
|
||||
return it->second;
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // box2d
|
||||
} // physics
|
||||
} // love
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
|
||||
// STD
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
// Box2D
|
||||
#include <Box2D/Box2D.h>
|
||||
@@ -73,7 +74,8 @@ public:
|
||||
public:
|
||||
Reference *ref;
|
||||
lua_State *L;
|
||||
ContactCallback();
|
||||
World *world;
|
||||
ContactCallback(World *world);
|
||||
~ContactCallback();
|
||||
void process(b2Contact *contact, const b2ContactImpulse *impulse = NULL);
|
||||
};
|
||||
@@ -91,10 +93,11 @@ public:
|
||||
class QueryCallback : public b2QueryCallback
|
||||
{
|
||||
public:
|
||||
QueryCallback(lua_State *L, int idx);
|
||||
QueryCallback(World *world, lua_State *L, int idx);
|
||||
~QueryCallback();
|
||||
virtual bool ReportFixture(b2Fixture *fixture);
|
||||
private:
|
||||
World *world;
|
||||
lua_State *L;
|
||||
int funcidx;
|
||||
};
|
||||
@@ -102,10 +105,11 @@ public:
|
||||
class RayCastCallback : public b2RayCastCallback
|
||||
{
|
||||
public:
|
||||
RayCastCallback(lua_State *L, int idx);
|
||||
RayCastCallback(World *world, lua_State *L, int idx);
|
||||
~RayCastCallback();
|
||||
virtual float32 ReportFixture(b2Fixture *fixture, const b2Vec2 &point, const b2Vec2 &normal, float32 fraction);
|
||||
private:
|
||||
World *world;
|
||||
lua_State *L;
|
||||
int funcidx;
|
||||
};
|
||||
@@ -257,7 +261,7 @@ public:
|
||||
* Get an array of all the Contacts in the World.
|
||||
* @return An array of Contacts.
|
||||
**/
|
||||
int getContacts(lua_State *L) const;
|
||||
int getContacts(lua_State *L);
|
||||
|
||||
/**
|
||||
* Gets the ground body.
|
||||
@@ -280,6 +284,10 @@ public:
|
||||
**/
|
||||
void destroy();
|
||||
|
||||
void registerObject(void *b2object, love::Object *object);
|
||||
void unregisterObject(void *b2object);
|
||||
love::Object *findObject(void *b2object) const;
|
||||
|
||||
private:
|
||||
|
||||
// Pointer to the Box2D world.
|
||||
@@ -297,7 +305,10 @@ private:
|
||||
// Contact callbacks.
|
||||
ContactCallback begin, end, presolve, postsolve;
|
||||
ContactFilter filter;
|
||||
};
|
||||
|
||||
std::unordered_map<void *, love::Object *> box2dObjectMap;
|
||||
|
||||
}; // World
|
||||
|
||||
} // box2d
|
||||
} // physics
|
||||
|
||||
Reference in New Issue
Block a user