From 09db462fd6ec07dfdb39d7968aac02750ca190ce Mon Sep 17 00:00:00 2001 From: Garrett Brown Date: Sun, 16 Aug 2020 19:01:59 -0400 Subject: [PATCH] Fixes --- src/libraries/Box2D/Box2D.h | 1 + src/libraries/Box2D/Common/b2_settings.cpp | 8 ++++ src/modules/physics/box2d/Body.cpp | 2 +- src/modules/physics/box2d/Body.h | 2 +- src/modules/physics/box2d/ChainShape.cpp | 12 ++--- src/modules/physics/box2d/ChainShape.h | 4 +- src/modules/physics/box2d/EdgeShape.cpp | 12 ++--- src/modules/physics/box2d/EdgeShape.h | 4 +- src/modules/physics/box2d/Physics.cpp | 30 ++++++++++-- src/modules/physics/box2d/wrap_Body.cpp | 4 +- src/modules/physics/box2d/wrap_ChainShape.cpp | 46 ++++++------------- .../physics/box2d/wrap_DistanceJoint.cpp | 16 +++---- src/modules/physics/box2d/wrap_EdgeShape.cpp | 46 ++++++------------- src/modules/physics/box2d/wrap_MouseJoint.cpp | 16 +++---- src/modules/physics/box2d/wrap_WeldJoint.cpp | 16 +++---- src/modules/physics/box2d/wrap_WheelJoint.cpp | 16 +++---- 16 files changed, 111 insertions(+), 124 deletions(-) diff --git a/src/libraries/Box2D/Box2D.h b/src/libraries/Box2D/Box2D.h index 71db86c58..516818f82 100644 --- a/src/libraries/Box2D/Box2D.h +++ b/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" diff --git a/src/libraries/Box2D/Common/b2_settings.cpp b/src/libraries/Box2D/Common/b2_settings.cpp index 11436f7de..2b8565c9e 100644 --- a/src/libraries/Box2D/Common/b2_settings.cpp +++ b/src/libraries/Box2D/Common/b2_settings.cpp @@ -27,6 +27,8 @@ #include #include +#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); +} diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index abba1a202..c61368636 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/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); } diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index f5281c486..723da4002 100644 --- a/src/modules/physics/box2d/Body.h +++ b/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. diff --git a/src/modules/physics/box2d/ChainShape.cpp b/src/modules/physics/box2d/ChainShape.cpp index 0d2a83de6..114e40beb 100644 --- a/src/modules/physics/box2d/ChainShape.cpp +++ b/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 diff --git a/src/modules/physics/box2d/ChainShape.h b/src/modules/physics/box2d/ChainShape.h index 67d7e7a7b..3afbc7365 100644 --- a/src/modules/physics/box2d/ChainShape.h +++ b/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. diff --git a/src/modules/physics/box2d/EdgeShape.cpp b/src/modules/physics/box2d/EdgeShape.cpp index 5810851e7..aad7a36e2 100644 --- a/src/modules/physics/box2d/EdgeShape.cpp +++ b/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) diff --git a/src/modules/physics/box2d/EdgeShape.h b/src/modules/physics/box2d/EdgeShape.h index 591606686..ac15b940b 100644 --- a/src/modules/physics/box2d/EdgeShape.h +++ b/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. diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index 4bc447bc7..33af95be1 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/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(L, 1); - Fixture *fixtureB = luax_checktype(L, 2); - return 0; + Fixture* fixtureA = luax_checktype(L, 1); + Fixture* fixtureB = luax_checktype(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) diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index 54703225e..67b9d9126 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/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; } diff --git a/src/modules/physics/box2d/wrap_ChainShape.cpp b/src/modules/physics/box2d/wrap_ChainShape.cpp index 02d823aa7..9f95e60ab 100644 --- a/src/modules/physics/box2d/wrap_ChainShape.cpp +++ b/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) diff --git a/src/modules/physics/box2d/wrap_DistanceJoint.cpp b/src/modules/physics/box2d/wrap_DistanceJoint.cpp index 615665922..1885d2d04 100644 --- a/src/modules/physics/box2d/wrap_DistanceJoint.cpp +++ b/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 } diff --git a/src/modules/physics/box2d/wrap_EdgeShape.cpp b/src/modules/physics/box2d/wrap_EdgeShape.cpp index 8d531d5cd..548fd82a2 100644 --- a/src/modules/physics/box2d/wrap_EdgeShape.cpp +++ b/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) diff --git a/src/modules/physics/box2d/wrap_MouseJoint.cpp b/src/modules/physics/box2d/wrap_MouseJoint.cpp index 023199cb2..0e9a3b333 100644 --- a/src/modules/physics/box2d/wrap_MouseJoint.cpp +++ b/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 } diff --git a/src/modules/physics/box2d/wrap_WeldJoint.cpp b/src/modules/physics/box2d/wrap_WeldJoint.cpp index ada93c2d5..123e36e37 100644 --- a/src/modules/physics/box2d/wrap_WeldJoint.cpp +++ b/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 }, diff --git a/src/modules/physics/box2d/wrap_WheelJoint.cpp b/src/modules/physics/box2d/wrap_WheelJoint.cpp index 11aef43f7..1ea4e9549 100644 --- a/src/modules/physics/box2d/wrap_WheelJoint.cpp +++ b/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 },