Garrett Brown 5 years ago
parent
commit
09db462fd6

+ 1 - 0
src/libraries/Box2D/Box2D.h

@@ -43,6 +43,7 @@
 #include "b2_time_step.h"
 #include "b2_world.h"
 #include "b2_world_callbacks.h"
+#include "b2_distance.h"
 
 #include "b2_distance_joint.h"
 #include "b2_friction_joint.h"

+ 8 - 0
src/libraries/Box2D/Common/b2_settings.cpp

@@ -27,6 +27,8 @@
 #include <stdarg.h>
 #include <stdlib.h>
 
+#include "common/Exception.h"
+
 b2Version b2_version = {2, 4, 0};
 
 // Memory allocators. Modify these to use your own allocator.
@@ -75,3 +77,9 @@ void b2CloseDump()
 	fclose(b2_dumpFile);
 	b2_dumpFile = nullptr;
 }
+
+void loveAssert(bool test, const char* teststr)
+{
+	if (!test)
+		throw love::Exception("Box2D assertion failed: %s", teststr);
+}

+ 1 - 1
src/modules/physics/box2d/Body.cpp

@@ -390,7 +390,7 @@ bool Body::isSleepingAllowed() const
 	return body->IsSleepingAllowed();
 }
 
-void Body::SetEnabled(bool enabled)
+void Body::setEnabled(bool enabled)
 {
 	body->SetEnabled(enabled);
 }

+ 1 - 1
src/modules/physics/box2d/Body.h

@@ -367,7 +367,7 @@ public:
 	/**
 	 * Changes the body's active state.
 	 **/
-	void SetEnabled(bool enabled);
+	void setEnabled(bool enabled);
 
 	/**
 	 * Changes the body's sleep state.

+ 4 - 8
src/modules/physics/box2d/ChainShape.cpp

@@ -57,22 +57,18 @@ void ChainShape::setPreviousVertex(float x, float y)
 	c->m_prevVertex = Physics::scaleDown(v);
 }
 
-void ChainShape::getNextVertex(float &x, float &y) const
+b2Vec2 ChainShape::getNextVertex() const
 {
 	b2ChainShape *c = (b2ChainShape *)shape;
 
-	b2Vec2 v = Physics::scaleUp(c->m_nextVertex);
-	x = v.x;
-	y = v.y;
+	return Physics::scaleUp(c->m_nextVertex);
 }
 
-void ChainShape::getPreviousVertex(float &x, float &y) const
+b2Vec2 ChainShape::getPreviousVertex() const
 {
 	b2ChainShape *c = (b2ChainShape *)shape;
 
-	b2Vec2 v = Physics::scaleUp(c->m_prevVertex);
-	x = v.x;
-	y = v.y;
+	return Physics::scaleUp(c->m_prevVertex);
 }
 
 EdgeShape *ChainShape::getChildEdge(int index) const

+ 2 - 2
src/modules/physics/box2d/ChainShape.h

@@ -68,12 +68,12 @@ public:
 	/**
 	 * Gets the vertex that follows the last vertex.
 	 **/
-	void getNextVertex(float &x, float &y) const;
+	b2Vec2 getNextVertex() const;
 
 	/**
 	 * Gets the vertex that precedes the first vertex.
 	 **/
