physics: newCircleBody and friends return the shape as a second return value.

This commit is contained in:
Sasha Szpakowski
2026-03-30 23:08:32 -03:00
parent b0a2cb4d7d
commit 762672882e
3 changed files with 44 additions and 29 deletions
+12 -12
View File
@@ -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> body(newBody(world, x, y, type), Acquire::NORETAIN);
StrongRef<CircleShape> 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> body(newBody(world, x, y, type), Acquire::NORETAIN);
StrongRef<PolygonShape> 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> body(newBody(world, origin.x, origin.y, type), Acquire::NORETAIN);
StrongRef<PolygonShape> 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> body(newBody(world, wx, wy, type), Acquire::NORETAIN);
StrongRef<EdgeShape> 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> body(newBody(world, wx, wy, type), Acquire::NORETAIN);
StrongRef<EdgeShape> 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> body(newBody(world, origin.x, origin.y, type), Acquire::NORETAIN);
StrongRef<ChainShape> shape(newChainShape(body, loop, localcoords.data(), count), Acquire::NORETAIN);
shape = newChainShape(body, loop, localcoords.data(), count);
body->retain();
return body.get();
}
+6 -6
View File
@@ -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);
+26 -11
View File
@@ -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)