From b36ff409aa7a77c0131d2d49416459b44ec01408 Mon Sep 17 00:00:00 2001 From: Bill Meltsner Date: Sat, 24 Sep 2011 09:27:39 -0400 Subject: [PATCH] Make Fixture creation API more lovely --HG-- branch : box2d-update --- src/modules/physics/box2d/Body.cpp | 10 --- src/modules/physics/box2d/Body.h | 4 - src/modules/physics/box2d/Physics.cpp | 5 ++ src/modules/physics/box2d/Physics.h | 86 ++++++++++++---------- src/modules/physics/box2d/wrap_Body.cpp | 20 ----- src/modules/physics/box2d/wrap_Body.h | 2 - src/modules/physics/box2d/wrap_Physics.cpp | 11 +++ src/modules/physics/box2d/wrap_Physics.h | 1 + 8 files changed, 64 insertions(+), 75 deletions(-) diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index 110b95642..a471a1738 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/src/modules/physics/box2d/Body.cpp @@ -383,16 +383,6 @@ namespace box2d { return world; } - - Fixture * Body::createFixture(Shape * shape, float density) - { - return new Fixture(this, shape, density); - } - - void Body::destroyFixture(Fixture * fixture) - { - fixture->release(); - } b2Vec2 Body::getVector(lua_State * L) { diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index 416dc926a..d14a00e2f 100644 --- a/src/modules/physics/box2d/Body.h +++ b/src/modules/physics/box2d/Body.h @@ -373,10 +373,6 @@ namespace box2d * Get the World this Body resides in. */ World * getWorld() const; - - Fixture * createFixture(Shape * shape, float density); - - void destroyFixture(Fixture * fixture); private: /** diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index e6dcabae0..9c76a7ff0 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -203,6 +203,11 @@ namespace box2d return new RopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected); } + Fixture * Physics::newFixture(Body * body, Shape * shape, float density) + { + return new Fixture(body, shape, density); + } + void Physics::setMeter(int meter) { Physics::meter = meter; diff --git a/src/modules/physics/box2d/Physics.h b/src/modules/physics/box2d/Physics.h index 9fa58876b..b26f61e42 100644 --- a/src/modules/physics/box2d/Physics.h +++ b/src/modules/physics/box2d/Physics.h @@ -250,74 +250,82 @@ namespace box2d RopeJoint * newRopeJoint(Body * body1, Body * body2, float x1, float y1, float x2, float y2, float maxLength, bool collideConnected); /** - * Sets the number of pixels in one meter. - * @param pixels The number of pixels in one meter. (1m ~= 3.3ft). - **/ + * Creates a new Fixture attaching shape to body. + * @param body The body to attach the Fixture to. + * @param shape The shape to attach to the Fixture, + * @param density The density of the Fixture. + **/ + + Fixture * newFixture(Body * body, Shape * shape, float density); + + /** + * Sets the number of pixels in one meter. + * @param pixels The number of pixels in one meter. (1m ~= 3.3ft). + **/ static void setMeter(int meter); /** - * Gets the number of pixels in one meter. - * @param pixels The number of pixels in one meter. (1m ~= 3.3ft). - **/ + * Gets the number of pixels in one meter. + * @param pixels The number of pixels in one meter. (1m ~= 3.3ft). + **/ static int getMeter(); /** - * Scales a value down according to the current meter in pixels. - * @param f The unscaled input value. - **/ + * Scales a value down according to the current meter in pixels. + * @param f The unscaled input value. + **/ static float scaleDown(float f); /** - * Scales a value up according to the current meter in pixels. - * @param f The unscaled input value. - **/ + * Scales a value up according to the current meter in pixels. + * @param f The unscaled input value. + **/ static float scaleUp(float f); /** - * Scales a point down according to the current meter - * in pixels, for instance x = x0/meter, y = x0/meter. - * @param x The x-coordinate of the point to scale. - * @param y The y-coordinate of the point to scale. - **/ + * Scales a point down according to the current meter + * in pixels, for instance x = x0/meter, y = x0/meter. + * @param x The x-coordinate of the point to scale. + * @param y The y-coordinate of the point to scale. + **/ static void scaleDown(float & x, float & y); /** - * Scales a point up according to the current meter - * in pixels, for instance x = x0/meter, y = x0/meter. - * @param x The x-coordinate of the point to scale. - * @param y The y-coordinate of the point to scale. - **/ + * Scales a point up according to the current meter + * in pixels, for instance x = x0/meter, y = x0/meter. + * @param x The x-coordinate of the point to scale. + * @param y The y-coordinate of the point to scale. + **/ static void scaleUp(float & x, float & y); /** - * Scales a b2Vec2 down according to the current meter in pixels. - * @param v The unscaled input vector. - * @return The scaled vector. - **/ + * Scales a b2Vec2 down according to the current meter in pixels. + * @param v The unscaled input vector. + * @return The scaled vector. + **/ static b2Vec2 scaleDown(const b2Vec2 & v); /** - * Scales a b2Vec up according to the current meter in pixels. - * @param v The unscaled input vector. - * @return The scaled vector. - **/ + * Scales a b2Vec up according to the current meter in pixels. + * @param v The unscaled input vector. + * @return The scaled vector. + **/ static b2Vec2 scaleUp(const b2Vec2 & v); /** - * Scales a b2AABB down according to the current meter in pixels. - * @param v The unscaled input AABB. - * @return The scaled AABB. - **/ + * Scales a b2AABB down according to the current meter in pixels. + * @param v The unscaled input AABB. + * @return The scaled AABB. + **/ static b2AABB scaleDown(const b2AABB & aabb); /** - * Scales a b2AABB up according to the current meter in pixels. - * @param v The unscaled input AABB. - * @return The scaled AABB. - **/ + * Scales a b2AABB up according to the current meter in pixels. + * @param v The unscaled input AABB. + * @return The scaled AABB. + **/ static b2AABB scaleUp(const b2AABB & aabb); - }; // Physics } // box2d diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index bc7883394..110dfacd3 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/src/modules/physics/box2d/wrap_Body.cpp @@ -499,24 +499,6 @@ namespace box2d luax_pushboolean(L, b); return 1; } - - int w_Body_createFixture(lua_State * L) - { - Body * t = luax_checkbody(L, 1); - Shape * s = luax_checktype(L, 2, "Shape", PHYSICS_SHAPE_T); - float d = (float)luaL_checknumber(L, 3); - Fixture * f = t->createFixture(s, d); - luax_newtype(L, "Fixture", PHYSICS_FIXTURE_T, (void*)f); - return 1; - } - - int w_Body_destroyFixture(lua_State * L) - { - Body * t = luax_checkbody(L, 1); - Fixture * f = luax_checktype(L, 2, "Fixture", PHYSICS_FIXTURE_T); - t->destroyFixture(f); - return 0; - } int w_Body_destroy(lua_State * L) { @@ -579,8 +561,6 @@ namespace box2d { "setAwake", w_Body_setAwake }, { "setFixedRotation", w_Body_setFixedRotation }, { "isFixedRotation", w_Body_isFixedRotation }, - { "createFixture", w_Body_createFixture }, - { "destroyFixture", w_Body_destroyFixture }, { "destroy", w_Body_destroy }, { 0, 0 } }; diff --git a/src/modules/physics/box2d/wrap_Body.h b/src/modules/physics/box2d/wrap_Body.h index bbee3e6b6..e7a8fe6e2 100644 --- a/src/modules/physics/box2d/wrap_Body.h +++ b/src/modules/physics/box2d/wrap_Body.h @@ -82,8 +82,6 @@ namespace box2d int w_Body_setAwake(lua_State * L); int w_Body_setFixedRotation(lua_State * L); int w_Body_isFixedRotation(lua_State * L); - int w_Body_createFixture(lua_State * L); - int w_Body_destroyFixture(lua_State * L); int w_Body_destroy(lua_State * L); int luaopen_body(lua_State * L); diff --git a/src/modules/physics/box2d/wrap_Physics.cpp b/src/modules/physics/box2d/wrap_Physics.cpp index e5557b0d8..e84839187 100644 --- a/src/modules/physics/box2d/wrap_Physics.cpp +++ b/src/modules/physics/box2d/wrap_Physics.cpp @@ -54,6 +54,16 @@ namespace box2d luax_newtype(L, "Body", PHYSICS_BODY_T, (void*)body); return 1; } + + int w_newFixture(lua_State * L) + { + Body * body = luax_checkbody(L, 1); + Shape * shape = luax_checkshape(L, 2); + float density = (float)luaL_checknumber(L, 3); + Fixture * fixture = instance->newFixture(body, shape, density); + luax_newtype(L, "Fixture", PHYSICS_FIXTURE_T, (void*)fixture); + return 1; + } int w_newCircleShape(lua_State * L) { @@ -279,6 +289,7 @@ namespace box2d static const luaL_Reg functions[] = { { "newWorld", w_newWorld }, { "newBody", w_newBody }, + { "newFixture", w_newFixture }, { "newCircleShape", w_newCircleShape }, { "newRectangleShape", w_newRectangleShape }, { "newPolygonShape", w_newPolygonShape }, diff --git a/src/modules/physics/box2d/wrap_Physics.h b/src/modules/physics/box2d/wrap_Physics.h index 47fde7933..8de805600 100644 --- a/src/modules/physics/box2d/wrap_Physics.h +++ b/src/modules/physics/box2d/wrap_Physics.h @@ -53,6 +53,7 @@ namespace box2d { int w_newWorld(lua_State * L); int w_newBody(lua_State * L); + int w_newFixture(lua_State * L); int w_newCircleShape(lua_State * L); int w_newRectangleShape(lua_State * L); int w_newPolygonShape(lua_State * L);