diff --git a/CMakeLists.txt b/CMakeLists.txt index 66088a08f..a74fd7056 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -199,6 +199,9 @@ Please see http://bitbucket.org/rude/megasource ${VORBISFILE_LIBRARY} ${LOVE_LUA_LIBRARY} ) + + # Work-around for gcc bug 1568899. + set(LOVE_LINK_LIBRARIES ${LOVE_LINK_LIBRARIES} gcc_s gcc) if(LOVE_MPG123) find_package(MPG123 REQUIRED) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml new file mode 100644 index 000000000..bb506983c --- /dev/null +++ b/bitbucket-pipelines.yml @@ -0,0 +1,7 @@ +image: mfcula/love-cmake:v1 +pipelines: + default: + - step: + script: + - cmake -G"Ninja" -H. -Bout + - ninja -C out \ No newline at end of file diff --git a/src/common/math.h b/src/common/math.h index a151ce628..0a77ee3b4 100644 --- a/src/common/math.h +++ b/src/common/math.h @@ -68,14 +68,6 @@ struct Vertex unsigned char r, g, b, a; }; -struct Triangle -{ - Triangle(const Vertex &x, const Vertex &y, const Vertex &z) - : a(x), b(y), c(z) - {} - Vertex a, b, c; -}; - inline int nextP2(int x) { x += (x == 0); diff --git a/src/modules/filesystem/wrap_Filesystem.cpp b/src/modules/filesystem/wrap_Filesystem.cpp index bb576d401..e68a4df27 100644 --- a/src/modules/filesystem/wrap_Filesystem.cpp +++ b/src/modules/filesystem/wrap_Filesystem.cpp @@ -218,6 +218,11 @@ FileData *luax_getfiledata(lua_State *L, int idx) return data; } +bool luax_cangetfiledata(lua_State *L, int idx) +{ + return lua_isstring(L, idx) || luax_istype(L, idx, FILESYSTEM_FILE_ID) || luax_istype(L, idx, FILESYSTEM_FILE_DATA_ID); +} + int w_newFileData(lua_State *L) { // Single argument: treat as filepath or File. diff --git a/src/modules/filesystem/wrap_Filesystem.h b/src/modules/filesystem/wrap_Filesystem.h index 0b27b3261..a010fd772 100644 --- a/src/modules/filesystem/wrap_Filesystem.h +++ b/src/modules/filesystem/wrap_Filesystem.h @@ -39,6 +39,7 @@ namespace filesystem * May trigger a Lua error. **/ FileData *luax_getfiledata(lua_State *L, int idx); +bool luax_cangetfiledata(lua_State *L, int idx); File *luax_getfile(lua_State *L, int idx); bool hack_setupWriteDirectory(); diff --git a/src/modules/graphics/opengl/Canvas.cpp b/src/modules/graphics/opengl/Canvas.cpp index 85475a637..bf05f820b 100644 --- a/src/modules/graphics/opengl/Canvas.cpp +++ b/src/modules/graphics/opengl/Canvas.cpp @@ -501,13 +501,15 @@ void Canvas::startGrab() gl.setFramebufferSRGB(false); } - if (attachedCanvases.size() == 0) - return; + if (attachedCanvases.size() > 0) + { + // Make sure the FBO is only using a single draw buffer. + // GLES3 only has glDrawBuffers, so we avoid using glDrawBuffer. + const GLenum buffers[] = {GL_COLOR_ATTACHMENT0}; + glDrawBuffers(1, buffers); - // Make sure the FBO is only using a single draw buffer. - glDrawBuffer(GL_COLOR_ATTACHMENT0); - - attachedCanvases.clear(); + attachedCanvases.clear(); + } } void Canvas::stopGrab(bool switchingToOtherCanvas) diff --git a/src/modules/graphics/opengl/wrap_Graphics.lua b/src/modules/graphics/opengl/wrap_Graphics.lua index 1a50be364..4a5ae629e 100644 --- a/src/modules/graphics/opengl/wrap_Graphics.lua +++ b/src/modules/graphics/opengl/wrap_Graphics.lua @@ -352,13 +352,17 @@ function love.graphics.newVideo(file, loadaudio) local video = love.graphics._newVideo(file) local source, success - if loadaudio ~= false then + if loadaudio ~= false and love.audio then success, source = pcall(love.audio.newSource, video:getStream():getFilename(), "stream") end if success then video:setSource(source) elseif loadaudio == true then - error("Video had no audio track", 2) + if love.audio then + error("Video had no audio track", 2) + else + error("love.audio was not loaded", 2) + end else video:getStream():setSync() end diff --git a/src/modules/image/wrap_Image.cpp b/src/modules/image/wrap_Image.cpp index 90a25b16a..96bcb05a1 100644 --- a/src/modules/image/wrap_Image.cpp +++ b/src/modules/image/wrap_Image.cpp @@ -77,19 +77,24 @@ int w_newImageData(lua_State *L) t->release(); return 1; } + else if (filesystem::luax_cangetfiledata(L, 1)) // Case 2: File(Data). + { + filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1); - // Case 2: File(Data). - love::filesystem::FileData *data = love::filesystem::luax_getfiledata(L, 1); + ImageData *t = nullptr; + luax_catchexcept(L, + [&]() { t = instance()->newImageData(data); }, + [&](bool) { data->release(); } + ); - ImageData *t = nullptr; - luax_catchexcept(L, - [&]() { t = instance()->newImageData(data); }, - [&](bool) { data->release(); } - ); - - luax_pushtype(L, IMAGE_IMAGE_DATA_ID, t); - t->release(); - return 1; + luax_pushtype(L, IMAGE_IMAGE_DATA_ID, t); + t->release(); + return 1; + } + else + { + return luax_typerror(L, 1, "value"); + } } int w_newCompressedData(lua_State *L) diff --git a/src/modules/math/MathModule.cpp b/src/modules/math/MathModule.cpp index 2c41f9608..02250e569 100644 --- a/src/modules/math/MathModule.cpp +++ b/src/modules/math/MathModule.cpp @@ -32,8 +32,7 @@ #include using std::list; -using std::vector; -using love::Vertex; +using love::Vector; namespace { @@ -117,14 +116,14 @@ love::uint8 *hexToBytes(const char *src, size_t srclen, size_t &dstlen) } // check if an angle is oriented counter clockwise -inline bool is_oriented_ccw(const Vertex &a, const Vertex &b, const Vertex &c) +inline bool is_oriented_ccw(const Vector &a, const Vector &b, const Vector &c) { // return det(b-a, c-a) >= 0 return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x)) >= 0; } // check if a and b are on the same side of the line c->d -bool on_same_side(const Vertex &a, const Vertex &b, const Vertex &c, const Vertex &d) +bool on_same_side(const Vector &a, const Vector &b, const Vector &c, const Vector &d) { float px = d.x - c.x, py = d.y - c.y; // return det(p, a-c) * det(p, b-c) >= 0 @@ -134,18 +133,16 @@ bool on_same_side(const Vertex &a, const Vertex &b, const Vertex &c, const Verte } // checks is p is contained in the triangle abc -inline bool point_in_triangle(const Vertex &p, const Vertex &a, const Vertex &b, const Vertex &c) +inline bool point_in_triangle(const Vector &p, const Vector &a, const Vector &b, const Vector &c) { return on_same_side(p,a, b,c) && on_same_side(p,b, a,c) && on_same_side(p,c, a,b); } // checks if any vertex in `vertices' is in the triangle abc. -bool any_point_in_triangle(const list &vertices, const Vertex &a, const Vertex &b, const Vertex &c) +bool any_point_in_triangle(const std::list &vertices, const Vector &a, const Vector &b, const Vector &c) { - list::const_iterator it, end = vertices.end(); - for (it = vertices.begin(); it != end; ++it) + for (const Vector *p : vertices) { - const Vertex *p = *it; if ((p != &a) && (p != &b) && (p != &c) && point_in_triangle(*p, a,b,c)) // oh god... return true; } @@ -153,7 +150,7 @@ bool any_point_in_triangle(const list &vertices, const Vertex &a return false; } -inline bool is_ear(const Vertex &a, const Vertex &b, const Vertex &c, const list &vertices) +inline bool is_ear(const Vector &a, const Vector &b, const Vector &c, const std::list &vertices) { return is_oriented_ccw(a,b,c) && !any_point_in_triangle(vertices, a,b,c); } @@ -165,20 +162,20 @@ namespace love namespace math { -vector triangulate(const vector &polygon) +std::vector triangulate(const std::vector &polygon) { if (polygon.size() < 3) throw love::Exception("Not a polygon"); else if (polygon.size() == 3) - return vector(1, Triangle(polygon[0], polygon[1], polygon[2])); + return std::vector(1, Triangle(polygon[0], polygon[1], polygon[2])); // collect list of connections and record leftmost item to check if the polygon // has the expected winding - vector next_idx(polygon.size()), prev_idx(polygon.size()); + std::vector next_idx(polygon.size()), prev_idx(polygon.size()); size_t idx_lm = 0; for (size_t i = 0; i < polygon.size(); ++i) { - const Vertex &lm = polygon[idx_lm], &p = polygon[i]; + const love::Vector &lm = polygon[idx_lm], &p = polygon[i]; if (p.x < lm.x || (p.x == lm.x && p.y < lm.y)) idx_lm = i; next_idx[i] = i+1; @@ -192,7 +189,7 @@ vector triangulate(const vector &polygon) next_idx.swap(prev_idx); // collect list of concave polygons - list concave_vertices; + std::list concave_vertices; for (size_t i = 0; i < polygon.size(); ++i) { if (!is_oriented_ccw(polygon[prev_idx[i]], polygon[i], polygon[next_idx[i]])) @@ -200,14 +197,14 @@ vector triangulate(const vector &polygon) } // triangulation according to kong - vector triangles; + std::vector triangles; size_t n_vertices = polygon.size(); size_t current = 1, skipped = 0, next, prev; while (n_vertices > 3) { next = next_idx[current]; prev = prev_idx[current]; - const Vertex &a = polygon[prev], &b = polygon[current], &c = polygon[next]; + const Vector &a = polygon[prev], &b = polygon[current], &c = polygon[next]; if (is_ear(a,b,c, concave_vertices)) { triangles.push_back(Triangle(a,b,c)); @@ -230,7 +227,7 @@ vector triangulate(const vector &polygon) return triangles; } -bool isConvex(const std::vector &polygon) +bool isConvex(const std::vector &polygon) { if (polygon.size() < 3) return false; @@ -372,7 +369,7 @@ RandomGenerator *Math::newRandomGenerator() return new RandomGenerator(); } -BezierCurve *Math::newBezierCurve(const vector &points) +BezierCurve *Math::newBezierCurve(const std::vector &points) { return new BezierCurve(points); } diff --git a/src/modules/math/MathModule.h b/src/modules/math/MathModule.h index b2b141d05..8937308b4 100644 --- a/src/modules/math/MathModule.h +++ b/src/modules/math/MathModule.h @@ -46,6 +46,14 @@ namespace math class BezierCurve; +struct Triangle +{ + Triangle(const Vector &x, const Vector &y, const Vector &z) + : a(x), b(y), c(z) + {} + Vector a, b, c; +}; + enum EncodeFormat { ENCODE_BASE64, @@ -59,7 +67,7 @@ enum EncodeFormat * @param polygon Polygon to triangulate. Must not intersect itself. * @return List of triangles the polygon is composed of. **/ -std::vector triangulate(const std::vector &polygon); +std::vector triangulate(const std::vector &polygon); /** * Checks whether a polygon is convex. @@ -67,7 +75,7 @@ std::vector triangulate(const std::vector &polygon); * @param polygon Polygon to test. * @return True if the polygon is convex, false otherwise. **/ -bool isConvex(const std::vector &polygon); +bool isConvex(const std::vector &polygon); /** * Converts a value from the sRGB (gamma) colorspace to linear RGB. diff --git a/src/modules/math/wrap_Math.cpp b/src/modules/math/wrap_Math.cpp index 255cd4c19..c53531c96 100644 --- a/src/modules/math/wrap_Math.cpp +++ b/src/modules/math/wrap_Math.cpp @@ -120,7 +120,7 @@ int w_newBezierCurve(lua_State *L) int w_triangulate(lua_State *L) { - std::vector vertices; + std::vector vertices; if (lua_istable(L, 1)) { int top = (int) luax_objlen(L, 1); @@ -130,7 +130,7 @@ int w_triangulate(lua_State *L) lua_rawgeti(L, 1, i); lua_rawgeti(L, 1, i+1); - Vertex v; + Vector v; v.x = (float) luaL_checknumber(L, -2); v.y = (float) luaL_checknumber(L, -1); vertices.push_back(v); @@ -144,7 +144,7 @@ int w_triangulate(lua_State *L) vertices.reserve(top / 2); for (int i = 1; i <= top; i += 2) { - Vertex v; + Vector v; v.x = (float) luaL_checknumber(L, i); v.y = (float) luaL_checknumber(L, i+1); vertices.push_back(v); @@ -190,7 +190,7 @@ int w_triangulate(lua_State *L) int w_isConvex(lua_State *L) { - std::vector vertices; + std::vector vertices; if (lua_istable(L, 1)) { int top = (int) luax_objlen(L, 1); @@ -200,7 +200,7 @@ int w_isConvex(lua_State *L) lua_rawgeti(L, 1, i); lua_rawgeti(L, 1, i+1); - Vertex v; + love::Vector v; v.x = (float) luaL_checknumber(L, -2); v.y = (float) luaL_checknumber(L, -1); vertices.push_back(v); @@ -214,7 +214,7 @@ int w_isConvex(lua_State *L) vertices.reserve(top / 2); for (int i = 1; i <= top; i += 2) { - Vertex v; + love::Vector v; v.x = (float) luaL_checknumber(L, i); v.y = (float) luaL_checknumber(L, i+1); vertices.push_back(v); diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index 778cc4566..e55be0c5e 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -228,11 +228,21 @@ RevoluteJoint *Physics::newRevoluteJoint(Body *body1, Body *body2, float xA, flo return new RevoluteJoint(body1, body2, xA, yA, xB, yB, collideConnected); } +RevoluteJoint *Physics::newRevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected, float referenceAngle) +{ + return new RevoluteJoint(body1, body2, xA, yA, xB, yB, collideConnected, referenceAngle); +} + PrismaticJoint *Physics::newPrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected) { return new PrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected); } +PrismaticJoint *Physics::newPrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected, float referenceAngle) +{ + return new PrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected, referenceAngle); +} + PulleyJoint *Physics::newPulleyJoint(Body *body1, Body *body2, b2Vec2 groundAnchor1, b2Vec2 groundAnchor2, b2Vec2 anchor1, b2Vec2 anchor2, float ratio, bool collideConnected) { return new PulleyJoint(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, ratio, collideConnected); @@ -253,6 +263,11 @@ WeldJoint *Physics::newWeldJoint(Body *body1, Body *body2, float xA, float yA, f return new WeldJoint(body1, body2, xA, yA, xB, yB, collideConnected); } +WeldJoint *Physics::newWeldJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected, float referenceAngle) +{ + return new WeldJoint(body1, body2, xA, yA, xB, yB, collideConnected, referenceAngle); +} + WheelJoint *Physics::newWheelJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected) { return new WheelJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected); diff --git a/src/modules/physics/box2d/Physics.h b/src/modules/physics/box2d/Physics.h index febe272b9..48089f5f9 100644 --- a/src/modules/physics/box2d/Physics.h +++ b/src/modules/physics/box2d/Physics.h @@ -176,9 +176,12 @@ public: * @param xB Anchor for body 2 along the x-axis. (World coordinates) * @param yB Anchor for body 2 along the y-axis. (World coordinates) * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false. + * @param referenceAngle The reference angle. **/ RevoluteJoint *newRevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected); + RevoluteJoint *newRevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected, float referenceAngle); + /** * Creates a new PrismaticJoint connecting body1 with body2. * @param xA World-anchor for body1 along the x-axis. @@ -188,9 +191,12 @@ public: * @param ax The x-component of the world-axis. * @param ay The y-component of the world-axis. * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false. + * @param referenceAngle The reference angle. **/ PrismaticJoint *newPrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected); + PrismaticJoint *newPrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected, float referenceAngle); + /** * Creates a new PulleyJoint connecting body1 with body2. * @param groundAnchor1 World ground-anchor for body1. @@ -228,9 +234,12 @@ public: * @param xB Anchor for body 2 along the x-axis. (World coordinates) * @param yB Anchor for body 2 along the y-axis. (World coordinates) * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false. + * @param referenceAngle The reference angle. **/ WeldJoint *newWeldJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected); + WeldJoint *newWeldJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected, float referenceAngle); + /** * Creates a new WheelJoint connecting body1 with body2. * @param xA Anchor for body 1 along the x-axis. (World coordinates) diff --git a/src/modules/physics/box2d/PrismaticJoint.cpp b/src/modules/physics/box2d/PrismaticJoint.cpp index e390405e8..8ee1e1084 100644 --- a/src/modules/physics/box2d/PrismaticJoint.cpp +++ b/src/modules/physics/box2d/PrismaticJoint.cpp @@ -37,18 +37,32 @@ PrismaticJoint::PrismaticJoint(Body *body1, Body *body2, float xA, float yA, flo , joint(NULL) { b2PrismaticJointDef def; + init(def, body1, body2, xA, yA, xB, yB, ax, ay, collideConnected); + joint = (b2PrismaticJoint *)createJoint(&def); +} +PrismaticJoint::PrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected, float referenceAngle) + : Joint(body1, body2) + , joint(NULL) +{ + b2PrismaticJointDef def; + init(def, body1, body2, xA, yA, xB, yB, ax, ay, collideConnected); + def.referenceAngle = referenceAngle; + joint = (b2PrismaticJoint *)createJoint(&def); +} + +PrismaticJoint::~PrismaticJoint() +{ +} + +void PrismaticJoint::init(b2PrismaticJointDef &def, Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected) +{ def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(xA,yA)), b2Vec2(ax,ay)); def.localAnchorB = body2->body->GetLocalPoint(Physics::scaleDown(b2Vec2(xB, yB))); def.lowerTranslation = 0.0f; def.upperTranslation = 100.0f; def.enableLimit = true; def.collideConnected = collideConnected; - joint = (b2PrismaticJoint *)createJoint(&def); -} - -PrismaticJoint::~PrismaticJoint() -{ } float PrismaticJoint::getJointTranslation() const @@ -147,6 +161,11 @@ int PrismaticJoint::getAxis(lua_State *L) return 2; } +float PrismaticJoint::getReferenceAngle() const +{ + return joint->GetReferenceAngle(); +} + } // box2d } // physics } // love diff --git a/src/modules/physics/box2d/PrismaticJoint.h b/src/modules/physics/box2d/PrismaticJoint.h index 584691544..dba1d4e70 100644 --- a/src/modules/physics/box2d/PrismaticJoint.h +++ b/src/modules/physics/box2d/PrismaticJoint.h @@ -44,6 +44,8 @@ public: **/ PrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected); + PrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected, float referenceAngle); + virtual ~PrismaticJoint(); /** @@ -141,10 +143,17 @@ public: **/ int getAxis(lua_State *L); + /** + * Gets the reference angle. + **/ + float getReferenceAngle() const; + private: // The Box2D prismatic joint object. b2PrismaticJoint *joint; + + void init(b2PrismaticJointDef &def, Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected); }; } // box2d diff --git a/src/modules/physics/box2d/RevoluteJoint.cpp b/src/modules/physics/box2d/RevoluteJoint.cpp index de9596328..ba1e1e87b 100644 --- a/src/modules/physics/box2d/RevoluteJoint.cpp +++ b/src/modules/physics/box2d/RevoluteJoint.cpp @@ -39,9 +39,17 @@ RevoluteJoint::RevoluteJoint(Body *body1, Body *body2, float xA, float yA, float , joint(NULL) { b2RevoluteJointDef def; - def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(xA,yA))); - def.localAnchorB = body2->body->GetLocalPoint(Physics::scaleDown(b2Vec2(xB, yB))); - def.collideConnected = collideConnected; + init(def, body1, body2, xA, yA, xB, yB, collideConnected); + joint = (b2RevoluteJoint *)createJoint(&def); +} + +RevoluteJoint::RevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected, float referenceAngle) + : Joint(body1, body2) + , joint(NULL) +{ + b2RevoluteJointDef def; + init(def, body1, body2, xA, yA, xB, yB, collideConnected); + def.referenceAngle = referenceAngle; joint = (b2RevoluteJoint *)createJoint(&def); } @@ -49,6 +57,13 @@ RevoluteJoint::~RevoluteJoint() { } +void RevoluteJoint::init(b2RevoluteJointDef &def, Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected) +{ + def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(xA,yA))); + def.localAnchorB = body2->body->GetLocalPoint(Physics::scaleDown(b2Vec2(xB, yB))); + def.collideConnected = collideConnected; +} + float RevoluteJoint::getJointAngle() const { return joint->GetJointAngle(); @@ -136,6 +151,11 @@ int RevoluteJoint::getLimits(lua_State *L) return 2; } +float RevoluteJoint::getReferenceAngle() const +{ + return joint->GetReferenceAngle(); +} + } // box2d } // physics } // love diff --git a/src/modules/physics/box2d/RevoluteJoint.h b/src/modules/physics/box2d/RevoluteJoint.h index ee490380d..f54026be5 100644 --- a/src/modules/physics/box2d/RevoluteJoint.h +++ b/src/modules/physics/box2d/RevoluteJoint.h @@ -44,6 +44,8 @@ public: **/ RevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected); + RevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected, float referenceAngle); + virtual ~RevoluteJoint(); /** @@ -134,10 +136,17 @@ public: **/ int getLimits(lua_State *L); + /** + * Gets the reference angle. + **/ + float getReferenceAngle() const; + private: // The Box2D revolute joint object. b2RevoluteJoint *joint; + + void init(b2RevoluteJointDef &def, Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected); }; } // box2d diff --git a/src/modules/physics/box2d/WeldJoint.cpp b/src/modules/physics/box2d/WeldJoint.cpp index 51b6b9f50..bc21a28f3 100644 --- a/src/modules/physics/box2d/WeldJoint.cpp +++ b/src/modules/physics/box2d/WeldJoint.cpp @@ -39,9 +39,17 @@ WeldJoint::WeldJoint(Body *body1, Body *body2, float xA, float yA, float xB, flo , joint(NULL) { b2WeldJointDef def; - def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(xA,yA))); - def.localAnchorB = body2->body->GetLocalPoint(Physics::scaleDown(b2Vec2(xB, yB))); - def.collideConnected = collideConnected; + init(def, body1, body2, xA, yA, xB, yB, collideConnected); + joint = (b2WeldJoint *)createJoint(&def); +} + +WeldJoint::WeldJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected, float referenceAngle) + : Joint(body1, body2) + , joint(NULL) +{ + b2WeldJointDef def; + init(def, body1, body2, xA, yA, xB, yB, collideConnected); + def.referenceAngle = referenceAngle; joint = (b2WeldJoint *)createJoint(&def); } @@ -49,6 +57,13 @@ WeldJoint::~WeldJoint() { } +void WeldJoint::init(b2WeldJointDef &def, Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected) +{ + def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(xA,yA))); + def.localAnchorB = body2->body->GetLocalPoint(Physics::scaleDown(b2Vec2(xB, yB))); + def.collideConnected = collideConnected; +} + void WeldJoint::setFrequency(float hz) { joint->SetFrequency(hz); @@ -69,6 +84,11 @@ float WeldJoint::getDampingRatio() const return joint->GetDampingRatio(); } +float WeldJoint::getReferenceAngle() const +{ + return joint->GetReferenceAngle(); +} + } // box2d } // physics } // love diff --git a/src/modules/physics/box2d/WeldJoint.h b/src/modules/physics/box2d/WeldJoint.h index 8ddbb9c8f..c3fa0a3d6 100644 --- a/src/modules/physics/box2d/WeldJoint.h +++ b/src/modules/physics/box2d/WeldJoint.h @@ -43,6 +43,8 @@ public: **/ WeldJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected); + WeldJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected, float referenceAngle); + virtual ~WeldJoint(); /** @@ -67,10 +69,17 @@ public: **/ float getDampingRatio() const; + /** + * Gets the reference angle. + **/ + float getReferenceAngle() const; + private: // The Box2D weld joint object. b2WeldJoint *joint; + + void init(b2WeldJointDef &def, Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected); }; } // box2d diff --git a/src/modules/physics/box2d/wrap_Physics.cpp b/src/modules/physics/box2d/wrap_Physics.cpp index 8ed78b48e..27dc7101a 100644 --- a/src/modules/physics/box2d/wrap_Physics.cpp +++ b/src/modules/physics/box2d/wrap_Physics.cpp @@ -219,11 +219,13 @@ int w_newRevoluteJoint(lua_State *L) float yA = (float)luaL_checknumber(L, 4); float xB, yB; bool collideConnected; + float referenceAngle = 0.0f; if (lua_gettop(L) >= 6) { xB = (float)luaL_checknumber(L, 5); yB = (float)luaL_checknumber(L, 6); collideConnected = luax_optboolean(L, 7, false); + referenceAngle = (float)luaL_optnumber(L, 8, referenceAngle); } else { @@ -233,7 +235,7 @@ int w_newRevoluteJoint(lua_State *L) } RevoluteJoint *j; luax_catchexcept(L, [&]() { - j = instance()->newRevoluteJoint(body1, body2, xA, yA, xB, yB, collideConnected); + j = instance()->newRevoluteJoint(body1, body2, xA, yA, xB, yB, collideConnected, referenceAngle); }); luax_pushtype(L, PHYSICS_REVOLUTE_JOINT_ID, j); j->release(); @@ -248,6 +250,7 @@ int w_newPrismaticJoint(lua_State *L) float yA = (float)luaL_checknumber(L, 4); float xB, yB, ax, ay; bool collideConnected; + float referenceAngle = 0.0f; if (lua_gettop(L) >= 8) { xB = (float)luaL_checknumber(L, 5); @@ -255,6 +258,7 @@ int w_newPrismaticJoint(lua_State *L) ax = (float)luaL_checknumber(L, 7); ay = (float)luaL_checknumber(L, 8); collideConnected = luax_optboolean(L, 9, false); + referenceAngle = (float)luaL_optnumber(L, 10, referenceAngle); } else { @@ -266,7 +270,7 @@ int w_newPrismaticJoint(lua_State *L) } PrismaticJoint *j; luax_catchexcept(L, [&]() { - j = instance()->newPrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected); + j = instance()->newPrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected, referenceAngle); }); luax_pushtype(L, PHYSICS_PRISMATIC_JOINT_ID, j); j->release(); @@ -350,11 +354,13 @@ int w_newWeldJoint(lua_State *L) float yA = (float)luaL_checknumber(L, 4); float xB, yB; bool collideConnected; + float referenceAngle = 0.0f; if (lua_gettop(L) >= 6) { xB = (float)luaL_checknumber(L, 5); yB = (float)luaL_checknumber(L, 6); collideConnected = luax_optboolean(L, 7, false); + referenceAngle = (float)luaL_optnumber(L, 8, referenceAngle); } else { @@ -364,7 +370,7 @@ int w_newWeldJoint(lua_State *L) } WeldJoint *j; luax_catchexcept(L, [&]() { - j = instance()->newWeldJoint(body1, body2, xA, yA, xB, yB, collideConnected); + j = instance()->newWeldJoint(body1, body2, xA, yA, xB, yB, collideConnected, referenceAngle); }); luax_pushtype(L, PHYSICS_WELD_JOINT_ID, j); j->release(); diff --git a/src/modules/physics/box2d/wrap_PrismaticJoint.cpp b/src/modules/physics/box2d/wrap_PrismaticJoint.cpp index c9e1b972c..a8c07331e 100644 --- a/src/modules/physics/box2d/wrap_PrismaticJoint.cpp +++ b/src/modules/physics/box2d/wrap_PrismaticJoint.cpp @@ -171,6 +171,13 @@ int w_PrismaticJoint_getAxis(lua_State *L) return t->getAxis(L); } +int w_PrismaticJoint_getReferenceAngle(lua_State *L) +{ + PrismaticJoint *t = luax_checkprismaticjoint(L, 1); + lua_pushnumber(L, t->getReferenceAngle()); + return 1; +} + static const luaL_Reg w_PrismaticJoint_functions[] = { { "getJointTranslation", w_PrismaticJoint_getJointTranslation }, @@ -191,6 +198,7 @@ static const luaL_Reg w_PrismaticJoint_functions[] = { "getUpperLimit", w_PrismaticJoint_getUpperLimit }, { "getLimits", w_PrismaticJoint_getLimits }, { "getAxis", w_PrismaticJoint_getAxis }, + { "getReferenceAngle", w_PrismaticJoint_getReferenceAngle }, { 0, 0 } }; diff --git a/src/modules/physics/box2d/wrap_RevoluteJoint.cpp b/src/modules/physics/box2d/wrap_RevoluteJoint.cpp index 9716e9944..10ef10580 100644 --- a/src/modules/physics/box2d/wrap_RevoluteJoint.cpp +++ b/src/modules/physics/box2d/wrap_RevoluteJoint.cpp @@ -164,6 +164,13 @@ int w_RevoluteJoint_getLimits(lua_State *L) return t->getLimits(L); } +int w_RevoluteJoint_getReferenceAngle(lua_State *L) +{ + RevoluteJoint *t = luax_checkrevolutejoint(L, 1); + lua_pushnumber(L, t->getReferenceAngle()); + return 1; +} + static const luaL_Reg w_RevoluteJoint_functions[] = { { "getJointAngle", w_RevoluteJoint_getJointAngle }, @@ -183,6 +190,7 @@ static const luaL_Reg w_RevoluteJoint_functions[] = { "getLowerLimit", w_RevoluteJoint_getLowerLimit }, { "getUpperLimit", w_RevoluteJoint_getUpperLimit }, { "getLimits", w_RevoluteJoint_getLimits }, + { "getReferenceAngle", w_RevoluteJoint_getReferenceAngle }, { 0, 0 } }; diff --git a/src/modules/physics/box2d/wrap_WeldJoint.cpp b/src/modules/physics/box2d/wrap_WeldJoint.cpp index 6b9ed8e75..92efc52a6 100644 --- a/src/modules/physics/box2d/wrap_WeldJoint.cpp +++ b/src/modules/physics/box2d/wrap_WeldJoint.cpp @@ -65,12 +65,20 @@ int w_WeldJoint_getDampingRatio(lua_State *L) return 1; } +int w_WeldJoint_getReferenceAngle(lua_State *L) +{ + WeldJoint *t = luax_checkweldjoint(L, 1); + lua_pushnumber(L, t->getReferenceAngle()); + return 1; +} + static const luaL_Reg w_WeldJoint_functions[] = { { "setFrequency", w_WeldJoint_setFrequency }, { "getFrequency", w_WeldJoint_getFrequency }, { "setDampingRatio", w_WeldJoint_setDampingRatio }, { "getDampingRatio", w_WeldJoint_getDampingRatio }, + { "getReferenceAngle", w_WeldJoint_getReferenceAngle }, { 0, 0 } };