From 3157e5df7bd2ec1dffe198d3ab041cda009c24ab Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sun, 31 Jul 2016 14:25:12 -0300 Subject: [PATCH] Added ChainShape:getNext/PreviousVertex (see issue #1191). Thanks airstruck! --- src/modules/physics/box2d/ChainShape.cpp | 26 ++++++++++++++++ src/modules/physics/box2d/ChainShape.h | 24 +++++++++++++++ src/modules/physics/box2d/wrap_ChainShape.cpp | 30 +++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/src/modules/physics/box2d/ChainShape.cpp b/src/modules/physics/box2d/ChainShape.cpp index 273d0ac1d..1521df558 100644 --- a/src/modules/physics/box2d/ChainShape.cpp +++ b/src/modules/physics/box2d/ChainShape.cpp @@ -67,6 +67,32 @@ void ChainShape::setPreviousVertex(float x, float y) c->SetPrevVertex(Physics::scaleDown(v)); } +bool ChainShape::hasNextVertex() const +{ + b2ChainShape *c = (b2ChainShape *)shape; + return c->m_hasNextVertex; +} + +bool ChainShape::hasPreviousVertex() const +{ + b2ChainShape *c = (b2ChainShape *)shape; + return c->m_hasPrevVertex; +} + +b2Vec2 ChainShape::getNextVertex() const +{ + b2ChainShape *c = (b2ChainShape *)shape; + const b2Vec2 &v = c->m_nextVertex; + return Physics::scaleUp(v); +} + +b2Vec2 ChainShape::getPreviousVertex() const +{ + b2ChainShape *c = (b2ChainShape *)shape; + const b2Vec2 &v = c->m_prevVertex; + return Physics::scaleUp(v); +} + EdgeShape *ChainShape::getChildEdge(int index) const { b2ChainShape *c = (b2ChainShape *)shape; diff --git a/src/modules/physics/box2d/ChainShape.h b/src/modules/physics/box2d/ChainShape.h index 9f53ca958..52598686c 100644 --- a/src/modules/physics/box2d/ChainShape.h +++ b/src/modules/physics/box2d/ChainShape.h @@ -63,6 +63,30 @@ public: **/ void setPreviousVertex(float x, float y); + /** + * Returns whether a vertex that follows the last vertex exists. + * @returns True if specified vertex exists, else false. + **/ + bool hasNextVertex() const; + + /** + * Returns whether a vertex that precedes the first vertex exists. + * @returns True if specified vertex exists, else false. + **/ + bool hasPreviousVertex() const; + + /** + * Returns the vertex that follows the last vertex. + * @returns The specified vertex. + **/ + b2Vec2 getNextVertex() const; + + /** + * Returns the vertex that precedes the first vertex. + * @returns The specified vertex. + **/ + b2Vec2 getPreviousVertex() const; + /** * Returns a child EdgeShape. * @param index The index of the child shape. diff --git a/src/modules/physics/box2d/wrap_ChainShape.cpp b/src/modules/physics/box2d/wrap_ChainShape.cpp index 8cf7d8f85..42875059e 100644 --- a/src/modules/physics/box2d/wrap_ChainShape.cpp +++ b/src/modules/physics/box2d/wrap_ChainShape.cpp @@ -82,6 +82,34 @@ int w_ChainShape_getPoint(lua_State *L) return 2; } +int w_ChainShape_getNextVertex(lua_State *L) +{ + ChainShape *c = luax_checkchainshape(L, 1); + if (c->hasNextVertex()) + { + b2Vec2 v; + luax_catchexcept(L, [&](){ v = c->getNextVertex(); }); + lua_pushnumber(L, v.x); + lua_pushnumber(L, v.y); + return 2; + } + return 0; +} + +int w_ChainShape_getPreviousVertex(lua_State *L) +{ + ChainShape *c = luax_checkchainshape(L, 1); + if (c->hasPreviousVertex()) + { + b2Vec2 v; + luax_catchexcept(L, [&](){ v = c->getPreviousVertex(); }); + lua_pushnumber(L, v.x); + lua_pushnumber(L, v.y); + return 2; + } + return 0; +} + int w_ChainShape_getPoints(lua_State *L) { ChainShape *c = luax_checkchainshape(L, 1); @@ -102,6 +130,8 @@ static const luaL_Reg w_ChainShape_functions[] = { { "setNextVertex", w_ChainShape_setNextVertex }, { "setPreviousVertex", w_ChainShape_setPreviousVertex }, + { "getNextVertex", w_ChainShape_getNextVertex }, + { "getPreviousVertex", w_ChainShape_getPreviousVertex }, { "getChildEdge", w_ChainShape_getChildEdge }, { "getVertexCount", w_ChainShape_getVertexCount }, { "getPoint", w_ChainShape_getPoint },