From 67e10b64f950c21bc1699325ed9bb0da7f089493 Mon Sep 17 00:00:00 2001 From: rude Date: Sun, 31 Jan 2010 14:24:22 +0100 Subject: [PATCH] Implmented a few 'missing methods' in love.physics. --- src/modules/physics/box2d/Body.cpp | 9 ++++++++ src/modules/physics/box2d/Body.h | 7 ++++++ src/modules/physics/box2d/CircleShape.cpp | 18 ++++++++++++++- src/modules/physics/box2d/CircleShape.h | 8 +++++++ src/modules/physics/box2d/wrap_Body.cpp | 9 ++++++++ src/modules/physics/box2d/wrap_Body.h | 1 + .../physics/box2d/wrap_CircleShape.cpp | 22 +++++++++++++++++++ src/modules/physics/box2d/wrap_CircleShape.h | 2 ++ 8 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index 96e5b3abc..ac105cb7d 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/src/modules/physics/box2d/Body.cpp @@ -195,6 +195,15 @@ namespace box2d body->SetMass(&massData); } + void Body::setInertia(float i) + { + b2MassData massData; + massData.center = body->GetLocalCenter(); + massData.mass = body->GetMass(); + massData.I = i; + body->SetMass(&massData); + } + void Body::getWorldPoint(float x, float y, float & x_o, float & y_o) { b2Vec2 v = world->scaleUp(body->GetWorldPoint(world->scaleDown(b2Vec2(x, y)))); diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index 8a6f570cd..bd67b41f8 100644 --- a/src/modules/physics/box2d/Body.h +++ b/src/modules/physics/box2d/Body.h @@ -214,6 +214,13 @@ namespace box2d **/ void setMass(float x, float y, float m, float i); + /** + * Sets the inertia while keeping the other properties + * (mass and local center). + * @param i The inertia. + **/ + void setInertia(float i); + /** * Sets the Body's angular damping. **/ diff --git a/src/modules/physics/box2d/CircleShape.cpp b/src/modules/physics/box2d/CircleShape.cpp index 47bec179d..e268d29be 100644 --- a/src/modules/physics/box2d/CircleShape.cpp +++ b/src/modules/physics/box2d/CircleShape.cpp @@ -31,11 +31,12 @@ namespace physics namespace box2d { CircleShape::CircleShape(Body * body, b2CircleDef * def) - : Shape(body), radius(def->radius) + : Shape(body) { def->localPosition = body->world->scaleDown(def->localPosition); def->radius = body->world->scaleDown(def->radius); radius = def->radius; + this->localPosition = def->localPosition; shape = body->body->CreateShape(def); shape->SetUserData((void*)data); } @@ -49,6 +50,21 @@ namespace box2d return body->world->scaleUp(radius); } + void CircleShape::getLocalCenter(float & x, float & y) const + { + x = localPosition.x; + y = localPosition.y; + body->world->scaleUp(x, y); + } + + void CircleShape::getWorldCenter(float & x, float & y) const + { + b2Vec2 worldCenter = body->body->GetWorldPoint(localPosition); + worldCenter = body->world->scaleUp(worldCenter); + x = worldCenter.x; + y = worldCenter.y; + } + } // box2d } // physics } // love diff --git a/src/modules/physics/box2d/CircleShape.h b/src/modules/physics/box2d/CircleShape.h index c51a3dc5a..0d5dd2e7c 100644 --- a/src/modules/physics/box2d/CircleShape.h +++ b/src/modules/physics/box2d/CircleShape.h @@ -42,9 +42,14 @@ namespace box2d class CircleShape : public Shape { private: + // The radius of the circle. We need to store this because // Box2D has no built-in method for getting the radius. float radius; + + // Local offset. + b2Vec2 localPosition; + public: /** @@ -63,6 +68,9 @@ namespace box2d float getRadius() const; // There is no support for setting the radius. + + void getLocalCenter(float & x, float & y) const; + void getWorldCenter(float & x, float & y) const; }; } // box2d diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index 49f855629..1d321b11d 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/src/modules/physics/box2d/wrap_Body.cpp @@ -260,6 +260,14 @@ namespace box2d return 0; } + int w_Body_setInertia(lua_State * L) + { + Body * t = luax_checkbody(L, 1); + float i = (float)luaL_checknumber(L, 2); + t->setInertia(i); + return 0; + } + int w_Body_setAngularDamping(lua_State * L) { Body * t = luax_checkbody(L, 1); @@ -476,6 +484,7 @@ namespace box2d { "setPosition", w_Body_setPosition }, { "setMassFromShapes", w_Body_setMassFromShapes }, { "setMass", w_Body_setMass }, + { "setInertia", w_Body_setInertia }, { "setAngularDamping", w_Body_setAngularDamping }, { "setLinearDamping", w_Body_setLinearDamping }, { "getWorldPoint", w_Body_getWorldPoint }, diff --git a/src/modules/physics/box2d/wrap_Body.h b/src/modules/physics/box2d/wrap_Body.h index 4fc8f5a5d..c70565cf4 100644 --- a/src/modules/physics/box2d/wrap_Body.h +++ b/src/modules/physics/box2d/wrap_Body.h @@ -56,6 +56,7 @@ namespace box2d int w_Body_setPosition(lua_State * L); int w_Body_setMassFromShapes(lua_State * L); int w_Body_setMass(lua_State * L); + int w_Body_setInertia(lua_State * L); int w_Body_setAngularDamping(lua_State * L); int w_Body_setLinearDamping(lua_State * L); int w_Body_getWorldPoint(lua_State * L); diff --git a/src/modules/physics/box2d/wrap_CircleShape.cpp b/src/modules/physics/box2d/wrap_CircleShape.cpp index c543ecba3..d7d33562f 100644 --- a/src/modules/physics/box2d/wrap_CircleShape.cpp +++ b/src/modules/physics/box2d/wrap_CircleShape.cpp @@ -38,8 +38,30 @@ namespace box2d return 1; } + int w_CircleShape_getLocalCenter(lua_State * L) + { + CircleShape * c = luax_checkcircleshape(L, 1); + float x, y; + c->getLocalCenter(x, y); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + return 2; + } + + int w_CircleShape_getWorldCenter(lua_State * L) + { + CircleShape * c = luax_checkcircleshape(L, 1); + float x, y; + c->getWorldCenter(x, y); + lua_pushnumber(L, x); + lua_pushnumber(L, y); + return 2; + } + static const luaL_Reg functions[] = { { "getRadius", w_CircleShape_getRadius }, + { "getLocalCenter", w_CircleShape_getLocalCenter }, + { "getWorldCenter", w_CircleShape_getWorldCenter }, // From Shape. { "getType", w_Shape_getType }, { "setFriction", w_Shape_setFriction }, diff --git a/src/modules/physics/box2d/wrap_CircleShape.h b/src/modules/physics/box2d/wrap_CircleShape.h index 7f1e1186d..453237938 100644 --- a/src/modules/physics/box2d/wrap_CircleShape.h +++ b/src/modules/physics/box2d/wrap_CircleShape.h @@ -34,6 +34,8 @@ namespace box2d { CircleShape * luax_checkcircleshape(lua_State * L, int idx); int w_CircleShape_getRadius(lua_State * L); + int w_CircleShape_getLocalCenter(lua_State * L); + int w_CircleShape_getWorldCenter(lua_State * L); int luaopen_circleshape(lua_State * L); } // box2d