Skip to content

Commit

Permalink
love.physics: simplify Body and Shape API.
Browse files Browse the repository at this point in the history
#1130

- Shapes are now directly attached to Bodies when they're created (similar to love 0.7 and older).
- Fixtures are removed.
- All methods that were in Fixtures now exist in Shapes.
- All APIs that used or returned a Fixture now do the same with a Shape.

- Add new love.physics.new*Shape variants that take a Body as the first parameter.
- Deprecate the new*Shape APIs that don't take a Body.
- Deprecate love.physics.newFixture (the deprecated function now returns a Shape).
- Replace Body:getFixture and Body:getFixtures with Body:getShape and Body:getShapes (Body:getFixtures is deprecated).
- Replace World:queryFixturesInArea and World:getFixturesInArea with World:queryShapesInArea and World:getShapesInArea (queryFixturesInArea is deprecated).
- Replace Contact:getFixtures with Contact:getShapes (Contact:getFixtures is deprecated).
- Replace all love.physics callback Fixture parameters with Shape parameters.
- Deprecate ChainShape:getChildEdge.
  • Loading branch information
slime73 committed Oct 7, 2023
1 parent 2702004 commit ac9ae82
Show file tree
Hide file tree
Showing 35 changed files with 1,139 additions and 1,238 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -837,8 +837,6 @@ set(LOVE_SRC_MODULE_PHYSICS_BOX2D
src/modules/physics/box2d/DistanceJoint.h
src/modules/physics/box2d/EdgeShape.cpp
src/modules/physics/box2d/EdgeShape.h
src/modules/physics/box2d/Fixture.cpp
src/modules/physics/box2d/Fixture.h
src/modules/physics/box2d/FrictionJoint.cpp
src/modules/physics/box2d/FrictionJoint.h
src/modules/physics/box2d/GearJoint.cpp
Expand Down Expand Up @@ -881,8 +879,6 @@ set(LOVE_SRC_MODULE_PHYSICS_BOX2D
src/modules/physics/box2d/wrap_DistanceJoint.h
src/modules/physics/box2d/wrap_EdgeShape.cpp
src/modules/physics/box2d/wrap_EdgeShape.h
src/modules/physics/box2d/wrap_Fixture.cpp
src/modules/physics/box2d/wrap_Fixture.h
src/modules/physics/box2d/wrap_FrictionJoint.cpp
src/modules/physics/box2d/wrap_FrictionJoint.h
src/modules/physics/box2d/wrap_GearJoint.cpp
Expand Down
6 changes: 6 additions & 0 deletions src/common/deprecation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,12 @@ std::string getDeprecationNotice(const DeprecationInfo &info, bool usewhere)

if (info.apiType == API_FUNCTION)
notice << "function ";
else if (info.apiType == API_FUNCTION_VARIANT)
notice << "function variant in ";
else if (info.apiType == API_METHOD)
notice << "method ";
else if (info.apiType == API_METHOD_VARIANT)
notice << "method variant in ";
else if (info.apiType == API_CALLBACK)
notice << "callback ";
else if (info.apiType == API_FIELD)
Expand Down Expand Up @@ -188,7 +192,9 @@ MarkDeprecated::~MarkDeprecated()
STRINGMAP_BEGIN(APIType, API_MAX_ENUM, apiType)
{
{ "function", API_FUNCTION },
{ "functionvariant", API_FUNCTION_VARIANT },
{ "method", API_METHOD },
{ "methodvariant", API_METHOD_VARIANT },
{ "callback", API_CALLBACK },
{ "field", API_FIELD },
{ "constant", API_CONSTANT },
Expand Down
2 changes: 2 additions & 0 deletions src/common/deprecation.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ namespace love
enum APIType
{
API_FUNCTION,
API_FUNCTION_VARIANT,
API_METHOD,
API_METHOD_VARIANT,
API_CALLBACK,
API_FIELD,
API_CONSTANT,
Expand Down
22 changes: 11 additions & 11 deletions src/modules/physics/box2d/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
#include "common/math.h"

#include "Shape.h"
#include "Fixture.h"
#include "World.h"
#include "Physics.h"

// Needed for luax_pushjoint.
#include "wrap_Joint.h"
#include "wrap_Shape.h"

namespace love
{
Expand Down Expand Up @@ -461,20 +461,20 @@ World *Body::getWorld() const
return world;
}

Fixture *Body::getFixture() const
Shape *Body::getShape() const
{
b2Fixture *f = body->GetFixtureList();
if (f == nullptr)
return nullptr;

Fixture *fixture = (Fixture *)(f->GetUserData().pointer);
if (!fixture)
throw love::Exception("A fixture has escaped Memoizer!");
Shape *shape = (Shape *)(f->GetUserData().pointer);
if (!shape)
throw love::Exception("A Shape has escaped Memoizer!");

return fixture;
return shape;
}

int Body::getFixtures(lua_State *L) const
int Body::getShapes(lua_State *L) const
{
lua_newtable(L);
b2Fixture *f = body->GetFixtureList();
Expand All @@ -483,10 +483,10 @@ int Body::getFixtures(lua_State *L) const
{
if (!f)
break;
Fixture *fixture = (Fixture *)(f->GetUserData().pointer);
if (!fixture)
throw love::Exception("A fixture has escaped Memoizer!");
luax_pushtype(L, fixture);
Shape *shape = (Shape *)(f->GetUserData().pointer);
if (!shape)
throw love::Exception("A Shape has escaped Memoizer!");
luax_pushshape(L, shape);
lua_rawseti(L, -2, i);
i++;
}
Expand Down
11 changes: 4 additions & 7 deletions src/modules/physics/box2d/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ namespace box2d
// Forward declarations.
class World;
class Shape;
class Fixture;

/**
* A Body is an entity which has position and orientation
Expand All @@ -57,7 +56,6 @@ class Body : public love::physics::Body
friend class CircleShape;
friend class PolygonShape;
friend class Shape;
friend class Fixture;

// Public because joints et al ask for b2body
b2Body *body;
Expand Down Expand Up @@ -391,15 +389,14 @@ class Body : public love::physics::Body
World *getWorld() const;

/**
* Gets the first Fixture attached to this Body.
* Gets the first Shape attached to this Body.
**/
Fixture *getFixture() const;
Shape *getShape() const;

/**
* Get an array of all the Fixtures attached to this Body.
* @return An array of Fixtures.
* Get an array of all the Shapes attached to this Body.
**/
int getFixtures(lua_State *L) const;
int getShapes(lua_State *L) const;

/**
* Get an array of all Joints attached to this Body.
Expand Down
33 changes: 16 additions & 17 deletions src/modules/physics/box2d/ChainShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace box2d

love::Type ChainShape::type("ChainShape", &Shape::type);

ChainShape::ChainShape(b2ChainShape *c, bool own)
: Shape(c, own)
ChainShape::ChainShape(Body *body, const b2ChainShape &c)
: Shape(body, c)
{
}

Expand All @@ -45,58 +45,56 @@ ChainShape::~ChainShape()

void ChainShape::setNextVertex(float x, float y)
{
throwIfShapeNotValid();
b2Vec2 v(x, y);
b2ChainShape *c = (b2ChainShape *)shape;
c->m_nextVertex = Physics::scaleDown(v);
}

void ChainShape::setPreviousVertex(float x, float y)
{
throwIfShapeNotValid();
b2Vec2 v(x, y);
b2ChainShape *c = (b2ChainShape *)shape;
c->m_prevVertex = Physics::scaleDown(v);
}

b2Vec2 ChainShape::getNextVertex() const
{
throwIfShapeNotValid();
b2ChainShape *c = (b2ChainShape *)shape;

return Physics::scaleUp(c->m_nextVertex);
}

b2Vec2 ChainShape::getPreviousVertex() const
{
throwIfShapeNotValid();
b2ChainShape *c = (b2ChainShape *)shape;

return Physics::scaleUp(c->m_prevVertex);
}

EdgeShape *ChainShape::getChildEdge(int index) const
{
throwIfShapeNotValid();

b2ChainShape *c = (b2ChainShape *)shape;
b2EdgeShape *e = new b2EdgeShape;

try
{
c->GetChildEdge(e, index);
}
catch (love::Exception &)
{
delete e;
throw;
}

return new EdgeShape(e, true);

b2EdgeShape e;
c->GetChildEdge(&e, index);

return new EdgeShape(nullptr, e);
}

int ChainShape::getVertexCount() const
{
throwIfShapeNotValid();
b2ChainShape *c = (b2ChainShape *)shape;
return c->m_count;
}

b2Vec2 ChainShape::getPoint(int index) const
{
throwIfShapeNotValid();
b2ChainShape *c = (b2ChainShape *)shape;
if (index < 0 || index >= c->m_count)
throw love::Exception("Physics error: index out of bounds");
Expand All @@ -106,6 +104,7 @@ b2Vec2 ChainShape::getPoint(int index) const

const b2Vec2 *ChainShape::getPoints() const
{
throwIfShapeNotValid();
b2ChainShape *c = (b2ChainShape *)shape;
return c->m_vertices;
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/physics/box2d/ChainShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ChainShape : public Shape
* Create a new ChainShape from a Box2D chain shape.
* @param c The chain shape.
**/
ChainShape(b2ChainShape *c, bool own = true);
ChainShape(Body *body, const b2ChainShape &c);

virtual ~ChainShape();

Expand Down
8 changes: 6 additions & 2 deletions src/modules/physics/box2d/CircleShape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace box2d

love::Type CircleShape::type("CircleShape", &Shape::type);

CircleShape::CircleShape(b2CircleShape *c, bool own)
: Shape(c, own)
CircleShape::CircleShape(Body *body, const b2CircleShape &c)
: Shape(body, c)
{
}

Expand All @@ -45,23 +45,27 @@ CircleShape::~CircleShape()

float CircleShape::getRadius() const
{
throwIfShapeNotValid();
return Physics::scaleUp(shape->m_radius);
}

void CircleShape::setRadius(float r)
{
throwIfShapeNotValid();
shape->m_radius = Physics::scaleDown(r);
}

void CircleShape::getPoint(float &x_o, float &y_o) const
{
throwIfShapeNotValid();
b2CircleShape *c = (b2CircleShape *) shape;
x_o = Physics::scaleUp(c->m_p.x);
y_o = Physics::scaleUp(c->m_p.y);
}

void CircleShape::setPoint(float x, float y)
{
throwIfShapeNotValid();
b2CircleShape *c = (b2CircleShape *) shape;
c->m_p = Physics::scaleDown(b2Vec2(x, y));
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/physics/box2d/CircleShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CircleShape : public Shape
* Create a new CircleShape from the a Box2D CircleShape definition.
* @param c The CircleShape definition.
**/
CircleShape(b2CircleShape *c, bool own = true);
CircleShape(Body *body, const b2CircleShape &c);

virtual ~CircleShape();

Expand Down
17 changes: 8 additions & 9 deletions src/modules/physics/box2d/Contact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ Contact::Contact(World *world, b2Contact *contact)
: contact(contact)
, world(world)
{
//contact->user
world->registerObject(contact, this);
}

Expand All @@ -46,16 +45,16 @@ Contact::~Contact()

void Contact::invalidate()
{
if (contact != NULL)
if (contact != nullptr)
{
world->unregisterObject(contact);
contact = NULL;
contact = nullptr;
}
}

bool Contact::isValid()
{
return contact != NULL;
return contact != nullptr;
}

int Contact::getPositions(lua_State *L)
Expand Down Expand Up @@ -144,13 +143,13 @@ void Contact::getChildren(int &childA, int &childB)
childB = contact->GetChildIndexB();
}

void Contact::getFixtures(Fixture *&fixtureA, Fixture *&fixtureB)
void Contact::getShapes(Shape *&shapeA, Shape *&shapeB)
{
fixtureA = (Fixture *) (contact->GetFixtureA()->GetUserData().pointer);
fixtureB = (Fixture *) (contact->GetFixtureB()->GetUserData().pointer);
shapeA = (Shape *) (contact->GetFixtureA()->GetUserData().pointer);
shapeB = (Shape *) (contact->GetFixtureB()->GetUserData().pointer);

if (!fixtureA || !fixtureB)
throw love::Exception("A fixture has escaped Memoizer!");
if (!shapeA || !shapeB)
throw love::Exception("A Shape has escaped Memoizer!");
}

} // box2d
Expand Down
4 changes: 2 additions & 2 deletions src/modules/physics/box2d/Contact.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ class Contact : public Object
void getChildren(int &childA, int &childB);

/**
* Gets the Fixtures associated with this Contact.
* Gets the Shapes associated with this Contact.
**/
void getFixtures(Fixture *&fixtureA, Fixture *&fixtureB);
void getShapes(Shape *&shapeA, Shape *&shapeB);

private:

Expand Down
Loading

0 comments on commit ac9ae82

Please sign in to comment.