From 9a2f99dc3de86b75af6ab62cfdba59cee875185a Mon Sep 17 00:00:00 2001 From: airstruck Date: Thu, 7 Jul 2016 21:18:35 +0200 Subject: [PATCH] Added 2-point version of love.physics.newRevoluteJoint (fixes #1175) --- changes.txt | 1 + src/modules/physics/box2d/Physics.cpp | 4 ++-- src/modules/physics/box2d/Physics.h | 8 +++++--- src/modules/physics/box2d/RevoluteJoint.cpp | 5 +++-- src/modules/physics/box2d/RevoluteJoint.h | 2 +- src/modules/physics/box2d/wrap_Physics.cpp | 21 +++++++++++++++++---- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/changes.txt b/changes.txt index 10f99fc13..9f7684f8d 100644 --- a/changes.txt +++ b/changes.txt @@ -7,6 +7,7 @@ Released: N/A * Added 'shaderswitches' field to the table returned by love.graphics.getStats. * Added Quad:getTextureDimensions. * Added PrismaticJoint:getAxis and WheelJoint:getAxis. + * Added 2-point version of love.physics.newRevoluteJoint. * Fixed love on iOS 6. * Fixed os.execute always returning -1 on Linux. diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index 8673c1ada..778cc4566 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -223,9 +223,9 @@ MouseJoint *Physics::newMouseJoint(Body *body, float x, float y) return new MouseJoint(body, x, y); } -RevoluteJoint *Physics::newRevoluteJoint(Body *body1, Body *body2, float x, float y, bool collideConnected) +RevoluteJoint *Physics::newRevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected) { - return new RevoluteJoint(body1, body2, x, y, collideConnected); + return new RevoluteJoint(body1, body2, xA, yA, xB, yB, collideConnected); } PrismaticJoint *Physics::newPrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected) diff --git a/src/modules/physics/box2d/Physics.h b/src/modules/physics/box2d/Physics.h index 113506379..febe272b9 100644 --- a/src/modules/physics/box2d/Physics.h +++ b/src/modules/physics/box2d/Physics.h @@ -171,11 +171,13 @@ public: /** * Creates a new RevoluteJoint connecting body1 with body2. - * @param x Anchor along the x-axis. (World coordinates) - * @param y Anchor along the y-axis. (World coordinates) + * @param xA Anchor for body 1 along the x-axis. (World coordinates) + * @param yA Anchor for body 1 along the y-axis. (World coordinates) + * @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. **/ - RevoluteJoint *newRevoluteJoint(Body *body1, Body *body2, float x, float y, bool collideConnected); + RevoluteJoint *newRevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected); /** * Creates a new PrismaticJoint connecting body1 with body2. diff --git a/src/modules/physics/box2d/RevoluteJoint.cpp b/src/modules/physics/box2d/RevoluteJoint.cpp index 513b1fa86..de9596328 100644 --- a/src/modules/physics/box2d/RevoluteJoint.cpp +++ b/src/modules/physics/box2d/RevoluteJoint.cpp @@ -34,12 +34,13 @@ namespace physics namespace box2d { -RevoluteJoint::RevoluteJoint(Body *body1, Body *body2, float x, float y, bool collideConnected) +RevoluteJoint::RevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected) : Joint(body1, body2) , joint(NULL) { b2RevoluteJointDef def; - def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(x,y))); + def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(xA,yA))); + def.localAnchorB = body2->body->GetLocalPoint(Physics::scaleDown(b2Vec2(xB, yB))); def.collideConnected = collideConnected; joint = (b2RevoluteJoint *)createJoint(&def); } diff --git a/src/modules/physics/box2d/RevoluteJoint.h b/src/modules/physics/box2d/RevoluteJoint.h index d3412e3c4..ee490380d 100644 --- a/src/modules/physics/box2d/RevoluteJoint.h +++ b/src/modules/physics/box2d/RevoluteJoint.h @@ -42,7 +42,7 @@ public: /** * Creates a new RevoluteJoint connecting body1 and body2. **/ - RevoluteJoint(Body *body1, Body *body2, float x, float y, bool collideConnected); + RevoluteJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected); virtual ~RevoluteJoint(); diff --git a/src/modules/physics/box2d/wrap_Physics.cpp b/src/modules/physics/box2d/wrap_Physics.cpp index ef6327c67..8ed78b48e 100644 --- a/src/modules/physics/box2d/wrap_Physics.cpp +++ b/src/modules/physics/box2d/wrap_Physics.cpp @@ -215,12 +215,25 @@ int w_newRevoluteJoint(lua_State *L) { Body *body1 = luax_checkbody(L, 1); Body *body2 = luax_checkbody(L, 2); - float x = (float)luaL_checknumber(L, 3); - float y = (float)luaL_checknumber(L, 4); - bool collideConnected = luax_optboolean(L, 5, false); + float xA = (float)luaL_checknumber(L, 3); + float yA = (float)luaL_checknumber(L, 4); + float xB, yB; + bool collideConnected; + if (lua_gettop(L) >= 6) + { + xB = (float)luaL_checknumber(L, 5); + yB = (float)luaL_checknumber(L, 6); + collideConnected = luax_optboolean(L, 7, false); + } + else + { + xB = xA; + yB = yA; + collideConnected = luax_optboolean(L, 5, false); + } RevoluteJoint *j; luax_catchexcept(L, [&]() { - j = instance()->newRevoluteJoint(body1, body2, x, y, collideConnected); + j = instance()->newRevoluteJoint(body1, body2, xA, yA, xB, yB, collideConnected); }); luax_pushtype(L, PHYSICS_REVOLUTE_JOINT_ID, j); j->release();