From 762672882e3c789ac1c4ea57e50bf91ab00e8c7f Mon Sep 17 00:00:00 2001 From: Sasha Szpakowski Date: Mon, 30 Mar 2026 23:08:32 -0300 Subject: [PATCH] physics: newCircleBody and friends return the shape as a second return value. --- src/modules/physics/box2d/Physics.cpp | 24 +++++++------- src/modules/physics/box2d/Physics.h | 12 +++---- src/modules/physics/box2d/wrap_Physics.cpp | 37 +++++++++++++++------- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index 691c5722b..0629fe443 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -60,23 +60,23 @@ Body *Physics::newBody(World *world, Body::Type type) return new Body(world, b2Vec2(0, 0), type); } -Body *Physics::newCircleBody(World *world, Body::Type type, float x, float y, float radius) +Body *Physics::newCircleBody(World *world, Body::Type type, float x, float y, float radius, CircleShape *&shape) { StrongRef body(newBody(world, x, y, type), Acquire::NORETAIN); - StrongRef shape(newCircleShape(body, 0, 0, radius), Acquire::NORETAIN); + shape = newCircleShape(body, 0, 0, radius); body->retain(); return body.get(); } -Body *Physics::newRectangleBody(World *world, Body::Type type, float x, float y, float w, float h, float angle) +Body *Physics::newRectangleBody(World *world, Body::Type type, float x, float y, float w, float h, float angle, PolygonShape *&shape) { StrongRef body(newBody(world, x, y, type), Acquire::NORETAIN); - StrongRef shape(newRectangleShape(body, 0, 0, w, h, angle), Acquire::NORETAIN); + shape = newRectangleShape(body, 0, 0, w, h, angle); body->retain(); return body.get(); } -Body *Physics::newPolygonBody(World *world, Body::Type type, const Vector2 *coords, int count) +Body *Physics::newPolygonBody(World *world, Body::Type type, const Vector2 *coords, int count, PolygonShape *&shape) { Vector2 origin(0, 0); @@ -88,32 +88,32 @@ Body *Physics::newPolygonBody(World *world, Body::Type type, const Vector2 *coor localcoords.push_back(coords[i] - origin); StrongRef body(newBody(world, origin.x, origin.y, type), Acquire::NORETAIN); - StrongRef shape(newPolygonShape(body, localcoords.data(), count), Acquire::NORETAIN); + shape = newPolygonShape(body, localcoords.data(), count); body->retain(); return body.get(); } -Body *Physics::newEdgeBody(World *world, Body::Type type, float x1, float y1, float x2, float y2) +Body *Physics::newEdgeBody(World *world, Body::Type type, float x1, float y1, float x2, float y2, EdgeShape *&shape) { float wx = (x2 - x1) / 2.0f; float wy = (y2 - y1) / 2.0f; StrongRef body(newBody(world, wx, wy, type), Acquire::NORETAIN); - StrongRef shape(newEdgeShape(body, x1 - wx, y1 - wy, x2 - wx, y2 - wy), Acquire::NORETAIN); + shape = newEdgeShape(body, x1 - wx, y1 - wy, x2 - wx, y2 - wy); body->retain(); return body.get(); } -Body *Physics::newEdgeBody(World *world, Body::Type type, float x1, float y1, float x2, float y2, float prevx, float prevy, float nextx, float nexty) +Body *Physics::newEdgeBody(World *world, Body::Type type, float x1, float y1, float x2, float y2, float prevx, float prevy, float nextx, float nexty, EdgeShape *&shape) { float wx = (x2 - x1) / 2.0f; float wy = (y2 - y1) / 2.0f; StrongRef body(newBody(world, wx, wy, type), Acquire::NORETAIN); - StrongRef shape(newEdgeShape(body, x1 - wx, y1 - wy, x2 - wx, y2 - wy, prevx - wx, prevy - wy, nextx - wx, nexty - wy), Acquire::NORETAIN); + shape = newEdgeShape(body, x1 - wx, y1 - wy, x2 - wx, y2 - wy, prevx - wx, prevy - wy, nextx - wx, nexty - wy); body->retain(); return body.get(); } -Body *Physics::newChainBody(World *world, Body::Type type, bool loop, const Vector2 *coords, int count) +Body *Physics::newChainBody(World *world, Body::Type type, bool loop, const Vector2 *coords, int count, ChainShape *&shape) { Vector2 origin(0, 0); @@ -125,7 +125,7 @@ Body *Physics::newChainBody(World *world, Body::Type type, bool loop, const Vect localcoords.push_back(coords[i] - origin); StrongRef body(newBody(world, origin.x, origin.y, type), Acquire::NORETAIN); - StrongRef shape(newChainShape(body, loop, localcoords.data(), count), Acquire::NORETAIN); + shape = newChainShape(body, loop, localcoords.data(), count); body->retain(); return body.get(); } diff --git a/src/modules/physics/box2d/Physics.h b/src/modules/physics/box2d/Physics.h index 39a919c5a..ed3c27b9e 100644 --- a/src/modules/physics/box2d/Physics.h +++ b/src/modules/physics/box2d/Physics.h @@ -94,12 +94,12 @@ public: * body's world position is the center/average of the given coordinates, * and the shape is centered at the local origin. **/ - Body *newCircleBody(World *world, Body::Type type, float x, float y, float radius); - Body *newRectangleBody(World *world, Body::Type type, float x, float y, float w, float h, float angle); - Body *newPolygonBody(World *world, Body::Type type, const Vector2 *coords, int count); - Body *newEdgeBody(World *world, Body::Type type, float x1, float y1, float x2, float y2); - Body *newEdgeBody(World *world, Body::Type type, float x1, float y1, float x2, float y2, float prevx, float prevy, float nextx, float nexty); - Body *newChainBody(World *world, Body::Type type, bool loop, const Vector2 *coords, int count); + Body *newCircleBody(World *world, Body::Type type, float x, float y, float radius, CircleShape *&shape); + Body *newRectangleBody(World *world, Body::Type type, float x, float y, float w, float h, float angle, PolygonShape *&shape); + Body *newPolygonBody(World *world, Body::Type type, const Vector2 *coords, int count, PolygonShape *&shape); + Body *newEdgeBody(World *world, Body::Type type, float x1, float y1, float x2, float y2, EdgeShape *&shape); + Body *newEdgeBody(World *world, Body::Type type, float x1, float y1, float x2, float y2, float prevx, float prevy, float nextx, float nexty, EdgeShape *&shape); + Body *newChainBody(World *world, Body::Type type, bool loop, const Vector2 *coords, int count, ChainShape *&shape); // Necessary to support the deprecated newFixture API. Shape *newAttachedShape(Body *body, Shape *prototype, float density); diff --git a/src/modules/physics/box2d/wrap_Physics.cpp b/src/modules/physics/box2d/wrap_Physics.cpp index a20811314..bf73fb607 100644 --- a/src/modules/physics/box2d/wrap_Physics.cpp +++ b/src/modules/physics/box2d/wrap_Physics.cpp @@ -96,11 +96,14 @@ int w_newCircleBody(lua_State *L) float radius = (float)luaL_checknumber(L, 5); Body *body = nullptr; - luax_catchexcept(L, [&]() { body = instance()->newCircleBody(world, btype, x, y, radius); }); + CircleShape *shape = nullptr; + luax_catchexcept(L, [&]() { body = instance()->newCircleBody(world, btype, x, y, radius, shape); }); luax_pushtype(L, body); + luax_pushshape(L, shape); body->release(); - return 1; + shape->release(); + return 2; } int w_newRectangleBody(lua_State *L) @@ -119,11 +122,14 @@ int w_newRectangleBody(lua_State *L) float angle = (float)luaL_optnumber(L, 7, 0.0); Body *body = nullptr; - luax_catchexcept(L, [&]() { body = instance()->newRectangleBody(world, btype, x, y, w, h, angle); }); + PolygonShape *shape = nullptr; + luax_catchexcept(L, [&]() { body = instance()->newRectangleBody(world, btype, x, y, w, h, angle, shape); }); luax_pushtype(L, body); + luax_pushshape(L, shape); body->release(); - return 1; + shape->release(); + return 2; } int w_newPolygonBody(lua_State *L) @@ -170,11 +176,14 @@ int w_newPolygonBody(lua_State *L) } Body *body = nullptr; - luax_catchexcept(L, [&]() { body = instance()->newPolygonBody(world, btype, coords.data(), (int)coords.size()); }); + PolygonShape *shape = nullptr; + luax_catchexcept(L, [&]() { body = instance()->newPolygonBody(world, btype, coords.data(), (int)coords.size(), shape); }); luax_pushtype(L, body); + luax_pushshape(L, shape); body->release(); - return 1; + shape->release(); + return 2; } int w_newEdgeBody(lua_State *L) @@ -192,10 +201,11 @@ int w_newEdgeBody(lua_State *L) float y2 = (float)luaL_checknumber(L, 6); Body *body = nullptr; + EdgeShape *shape = nullptr; if (lua_isnoneornil(L, 7)) { - luax_catchexcept(L, [&]() { body = instance()->newEdgeBody(world, btype, x1, y1, x2, y2); }); + luax_catchexcept(L, [&]() { body = instance()->newEdgeBody(world, btype, x1, y1, x2, y2, shape); }); } else { @@ -203,12 +213,14 @@ int w_newEdgeBody(lua_State *L) float prevy = (float)luaL_checknumber(L, 8); float nextx = (float)luaL_checknumber(L, 9); float nexty = (float)luaL_checknumber(L, 10); - luax_catchexcept(L, [&]() { body = instance()->newEdgeBody(world, btype, x1, y1, x2, y2, prevx, prevy, nextx, nexty); }); + luax_catchexcept(L, [&]() { body = instance()->newEdgeBody(world, btype, x1, y1, x2, y2, prevx, prevy, nextx, nexty, shape); }); } luax_pushtype(L, body); + luax_pushshape(L, shape); body->release(); - return 1; + shape->release(); + return 2; } int w_newChainBody(lua_State *L) @@ -257,11 +269,14 @@ int w_newChainBody(lua_State *L) } Body *body = nullptr; - luax_catchexcept(L, [&]() { body = instance()->newChainBody(world, btype, loop, coords.data(), (int)coords.size()); }); + ChainShape *shape = nullptr; + luax_catchexcept(L, [&]() { body = instance()->newChainBody(world, btype, loop, coords.data(), (int)coords.size(), shape); }); luax_pushtype(L, body); + luax_pushshape(L, shape); body->release(); - return 1; + shape->release(); + return 2; } int w_newFixture(lua_State *L)