-	void getPreviousVertex(float &x, float &y) const;
+	b2Vec2 getPreviousVertex() const;
 
 	/**
 	 * Returns a child EdgeShape.

+ 4 - 8
src/modules/physics/box2d/EdgeShape.cpp

@@ -50,13 +50,11 @@ void EdgeShape::setNextVertex(float x, float y)
 	e->m_vertex3 = Physics::scaleDown(v);
 }
 
-void EdgeShape::getNextVertex(float &x, float &y) const
+b2Vec2 EdgeShape::getNextVertex() const
 {
 	b2EdgeShape *e = (b2EdgeShape *)shape;
 
-	b2Vec2 v = Physics::scaleUp(e->m_vertex3);
-	x = v.x;
-	y = v.y;
+	return Physics::scaleUp(e->m_vertex3);
 }
 
 void EdgeShape::setPreviousVertex(float x, float y)
@@ -66,13 +64,11 @@ void EdgeShape::setPreviousVertex(float x, float y)
 	e->m_vertex0 = Physics::scaleDown(v);
 }
 
-void EdgeShape::getPreviousVertex(float &x, float &y) const
+b2Vec2 EdgeShape::getPreviousVertex() const
 {
 	b2EdgeShape *e = (b2EdgeShape *)shape;
 
-	b2Vec2 v = Physics::scaleUp(e->m_vertex0);
-	x = v.x;
-	y = v.y;
+	return Physics::scaleUp(e->m_vertex0);
 }
 
 int EdgeShape::getPoints(lua_State *L)

+ 2 - 2
src/modules/physics/box2d/EdgeShape.h

@@ -50,10 +50,10 @@ public:
 	virtual ~EdgeShape();
 
 	void setNextVertex(float x, float y);
-	void getNextVertex(float &x, float &y) const;
+	b2Vec2 getNextVertex() const;
 
 	void setPreviousVertex(float x, float y);
-	void getPreviousVertex(float &x, float &y) const;
+	b2Vec2 getPreviousVertex() const;
 
 	/**
 	 * Returns the transformed points of the edge shape.

+ 26 - 4
src/modules/physics/box2d/Physics.cpp

@@ -160,7 +160,7 @@ int Physics::newChainShape(lua_State *L)
 	if (istable)
 		argc = (int) luax_objlen(L, 2);
 
-	if (argc % 2 != 0)
+	if (argc == 0 || argc % 2 != 0)
 		return luaL_error(L, "Number of vertex components must be a multiple of two.");
 
 	int vcount = argc/2;
@@ -296,9 +296,31 @@ Fixture *Physics::newFixture(Body *body, Shape *shape, float density)
 
 int Physics::getDistance(lua_State *L)
 {
-	Fixture *fixtureA = luax_checktype<Fixture>(L, 1);
-	Fixture *fixtureB = luax_checktype<Fixture>(L, 2);
-	return 0;
+	Fixture* fixtureA = luax_checktype<Fixture>(L, 1);
+	Fixture* fixtureB = luax_checktype<Fixture>(L, 2);
+	b2DistanceProxy pA, pB;
+	b2DistanceInput i;
+	b2DistanceOutput o;
+	b2SimplexCache c;
+	c.count = 0;
+
+	luax_catchexcept(L, [&]() {
+		pA.Set(fixtureA->fixture->GetShape(), 0);
+		pB.Set(fixtureB->fixture->GetShape(), 0);
+		i.proxyA = pA;
+		i.proxyB = pB;
+		i.transformA = fixtureA->fixture->GetBody()->GetTransform();
+		i.transformB = fixtureB->fixture->GetBody()->GetTransform();
+		i.useRadii = true;
+		b2Distance(&o, &c, &i);
+		});
+
+	lua_pushnumber(L, Physics::scaleUp(o.distance));
+	lua_pushnumber(L, Physics::scaleUp(o.pointA.x));
+	lua_pushnumber(L, Physics::scaleUp(o.pointA.y));
+	lua_pushnumber(L, Physics::scaleUp(o.pointB.x));
+	lua_pushnumber(L, Physics::scaleUp(o.pointB.y));
+	return 5;
 }
 
 void Physics::setMeter(float scale)

+ 2 - 2
src/modules/physics/box2d/wrap_Body.cpp

@@ -490,7 +490,7 @@ int w_Body_setBullet(lua_State *L)
 int w_Body_isActive(lua_State *L)
 {
 	Body *t = luax_checkbody(L, 1);
-	luax_pushboolean(L, t->isActive());
+	luax_pushboolean(L, t->isEnabled());
 	return 1;
 }
 
@@ -520,7 +520,7 @@ int w_Body_setActive(lua_State *L)
 {
 	Body *t = luax_checkbody(L, 1);
 	bool b = luax_checkboolean(L, 2);
-	luax_catchexcept(L, [&](){ t->setActive(b); });
+	luax_catchexcept(L, [&](){ t->setEnabled(b); });
 	return 0;
 }
 

+ 14 - 32
src/modules/physics/box2d/wrap_ChainShape.cpp

@@ -37,28 +37,18 @@ ChainShape *luax_checkchainshape(lua_State *L, int idx)
 int w_ChainShape_setNextVertex(lua_State *L)
 {
 	ChainShape *c = luax_checkchainshape(L, 1);
-	if (lua_isnoneornil(L, 2))
-		c->setNextVertex();
-	else
-	{
-		float x = (float)luaL_checknumber(L, 2);
-		float y = (float)luaL_checknumber(L, 3);
-		luax_catchexcept(L, [&](){ c->setNextVertex(x, y); });
-	}
+	float x = (float)luaL_checknumber(L, 2);
+	float y = (float)luaL_checknumber(L, 3);
+	luax_catchexcept(L, [&](){ c->setNextVertex(x, y); });
 	return 0;
 }
 
 int w_ChainShape_setPreviousVertex(lua_State *L)
 {
 	ChainShape *c = luax_checkchainshape(L, 1);
-	if (lua_isnoneornil(L, 2))
-		c->setPreviousVertex();
-	else
-	{
-		float x = (float)luaL_checknumber(L, 2);
-		float y = (float)luaL_checknumber(L, 3);
-		luax_catchexcept(L, [&](){ c->setPreviousVertex(x, y); });
-	}
+	float x = (float)luaL_checknumber(L, 2);
+	float y = (float)luaL_checknumber(L, 3);
+	luax_catchexcept(L, [&](){ c->setPreviousVertex(x, y); });
 	return 0;
 }
 
@@ -95,27 +85,19 @@ int w_ChainShape_getPoint(lua_State *L)
 int w_ChainShape_getNextVertex(lua_State *L)
 {
 	ChainShape *c = luax_checkchainshape(L, 1);
-	float x, y;
-	if (c->getNextVertex(x, y))
-	{
-		lua_pushnumber(L, x);
-		lua_pushnumber(L, y);
-		return 2;
-	}
-	return 0;
+	b2Vec2 v = c->getNextVertex();
+	lua_pushnumber(L, v.x);
+	lua_pushnumber(L, v.y);
+	return 2;
 }
 
 int w_ChainShape_getPreviousVertex(lua_State *L)
 {
 	ChainShape *c = luax_checkchainshape(L, 1);
-	float x, y;
-	if (c->getPreviousVertex(x, y))
-	{
-		lua_pushnumber(L, x);
-		lua_pushnumber(L, y);
-		return 2;
-	}
-	return 0;
+	b2Vec2 v = c->getPreviousVertex();
+	lua_pushnumber(L, v.x);
+	lua_pushnumber(L, v.y);
+	return 2;
 }
 
 int w_ChainShape_getPoints(lua_State *L)

+ 8 - 8
src/modules/physics/box2d/wrap_DistanceJoint.cpp

@@ -50,18 +50,18 @@ int w_DistanceJoint_getLength(lua_State *L)
 	return 1;
 }
 
-int w_DistanceJoint_setFrequency(lua_State *L)
+int w_DistanceJoint_setStiffness(lua_State *L)
 {
 	DistanceJoint *t = luax_checkdistancejoint(L, 1);
 	float arg1 = (float)luaL_checknumber(L, 2);
-	t->setFrequency(arg1);
+	t->setStiffness(arg1);
 	return 0;
 }
 
-int w_DistanceJoint_getFrequency(lua_State *L)
+int w_DistanceJoint_getStiffness(lua_State *L)
 {
 	DistanceJoint *t = luax_checkdistancejoint(L, 1);
-	lua_pushnumber(L, t->getFrequency());
+	lua_pushnumber(L, t->getStiffness());
 	return 1;
 }
 
@@ -69,14 +69,14 @@ int w_DistanceJoint_setDampingRatio(lua_State *L)
 {
 	DistanceJoint *t = luax_checkdistancejoint(L, 1);
 	float arg1 = (float)luaL_checknumber(L, 2);
-	t->setDampingRatio(arg1);
+	t->setDamping(arg1);
 	return 0;
 }
 
 int w_DistanceJoint_getDampingRatio(lua_State *L)
 {
 	DistanceJoint *t = luax_checkdistancejoint(L, 1);
-	lua_pushnumber(L, t->getDampingRatio());
+	lua_pushnumber(L, t->getDamping());
 	return 1;
 }
 
@@ -84,8 +84,8 @@ static const luaL_Reg w_DistanceJoint_functions[] =
 {
 	{ "setLength", w_DistanceJoint_setLength },
 	{ "getLength", w_DistanceJoint_getLength },
-	{ "setFrequency", w_DistanceJoint_setFrequency },
-	{ "getFrequency", w_DistanceJoint_getFrequency },
+	{ "setStiffness", w_DistanceJoint_setStiffness },
+	{ "getStiffness", w_DistanceJoint_getStiffness },
 	{ "setDampingRatio", w_DistanceJoint_setDampingRatio },
 	{ "getDampingRatio", w_DistanceJoint_getDampingRatio },
 	{ 0, 0 }

+ 14 - 32
src/modules/physics/box2d/wrap_EdgeShape.cpp

@@ -35,55 +35,37 @@ EdgeShape *luax_checkedgeshape(lua_State *L, int idx)
 int w_EdgeShape_setNextVertex(lua_State *L)
 {
 	EdgeShape *t = luax_checkedgeshape(L, 1);
-	if (lua_isnoneornil(L, 2))
-		t->setNextVertex();
-	else
-	{
-		float x = (float)luaL_checknumber(L, 2);
-		float y = (float)luaL_checknumber(L, 3);
-		t->setNextVertex(x, y);
-	}
+	float x = (float)luaL_checknumber(L, 2);
+	float y = (float)luaL_checknumber(L, 3);
+	t->setNextVertex(x, y);
 	return 0;
 }
 
 int w_EdgeShape_setPreviousVertex(lua_State *L)
 {
 	EdgeShape *t = luax_checkedgeshape(L, 1);
-	if (lua_isnoneornil(L, 2))
-		t->setPreviousVertex();
-	else
-	{
-		float x = (float)luaL_checknumber(L, 2);
-		float y = (float)luaL_checknumber(L, 3);
-		t->setPreviousVertex(x, y);
-	}
+	float x = (float)luaL_checknumber(L, 2);
+	float y = (float)luaL_checknumber(L, 3);
+	t->setPreviousVertex(x, y);
 	return 0;
 }
 
 int w_EdgeShape_getNextVertex(lua_State *L)
 {
 	EdgeShape *t = luax_checkedgeshape(L, 1);
-	float x, y;
-	if (t->getNextVertex(x, y))
-	{
-		lua_pushnumber(L, x);
-		lua_pushnumber(L, y);
-		return 2;
-	}
-	return 0;
+	b2Vec2 v = t->getNextVertex();
+	lua_pushnumber(L, v.x);
+	lua_pushnumber(L, v.y);
+	return 2;
 }
 
 int w_EdgeShape_getPreviousVertex(lua_State *L)
 {
 	EdgeShape *t = luax_checkedgeshape(L, 1);
-	float x, y;
-	if (t->getPreviousVertex(x, y))
-	{
-		lua_pushnumber(L, x);
-		lua_pushnumber(L, y);
-		return 2;
-	}
-	return 0;
+	b2Vec2 v = t->getPreviousVertex();
+	lua_pushnumber(L, v.x);
+	lua_pushnumber(L, v.y);
+	return 2;
 }
 
 int w_EdgeShape_getPoints(lua_State *L)

+ 8 - 8
src/modules/physics/box2d/wrap_MouseJoint.cpp

@@ -66,18 +66,18 @@ int w_MouseJoint_getMaxForce(lua_State *L)
 	return 1;
 }
 
-int w_MouseJoint_setFrequency(lua_State *L)
+int w_MouseJoint_setStiffness(lua_State *L)
 {
 	MouseJoint *t = luax_checkmousejoint(L, 1);
 	float arg1 = (float)luaL_checknumber(L, 2);
-	luax_catchexcept(L, [&]() { t->setFrequency(arg1); });
+	luax_catchexcept(L, [&]() { t->setStiffness(arg1); });
 	return 0;
 }
 
-int w_MouseJoint_getFrequency(lua_State *L)
+int w_MouseJoint_getStiffness(lua_State *L)
 {
 	MouseJoint *t = luax_checkmousejoint(L, 1);
-	lua_pushnumber(L, t->getFrequency());
+	lua_pushnumber(L, t->getStiffness());
 	return 1;
 }
 
@@ -85,14 +85,14 @@ int w_MouseJoint_setDampingRatio(lua_State *L)
 {
 	MouseJoint *t = luax_checkmousejoint(L, 1);
 	float arg1 = (float)luaL_checknumber(L, 2);
-	t->setDampingRatio(arg1);
+	t->setDamping(arg1);
 	return 0;
 }
 
 int w_MouseJoint_getDampingRatio(lua_State *L)
 {
 	MouseJoint *t = luax_checkmousejoint(L, 1);
-	lua_pushnumber(L, t->getDampingRatio());
+	lua_pushnumber(L, t->getDamping());
 	return 1;
 }
 
@@ -102,8 +102,8 @@ static const luaL_Reg w_MouseJoint_functions[] =
 	{ "getTarget", w_MouseJoint_getTarget },
 	{ "setMaxForce", w_MouseJoint_setMaxForce },
 	{ "getMaxForce", w_MouseJoint_getMaxForce },
-	{ "setFrequency", w_MouseJoint_setFrequency },
-	{ "getFrequency", w_MouseJoint_getFrequency },
+	{ "setStiffness", w_MouseJoint_setStiffness },
+	{ "getStiffness", w_MouseJoint_getStiffness },
 	{ "setDampingRatio", w_MouseJoint_setDampingRatio },
 	{ "getDampingRatio", w_MouseJoint_getDampingRatio },
 	{ 0, 0 }

+ 8 - 8
src/modules/physics/box2d/wrap_WeldJoint.cpp

@@ -35,18 +35,18 @@ WeldJoint *luax_checkweldjoint(lua_State *L, int idx)
 	return j;
 }
 
-int w_WeldJoint_setFrequency(lua_State *L)
+int w_WeldJoint_setStiffness(lua_State *L)
 {
 	WeldJoint *t = luax_checkweldjoint(L, 1);
 	float arg1 = (float)luaL_checknumber(L, 2);
-	t->setFrequency(arg1);
+	t->setStiffness(arg1);
 	return 0;
 }
 
-int w_WeldJoint_getFrequency(lua_State *L)
+int w_WeldJoint_getStiffness(lua_State *L)
 {
 	WeldJoint *t = luax_checkweldjoint(L, 1);
-	lua_pushnumber(L, t->getFrequency());
+	lua_pushnumber(L, t->getStiffness());
 	return 1;
 }
 
@@ -54,14 +54,14 @@ int w_WeldJoint_setDampingRatio(lua_State *L)
 {
 	WeldJoint *t = luax_checkweldjoint(L, 1);
 	float arg1 = (float)luaL_checknumber(L, 2);
-	t->setDampingRatio(arg1);
+	t->setDamping(arg1);
 	return 0;
 }
 
 int w_WeldJoint_getDampingRatio(lua_State *L)
 {
 	WeldJoint *t = luax_checkweldjoint(L, 1);
-	lua_pushnumber(L, t->getDampingRatio());
+	lua_pushnumber(L, t->getDamping());
 	return 1;
 }
 
@@ -74,8 +74,8 @@ int w_WeldJoint_getReferenceAngle(lua_State *L)
 
 static const luaL_Reg w_WeldJoint_functions[] =
 {
-	{ "setFrequency", w_WeldJoint_setFrequency },
-	{ "getFrequency", w_WeldJoint_getFrequency },
+	{ "setStiffness", w_WeldJoint_setStiffness },
+	{ "getStiffness", w_WeldJoint_getStiffness },
 	{ "setDampingRatio", w_WeldJoint_setDampingRatio },
 	{ "getDampingRatio", w_WeldJoint_getDampingRatio },
 	{ "getReferenceAngle", w_WeldJoint_getReferenceAngle },

+ 8 - 8
src/modules/physics/box2d/wrap_WheelJoint.cpp

@@ -102,18 +102,18 @@ int w_WheelJoint_getMotorTorque(lua_State *L)
 	return 1;
 }
 
-int w_WheelJoint_setSpringFrequency(lua_State *L)
+int w_WheelJoint_setSpringStiffness(lua_State *L)
 {
 	WheelJoint *t = luax_checkwheeljoint(L, 1);
 	float arg1 = (float)luaL_checknumber(L, 2);
-	t->setSpringFrequency(arg1);
+	t->setStiffness(arg1);
 	return 0;
 }
 
-int w_WheelJoint_getSpringFrequency(lua_State *L)
+int w_WheelJoint_getSpringStiffness(lua_State *L)
 {
 	WheelJoint *t = luax_checkwheeljoint(L, 1);
-	lua_pushnumber(L, t->getSpringFrequency());
+	lua_pushnumber(L, t->getStiffness());
 	return 1;
 }
 
@@ -121,14 +121,14 @@ int w_WheelJoint_setSpringDampingRatio(lua_State *L)
 {
 	WheelJoint *t = luax_checkwheeljoint(L, 1);
 	float arg1 = (float)luaL_checknumber(L, 2);
-	t->setSpringDampingRatio(arg1);
+	t->setDamping(arg1);
 	return 0;
 }
 
 int w_WheelJoint_getSpringDampingRatio(lua_State *L)
 {
 	WheelJoint *t = luax_checkwheeljoint(L, 1);
-	lua_pushnumber(L, t->getSpringDampingRatio());
+	lua_pushnumber(L, t->getDamping());
 	return 1;
 }
 
@@ -150,8 +150,8 @@ static const luaL_Reg w_WheelJoint_functions[] =
 	{ "setMaxMotorTorque", w_WheelJoint_setMaxMotorTorque },
 	{ "getMaxMotorTorque", w_WheelJoint_getMaxMotorTorque },
 	{ "getMotorTorque", w_WheelJoint_getMotorTorque },
-	{ "setSpringFrequency", w_WheelJoint_setSpringFrequency },
-	{ "getSpringFrequency", w_WheelJoint_getSpringFrequency },
+	{ "setSpringStiffness", w_WheelJoint_setSpringStiffness },
+	{ "getSpringStiffness", w_WheelJoint_getSpringStiffness },
 	{ "setSpringDampingRatio", w_WheelJoint_setSpringDampingRatio },
 	{ "getSpringDampingRatio", w_WheelJoint_getSpringDampingRatio },
 	{ "getAxis", w_WheelJoint_getAxis },