Browse Source

Added ChainShape:getNext/PreviousVertex (see issue #1191). Thanks airstruck!

Alex Szpakowski 9 years ago
parent
commit
3157e5df7b

+ 26 - 0
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;

+ 24 - 0
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.

+ 30 - 0
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 },