diff --git a/platform/macosx/love-framework.xcodeproj/project.pbxproj b/platform/macosx/love-framework.xcodeproj/project.pbxproj index 3c0240316..32bda4b1c 100644 --- a/platform/macosx/love-framework.xcodeproj/project.pbxproj +++ b/platform/macosx/love-framework.xcodeproj/project.pbxproj @@ -206,6 +206,12 @@ FA08F67A16C754B100F007B5 /* wrap_Timer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 695E4ED13AA0689E64280573 /* wrap_Timer.cpp */; }; FA08F67B16C754BA00F007B5 /* Window.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 351B09E51FDC338622F44624 /* Window.cpp */; }; FA08F67C16C754BA00F007B5 /* Window.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6CDD4F3320303D222C180CD0 /* Window.cpp */; }; + FA0A4BF0182E0C2800E1E4D2 /* b2MotorJoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0A4BEE182E0C2800E1E4D2 /* b2MotorJoint.cpp */; }; + FA0A4BF1182E0C2800E1E4D2 /* b2MotorJoint.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0A4BEF182E0C2800E1E4D2 /* b2MotorJoint.h */; }; + FA0A4BF4182E1AD600E1E4D2 /* MotorJoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0A4BF2182E1AD600E1E4D2 /* MotorJoint.cpp */; }; + FA0A4BF5182E1AD600E1E4D2 /* MotorJoint.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0A4BF3182E1AD600E1E4D2 /* MotorJoint.h */; }; + FA0A4BF9182E260200E1E4D2 /* wrap_MotorJoint.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0A4BF7182E260200E1E4D2 /* wrap_MotorJoint.h */; }; + FA0A4BFA182E26F500E1E4D2 /* wrap_MotorJoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA0A4BF6182E260200E1E4D2 /* wrap_MotorJoint.cpp */; }; FA0CDE3D1710F9A50056E8D7 /* FormatHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = FA0CDE3B1710F9A50056E8D7 /* FormatHandler.h */; }; FA1EF7C41799FEAC00FF380C /* wrap_System.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FA34E7D4174B55AF00E04D3F /* wrap_System.cpp */; }; FA1EF7C51799FEB200FF380C /* wrap_System.h in Headers */ = {isa = PBXBuildFile; fileRef = FA34E7D5174B55AF00E04D3F /* wrap_System.h */; }; @@ -749,6 +755,12 @@ FA03546A1731F3A700284828 /* simplexnoise1234.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = simplexnoise1234.cpp; sourceTree = ""; }; FA03546B1731F3A700284828 /* simplexnoise1234.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = simplexnoise1234.h; sourceTree = ""; }; FA08F5AE16C7525600F007B5 /* Info-Framework.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-Framework.plist"; sourceTree = ""; }; + FA0A4BEE182E0C2800E1E4D2 /* b2MotorJoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = b2MotorJoint.cpp; sourceTree = ""; }; + FA0A4BEF182E0C2800E1E4D2 /* b2MotorJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = b2MotorJoint.h; sourceTree = ""; }; + FA0A4BF2182E1AD600E1E4D2 /* MotorJoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MotorJoint.cpp; sourceTree = ""; }; + FA0A4BF3182E1AD600E1E4D2 /* MotorJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MotorJoint.h; sourceTree = ""; }; + FA0A4BF6182E260200E1E4D2 /* wrap_MotorJoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wrap_MotorJoint.cpp; sourceTree = ""; }; + FA0A4BF7182E260200E1E4D2 /* wrap_MotorJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wrap_MotorJoint.h; sourceTree = ""; }; FA0CDE3B1710F9A50056E8D7 /* FormatHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FormatHandler.h; sourceTree = ""; }; FA34E7CB174B513F00E04D3F /* System.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = System.cpp; sourceTree = ""; }; FA34E7CC174B513F00E04D3F /* System.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = System.h; sourceTree = ""; }; @@ -1202,6 +1214,8 @@ 25CE236F66F70EB3444A7CC8 /* GearJoint.h */, 3EA80A4E0CE0014052076037 /* Joint.cpp */, 138913BE5126483748FA43D0 /* Joint.h */, + FA0A4BF2182E1AD600E1E4D2 /* MotorJoint.cpp */, + FA0A4BF3182E1AD600E1E4D2 /* MotorJoint.h */, 3C0B06AF6B5326C840477B18 /* MouseJoint.cpp */, 208275724C9421035EA145A4 /* MouseJoint.h */, 370D76DC224F2EB300CB4E2F /* Physics.cpp */, @@ -1244,6 +1258,8 @@ 7BAB25936D207169591A666A /* wrap_GearJoint.h */, 71810207414B52F8340D7797 /* wrap_Joint.cpp */, 047815B73C1C5373551442A6 /* wrap_Joint.h */, + FA0A4BF6182E260200E1E4D2 /* wrap_MotorJoint.cpp */, + FA0A4BF7182E260200E1E4D2 /* wrap_MotorJoint.h */, 4D81102E7ABD1C282BE42CE3 /* wrap_MouseJoint.cpp */, 64BA1CE328FF17144B475111 /* wrap_MouseJoint.h */, 6CEB48E969FC42C53F9432B1 /* wrap_Physics.cpp */, @@ -1569,6 +1585,8 @@ 6590063A6E4B3AEF4550443C /* b2GearJoint.h */, 6F1B61350B6B36AF216C57D7 /* b2Joint.cpp */, 10F83B5848B77A937C250FEB /* b2Joint.h */, + FA0A4BEE182E0C2800E1E4D2 /* b2MotorJoint.cpp */, + FA0A4BEF182E0C2800E1E4D2 /* b2MotorJoint.h */, 4A47384208BE218F688C4EFA /* b2MouseJoint.cpp */, 0AA1539E66B2641B66130709 /* b2MouseJoint.h */, 1E27263847302FCA1F843B47 /* b2PrismaticJoint.cpp */, @@ -1944,6 +1962,7 @@ FAF272A916E3D44400CC193A /* ThreadModule.h in Headers */, FAF6705018184FF800DBDEEA /* wuff_convert.h in Headers */, FAF272AB16E3D44400CC193A /* wrap_Channel.h in Headers */, + FA0A4BF9182E260200E1E4D2 /* wrap_MotorJoint.h in Headers */, FAF272AD16E3D44400CC193A /* wrap_LuaThread.h in Headers */, FAF272AF16E3D44400CC193A /* wrap_ThreadModule.h in Headers */, FAF272B616E3D46400CC193A /* Thread.h in Headers */, @@ -1981,7 +2000,9 @@ FA5FDC811788D548002F0ED2 /* protocol.h in Headers */, FA5FDC821788D548002F0ED2 /* time.h in Headers */, FA5FDC831788D548002F0ED2 /* types.h in Headers */, + FA0A4BF5182E1AD600E1E4D2 /* MotorJoint.h in Headers */, FA5FDC841788D548002F0ED2 /* unix.h in Headers */, + FA0A4BF1182E0C2800E1E4D2 /* b2MotorJoint.h in Headers */, FA5FDC851788D548002F0ED2 /* utility.h in Headers */, FAF6704D18184FF800DBDEEA /* wuff.h in Headers */, FA5FDC861788D548002F0ED2 /* win32.h in Headers */, @@ -2093,6 +2114,7 @@ FA08F5D216C7534400F007B5 /* b2PolygonShape.cpp in Sources */, FA08F5D316C7535000F007B5 /* b2BlockAllocator.cpp in Sources */, FA08F5D416C7535000F007B5 /* b2Draw.cpp in Sources */, + FA0A4BF4182E1AD600E1E4D2 /* MotorJoint.cpp in Sources */, FA08F5D516C7535000F007B5 /* b2Math.cpp in Sources */, FA08F5D616C7535000F007B5 /* b2Settings.cpp in Sources */, FA08F5D716C7535000F007B5 /* b2StackAllocator.cpp in Sources */, @@ -2180,6 +2202,7 @@ FA08F62916C7541400F007B5 /* wrap_Image.cpp in Sources */, FA08F62A16C7541400F007B5 /* wrap_ParticleSystem.cpp in Sources */, FA08F62C16C7541400F007B5 /* wrap_Shader.cpp in Sources */, + FA0A4BFA182E26F500E1E4D2 /* wrap_MotorJoint.cpp in Sources */, FA08F62D16C7541400F007B5 /* wrap_SpriteBatch.cpp in Sources */, FA08F62E16C7542600F007B5 /* ImageData.cpp in Sources */, FA08F62F16C7542600F007B5 /* Image.cpp in Sources */, @@ -2262,6 +2285,7 @@ FA7C937A16DCC6C2006F2BEE /* wrap_Math.cpp in Sources */, FAF272A416E3D44400CC193A /* Channel.cpp in Sources */, FAF272A616E3D44400CC193A /* LuaThread.cpp in Sources */, + FA0A4BF0182E0C2800E1E4D2 /* b2MotorJoint.cpp in Sources */, FAF272A816E3D44400CC193A /* ThreadModule.cpp in Sources */, FAF272AA16E3D44400CC193A /* wrap_Channel.cpp in Sources */, FAF272AC16E3D44400CC193A /* wrap_LuaThread.cpp in Sources */, diff --git a/src/common/runtime.cpp b/src/common/runtime.cpp index 3bad959a9..75558edd9 100644 --- a/src/common/runtime.cpp +++ b/src/common/runtime.cpp @@ -722,6 +722,7 @@ StringMap::Entry typeEntries[] = {"WeldJoint", PHYSICS_WELD_JOINT_ID}, {"RopeJoint", PHYSICS_ROPE_JOINT_ID}, {"WheelJoint", PHYSICS_WHEEL_JOINT_ID}, + {"MotorJoint", PHYSICS_MOTOR_JOINT_ID}, // Thread {"Thread", THREAD_THREAD_ID}, diff --git a/src/common/types.h b/src/common/types.h index c6f56f6e4..a161a80b3 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -97,6 +97,7 @@ enum Type PHYSICS_WELD_JOINT_ID, PHYSICS_ROPE_JOINT_ID, PHYSICS_WHEEL_JOINT_ID, + PHYSICS_MOTOR_JOINT_ID, // Thread THREAD_THREAD_ID, @@ -181,6 +182,7 @@ const bits PHYSICS_FRICTION_JOINT_T = (bits(1) << PHYSICS_FRICTION_JOINT_ID) | P const bits PHYSICS_WELD_JOINT_T = (bits(1) << PHYSICS_WELD_JOINT_ID) | PHYSICS_JOINT_T; const bits PHYSICS_ROPE_JOINT_T = (bits(1) << PHYSICS_ROPE_JOINT_ID) | PHYSICS_JOINT_T; const bits PHYSICS_WHEEL_JOINT_T = (bits(1) << PHYSICS_WHEEL_JOINT_ID) | PHYSICS_JOINT_T; +const bits PHYSICS_MOTOR_JOINT_T = (bits(1) << PHYSICS_MOTOR_JOINT_ID) | PHYSICS_JOINT_T; // Thread. const bits THREAD_THREAD_T = (bits(1) << THREAD_THREAD_ID) | OBJECT_T; diff --git a/src/libraries/Box2D/Box2D.h b/src/libraries/Box2D/Box2D.h index f674d8226..187046131 100755 --- a/src/libraries/Box2D/Box2D.h +++ b/src/libraries/Box2D/Box2D.h @@ -56,12 +56,13 @@ For discussion please visit http://box2d.org/forum #include #include #include -#include +#include #include #include #include #include #include #include +#include #endif diff --git a/src/libraries/Box2D/Collision/Shapes/b2ChainShape.cpp b/src/libraries/Box2D/Collision/Shapes/b2ChainShape.cpp index bd23346a5..069b4cae8 100755 --- a/src/libraries/Box2D/Collision/Shapes/b2ChainShape.cpp +++ b/src/libraries/Box2D/Collision/Shapes/b2ChainShape.cpp @@ -19,8 +19,7 @@ #include #include #include -#include -using namespace std; +#include b2ChainShape::~b2ChainShape() { @@ -33,6 +32,14 @@ void b2ChainShape::CreateLoop(const b2Vec2* vertices, int32 count) { b2Assert(m_vertices == NULL && m_count == 0); b2Assert(count >= 3); + for (int32 i = 1; i < count; ++i) + { + b2Vec2 v1 = vertices[i-1]; + b2Vec2 v2 = vertices[i]; + // If the code crashes here, it means your vertices are too close together. + b2Assert(b2DistanceSquared(v1, v2) > b2_linearSlop * b2_linearSlop); + } + m_count = count + 1; m_vertices = (b2Vec2*)b2Alloc(m_count * sizeof(b2Vec2)); memcpy(m_vertices, vertices, count * sizeof(b2Vec2)); @@ -47,11 +54,23 @@ void b2ChainShape::CreateChain(const b2Vec2* vertices, int32 count) { b2Assert(m_vertices == NULL && m_count == 0); b2Assert(count >= 2); + for (int32 i = 1; i < count; ++i) + { + b2Vec2 v1 = vertices[i-1]; + b2Vec2 v2 = vertices[i]; + // If the code crashes here, it means your vertices are too close together. + b2Assert(b2DistanceSquared(v1, v2) > b2_linearSlop * b2_linearSlop); + } + m_count = count; m_vertices = (b2Vec2*)b2Alloc(count * sizeof(b2Vec2)); memcpy(m_vertices, vertices, m_count * sizeof(b2Vec2)); + m_hasPrevVertex = false; m_hasNextVertex = false; + + m_prevVertex.SetZero(); + m_nextVertex.SetZero(); } void b2ChainShape::SetPrevVertex(const b2Vec2& prevVertex) diff --git a/src/libraries/Box2D/Collision/Shapes/b2ChainShape.h b/src/libraries/Box2D/Collision/Shapes/b2ChainShape.h index 046a29890..e836dfef9 100755 --- a/src/libraries/Box2D/Collision/Shapes/b2ChainShape.h +++ b/src/libraries/Box2D/Collision/Shapes/b2ChainShape.h @@ -14,9 +14,6 @@ * 2. Altered source versions must be plainly marked as such, and must not be * misrepresented as being the original software. * 3. This notice may not be removed or altered from any source distribution. -* -* -* THIS FILE HAS BEEN MODIFIED BY THE LÖVE DEVELOPMENT TEAM */ #ifndef B2_CHAIN_SHAPE_H diff --git a/src/libraries/Box2D/Collision/Shapes/b2CircleShape.cpp b/src/libraries/Box2D/Collision/Shapes/b2CircleShape.cpp index d494b134a..39d4ae999 100755 --- a/src/libraries/Box2D/Collision/Shapes/b2CircleShape.cpp +++ b/src/libraries/Box2D/Collision/Shapes/b2CircleShape.cpp @@ -18,7 +18,6 @@ #include #include -using namespace std; b2Shape* b2CircleShape::Clone(b2BlockAllocator* allocator) const { diff --git a/src/libraries/Box2D/Collision/Shapes/b2EdgeShape.cpp b/src/libraries/Box2D/Collision/Shapes/b2EdgeShape.cpp index cbac41cfa..8bcd12f7a 100755 --- a/src/libraries/Box2D/Collision/Shapes/b2EdgeShape.cpp +++ b/src/libraries/Box2D/Collision/Shapes/b2EdgeShape.cpp @@ -18,7 +18,6 @@ #include #include -using namespace std; void b2EdgeShape::Set(const b2Vec2& v1, const b2Vec2& v2) { @@ -105,11 +104,11 @@ bool b2EdgeShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& input, output->fraction = t; if (numerator > 0.0f) { - output->normal = -normal; + output->normal = -b2Mul(xf.q, normal); } else { - output->normal = normal; + output->normal = b2Mul(xf.q, normal); } return true; } diff --git a/src/libraries/Box2D/Collision/Shapes/b2PolygonShape.cpp b/src/libraries/Box2D/Collision/Shapes/b2PolygonShape.cpp index 499e168f8..2f32e9fe2 100755 --- a/src/libraries/Box2D/Collision/Shapes/b2PolygonShape.cpp +++ b/src/libraries/Box2D/Collision/Shapes/b2PolygonShape.cpp @@ -29,7 +29,7 @@ b2Shape* b2PolygonShape::Clone(b2BlockAllocator* allocator) const void b2PolygonShape::SetAsBox(float32 hx, float32 hy) { - m_vertexCount = 4; + m_count = 4; m_vertices[0].Set(-hx, -hy); m_vertices[1].Set( hx, -hy); m_vertices[2].Set( hx, hy); @@ -43,7 +43,7 @@ void b2PolygonShape::SetAsBox(float32 hx, float32 hy) void b2PolygonShape::SetAsBox(float32 hx, float32 hy, const b2Vec2& center, float32 angle) { - m_vertexCount = 4; + m_count = 4; m_vertices[0].Set(-hx, -hy); m_vertices[1].Set( hx, -hy); m_vertices[2].Set( hx, hy); @@ -59,7 +59,7 @@ void b2PolygonShape::SetAsBox(float32 hx, float32 hy, const b2Vec2& center, floa xf.q.Set(angle); // Transform vertices and normals. - for (int32 i = 0; i < m_vertexCount; ++i) + for (int32 i = 0; i < m_count; ++i) { m_vertices[i] = b2Mul(xf, m_vertices[i]); m_normals[i] = b2Mul(xf.q, m_normals[i]); @@ -120,61 +120,131 @@ static b2Vec2 ComputeCentroid(const b2Vec2* vs, int32 count) void b2PolygonShape::Set(const b2Vec2* vertices, int32 count) { b2Assert(3 <= count && count <= b2_maxPolygonVertices); - m_vertexCount = count; + if (count < 3) + { + SetAsBox(1.0f, 1.0f); + return; + } + + int32 n = b2Min(count, b2_maxPolygonVertices); + + // Perform welding and copy vertices into local buffer. + b2Vec2 ps[b2_maxPolygonVertices]; + int32 tempCount = 0; + for (int32 i = 0; i < n; ++i) + { + b2Vec2 v = vertices[i]; + + bool unique = true; + for (int32 j = 0; j < tempCount; ++j) + { + if (b2DistanceSquared(v, ps[j]) < 0.5f * b2_linearSlop) + { + unique = false; + break; + } + } + + if (unique) + { + ps[tempCount++] = v; + } + } + + n = tempCount; + if (n < 3) + { + // Polygon is degenerate. + b2Assert(false); + SetAsBox(1.0f, 1.0f); + return; + } + + // Create the convex hull using the Gift wrapping algorithm + // http://en.wikipedia.org/wiki/Gift_wrapping_algorithm + + // Find the right most point on the hull + int32 i0 = 0; + float32 x0 = ps[0].x; + for (int32 i = 1; i < n; ++i) + { + float32 x = ps[i].x; + if (x > x0 || (x == x0 && ps[i].y < ps[i0].y)) + { + i0 = i; + x0 = x; + } + } + + int32 hull[b2_maxPolygonVertices]; + int32 m = 0; + int32 ih = i0; + + for (;;) + { + hull[m] = ih; + + int32 ie = 0; + for (int32 j = 1; j < n; ++j) + { + if (ie == ih) + { + ie = j; + continue; + } + + b2Vec2 r = ps[ie] - ps[hull[m]]; + b2Vec2 v = ps[j] - ps[hull[m]]; + float32 c = b2Cross(r, v); + if (c < 0.0f) + { + ie = j; + } + + // Collinearity check + if (c == 0.0f && v.LengthSquared() > r.LengthSquared()) + { + ie = j; + } + } + + ++m; + ih = ie; + + if (ie == i0) + { + break; + } + } + + m_count = m; // Copy vertices. - for (int32 i = 0; i < m_vertexCount; ++i) + for (int32 i = 0; i < m; ++i) { - m_vertices[i] = vertices[i]; + m_vertices[i] = ps[hull[i]]; } // Compute normals. Ensure the edges have non-zero length. - for (int32 i = 0; i < m_vertexCount; ++i) + for (int32 i = 0; i < m; ++i) { int32 i1 = i; - int32 i2 = i + 1 < m_vertexCount ? i + 1 : 0; + int32 i2 = i + 1 < m ? i + 1 : 0; b2Vec2 edge = m_vertices[i2] - m_vertices[i1]; b2Assert(edge.LengthSquared() > b2_epsilon * b2_epsilon); m_normals[i] = b2Cross(edge, 1.0f); m_normals[i].Normalize(); } -#ifdef _DEBUG - // Ensure the polygon is convex and the interior - // is to the left of each edge. - for (int32 i = 0; i < m_vertexCount; ++i) - { - int32 i1 = i; - int32 i2 = i + 1 < m_vertexCount ? i + 1 : 0; - b2Vec2 edge = m_vertices[i2] - m_vertices[i1]; - - for (int32 j = 0; j < m_vertexCount; ++j) - { - // Don't check vertices on the current edge. - if (j == i1 || j == i2) - { - continue; - } - - b2Vec2 r = m_vertices[j] - m_vertices[i1]; - - // If this crashes, your polygon is non-convex, has colinear edges, - // or the winding order is wrong. - float32 s = b2Cross(edge, r); - b2Assert(s > 0.0f && "ERROR: Please ensure your polygon is convex and has a CCW winding order"); - } - } -#endif - // Compute the polygon centroid. - m_centroid = ComputeCentroid(m_vertices, m_vertexCount); + m_centroid = ComputeCentroid(m_vertices, m); } bool b2PolygonShape::TestPoint(const b2Transform& xf, const b2Vec2& p) const { b2Vec2 pLocal = b2MulT(xf.q, p - xf.p); - for (int32 i = 0; i < m_vertexCount; ++i) + for (int32 i = 0; i < m_count; ++i) { float32 dot = b2Dot(m_normals[i], pLocal - m_vertices[i]); if (dot > 0.0f) @@ -200,7 +270,7 @@ bool b2PolygonShape::RayCast(b2RayCastOutput* output, const b2RayCastInput& inpu int32 index = -1; - for (int32 i = 0; i < m_vertexCount; ++i) + for (int32 i = 0; i < m_count; ++i) { // p = p1 + a * d // dot(normal, p - v) = 0 @@ -265,7 +335,7 @@ void b2PolygonShape::ComputeAABB(b2AABB* aabb, const b2Transform& xf, int32 chil b2Vec2 lower = b2Mul(xf, m_vertices[0]); b2Vec2 upper = lower; - for (int32 i = 1; i < m_vertexCount; ++i) + for (int32 i = 1; i < m_count; ++i) { b2Vec2 v = b2Mul(xf, m_vertices[i]); lower = b2Min(lower, v); @@ -303,7 +373,7 @@ void b2PolygonShape::ComputeMass(b2MassData* massData, float32 density) const // // The rest of the derivation is handled by computer algebra. - b2Assert(m_vertexCount >= 3); + b2Assert(m_count >= 3); b2Vec2 center; center.Set(0.0f, 0.0f); float32 area = 0.0f; @@ -314,19 +384,19 @@ void b2PolygonShape::ComputeMass(b2MassData* massData, float32 density) const b2Vec2 s(0.0f, 0.0f); // This code would put the reference point inside the polygon. - for (int32 i = 0; i < m_vertexCount; ++i) + for (int32 i = 0; i < m_count; ++i) { s += m_vertices[i]; } - s *= 1.0f / m_vertexCount; + s *= 1.0f / m_count; const float32 k_inv3 = 1.0f / 3.0f; - for (int32 i = 0; i < m_vertexCount; ++i) + for (int32 i = 0; i < m_count; ++i) { // Triangle vertices. b2Vec2 e1 = m_vertices[i] - s; - b2Vec2 e2 = i + 1 < m_vertexCount ? m_vertices[i+1] - s : m_vertices[0] - s; + b2Vec2 e2 = i + 1 < m_count ? m_vertices[i+1] - s : m_vertices[0] - s; float32 D = b2Cross(e1, e2); @@ -359,3 +429,31 @@ void b2PolygonShape::ComputeMass(b2MassData* massData, float32 density) const // Shift to center of mass then to original body origin. massData->I += massData->mass * (b2Dot(massData->center, massData->center) - b2Dot(center, center)); } + +bool b2PolygonShape::Validate() const +{ + for (int32 i = 0; i < m_count; ++i) + { + int32 i1 = i; + int32 i2 = i < m_count - 1 ? i1 + 1 : 0; + b2Vec2 p = m_vertices[i1]; + b2Vec2 e = m_vertices[i2] - p; + + for (int32 j = 0; j < m_count; ++j) + { + if (j == i1 || j == i2) + { + continue; + } + + b2Vec2 v = m_vertices[j] - p; + float32 c = b2Cross(e, v); + if (c < 0.0f) + { + return false; + } + } + } + + return true; +} diff --git a/src/libraries/Box2D/Collision/Shapes/b2PolygonShape.h b/src/libraries/Box2D/Collision/Shapes/b2PolygonShape.h index 5c5321221..b19298038 100755 --- a/src/libraries/Box2D/Collision/Shapes/b2PolygonShape.h +++ b/src/libraries/Box2D/Collision/Shapes/b2PolygonShape.h @@ -36,12 +36,14 @@ public: /// @see b2Shape::GetChildCount int32 GetChildCount() const; - /// Copy vertices. This assumes the vertices define a convex polygon. - /// It is assumed that the exterior is the the right of each edge. + /// Create a convex hull from the given array of local points. /// The count must be in the range [3, b2_maxPolygonVertices]. - void Set(const b2Vec2* vertices, int32 vertexCount); + /// @warning the points may be re-ordered, even if they form a convex polygon + /// @warning collinear points are handled but not removed. Collinear points + /// may lead to poor stacking behavior. + void Set(const b2Vec2* points, int32 count); - /// Build vertices to represent an axis-aligned box. + /// Build vertices to represent an axis-aligned box centered on the local origin. /// @param hx the half-width. /// @param hy the half-height. void SetAsBox(float32 hx, float32 hy); @@ -67,28 +69,32 @@ public: void ComputeMass(b2MassData* massData, float32 density) const; /// Get the vertex count. - int32 GetVertexCount() const { return m_vertexCount; } + int32 GetVertexCount() const { return m_count; } /// Get a vertex by index. const b2Vec2& GetVertex(int32 index) const; + /// Validate convexity. This is a very time consuming operation. + /// @returns true if valid + bool Validate() const; + b2Vec2 m_centroid; b2Vec2 m_vertices[b2_maxPolygonVertices]; b2Vec2 m_normals[b2_maxPolygonVertices]; - int32 m_vertexCount; + int32 m_count; }; inline b2PolygonShape::b2PolygonShape() { m_type = e_polygon; m_radius = b2_polygonRadius; - m_vertexCount = 0; + m_count = 0; m_centroid.SetZero(); } inline const b2Vec2& b2PolygonShape::GetVertex(int32 index) const { - b2Assert(0 <= index && index < m_vertexCount); + b2Assert(0 <= index && index < m_count); return m_vertices[index]; } diff --git a/src/libraries/Box2D/Collision/b2BroadPhase.cpp b/src/libraries/Box2D/Collision/b2BroadPhase.cpp index b34df8707..5cd6f0e8a 100755 --- a/src/libraries/Box2D/Collision/b2BroadPhase.cpp +++ b/src/libraries/Box2D/Collision/b2BroadPhase.cpp @@ -17,8 +17,7 @@ */ #include -#include -using namespace std; +#include b2BroadPhase::b2BroadPhase() { @@ -90,7 +89,6 @@ void b2BroadPhase::UnBufferMove(int32 proxyId) if (m_moveBuffer[i] == proxyId) { m_moveBuffer[i] = e_nullProxy; - return; } } } diff --git a/src/libraries/Box2D/Collision/b2BroadPhase.h b/src/libraries/Box2D/Collision/b2BroadPhase.h index a289bbf6c..0c460cabb 100755 --- a/src/libraries/Box2D/Collision/b2BroadPhase.h +++ b/src/libraries/Box2D/Collision/b2BroadPhase.h @@ -28,7 +28,6 @@ struct b2Pair { int32 proxyIdA; int32 proxyIdB; - int32 next; }; /// The broad-phase is used for computing pairs and performing volume queries and ray casts. @@ -100,6 +99,11 @@ public: /// Get the quality metric of the embedded tree. float32 GetTreeQuality() const; + /// Shift the world origin. Useful for large worlds. + /// The shift formula is: position -= newOrigin + /// @param newOrigin the new origin with respect to the old origin + void ShiftOrigin(const b2Vec2& newOrigin); + private: friend class b2DynamicTree; @@ -245,4 +249,9 @@ inline void b2BroadPhase::RayCast(T* callback, const b2RayCastInput& input) cons m_tree.RayCast(callback, input); } +inline void b2BroadPhase::ShiftOrigin(const b2Vec2& newOrigin) +{ + m_tree.ShiftOrigin(newOrigin); +} + #endif diff --git a/src/libraries/Box2D/Collision/b2CollideCircle.cpp b/src/libraries/Box2D/Collision/b2CollideCircle.cpp index ae91c733c..4181fd47c 100755 --- a/src/libraries/Box2D/Collision/b2CollideCircle.cpp +++ b/src/libraries/Box2D/Collision/b2CollideCircle.cpp @@ -63,7 +63,7 @@ void b2CollidePolygonAndCircle( int32 normalIndex = 0; float32 separation = -b2_maxFloat; float32 radius = polygonA->m_radius + circleB->m_radius; - int32 vertexCount = polygonA->m_vertexCount; + int32 vertexCount = polygonA->m_count; const b2Vec2* vertices = polygonA->m_vertices; const b2Vec2* normals = polygonA->m_normals; diff --git a/src/libraries/Box2D/Collision/b2CollideEdge.cpp b/src/libraries/Box2D/Collision/b2CollideEdge.cpp index 92a8e1157..88e311c66 100755 --- a/src/libraries/Box2D/Collision/b2CollideEdge.cpp +++ b/src/libraries/Box2D/Collision/b2CollideEdge.cpp @@ -425,8 +425,8 @@ void b2EPCollider::Collide(b2Manifold* manifold, const b2EdgeShape* edgeA, const } // Get polygonB in frameA - m_polygonB.count = polygonB->m_vertexCount; - for (int32 i = 0; i < polygonB->m_vertexCount; ++i) + m_polygonB.count = polygonB->m_count; + for (int32 i = 0; i < polygonB->m_count; ++i) { m_polygonB.vertices[i] = b2Mul(m_xf, polygonB->m_vertices[i]); m_polygonB.normals[i] = b2Mul(m_xf.q, polygonB->m_normals[i]); @@ -497,13 +497,13 @@ void b2EPCollider::Collide(b2Manifold* manifold, const b2EdgeShape* edgeA, const ie[0].v = m_polygonB.vertices[i1]; ie[0].id.cf.indexA = 0; - ie[0].id.cf.indexB = i1; + ie[0].id.cf.indexB = static_cast(i1); ie[0].id.cf.typeA = b2ContactFeature::e_face; ie[0].id.cf.typeB = b2ContactFeature::e_vertex; ie[1].v = m_polygonB.vertices[i2]; ie[1].id.cf.indexA = 0; - ie[1].id.cf.indexB = i2; + ie[1].id.cf.indexB = static_cast(i2); ie[1].id.cf.typeA = b2ContactFeature::e_face; ie[1].id.cf.typeB = b2ContactFeature::e_vertex; @@ -530,13 +530,13 @@ void b2EPCollider::Collide(b2Manifold* manifold, const b2EdgeShape* edgeA, const ie[0].v = m_v1; ie[0].id.cf.indexA = 0; - ie[0].id.cf.indexB = primaryAxis.index; + ie[0].id.cf.indexB = static_cast(primaryAxis.index); ie[0].id.cf.typeA = b2ContactFeature::e_vertex; ie[0].id.cf.typeB = b2ContactFeature::e_face; ie[1].v = m_v2; ie[1].id.cf.indexA = 0; - ie[1].id.cf.indexB = primaryAxis.index; + ie[1].id.cf.indexB = static_cast(primaryAxis.index); ie[1].id.cf.typeA = b2ContactFeature::e_vertex; ie[1].id.cf.typeB = b2ContactFeature::e_face; diff --git a/src/libraries/Box2D/Collision/b2CollidePolygon.cpp b/src/libraries/Box2D/Collision/b2CollidePolygon.cpp index 68695285c..0aac38b2a 100755 --- a/src/libraries/Box2D/Collision/b2CollidePolygon.cpp +++ b/src/libraries/Box2D/Collision/b2CollidePolygon.cpp @@ -19,123 +19,46 @@ #include #include -// Find the separation between poly1 and poly2 for a give edge normal on poly1. -static float32 b2EdgeSeparation(const b2PolygonShape* poly1, const b2Transform& xf1, int32 edge1, - const b2PolygonShape* poly2, const b2Transform& xf2) -{ - const b2Vec2* vertices1 = poly1->m_vertices; - const b2Vec2* normals1 = poly1->m_normals; - - int32 count2 = poly2->m_vertexCount; - const b2Vec2* vertices2 = poly2->m_vertices; - - b2Assert(0 <= edge1 && edge1 < poly1->m_vertexCount); - - // Convert normal from poly1's frame into poly2's frame. - b2Vec2 normal1World = b2Mul(xf1.q, normals1[edge1]); - b2Vec2 normal1 = b2MulT(xf2.q, normal1World); - - // Find support vertex on poly2 for -normal. - int32 index = 0; - float32 minDot = b2_maxFloat; - - for (int32 i = 0; i < count2; ++i) - { - float32 dot = b2Dot(vertices2[i], normal1); - if (dot < minDot) - { - minDot = dot; - index = i; - } - } - - b2Vec2 v1 = b2Mul(xf1, vertices1[edge1]); - b2Vec2 v2 = b2Mul(xf2, vertices2[index]); - float32 separation = b2Dot(v2 - v1, normal1World); - return separation; -} - // Find the max separation between poly1 and poly2 using edge normals from poly1. static float32 b2FindMaxSeparation(int32* edgeIndex, const b2PolygonShape* poly1, const b2Transform& xf1, const b2PolygonShape* poly2, const b2Transform& xf2) { - int32 count1 = poly1->m_vertexCount; - const b2Vec2* normals1 = poly1->m_normals; + int32 count1 = poly1->m_count; + int32 count2 = poly2->m_count; + const b2Vec2* n1s = poly1->m_normals; + const b2Vec2* v1s = poly1->m_vertices; + const b2Vec2* v2s = poly2->m_vertices; + b2Transform xf = b2MulT(xf2, xf1); - // Vector pointing from the centroid of poly1 to the centroid of poly2. - b2Vec2 d = b2Mul(xf2, poly2->m_centroid) - b2Mul(xf1, poly1->m_centroid); - b2Vec2 dLocal1 = b2MulT(xf1.q, d); - - // Find edge normal on poly1 that has the largest projection onto d. - int32 edge = 0; - float32 maxDot = -b2_maxFloat; + int32 bestIndex = 0; + float32 maxSeparation = -b2_maxFloat; for (int32 i = 0; i < count1; ++i) { - float32 dot = b2Dot(normals1[i], dLocal1); - if (dot > maxDot) + // Get poly1 normal in frame2. + b2Vec2 n = b2Mul(xf.q, n1s[i]); + b2Vec2 v1 = b2Mul(xf, v1s[i]); + + // Find deepest point for normal i. + float32 si = b2_maxFloat; + for (int32 j = 0; j < count2; ++j) { - maxDot = dot; - edge = i; + float32 sij = b2Dot(n, v2s[j] - v1); + if (sij < si) + { + si = sij; + } + } + + if (si > maxSeparation) + { + maxSeparation = si; + bestIndex = i; } } - // Get the separation for the edge normal. - float32 s = b2EdgeSeparation(poly1, xf1, edge, poly2, xf2); - - // Check the separation for the previous edge normal. - int32 prevEdge = edge - 1 >= 0 ? edge - 1 : count1 - 1; - float32 sPrev = b2EdgeSeparation(poly1, xf1, prevEdge, poly2, xf2); - - // Check the separation for the next edge normal. - int32 nextEdge = edge + 1 < count1 ? edge + 1 : 0; - float32 sNext = b2EdgeSeparation(poly1, xf1, nextEdge, poly2, xf2); - - // Find the best edge and the search direction. - int32 bestEdge; - float32 bestSeparation; - int32 increment; - if (sPrev > s && sPrev > sNext) - { - increment = -1; - bestEdge = prevEdge; - bestSeparation = sPrev; - } - else if (sNext > s) - { - increment = 1; - bestEdge = nextEdge; - bestSeparation = sNext; - } - else - { - *edgeIndex = edge; - return s; - } - - // Perform a local search for the best edge normal. - for ( ; ; ) - { - if (increment == -1) - edge = bestEdge - 1 >= 0 ? bestEdge - 1 : count1 - 1; - else - edge = bestEdge + 1 < count1 ? bestEdge + 1 : 0; - - s = b2EdgeSeparation(poly1, xf1, edge, poly2, xf2); - - if (s > bestSeparation) - { - bestEdge = edge; - bestSeparation = s; - } - else - { - break; - } - } - - *edgeIndex = bestEdge; - return bestSeparation; + *edgeIndex = bestIndex; + return maxSeparation; } static void b2FindIncidentEdge(b2ClipVertex c[2], @@ -144,11 +67,11 @@ static void b2FindIncidentEdge(b2ClipVertex c[2], { const b2Vec2* normals1 = poly1->m_normals; - int32 count2 = poly2->m_vertexCount; + int32 count2 = poly2->m_count; const b2Vec2* vertices2 = poly2->m_vertices; const b2Vec2* normals2 = poly2->m_normals; - b2Assert(0 <= edge1 && edge1 < poly1->m_vertexCount); + b2Assert(0 <= edge1 && edge1 < poly1->m_count); // Get the normal of the reference edge in poly2's frame. b2Vec2 normal1 = b2MulT(xf2.q, b2Mul(xf1.q, normals1[edge1])); @@ -210,12 +133,11 @@ void b2CollidePolygons(b2Manifold* manifold, const b2PolygonShape* poly1; // reference polygon const b2PolygonShape* poly2; // incident polygon b2Transform xf1, xf2; - int32 edge1; // reference edge + int32 edge1; // reference edge uint8 flip; - const float32 k_relativeTol = 0.98f; - const float32 k_absoluteTol = 0.001f; + const float32 k_tol = 0.1f * b2_linearSlop; - if (separationB > k_relativeTol * separationA + k_absoluteTol) + if (separationB > separationA + k_tol) { poly1 = polyB; poly2 = polyA; @@ -239,7 +161,7 @@ void b2CollidePolygons(b2Manifold* manifold, b2ClipVertex incidentEdge[2]; b2FindIncidentEdge(incidentEdge, poly1, xf1, edge1, poly2, xf2); - int32 count1 = poly1->m_vertexCount; + int32 count1 = poly1->m_count; const b2Vec2* vertices1 = poly1->m_vertices; int32 iv1 = edge1; diff --git a/src/libraries/Box2D/Collision/b2Collision.cpp b/src/libraries/Box2D/Collision/b2Collision.cpp index 8ecb33775..4ebf35565 100755 --- a/src/libraries/Box2D/Collision/b2Collision.cpp +++ b/src/libraries/Box2D/Collision/b2Collision.cpp @@ -44,6 +44,7 @@ void b2WorldManifold::Initialize(const b2Manifold* manifold, b2Vec2 cA = pointA + radiusA * normal; b2Vec2 cB = pointB - radiusB * normal; points[0] = 0.5f * (cA + cB); + separations[0] = b2Dot(cB - cA, normal); } break; @@ -58,6 +59,7 @@ void b2WorldManifold::Initialize(const b2Manifold* manifold, b2Vec2 cA = clipPoint + (radiusA - b2Dot(clipPoint - planePoint, normal)) * normal; b2Vec2 cB = clipPoint - radiusB * normal; points[i] = 0.5f * (cA + cB); + separations[i] = b2Dot(cB - cA, normal); } } break; @@ -73,6 +75,7 @@ void b2WorldManifold::Initialize(const b2Manifold* manifold, b2Vec2 cB = clipPoint + (radiusB - b2Dot(clipPoint - planePoint, normal)) * normal; b2Vec2 cA = clipPoint - radiusA * normal; points[i] = 0.5f * (cA + cB); + separations[i] = b2Dot(cA - cB, normal); } // Ensure normal points from A to B. @@ -217,7 +220,7 @@ int32 b2ClipSegmentToLine(b2ClipVertex vOut[2], const b2ClipVertex vIn[2], vOut[numOut].v = vIn[0].v + interp * (vIn[1].v - vIn[0].v); // VertexA is hitting edgeB. - vOut[numOut].id.cf.indexA = vertexIndexA; + vOut[numOut].id.cf.indexA = static_cast(vertexIndexA); vOut[numOut].id.cf.indexB = vIn[0].id.cf.indexB; vOut[numOut].id.cf.typeA = b2ContactFeature::e_vertex; vOut[numOut].id.cf.typeB = b2ContactFeature::e_face; diff --git a/src/libraries/Box2D/Collision/b2Collision.h b/src/libraries/Box2D/Collision/b2Collision.h index 8bb316caa..ad023b3ca 100755 --- a/src/libraries/Box2D/Collision/b2Collision.h +++ b/src/libraries/Box2D/Collision/b2Collision.h @@ -20,7 +20,7 @@ #define B2_COLLISION_H #include -#include +#include /// @file /// Structures and functions used for computing contact points, distance @@ -117,8 +117,9 @@ struct b2WorldManifold const b2Transform& xfA, float32 radiusA, const b2Transform& xfB, float32 radiusB); - b2Vec2 normal; ///< world vector pointing from A to B - b2Vec2 points[b2_maxManifoldPoints]; ///< world contact point (point of intersection) + b2Vec2 normal; ///< world vector pointing from A to B + b2Vec2 points[b2_maxManifoldPoints]; ///< world contact point (point of intersection) + float32 separations[b2_maxManifoldPoints]; ///< a negative value indicates overlap, in meters }; /// This is used for determining the state of contact points. diff --git a/src/libraries/Box2D/Collision/b2Distance.cpp b/src/libraries/Box2D/Collision/b2Distance.cpp index 8309ea4fe..78daab3e2 100755 --- a/src/libraries/Box2D/Collision/b2Distance.cpp +++ b/src/libraries/Box2D/Collision/b2Distance.cpp @@ -31,7 +31,7 @@ void b2DistanceProxy::Set(const b2Shape* shape, int32 index) { case b2Shape::e_circle: { - const b2CircleShape* circle = (b2CircleShape*)shape; + const b2CircleShape* circle = static_cast(shape); m_vertices = &circle->m_p; m_count = 1; m_radius = circle->m_radius; @@ -40,16 +40,16 @@ void b2DistanceProxy::Set(const b2Shape* shape, int32 index) case b2Shape::e_polygon: { - const b2PolygonShape* polygon = (b2PolygonShape*)shape; + const b2PolygonShape* polygon = static_cast(shape); m_vertices = polygon->m_vertices; - m_count = polygon->m_vertexCount; + m_count = polygon->m_count; m_radius = polygon->m_radius; } break; case b2Shape::e_chain: { - const b2ChainShape* chain = (b2ChainShape*)shape; + const b2ChainShape* chain = static_cast(shape); b2Assert(0 <= index && index < chain->m_count); m_buffer[0] = chain->m_vertices[index]; @@ -70,7 +70,7 @@ void b2DistanceProxy::Set(const b2Shape* shape, int32 index) case b2Shape::e_edge: { - const b2EdgeShape* edge = (b2EdgeShape*)shape; + const b2EdgeShape* edge = static_cast(shape); m_vertices = &edge->m_vertex1; m_count = 2; m_radius = edge->m_radius; @@ -141,6 +141,7 @@ struct b2Simplex v->wA = b2Mul(transformA, wALocal); v->wB = b2Mul(transformB, wBLocal); v->w = v->wB - v->wA; + v->a = 1.0f; m_count = 1; } } @@ -244,7 +245,7 @@ struct b2Simplex { case 0: b2Assert(false); - return 0.0; + return 0.0f; case 1: return 0.0f; @@ -465,8 +466,7 @@ void b2Distance(b2DistanceOutput* output, int32 saveA[3], saveB[3]; int32 saveCount = 0; - b2Vec2 closestPoint = simplex.GetClosestPoint(); - float32 distanceSqr1 = closestPoint.LengthSquared(); + float32 distanceSqr1 = b2_maxFloat; float32 distanceSqr2 = distanceSqr1; // Main iteration loop. diff --git a/src/libraries/Box2D/Collision/b2DynamicTree.cpp b/src/libraries/Box2D/Collision/b2DynamicTree.cpp index 2f81958e3..7035ab8b8 100755 --- a/src/libraries/Box2D/Collision/b2DynamicTree.cpp +++ b/src/libraries/Box2D/Collision/b2DynamicTree.cpp @@ -17,10 +17,7 @@ */ #include -#include -#include -using namespace std; - +#include b2DynamicTree::b2DynamicTree() { @@ -769,3 +766,13 @@ void b2DynamicTree::RebuildBottomUp() Validate(); } + +void b2DynamicTree::ShiftOrigin(const b2Vec2& newOrigin) +{ + // Build array of leaves. Free the rest. + for (int32 i = 0; i < m_nodeCapacity; ++i) + { + m_nodes[i].aabb.lowerBound -= newOrigin; + m_nodes[i].aabb.upperBound -= newOrigin; + } +} diff --git a/src/libraries/Box2D/Collision/b2DynamicTree.h b/src/libraries/Box2D/Collision/b2DynamicTree.h index 0787785b7..bf80f73a1 100755 --- a/src/libraries/Box2D/Collision/b2DynamicTree.h +++ b/src/libraries/Box2D/Collision/b2DynamicTree.h @@ -118,6 +118,11 @@ public: /// Build an optimal tree. Very expensive. For testing. void RebuildBottomUp(); + /// Shift the world origin. Useful for large worlds. + /// The shift formula is: position -= newOrigin + /// @param newOrigin the new origin with respect to the old origin + void ShiftOrigin(const b2Vec2& newOrigin); + private: int32 AllocateNode(); diff --git a/src/libraries/Box2D/Collision/b2TimeOfImpact.cpp b/src/libraries/Box2D/Collision/b2TimeOfImpact.cpp index 9b456a999..60da95c38 100755 --- a/src/libraries/Box2D/Collision/b2TimeOfImpact.cpp +++ b/src/libraries/Box2D/Collision/b2TimeOfImpact.cpp @@ -21,13 +21,15 @@ #include #include #include +#include -#include -using namespace std; +#include +float32 b2_toiTime, b2_toiMaxTime; int32 b2_toiCalls, b2_toiIters, b2_toiMaxIters; int32 b2_toiRootIters, b2_toiMaxRootIters; +// struct b2SeparationFunction { enum Type @@ -119,6 +121,7 @@ struct b2SeparationFunction } } + // float32 FindMinSeparation(int32* indexA, int32* indexB, float32 t) const { b2Transform xfA, xfB; @@ -187,6 +190,7 @@ struct b2SeparationFunction } } + // float32 Evaluate(int32 indexA, int32 indexB, float32 t) const { b2Transform xfA, xfB; @@ -197,9 +201,6 @@ struct b2SeparationFunction { case e_points: { - b2Vec2 axisA = b2MulT(xfA.q, m_axis); - b2Vec2 axisB = b2MulT(xfB.q, -m_axis); - b2Vec2 localPointA = m_proxyA->GetVertex(indexA); b2Vec2 localPointB = m_proxyB->GetVertex(indexB); @@ -215,8 +216,6 @@ struct b2SeparationFunction b2Vec2 normal = b2Mul(xfA.q, m_axis); b2Vec2 pointA = b2Mul(xfA, m_localPoint); - b2Vec2 axisB = b2MulT(xfB.q, -normal); - b2Vec2 localPointB = m_proxyB->GetVertex(indexB); b2Vec2 pointB = b2Mul(xfB, localPointB); @@ -229,8 +228,6 @@ struct b2SeparationFunction b2Vec2 normal = b2Mul(xfB.q, m_axis); b2Vec2 pointB = b2Mul(xfB, m_localPoint); - b2Vec2 axisA = b2MulT(xfA.q, -normal); - b2Vec2 localPointA = m_proxyA->GetVertex(indexA); b2Vec2 pointA = b2Mul(xfA, localPointA); @@ -256,6 +253,8 @@ struct b2SeparationFunction // by computing the largest time at which separation is maintained. void b2TimeOfImpact(b2TOIOutput* output, const b2TOIInput* input) { + b2Timer timer; + ++b2_toiCalls; output->state = b2TOIOutput::e_unknown; @@ -422,6 +421,9 @@ void b2TimeOfImpact(b2TOIOutput* output, const b2TOIInput* input) t = 0.5f * (a1 + a2); } + ++rootIterCount; + ++b2_toiRootIters; + float32 s = fcn.Evaluate(indexA, indexB, t); if (b2Abs(s - target) < tolerance) @@ -442,10 +444,7 @@ void b2TimeOfImpact(b2TOIOutput* output, const b2TOIInput* input) a2 = t; s2 = s; } - - ++rootIterCount; - ++b2_toiRootIters; - + if (rootIterCount == 50) { break; @@ -480,4 +479,8 @@ void b2TimeOfImpact(b2TOIOutput* output, const b2TOIInput* input) } b2_toiMaxIters = b2Max(b2_toiMaxIters, iter); + + float32 time = timer.GetMilliseconds(); + b2_toiMaxTime = b2Max(b2_toiMaxTime, time); + b2_toiTime += time; } diff --git a/src/libraries/Box2D/Common/b2BlockAllocator.cpp b/src/libraries/Box2D/Common/b2BlockAllocator.cpp index d5fa97db9..f7a4688ea 100755 --- a/src/libraries/Box2D/Common/b2BlockAllocator.cpp +++ b/src/libraries/Box2D/Common/b2BlockAllocator.cpp @@ -17,11 +17,9 @@ */ #include -#include -#include -#include -#include -using namespace std; +#include +#include +#include int32 b2BlockAllocator::s_blockSizes[b2_blockSizes] = { diff --git a/src/libraries/Box2D/Common/b2Draw.h b/src/libraries/Box2D/Common/b2Draw.h index 7c4676b50..20359c913 100755 --- a/src/libraries/Box2D/Common/b2Draw.h +++ b/src/libraries/Box2D/Common/b2Draw.h @@ -16,6 +16,9 @@ * 3. This notice may not be removed or altered from any source distribution. */ +#ifndef B2_DRAW_H +#define B2_DRAW_H + #include /// Color for debug drawing. Each value has the range [0,1]. @@ -79,3 +82,5 @@ public: protected: uint32 m_drawFlags; }; + +#endif diff --git a/src/libraries/Box2D/Common/b2GrowableStack.h b/src/libraries/Box2D/Common/b2GrowableStack.h index 27a8eb25d..4094644a2 100755 --- a/src/libraries/Box2D/Common/b2GrowableStack.h +++ b/src/libraries/Box2D/Common/b2GrowableStack.h @@ -19,7 +19,7 @@ #ifndef B2_GROWABLE_STACK_H #define B2_GROWABLE_STACK_H #include -#include +#include /// This is a growable LIFO stack with an initial capacity of N. /// If the stack size exceeds the initial capacity, the heap is used @@ -51,7 +51,7 @@ public: T* old = m_stack; m_capacity *= 2; m_stack = (T*)b2Alloc(m_capacity * sizeof(T)); - std::memcpy(m_stack, old, m_count * sizeof(T)); + memcpy(m_stack, old, m_count * sizeof(T)); if (old != m_array) { b2Free(old); diff --git a/src/libraries/Box2D/Common/b2Math.h b/src/libraries/Box2D/Common/b2Math.h index 11c70707a..713100dfa 100755 --- a/src/libraries/Box2D/Common/b2Math.h +++ b/src/libraries/Box2D/Common/b2Math.h @@ -20,24 +20,14 @@ #define B2_MATH_H #include +#include +#include -#include -#include -#include -#include - -/// This function is used to ensure that a floating point number is -/// not a NaN or infinity. +/// This function is used to ensure that a floating point number is not a NaN or infinity. inline bool b2IsValid(float32 x) { - if (x != x) - { - // NaN. - return false; - } - - float32 infinity = std::numeric_limits::infinity(); - return -infinity < x && x < infinity; + int32 ix = *reinterpret_cast(&x); + return (ix & 0x7f800000) != 0x7f800000; } /// This is a approximate yet fast inverse square-root. @@ -57,8 +47,8 @@ inline float32 b2InvSqrt(float32 x) return x; } -#define b2Sqrt(x) std::sqrt(x) -#define b2Atan2(y, x) std::atan2(y, x) +#define b2Sqrt(x) sqrtf(x) +#define b2Atan2(y, x) atan2f(y, x) /// A 2D column vector. struct b2Vec2 @@ -714,8 +704,8 @@ inline void b2Sweep::Advance(float32 alpha) { b2Assert(alpha0 < 1.0f); float32 beta = (alpha - alpha0) / (1.0f - alpha0); - c0 = (1.0f - beta) * c0 + beta * c; - a0 = (1.0f - beta) * a0 + beta * a; + c0 += beta * (c - c0); + a0 += beta * (a - a0); alpha0 = alpha; } diff --git a/src/libraries/Box2D/Common/b2Settings.cpp b/src/libraries/Box2D/Common/b2Settings.cpp index 416e72c16..e2ceefc1d 100755 --- a/src/libraries/Box2D/Common/b2Settings.cpp +++ b/src/libraries/Box2D/Common/b2Settings.cpp @@ -17,13 +17,13 @@ */ #include -#include -#include -#include +#include +#include +#include #include "common/Exception.h" -b2Version b2_version = {2, 2, 1}; +b2Version b2_version = {2, 3, 0}; // Memory allocators. Modify these to use your own allocator. void* b2Alloc(int32 size) @@ -48,5 +48,5 @@ void b2Log(const char* string, ...) void loveAssert(bool test, const char *teststr) { if (!test) - throw love::Exception("Box2D error: %s", teststr); + throw love::Exception("Box2D assertion failed: %s", teststr); } diff --git a/src/libraries/Box2D/Common/b2Settings.h b/src/libraries/Box2D/Common/b2Settings.h index 0eb7a0fa4..beaa1d47b 100755 --- a/src/libraries/Box2D/Common/b2Settings.h +++ b/src/libraries/Box2D/Common/b2Settings.h @@ -19,8 +19,9 @@ #ifndef B2_SETTINGS_H #define B2_SETTINGS_H -#include -#include +#include +#include +#include void loveAssert(bool test, const char *teststr); diff --git a/src/libraries/Box2D/Common/b2Timer.cpp b/src/libraries/Box2D/Common/b2Timer.cpp index 17cd247d4..cbea5309e 100755 --- a/src/libraries/Box2D/Common/b2Timer.cpp +++ b/src/libraries/Box2D/Common/b2Timer.cpp @@ -22,6 +22,7 @@ float64 b2Timer::s_invFrequency = 0.0f; +#define WIN32_LEAN_AND_MEAN #include b2Timer::b2Timer() @@ -72,14 +73,14 @@ void b2Timer::Reset() timeval t; gettimeofday(&t, 0); m_start_sec = t.tv_sec; - m_start_msec = t.tv_usec * 0.001f; + m_start_usec = t.tv_usec; } float32 b2Timer::GetMilliseconds() const { timeval t; gettimeofday(&t, 0); - return (t.tv_sec - m_start_sec) * 1000 + t.tv_usec * 0.001f - m_start_msec; + return 1000.0f * (t.tv_sec - m_start_sec) + 0.001f * (t.tv_usec - m_start_usec); } #else diff --git a/src/libraries/Box2D/Common/b2Timer.h b/src/libraries/Box2D/Common/b2Timer.h index b6db8884d..edf1ca459 100755 --- a/src/libraries/Box2D/Common/b2Timer.h +++ b/src/libraries/Box2D/Common/b2Timer.h @@ -16,6 +16,9 @@ * 3. This notice may not be removed or altered from any source distribution. */ +#ifndef B2_TIMER_H +#define B2_TIMER_H + #include /// Timer for profiling. This has platform specific code and may @@ -40,6 +43,8 @@ private: static float64 s_invFrequency; #elif defined(__linux__) || defined (__APPLE__) unsigned long m_start_sec; - unsigned long m_start_msec; + unsigned long m_start_usec; #endif }; + +#endif diff --git a/src/libraries/Box2D/Dynamics/Contacts/b2ChainAndCircleContact.cpp b/src/libraries/Box2D/Dynamics/Contacts/b2ChainAndCircleContact.cpp index 81bf68230..01c921f3d 100755 --- a/src/libraries/Box2D/Dynamics/Contacts/b2ChainAndCircleContact.cpp +++ b/src/libraries/Box2D/Dynamics/Contacts/b2ChainAndCircleContact.cpp @@ -23,7 +23,6 @@ #include #include -using namespace std; b2Contact* b2ChainAndCircleContact::Create(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator) { diff --git a/src/libraries/Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.cpp b/src/libraries/Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.cpp index 0da218786..4642fa46a 100755 --- a/src/libraries/Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.cpp +++ b/src/libraries/Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.cpp @@ -23,7 +23,6 @@ #include #include -using namespace std; b2Contact* b2ChainAndPolygonContact::Create(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator) { diff --git a/src/libraries/Box2D/Dynamics/Contacts/b2CircleContact.cpp b/src/libraries/Box2D/Dynamics/Contacts/b2CircleContact.cpp index ade092049..b47f35aed 100755 --- a/src/libraries/Box2D/Dynamics/Contacts/b2CircleContact.cpp +++ b/src/libraries/Box2D/Dynamics/Contacts/b2CircleContact.cpp @@ -24,7 +24,6 @@ #include #include -using namespace std; b2Contact* b2CircleContact::Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) { diff --git a/src/libraries/Box2D/Dynamics/Contacts/b2Contact.cpp b/src/libraries/Box2D/Dynamics/Contacts/b2Contact.cpp index e7e2a2e65..d9f1021e4 100755 --- a/src/libraries/Box2D/Dynamics/Contacts/b2Contact.cpp +++ b/src/libraries/Box2D/Dynamics/Contacts/b2Contact.cpp @@ -102,14 +102,19 @@ void b2Contact::Destroy(b2Contact* contact, b2BlockAllocator* allocator) { b2Assert(s_initialized == true); - if (contact->m_manifold.pointCount > 0) + b2Fixture* fixtureA = contact->m_fixtureA; + b2Fixture* fixtureB = contact->m_fixtureB; + + if (contact->m_manifold.pointCount > 0 && + fixtureA->IsSensor() == false && + fixtureB->IsSensor() == false) { - contact->GetFixtureA()->GetBody()->SetAwake(true); - contact->GetFixtureB()->GetBody()->SetAwake(true); + fixtureA->GetBody()->SetAwake(true); + fixtureB->GetBody()->SetAwake(true); } - b2Shape::Type typeA = contact->GetFixtureA()->GetType(); - b2Shape::Type typeB = contact->GetFixtureB()->GetType(); + b2Shape::Type typeA = fixtureA->GetType(); + b2Shape::Type typeB = fixtureB->GetType(); b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount); b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount); @@ -147,6 +152,8 @@ b2Contact::b2Contact(b2Fixture* fA, int32 indexA, b2Fixture* fB, int32 indexB) m_friction = b2MixFriction(m_fixtureA->m_friction, m_fixtureB->m_friction); m_restitution = b2MixRestitution(m_fixtureA->m_restitution, m_fixtureB->m_restitution); + + m_tangentSpeed = 0.0f; } // Update the contact manifold and touching status. diff --git a/src/libraries/Box2D/Dynamics/Contacts/b2Contact.h b/src/libraries/Box2D/Dynamics/Contacts/b2Contact.h index f8e74e923..36e31e256 100755 --- a/src/libraries/Box2D/Dynamics/Contacts/b2Contact.h +++ b/src/libraries/Box2D/Dynamics/Contacts/b2Contact.h @@ -36,7 +36,7 @@ class b2ContactListener; /// For example, anything slides on ice. inline float32 b2MixFriction(float32 friction1, float32 friction2) { - return std::sqrt(friction1 * friction2); + return b2Sqrt(friction1 * friction2); } /// Restitution mixing law. The idea is allow for anything to bounce off an inelastic surface. @@ -135,6 +135,12 @@ public: /// Reset the restitution to the default value. void ResetRestitution(); + /// Set the desired tangent speed for a conveyor belt behavior. In meters per second. + void SetTangentSpeed(float32 speed); + + /// Get the desired tangent speed. In meters per second. + float32 GetTangentSpeed() const; + /// Evaluate this contact with your own manifold and transforms. virtual void Evaluate(b2Manifold* manifold, const b2Transform& xfA, const b2Transform& xfB) = 0; @@ -209,6 +215,8 @@ protected: float32 m_friction; float32 m_restitution; + + float32 m_tangentSpeed; }; inline b2Manifold* b2Contact::GetManifold() @@ -328,4 +336,14 @@ inline void b2Contact::ResetRestitution() m_restitution = b2MixRestitution(m_fixtureA->m_restitution, m_fixtureB->m_restitution); } +inline void b2Contact::SetTangentSpeed(float32 speed) +{ + m_tangentSpeed = speed; +} + +inline float32 b2Contact::GetTangentSpeed() const +{ + return m_tangentSpeed; +} + #endif diff --git a/src/libraries/Box2D/Dynamics/Contacts/b2ContactSolver.cpp b/src/libraries/Box2D/Dynamics/Contacts/b2ContactSolver.cpp index 4e510a620..8224fdc2d 100755 --- a/src/libraries/Box2D/Dynamics/Contacts/b2ContactSolver.cpp +++ b/src/libraries/Box2D/Dynamics/Contacts/b2ContactSolver.cpp @@ -73,6 +73,7 @@ b2ContactSolver::b2ContactSolver(b2ContactSolverDef* def) b2ContactVelocityConstraint* vc = m_velocityConstraints + i; vc->friction = contact->m_friction; vc->restitution = contact->m_restitution; + vc->tangentSpeed = contact->m_tangentSpeed; vc->indexA = bodyA->m_islandIndex; vc->indexB = bodyB->m_islandIndex; vc->invMassA = bodyA->m_invMass; @@ -320,7 +321,7 @@ void b2ContactSolver::SolveVelocityConstraints() b2Vec2 dv = vB + b2Cross(wB, vcp->rB) - vA - b2Cross(wA, vcp->rA); // Compute tangent force - float32 vt = b2Dot(dv, tangent); + float32 vt = b2Dot(dv, tangent) - vc->tangentSpeed; float32 lambda = vcp->tangentMass * (-vt); // b2Clamp the accumulated force @@ -763,8 +764,8 @@ bool b2ContactSolver::SolveTOIPositionConstraints(int32 toiIndexA, int32 toiInde iA = pc->invIA; } - float32 mB = pc->invMassB; - float32 iB = pc->invIB; + float32 mB = 0.0f; + float32 iB = 0.; if (indexB == toiIndexA || indexB == toiIndexB) { mB = pc->invMassB; diff --git a/src/libraries/Box2D/Dynamics/Contacts/b2ContactSolver.h b/src/libraries/Box2D/Dynamics/Contacts/b2ContactSolver.h index ff3a49c4b..a622e30d8 100755 --- a/src/libraries/Box2D/Dynamics/Contacts/b2ContactSolver.h +++ b/src/libraries/Box2D/Dynamics/Contacts/b2ContactSolver.h @@ -51,6 +51,7 @@ struct b2ContactVelocityConstraint float32 invIA, invIB; float32 friction; float32 restitution; + float32 tangentSpeed; int32 pointCount; int32 contactIndex; }; diff --git a/src/libraries/Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.cpp b/src/libraries/Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.cpp index 742a10167..d1fd3295c 100755 --- a/src/libraries/Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.cpp +++ b/src/libraries/Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.cpp @@ -21,7 +21,6 @@ #include #include -using namespace std; b2Contact* b2EdgeAndCircleContact::Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) { diff --git a/src/libraries/Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.cpp b/src/libraries/Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.cpp index 0eb09cffa..ba536bc39 100755 --- a/src/libraries/Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.cpp +++ b/src/libraries/Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.cpp @@ -21,7 +21,6 @@ #include #include -using namespace std; b2Contact* b2EdgeAndPolygonContact::Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) { diff --git a/src/libraries/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.cpp b/src/libraries/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.cpp index b5712af8a..7c9cbcc92 100755 --- a/src/libraries/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.cpp +++ b/src/libraries/Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.cpp @@ -21,7 +21,6 @@ #include #include -using namespace std; b2Contact* b2PolygonAndCircleContact::Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) { diff --git a/src/libraries/Box2D/Dynamics/Contacts/b2PolygonContact.cpp b/src/libraries/Box2D/Dynamics/Contacts/b2PolygonContact.cpp index 47dce21fe..b5706ca10 100755 --- a/src/libraries/Box2D/Dynamics/Contacts/b2PolygonContact.cpp +++ b/src/libraries/Box2D/Dynamics/Contacts/b2PolygonContact.cpp @@ -24,7 +24,6 @@ #include #include -using namespace std; b2Contact* b2PolygonContact::Create(b2Fixture* fixtureA, int32, b2Fixture* fixtureB, int32, b2BlockAllocator* allocator) { diff --git a/src/libraries/Box2D/Dynamics/Joints/b2GearJoint.cpp b/src/libraries/Box2D/Dynamics/Joints/b2GearJoint.cpp index 023d107b4..155eb0c1d 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2GearJoint.cpp +++ b/src/libraries/Box2D/Dynamics/Joints/b2GearJoint.cpp @@ -147,22 +147,18 @@ void b2GearJoint::InitVelocityConstraints(const b2SolverData& data) m_iC = m_bodyC->m_invI; m_iD = m_bodyD->m_invI; - b2Vec2 cA = data.positions[m_indexA].c; float32 aA = data.positions[m_indexA].a; b2Vec2 vA = data.velocities[m_indexA].v; float32 wA = data.velocities[m_indexA].w; - b2Vec2 cB = data.positions[m_indexB].c; float32 aB = data.positions[m_indexB].a; b2Vec2 vB = data.velocities[m_indexB].v; float32 wB = data.velocities[m_indexB].w; - b2Vec2 cC = data.positions[m_indexC].c; float32 aC = data.positions[m_indexC].a; b2Vec2 vC = data.velocities[m_indexC].v; float32 wC = data.velocities[m_indexC].w; - b2Vec2 cD = data.positions[m_indexD].c; float32 aD = data.positions[m_indexD].a; b2Vec2 vD = data.velocities[m_indexD].v; float32 wD = data.velocities[m_indexD].w; diff --git a/src/libraries/Box2D/Dynamics/Joints/b2Joint.cpp b/src/libraries/Box2D/Dynamics/Joints/b2Joint.cpp index 84802bff9..dd55be94f 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2Joint.cpp +++ b/src/libraries/Box2D/Dynamics/Joints/b2Joint.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -42,70 +43,77 @@ b2Joint* b2Joint::Create(const b2JointDef* def, b2BlockAllocator* allocator) case e_distanceJoint: { void* mem = allocator->Allocate(sizeof(b2DistanceJoint)); - joint = new (mem) b2DistanceJoint((b2DistanceJointDef*)def); + joint = new (mem) b2DistanceJoint(static_cast(def)); } break; case e_mouseJoint: { void* mem = allocator->Allocate(sizeof(b2MouseJoint)); - joint = new (mem) b2MouseJoint((b2MouseJointDef*)def); + joint = new (mem) b2MouseJoint(static_cast(def)); } break; case e_prismaticJoint: { void* mem = allocator->Allocate(sizeof(b2PrismaticJoint)); - joint = new (mem) b2PrismaticJoint((b2PrismaticJointDef*)def); + joint = new (mem) b2PrismaticJoint(static_cast(def)); } break; case e_revoluteJoint: { void* mem = allocator->Allocate(sizeof(b2RevoluteJoint)); - joint = new (mem) b2RevoluteJoint((b2RevoluteJointDef*)def); + joint = new (mem) b2RevoluteJoint(static_cast(def)); } break; case e_pulleyJoint: { void* mem = allocator->Allocate(sizeof(b2PulleyJoint)); - joint = new (mem) b2PulleyJoint((b2PulleyJointDef*)def); + joint = new (mem) b2PulleyJoint(static_cast(def)); } break; case e_gearJoint: { void* mem = allocator->Allocate(sizeof(b2GearJoint)); - joint = new (mem) b2GearJoint((b2GearJointDef*)def); + joint = new (mem) b2GearJoint(static_cast(def)); } break; case e_wheelJoint: { void* mem = allocator->Allocate(sizeof(b2WheelJoint)); - joint = new (mem) b2WheelJoint((b2WheelJointDef*)def); + joint = new (mem) b2WheelJoint(static_cast(def)); } break; case e_weldJoint: { void* mem = allocator->Allocate(sizeof(b2WeldJoint)); - joint = new (mem) b2WeldJoint((b2WeldJointDef*)def); + joint = new (mem) b2WeldJoint(static_cast(def)); } break; case e_frictionJoint: { void* mem = allocator->Allocate(sizeof(b2FrictionJoint)); - joint = new (mem) b2FrictionJoint((b2FrictionJointDef*)def); + joint = new (mem) b2FrictionJoint(static_cast(def)); } break; case e_ropeJoint: { void* mem = allocator->Allocate(sizeof(b2RopeJoint)); - joint = new (mem) b2RopeJoint((b2RopeJointDef*)def); + joint = new (mem) b2RopeJoint(static_cast(def)); + } + break; + + case e_motorJoint: + { + void* mem = allocator->Allocate(sizeof(b2MotorJoint)); + joint = new (mem) b2MotorJoint(static_cast(def)); } break; @@ -162,6 +170,10 @@ void b2Joint::Destroy(b2Joint* joint, b2BlockAllocator* allocator) allocator->Free(joint, sizeof(b2RopeJoint)); break; + case e_motorJoint: + allocator->Free(joint, sizeof(b2MotorJoint)); + break; + default: b2Assert(false); break; diff --git a/src/libraries/Box2D/Dynamics/Joints/b2Joint.h b/src/libraries/Box2D/Dynamics/Joints/b2Joint.h index c0f9db832..dd076b6b2 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2Joint.h +++ b/src/libraries/Box2D/Dynamics/Joints/b2Joint.h @@ -38,7 +38,8 @@ enum b2JointType e_wheelJoint, e_weldJoint, e_frictionJoint, - e_ropeJoint + e_ropeJoint, + e_motorJoint }; enum b2LimitState @@ -145,6 +146,9 @@ public: /// Dump this joint to the log file. virtual void Dump() { b2Log("// Dump is not supported for this joint type.\n"); } + /// Shift the origin for any points stored in world coordinates. + virtual void ShiftOrigin(const b2Vec2& newOrigin) { B2_NOT_USED(newOrigin); } + protected: friend class b2World; friend class b2Body; diff --git a/src/libraries/Box2D/Dynamics/Joints/b2MotorJoint.cpp b/src/libraries/Box2D/Dynamics/Joints/b2MotorJoint.cpp new file mode 100755 index 000000000..e5bbcb165 --- /dev/null +++ b/src/libraries/Box2D/Dynamics/Joints/b2MotorJoint.cpp @@ -0,0 +1,304 @@ +/* +* Copyright (c) 2006-2012 Erin Catto http://www.box2d.org +* +* This software is provided 'as-is', without any express or implied +* warranty. In no event will the authors be held liable for any damages +* arising from the use of this software. +* Permission is granted to anyone to use this software for any purpose, +* including commercial applications, and to alter it and redistribute it +* freely, subject to the following restrictions: +* 1. The origin of this software must not be misrepresented; you must not +* claim that you wrote the original software. If you use this software +* in a product, an acknowledgment in the product documentation would be +* appreciated but is not required. +* 2. Altered source versions must be plainly marked as such, and must not be +* misrepresented as being the original software. +* 3. This notice may not be removed or altered from any source distribution. +*/ + +#include +#include +#include + +// Point-to-point constraint +// Cdot = v2 - v1 +// = v2 + cross(w2, r2) - v1 - cross(w1, r1) +// J = [-I -r1_skew I r2_skew ] +// Identity used: +// w k % (rx i + ry j) = w * (-ry i + rx j) + +// Angle constraint +// Cdot = w2 - w1 +// J = [0 0 -1 0 0 1] +// K = invI1 + invI2 + +void b2MotorJointDef::Initialize(b2Body* bA, b2Body* bB) +{ + bodyA = bA; + bodyB = bB; + b2Vec2 xB = bodyB->GetPosition(); + linearOffset = bodyA->GetLocalPoint(xB); + + float32 angleA = bodyA->GetAngle(); + float32 angleB = bodyB->GetAngle(); + angularOffset = angleB - angleA; +} + +b2MotorJoint::b2MotorJoint(const b2MotorJointDef* def) +: b2Joint(def) +{ + m_linearOffset = def->linearOffset; + m_angularOffset = def->angularOffset; + + m_linearImpulse.SetZero(); + m_angularImpulse = 0.0f; + + m_maxForce = def->maxForce; + m_maxTorque = def->maxTorque; + m_correctionFactor = def->correctionFactor; +} + +void b2MotorJoint::InitVelocityConstraints(const b2SolverData& data) +{ + m_indexA = m_bodyA->m_islandIndex; + m_indexB = m_bodyB->m_islandIndex; + m_localCenterA = m_bodyA->m_sweep.localCenter; + m_localCenterB = m_bodyB->m_sweep.localCenter; + m_invMassA = m_bodyA->m_invMass; + m_invMassB = m_bodyB->m_invMass; + m_invIA = m_bodyA->m_invI; + m_invIB = m_bodyB->m_invI; + + b2Vec2 cA = data.positions[m_indexA].c; + float32 aA = data.positions[m_indexA].a; + b2Vec2 vA = data.velocities[m_indexA].v; + float32 wA = data.velocities[m_indexA].w; + + b2Vec2 cB = data.positions[m_indexB].c; + float32 aB = data.positions[m_indexB].a; + b2Vec2 vB = data.velocities[m_indexB].v; + float32 wB = data.velocities[m_indexB].w; + + b2Rot qA(aA), qB(aB); + + // Compute the effective mass matrix. + m_rA = b2Mul(qA, -m_localCenterA); + m_rB = b2Mul(qB, -m_localCenterB); + + // J = [-I -r1_skew I r2_skew] + // [ 0 -1 0 1] + // r_skew = [-ry; rx] + + // Matlab + // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB] + // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB] + // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB] + + float32 mA = m_invMassA, mB = m_invMassB; + float32 iA = m_invIA, iB = m_invIB; + + b2Mat22 K; + K.ex.x = mA + mB + iA * m_rA.y * m_rA.y + iB * m_rB.y * m_rB.y; + K.ex.y = -iA * m_rA.x * m_rA.y - iB * m_rB.x * m_rB.y; + K.ey.x = K.ex.y; + K.ey.y = mA + mB + iA * m_rA.x * m_rA.x + iB * m_rB.x * m_rB.x; + + m_linearMass = K.GetInverse(); + + m_angularMass = iA + iB; + if (m_angularMass > 0.0f) + { + m_angularMass = 1.0f / m_angularMass; + } + + m_linearError = cB + m_rB - cA - m_rA - b2Mul(qA, m_linearOffset); + m_angularError = aB - aA - m_angularOffset; + + if (data.step.warmStarting) + { + // Scale impulses to support a variable time step. + m_linearImpulse *= data.step.dtRatio; + m_angularImpulse *= data.step.dtRatio; + + b2Vec2 P(m_linearImpulse.x, m_linearImpulse.y); + vA -= mA * P; + wA -= iA * (b2Cross(m_rA, P) + m_angularImpulse); + vB += mB * P; + wB += iB * (b2Cross(m_rB, P) + m_angularImpulse); + } + else + { + m_linearImpulse.SetZero(); + m_angularImpulse = 0.0f; + } + + data.velocities[m_indexA].v = vA; + data.velocities[m_indexA].w = wA; + data.velocities[m_indexB].v = vB; + data.velocities[m_indexB].w = wB; +} + +void b2MotorJoint::SolveVelocityConstraints(const b2SolverData& data) +{ + b2Vec2 vA = data.velocities[m_indexA].v; + float32 wA = data.velocities[m_indexA].w; + b2Vec2 vB = data.velocities[m_indexB].v; + float32 wB = data.velocities[m_indexB].w; + + float32 mA = m_invMassA, mB = m_invMassB; + float32 iA = m_invIA, iB = m_invIB; + + float32 h = data.step.dt; + float32 inv_h = data.step.inv_dt; + + // Solve angular friction + { + float32 Cdot = wB - wA + inv_h * m_correctionFactor * m_angularError; + float32 impulse = -m_angularMass * Cdot; + + float32 oldImpulse = m_angularImpulse; + float32 maxImpulse = h * m_maxTorque; + m_angularImpulse = b2Clamp(m_angularImpulse + impulse, -maxImpulse, maxImpulse); + impulse = m_angularImpulse - oldImpulse; + + wA -= iA * impulse; + wB += iB * impulse; + } + + // Solve linear friction + { + b2Vec2 Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA) + inv_h * m_correctionFactor * m_linearError; + + b2Vec2 impulse = -b2Mul(m_linearMass, Cdot); + b2Vec2 oldImpulse = m_linearImpulse; + m_linearImpulse += impulse; + + float32 maxImpulse = h * m_maxForce; + + if (m_linearImpulse.LengthSquared() > maxImpulse * maxImpulse) + { + m_linearImpulse.Normalize(); + m_linearImpulse *= maxImpulse; + } + + impulse = m_linearImpulse - oldImpulse; + + vA -= mA * impulse; + wA -= iA * b2Cross(m_rA, impulse); + + vB += mB * impulse; + wB += iB * b2Cross(m_rB, impulse); + } + + data.velocities[m_indexA].v = vA; + data.velocities[m_indexA].w = wA; + data.velocities[m_indexB].v = vB; + data.velocities[m_indexB].w = wB; +} + +bool b2MotorJoint::SolvePositionConstraints(const b2SolverData& data) +{ + B2_NOT_USED(data); + + return true; +} + +b2Vec2 b2MotorJoint::GetAnchorA() const +{ + return m_bodyA->GetPosition(); +} + +b2Vec2 b2MotorJoint::GetAnchorB() const +{ + return m_bodyB->GetPosition(); +} + +b2Vec2 b2MotorJoint::GetReactionForce(float32 inv_dt) const +{ + return inv_dt * m_linearImpulse; +} + +float32 b2MotorJoint::GetReactionTorque(float32 inv_dt) const +{ + return inv_dt * m_angularImpulse; +} + +void b2MotorJoint::SetMaxForce(float32 force) +{ + b2Assert(b2IsValid(force) && force >= 0.0f); + m_maxForce = force; +} + +float32 b2MotorJoint::GetMaxForce() const +{ + return m_maxForce; +} + +void b2MotorJoint::SetMaxTorque(float32 torque) +{ + b2Assert(b2IsValid(torque) && torque >= 0.0f); + m_maxTorque = torque; +} + +float32 b2MotorJoint::GetMaxTorque() const +{ + return m_maxTorque; +} + +void b2MotorJoint::SetCorrectionFactor(float32 factor) +{ + b2Assert(b2IsValid(factor) && 0.0f <= factor && factor <= 1.0f); + m_correctionFactor = factor; +} + +float32 b2MotorJoint::GetCorrectionFactor() const +{ + return m_correctionFactor; +} + +void b2MotorJoint::SetLinearOffset(const b2Vec2& linearOffset) +{ + if (linearOffset.x != m_linearOffset.x || linearOffset.y != m_linearOffset.y) + { + m_bodyA->SetAwake(true); + m_bodyB->SetAwake(true); + m_linearOffset = linearOffset; + } +} + +const b2Vec2& b2MotorJoint::GetLinearOffset() const +{ + return m_linearOffset; +} + +void b2MotorJoint::SetAngularOffset(float32 angularOffset) +{ + if (angularOffset != m_angularOffset) + { + m_bodyA->SetAwake(true); + m_bodyB->SetAwake(true); + m_angularOffset = angularOffset; + } +} + +float32 b2MotorJoint::GetAngularOffset() const +{ + return m_angularOffset; +} + +void b2MotorJoint::Dump() +{ + int32 indexA = m_bodyA->m_islandIndex; + int32 indexB = m_bodyB->m_islandIndex; + + b2Log(" b2MotorJointDef jd;\n"); + b2Log(" jd.bodyA = bodies[%d];\n", indexA); + b2Log(" jd.bodyB = bodies[%d];\n", indexB); + b2Log(" jd.collideConnected = bool(%d);\n", m_collideConnected); + b2Log(" jd.linearOffset.Set(%.15lef, %.15lef);\n", m_linearOffset.x, m_linearOffset.y); + b2Log(" jd.angularOffset = %.15lef;\n", m_angularOffset); + b2Log(" jd.maxForce = %.15lef;\n", m_maxForce); + b2Log(" jd.maxTorque = %.15lef;\n", m_maxTorque); + b2Log(" jd.correctionFactor = %.15lef;\n", m_correctionFactor); + b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index); +} diff --git a/src/libraries/Box2D/Dynamics/Joints/b2MotorJoint.h b/src/libraries/Box2D/Dynamics/Joints/b2MotorJoint.h new file mode 100755 index 000000000..0b76d6fe7 --- /dev/null +++ b/src/libraries/Box2D/Dynamics/Joints/b2MotorJoint.h @@ -0,0 +1,133 @@ +/* +* Copyright (c) 2006-2012 Erin Catto http://www.box2d.org +* +* This software is provided 'as-is', without any express or implied +* warranty. In no event will the authors be held liable for any damages +* arising from the use of this software. +* Permission is granted to anyone to use this software for any purpose, +* including commercial applications, and to alter it and redistribute it +* freely, subject to the following restrictions: +* 1. The origin of this software must not be misrepresented; you must not +* claim that you wrote the original software. If you use this software +* in a product, an acknowledgment in the product documentation would be +* appreciated but is not required. +* 2. Altered source versions must be plainly marked as such, and must not be +* misrepresented as being the original software. +* 3. This notice may not be removed or altered from any source distribution. +*/ + +#ifndef B2_MOTOR_JOINT_H +#define B2_MOTOR_JOINT_H + +#include + +/// Motor joint definition. +struct b2MotorJointDef : public b2JointDef +{ + b2MotorJointDef() + { + type = e_motorJoint; + linearOffset.SetZero(); + angularOffset = 0.0f; + maxForce = 1.0f; + maxTorque = 1.0f; + correctionFactor = 0.3f; + } + + /// Initialize the bodies and offsets using the current transforms. + void Initialize(b2Body* bodyA, b2Body* bodyB); + + /// Position of bodyB minus the position of bodyA, in bodyA's frame, in meters. + b2Vec2 linearOffset; + + /// The bodyB angle minus bodyA angle in radians. + float32 angularOffset; + + /// The maximum motor force in N. + float32 maxForce; + + /// The maximum motor torque in N-m. + float32 maxTorque; + + /// Position correction factor in the range [0,1]. + float32 correctionFactor; +}; + +/// A motor joint is used to control the relative motion +/// between two bodies. A typical usage is to control the movement +/// of a dynamic body with respect to the ground. +class b2MotorJoint : public b2Joint +{ +public: + b2Vec2 GetAnchorA() const; + b2Vec2 GetAnchorB() const; + + b2Vec2 GetReactionForce(float32 inv_dt) const; + float32 GetReactionTorque(float32 inv_dt) const; + + /// Set/get the target linear offset, in frame A, in meters. + void SetLinearOffset(const b2Vec2& linearOffset); + const b2Vec2& GetLinearOffset() const; + + /// Set/get the target angular offset, in radians. + void SetAngularOffset(float32 angularOffset); + float32 GetAngularOffset() const; + + /// Set the maximum friction force in N. + void SetMaxForce(float32 force); + + /// Get the maximum friction force in N. + float32 GetMaxForce() const; + + /// Set the maximum friction torque in N*m. + void SetMaxTorque(float32 torque); + + /// Get the maximum friction torque in N*m. + float32 GetMaxTorque() const; + + /// Set the position correction factor in the range [0,1]. + void SetCorrectionFactor(float32 factor); + + /// Get the position correction factor in the range [0,1]. + float32 GetCorrectionFactor() const; + + /// Dump to b2Log + void Dump(); + +protected: + + friend class b2Joint; + + b2MotorJoint(const b2MotorJointDef* def); + + void InitVelocityConstraints(const b2SolverData& data); + void SolveVelocityConstraints(const b2SolverData& data); + bool SolvePositionConstraints(const b2SolverData& data); + + // Solver shared + b2Vec2 m_linearOffset; + float32 m_angularOffset; + b2Vec2 m_linearImpulse; + float32 m_angularImpulse; + float32 m_maxForce; + float32 m_maxTorque; + float32 m_correctionFactor; + + // Solver temp + int32 m_indexA; + int32 m_indexB; + b2Vec2 m_rA; + b2Vec2 m_rB; + b2Vec2 m_localCenterA; + b2Vec2 m_localCenterB; + b2Vec2 m_linearError; + float32 m_angularError; + float32 m_invMassA; + float32 m_invMassB; + float32 m_invIA; + float32 m_invIB; + b2Mat22 m_linearMass; + float32 m_angularMass; +}; + +#endif diff --git a/src/libraries/Box2D/Dynamics/Joints/b2MouseJoint.cpp b/src/libraries/Box2D/Dynamics/Joints/b2MouseJoint.cpp index 4d19bd407..ea96edde9 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2MouseJoint.cpp +++ b/src/libraries/Box2D/Dynamics/Joints/b2MouseJoint.cpp @@ -215,3 +215,8 @@ float32 b2MouseJoint::GetReactionTorque(float32 inv_dt) const { return inv_dt * 0.0f; } + +void b2MouseJoint::ShiftOrigin(const b2Vec2& newOrigin) +{ + m_targetA -= newOrigin; +} diff --git a/src/libraries/Box2D/Dynamics/Joints/b2MouseJoint.h b/src/libraries/Box2D/Dynamics/Joints/b2MouseJoint.h index 682e78340..8b50a0e14 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2MouseJoint.h +++ b/src/libraries/Box2D/Dynamics/Joints/b2MouseJoint.h @@ -92,6 +92,9 @@ public: /// The mouse joint does not support dumping. void Dump() { b2Log("Mouse joint dumping is not supported.\n"); } + /// Implement b2Joint::ShiftOrigin + void ShiftOrigin(const b2Vec2& newOrigin); + protected: friend class b2Joint; diff --git a/src/libraries/Box2D/Dynamics/Joints/b2PrismaticJoint.cpp b/src/libraries/Box2D/Dynamics/Joints/b2PrismaticJoint.cpp index 1df28b9db..26f539681 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2PrismaticJoint.cpp +++ b/src/libraries/Box2D/Dynamics/Joints/b2PrismaticJoint.cpp @@ -108,7 +108,7 @@ b2PrismaticJoint::b2PrismaticJoint(const b2PrismaticJointDef* def) m_referenceAngle = def->referenceAngle; m_impulse.SetZero(); - m_motorMass = 0.0; + m_motorMass = 0.0f; m_motorImpulse = 0.0f; m_lowerTranslation = def->lowerTranslation; @@ -349,17 +349,6 @@ void b2PrismaticJoint::SolveVelocityConstraints(const b2SolverData& data) vB += mB * P; wB += iB * LB; - - b2Vec2 Cdot10 = Cdot1; - - Cdot1.x = b2Dot(m_perp, vB - vA) + m_s2 * wB - m_s1 * wA; - Cdot1.y = wB - wA; - - if (b2Abs(Cdot1.x) > 0.01f || b2Abs(Cdot1.y) > 0.01f) - { - b2Vec2 test = b2Mul22(m_K, df); - Cdot1.x += 0.0f; - } } data.velocities[m_indexA].v = vA; diff --git a/src/libraries/Box2D/Dynamics/Joints/b2PulleyJoint.cpp b/src/libraries/Box2D/Dynamics/Joints/b2PulleyJoint.cpp index b7bab69fc..9cd340f1b 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2PulleyJoint.cpp +++ b/src/libraries/Box2D/Dynamics/Joints/b2PulleyJoint.cpp @@ -292,6 +292,21 @@ b2Vec2 b2PulleyJoint::GetGroundAnchorB() const } float32 b2PulleyJoint::GetLengthA() const +{ + return m_lengthA; +} + +float32 b2PulleyJoint::GetLengthB() const +{ + return m_lengthB; +} + +float32 b2PulleyJoint::GetRatio() const +{ + return m_ratio; +} + +float32 b2PulleyJoint::GetCurrentLengthA() const { b2Vec2 p = m_bodyA->GetWorldPoint(m_localAnchorA); b2Vec2 s = m_groundAnchorA; @@ -299,7 +314,7 @@ float32 b2PulleyJoint::GetLengthA() const return d.Length(); } -float32 b2PulleyJoint::GetLengthB() const +float32 b2PulleyJoint::GetCurrentLengthB() const { b2Vec2 p = m_bodyB->GetWorldPoint(m_localAnchorB); b2Vec2 s = m_groundAnchorB; @@ -307,11 +322,6 @@ float32 b2PulleyJoint::GetLengthB() const return d.Length(); } -float32 b2PulleyJoint::GetRatio() const -{ - return m_ratio; -} - void b2PulleyJoint::Dump() { int32 indexA = m_bodyA->m_islandIndex; @@ -330,3 +340,9 @@ void b2PulleyJoint::Dump() b2Log(" jd.ratio = %.15lef;\n", m_ratio); b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index); } + +void b2PulleyJoint::ShiftOrigin(const b2Vec2& newOrigin) +{ + m_groundAnchorA -= newOrigin; + m_groundAnchorB -= newOrigin; +} diff --git a/src/libraries/Box2D/Dynamics/Joints/b2PulleyJoint.h b/src/libraries/Box2D/Dynamics/Joints/b2PulleyJoint.h index 24480ba05..44843e7b4 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2PulleyJoint.h +++ b/src/libraries/Box2D/Dynamics/Joints/b2PulleyJoint.h @@ -100,9 +100,18 @@ public: /// Get the pulley ratio. float32 GetRatio() const; + /// Get the current length of the segment attached to bodyA. + float32 GetCurrentLengthA() const; + + /// Get the current length of the segment attached to bodyB. + float32 GetCurrentLengthB() const; + /// Dump joint to dmLog void Dump(); + /// Implement b2Joint::ShiftOrigin + void ShiftOrigin(const b2Vec2& newOrigin); + protected: friend class b2Joint; diff --git a/src/libraries/Box2D/Dynamics/Joints/b2RevoluteJoint.cpp b/src/libraries/Box2D/Dynamics/Joints/b2RevoluteJoint.cpp index e658f38b0..64bd19070 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2RevoluteJoint.cpp +++ b/src/libraries/Box2D/Dynamics/Joints/b2RevoluteJoint.cpp @@ -72,12 +72,10 @@ void b2RevoluteJoint::InitVelocityConstraints(const b2SolverData& data) m_invIA = m_bodyA->m_invI; m_invIB = m_bodyB->m_invI; - b2Vec2 cA = data.positions[m_indexA].c; float32 aA = data.positions[m_indexA].a; b2Vec2 vA = data.velocities[m_indexA].v; float32 wA = data.velocities[m_indexA].w; - b2Vec2 cB = data.positions[m_indexB].c; float32 aB = data.positions[m_indexB].a; b2Vec2 vB = data.velocities[m_indexB].v; float32 wB = data.velocities[m_indexB].w; diff --git a/src/libraries/Box2D/Dynamics/Joints/b2WeldJoint.cpp b/src/libraries/Box2D/Dynamics/Joints/b2WeldJoint.cpp index 8e49aca17..253f7216e 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2WeldJoint.cpp +++ b/src/libraries/Box2D/Dynamics/Joints/b2WeldJoint.cpp @@ -66,12 +66,10 @@ void b2WeldJoint::InitVelocityConstraints(const b2SolverData& data) m_invIA = m_bodyA->m_invI; m_invIB = m_bodyB->m_invI; - b2Vec2 cA = data.positions[m_indexA].c; float32 aA = data.positions[m_indexA].a; b2Vec2 vA = data.velocities[m_indexA].v; float32 wA = data.velocities[m_indexA].w; - b2Vec2 cB = data.positions[m_indexB].c; float32 aB = data.positions[m_indexB].a; b2Vec2 vB = data.velocities[m_indexB].v; float32 wB = data.velocities[m_indexB].w; diff --git a/src/libraries/Box2D/Dynamics/Joints/b2WheelJoint.cpp b/src/libraries/Box2D/Dynamics/Joints/b2WheelJoint.cpp index 5ecc8928c..c9f6de600 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2WheelJoint.cpp +++ b/src/libraries/Box2D/Dynamics/Joints/b2WheelJoint.cpp @@ -55,7 +55,7 @@ b2WheelJoint::b2WheelJoint(const b2WheelJointDef* def) m_mass = 0.0f; m_impulse = 0.0f; - m_motorMass = 0.0; + m_motorMass = 0.0f; m_motorImpulse = 0.0f; m_springMass = 0.0f; m_springImpulse = 0.0f; diff --git a/src/libraries/Box2D/Dynamics/Joints/b2WheelJoint.h b/src/libraries/Box2D/Dynamics/Joints/b2WheelJoint.h index 968eeafe5..ca59d7a64 100755 --- a/src/libraries/Box2D/Dynamics/Joints/b2WheelJoint.h +++ b/src/libraries/Box2D/Dynamics/Joints/b2WheelJoint.h @@ -79,8 +79,6 @@ struct b2WheelJointDef : public b2JointDef class b2WheelJoint : public b2Joint { public: - void GetDefinition(b2WheelJointDef* def) const; - b2Vec2 GetAnchorA() const; b2Vec2 GetAnchorB() const; diff --git a/src/libraries/Box2D/Dynamics/b2Body.cpp b/src/libraries/Box2D/Dynamics/b2Body.cpp index b19c7ffd5..b6550531f 100755 --- a/src/libraries/Box2D/Dynamics/b2Body.cpp +++ b/src/libraries/Box2D/Dynamics/b2Body.cpp @@ -141,10 +141,25 @@ void b2Body::SetType(b2BodyType type) m_force.SetZero(); m_torque = 0.0f; - // Since the body type changed, we need to flag contacts for filtering. + // Delete the attached contacts. + b2ContactEdge* ce = m_contactList; + while (ce) + { + b2ContactEdge* ce0 = ce; + ce = ce->next; + m_world->m_contactManager.Destroy(ce0->contact); + } + m_contactList = NULL; + + // Touch the proxies so that new contacts will be created (when appropriate) + b2BroadPhase* broadPhase = &m_world->m_contactManager.m_broadPhase; for (b2Fixture* f = m_fixtureList; f; f = f->m_next) { - f->Refilter(); + int32 proxyCount = f->m_proxyCount; + for (int32 i = 0; i < proxyCount; ++i) + { + broadPhase->TouchProxy(f->m_proxies[i].proxyId); + } } } @@ -421,8 +436,6 @@ void b2Body::SetTransform(const b2Vec2& position, float32 angle) { f->Synchronize(broadPhase, m_xf, m_xf); } - - m_world->m_contactManager.FindNewContacts(); } void b2Body::SynchronizeFixtures() @@ -483,6 +496,28 @@ void b2Body::SetActive(bool flag) } } +void b2Body::SetFixedRotation(bool flag) +{ + bool status = (m_flags & e_fixedRotationFlag) == e_fixedRotationFlag; + if (status == flag) + { + return; + } + + if (flag) + { + m_flags |= e_fixedRotationFlag; + } + else + { + m_flags &= ~e_fixedRotationFlag; + } + + m_angularVelocity = 0.0f; + + ResetMassData(); +} + void b2Body::Dump() { int32 bodyIndex = m_islandIndex; @@ -511,4 +546,4 @@ void b2Body::Dump() b2Log(" }\n"); } b2Log("}\n"); -} \ No newline at end of file +} diff --git a/src/libraries/Box2D/Dynamics/b2Body.h b/src/libraries/Box2D/Dynamics/b2Body.h index c6009513a..24bae6403 100755 --- a/src/libraries/Box2D/Dynamics/b2Body.h +++ b/src/libraries/Box2D/Dynamics/b2Body.h @@ -154,8 +154,8 @@ public: void DestroyFixture(b2Fixture* fixture); /// Set the position of the body's origin and rotation. - /// This breaks any contacts and wakes the other bodies. /// Manipulating a body's transform may cause non-physical behavior. + /// Note: contacts are updated on the next call to b2World::Step. /// @param position the world position of the body's local origin. /// @param angle the world rotation in radians. void SetTransform(const b2Vec2& position, float32 angle); @@ -184,7 +184,7 @@ public: /// Get the linear velocity of the center of mass. /// @return the linear velocity of the center of mass. - b2Vec2 GetLinearVelocity() const; + const b2Vec2& GetLinearVelocity() const; /// Set the angular velocity. /// @param omega the new angular velocity in radians/second. @@ -199,28 +199,33 @@ public: /// affect the angular velocity. This wakes up the body. /// @param force the world force vector, usually in Newtons (N). /// @param point the world position of the point of application. - void ApplyForce(const b2Vec2& force, const b2Vec2& point); + /// @param wake also wake up the body + void ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake); /// Apply a force to the center of mass. This wakes up the body. /// @param force the world force vector, usually in Newtons (N). - void ApplyForceToCenter(const b2Vec2& force); + /// @param wake also wake up the body + void ApplyForceToCenter(const b2Vec2& force, bool wake); /// Apply a torque. This affects the angular velocity /// without affecting the linear velocity of the center of mass. /// This wakes up the body. /// @param torque about the z-axis (out of the screen), usually in N-m. - void ApplyTorque(float32 torque); + /// @param wake also wake up the body + void ApplyTorque(float32 torque, bool wake); /// Apply an impulse at a point. This immediately modifies the velocity. /// It also modifies the angular velocity if the point of application /// is not at the center of mass. This wakes up the body. /// @param impulse the world impulse vector, usually in N-seconds or kg-m/s. /// @param point the world position of the point of application. - void ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point); + /// @param wake also wake up the body + void ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake); /// Apply an angular impulse. /// @param impulse the angular impulse in units of kg*m*m/s - void ApplyAngularImpulse(float32 impulse); + /// @param wake also wake up the body + void ApplyAngularImpulse(float32 impulse, bool wake); /// Get the total mass of the body. /// @return the mass, usually in kilograms (kg). @@ -315,11 +320,11 @@ public: /// Set the sleep state of the body. A sleeping body has very /// low CPU cost. - /// @param flag set to true to put body to sleep, false to wake it. + /// @param flag set to true to wake the body, false to put it to sleep. void SetAwake(bool flag); /// Get the sleeping state of this body. - /// @return true if the body is sleeping. + /// @return true if the body is awake. bool IsAwake() const; /// Set the active state of the body. An inactive body is not @@ -387,15 +392,16 @@ private: friend class b2Contact; friend class b2DistanceJoint; + friend class b2FrictionJoint; friend class b2GearJoint; - friend class b2WheelJoint; + friend class b2MotorJoint; friend class b2MouseJoint; friend class b2PrismaticJoint; friend class b2PulleyJoint; friend class b2RevoluteJoint; - friend class b2WeldJoint; - friend class b2FrictionJoint; friend class b2RopeJoint; + friend class b2WeldJoint; + friend class b2WheelJoint; // m_flags enum @@ -505,7 +511,7 @@ inline void b2Body::SetLinearVelocity(const b2Vec2& v) m_linearVelocity = v; } -inline b2Vec2 b2Body::GetLinearVelocity() const +inline const b2Vec2& b2Body::GetLinearVelocity() const { return m_linearVelocity; } @@ -655,20 +661,6 @@ inline bool b2Body::IsActive() const return (m_flags & e_activeFlag) == e_activeFlag; } -inline void b2Body::SetFixedRotation(bool flag) -{ - if (flag) - { - m_flags |= e_fixedRotationFlag; - } - else - { - m_flags &= ~e_fixedRotationFlag; - } - - ResetMassData(); -} - inline bool b2Body::IsFixedRotation() const { return (m_flags & e_fixedRotationFlag) == e_fixedRotationFlag; @@ -742,79 +734,101 @@ inline void* b2Body::GetUserData() const return m_userData; } -inline void b2Body::ApplyForce(const b2Vec2& force, const b2Vec2& point) +inline void b2Body::ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake) { if (m_type != b2_dynamicBody) { return; } - if (IsAwake() == false) + if (wake && (m_flags & e_awakeFlag) == 0) { SetAwake(true); } - m_force += force; - m_torque += b2Cross(point - m_sweep.c, force); + // Don't accumulate a force if the body is sleeping. + if (m_flags & e_awakeFlag) + { + m_force += force; + m_torque += b2Cross(point - m_sweep.c, force); + } } -inline void b2Body::ApplyForceToCenter(const b2Vec2& force) +inline void b2Body::ApplyForceToCenter(const b2Vec2& force, bool wake) { if (m_type != b2_dynamicBody) { return; } - if (IsAwake() == false) + if (wake && (m_flags & e_awakeFlag) == 0) { SetAwake(true); } - m_force += force; + // Don't accumulate a force if the body is sleeping + if (m_flags & e_awakeFlag) + { + m_force += force; + } } -inline void b2Body::ApplyTorque(float32 torque) +inline void b2Body::ApplyTorque(float32 torque, bool wake) { if (m_type != b2_dynamicBody) { return; } - if (IsAwake() == false) + if (wake && (m_flags & e_awakeFlag) == 0) { SetAwake(true); } - m_torque += torque; + // Don't accumulate a force if the body is sleeping + if (m_flags & e_awakeFlag) + { + m_torque += torque; + } } -inline void b2Body::ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point) +inline void b2Body::ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake) { if (m_type != b2_dynamicBody) { return; } - if (IsAwake() == false) + if (wake && (m_flags & e_awakeFlag) == 0) { SetAwake(true); } - m_linearVelocity += m_invMass * impulse; - m_angularVelocity += m_invI * b2Cross(point - m_sweep.c, impulse); + + // Don't accumulate velocity if the body is sleeping + if (m_flags & e_awakeFlag) + { + m_linearVelocity += m_invMass * impulse; + m_angularVelocity += m_invI * b2Cross(point - m_sweep.c, impulse); + } } -inline void b2Body::ApplyAngularImpulse(float32 impulse) +inline void b2Body::ApplyAngularImpulse(float32 impulse, bool wake) { if (m_type != b2_dynamicBody) { return; } - if (IsAwake() == false) + if (wake && (m_flags & e_awakeFlag) == 0) { SetAwake(true); } - m_angularVelocity += m_invI * impulse; + + // Don't accumulate velocity if the body is sleeping + if (m_flags & e_awakeFlag) + { + m_angularVelocity += m_invI * impulse; + } } inline void b2Body::SynchronizeTransform() diff --git a/src/libraries/Box2D/Dynamics/b2ContactManager.cpp b/src/libraries/Box2D/Dynamics/b2ContactManager.cpp index 47331bd9e..d8a189b74 100755 --- a/src/libraries/Box2D/Dynamics/b2ContactManager.cpp +++ b/src/libraries/Box2D/Dynamics/b2ContactManager.cpp @@ -286,8 +286,11 @@ void b2ContactManager::AddPair(void* proxyUserDataA, void* proxyUserDataB) bodyB->m_contactList = &c->m_nodeB; // Wake up the bodies - bodyA->SetAwake(true); - bodyB->SetAwake(true); + if (fixtureA->IsSensor() == false && fixtureB->IsSensor() == false) + { + bodyA->SetAwake(true); + bodyB->SetAwake(true); + } ++m_contactCount; } diff --git a/src/libraries/Box2D/Dynamics/b2Fixture.cpp b/src/libraries/Box2D/Dynamics/b2Fixture.cpp index 1d09c6d57..fb17f0a3e 100755 --- a/src/libraries/Box2D/Dynamics/b2Fixture.cpp +++ b/src/libraries/Box2D/Dynamics/b2Fixture.cpp @@ -267,11 +267,11 @@ void b2Fixture::Dump(int32 bodyIndex) b2PolygonShape* s = (b2PolygonShape*)m_shape; b2Log(" b2PolygonShape shape;\n"); b2Log(" b2Vec2 vs[%d];\n", b2_maxPolygonVertices); - for (int32 i = 0; i < s->m_vertexCount; ++i) + for (int32 i = 0; i < s->m_count; ++i) { b2Log(" vs[%d].Set(%.15lef, %.15lef);\n", i, s->m_vertices[i].x, s->m_vertices[i].y); } - b2Log(" shape.Set(vs, %d);\n", s->m_vertexCount); + b2Log(" shape.Set(vs, %d);\n", s->m_count); } break; diff --git a/src/libraries/Box2D/Dynamics/b2Island.cpp b/src/libraries/Box2D/Dynamics/b2Island.cpp index 0ecb273a5..d46589dee 100755 --- a/src/libraries/Box2D/Dynamics/b2Island.cpp +++ b/src/libraries/Box2D/Dynamics/b2Island.cpp @@ -211,10 +211,10 @@ void b2Island::Solve(b2Profile* profile, const b2TimeStep& step, const b2Vec2& g // Solution: v(t) = v0 * exp(-c * t) // Time step: v(t + dt) = v0 * exp(-c * (t + dt)) = v0 * exp(-c * t) * exp(-c * dt) = v * exp(-c * dt) // v2 = exp(-c * dt) * v1 - // Taylor expansion: - // v2 = (1.0f - c * dt) * v1 - v *= b2Clamp(1.0f - h * b->m_linearDamping, 0.0f, 1.0f); - w *= b2Clamp(1.0f - h * b->m_angularDamping, 0.0f, 1.0f); + // Pade approximation: + // v2 = v1 * 1 / (1 + c * dt) + v *= 1.0f / (1.0f + h * b->m_linearDamping); + w *= 1.0f / (1.0f + h * b->m_angularDamping); } m_positions[i].c = c; diff --git a/src/libraries/Box2D/Dynamics/b2World.cpp b/src/libraries/Box2D/Dynamics/b2World.cpp index fae03ecb5..a3a6a1e41 100755 --- a/src/libraries/Box2D/Dynamics/b2World.cpp +++ b/src/libraries/Box2D/Dynamics/b2World.cpp @@ -1073,7 +1073,7 @@ void b2World::DrawShape(b2Fixture* fixture, const b2Transform& xf, const b2Color case b2Shape::e_polygon: { b2PolygonShape* poly = (b2PolygonShape*)fixture->GetShape(); - int32 vertexCount = poly->m_vertexCount; + int32 vertexCount = poly->m_count; b2Assert(vertexCount <= b2_maxPolygonVertices); b2Vec2 vertices[b2_maxPolygonVertices]; @@ -1256,6 +1256,29 @@ float32 b2World::GetTreeQuality() const return m_contactManager.m_broadPhase.GetTreeQuality(); } +void b2World::ShiftOrigin(const b2Vec2& newOrigin) +{ + b2Assert((m_flags & e_locked) == 0); + if ((m_flags & e_locked) == e_locked) + { + return; + } + + for (b2Body* b = m_bodyList; b; b = b->m_next) + { + b->m_xf.p -= newOrigin; + b->m_sweep.c0 -= newOrigin; + b->m_sweep.c -= newOrigin; + } + + for (b2Joint* j = m_jointList; j; j = j->m_next) + { + j->ShiftOrigin(newOrigin); + } + + m_contactManager.m_broadPhase.ShiftOrigin(newOrigin); +} + void b2World::Dump() { if ((m_flags & e_locked) == e_locked) diff --git a/src/libraries/Box2D/Dynamics/b2World.h b/src/libraries/Box2D/Dynamics/b2World.h index eba757276..4f94f6efe 100755 --- a/src/libraries/Box2D/Dynamics/b2World.h +++ b/src/libraries/Box2D/Dynamics/b2World.h @@ -104,7 +104,7 @@ public: /// @see SetAutoClearForces void ClearForces(); - /// Call this to draw shapes and other debug draw data. + /// Call this to draw shapes and other debug draw data. This is intentionally non-const. void DrawDebugData(); /// Query the world for all fixtures that potentially overlap the @@ -194,6 +194,11 @@ public: /// Get the flag that controls automatic clearing of forces after each time step. bool GetAutoClearForces() const; + /// Shift the world origin. Useful for large worlds. + /// The body shift formula is: position -= newOrigin + /// @param newOrigin the new origin with respect to the old origin + void ShiftOrigin(const b2Vec2& newOrigin); + /// Get the contact manager for testing. const b2ContactManager& GetContactManager() const; diff --git a/src/modules/physics/Joint.cpp b/src/modules/physics/Joint.cpp index 5df7c079e..66700e89c 100644 --- a/src/modules/physics/Joint.cpp +++ b/src/modules/physics/Joint.cpp @@ -51,6 +51,7 @@ StringMap::Entry Joint::typeEntries[] = {"weld", Joint::JOINT_WELD}, {"wheel", Joint::JOINT_WHEEL}, {"rope", Joint::JOINT_ROPE}, + {"motor", Joint::JOINT_MOTOR}, }; StringMap Joint::types(Joint::typeEntries, sizeof(Joint::typeEntries)); diff --git a/src/modules/physics/Joint.h b/src/modules/physics/Joint.h index c0155e64c..f5ac26cff 100644 --- a/src/modules/physics/Joint.h +++ b/src/modules/physics/Joint.h @@ -47,6 +47,7 @@ public: JOINT_WELD, JOINT_WHEEL, JOINT_ROPE, + JOINT_MOTOR, JOINT_MAX_ENUM }; diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index 2e9cb3f29..29c941b67 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/src/modules/physics/box2d/Body.cpp @@ -168,36 +168,36 @@ Body::Type Body::getType() const } } -void Body::applyLinearImpulse(float jx, float jy) +void Body::applyLinearImpulse(float jx, float jy, bool wake) { - body->ApplyLinearImpulse(Physics::scaleDown(b2Vec2(jx, jy)), body->GetWorldCenter()); + body->ApplyLinearImpulse(Physics::scaleDown(b2Vec2(jx, jy)), body->GetWorldCenter(), wake); } -void Body::applyLinearImpulse(float jx, float jy, float rx, float ry) +void Body::applyLinearImpulse(float jx, float jy, float rx, float ry, bool wake) { - body->ApplyLinearImpulse(Physics::scaleDown(b2Vec2(jx, jy)), Physics::scaleDown(b2Vec2(rx, ry))); + body->ApplyLinearImpulse(Physics::scaleDown(b2Vec2(jx, jy)), Physics::scaleDown(b2Vec2(rx, ry)), wake); } -void Body::applyAngularImpulse(float impulse) +void Body::applyAngularImpulse(float impulse, bool wake) { // Angular impulse is in kg*m^2/s, meaning it needs to be scaled twice - body->ApplyAngularImpulse(Physics::scaleDown(Physics::scaleDown(impulse))); + body->ApplyAngularImpulse(Physics::scaleDown(Physics::scaleDown(impulse)), wake); } -void Body::applyTorque(float t) +void Body::applyTorque(float t, bool wake) { // Torque is in N*m, or kg*m^2/s^2, meaning it also needs to be scaled twice - body->ApplyTorque(Physics::scaleDown(Physics::scaleDown(t))); + body->ApplyTorque(Physics::scaleDown(Physics::scaleDown(t)), wake); } -void Body::applyForce(float fx, float fy, float rx, float ry) +void Body::applyForce(float fx, float fy, float rx, float ry, bool wake) { - body->ApplyForce(Physics::scaleDown(b2Vec2(fx, fy)), Physics::scaleDown(b2Vec2(rx, ry))); + body->ApplyForce(Physics::scaleDown(b2Vec2(fx, fy)), Physics::scaleDown(b2Vec2(rx, ry)), wake); } -void Body::applyForce(float fx, float fy) +void Body::applyForce(float fx, float fy, bool wake) { - body->ApplyForceToCenter(Physics::scaleDown(b2Vec2(fx, fy))); + body->ApplyForceToCenter(Physics::scaleDown(b2Vec2(fx, fy)), wake); } void Body::setX(float x) diff --git a/src/modules/physics/box2d/Body.h b/src/modules/physics/box2d/Body.h index a18db857c..6ee019fdb 100644 --- a/src/modules/physics/box2d/Body.h +++ b/src/modules/physics/box2d/Body.h @@ -162,32 +162,32 @@ public: /** * Apply an impulse (jx, jy) with offset (0, 0). **/ - void applyLinearImpulse(float jx, float jy); + void applyLinearImpulse(float jx, float jy, bool wake); /** * Apply an impulse (jx, jy) with offset (rx, ry). **/ - void applyLinearImpulse(float jx, float jy, float rx, float ry); + void applyLinearImpulse(float jx, float jy, float rx, float ry, bool wake); /** * Apply an angular impulse to the body. **/ - void applyAngularImpulse(float impulse); + void applyAngularImpulse(float impulse, bool wake); /** * Apply torque (t). **/ - void applyTorque(float t); + void applyTorque(float t, bool wake); /** * Apply force (fx, fy) with offset (0, 0). **/ - void applyForce(float fx, float fy); + void applyForce(float fx, float fy, bool wake); /** * Apply force (fx, fy) with offset (rx, ry). **/ - void applyForce(float fx, float fy, float rx, float ry); + void applyForce(float fx, float fy, float rx, float ry, bool wake); /** * Sets the x-position of the Body. diff --git a/src/modules/physics/box2d/Contact.cpp b/src/modules/physics/box2d/Contact.cpp index 819a11d10..42416da81 100644 --- a/src/modules/physics/box2d/Contact.cpp +++ b/src/modules/physics/box2d/Contact.cpp @@ -126,6 +126,16 @@ void Contact::resetRestitution() contact->ResetRestitution(); } +void Contact::setTangentSpeed(float speed) +{ + contact->SetTangentSpeed(speed); +} + +float Contact::getTangentSpeed() const +{ + return contact->GetTangentSpeed(); +} + void Contact::getChildren(int &childA, int &childB) { childA = contact->GetChildIndexA(); diff --git a/src/modules/physics/box2d/Contact.h b/src/modules/physics/box2d/Contact.h index 0e026a2bc..c68ff22cf 100644 --- a/src/modules/physics/box2d/Contact.h +++ b/src/modules/physics/box2d/Contact.h @@ -136,6 +136,15 @@ public: **/ void resetRestitution(); + /** + * Set the desired tangent speed. + **/ + void setTangentSpeed(float speed); + + /** + * Get the desired tangent speed. + **/ + float getTangentSpeed() const; void getChildren(int &childA, int &childB); diff --git a/src/modules/physics/box2d/MotorJoint.cpp b/src/modules/physics/box2d/MotorJoint.cpp new file mode 100644 index 000000000..bcf4003eb --- /dev/null +++ b/src/modules/physics/box2d/MotorJoint.cpp @@ -0,0 +1,115 @@ +/** + * Copyright (c) 2006-2013 LOVE Development Team + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + **/ + +#include "MotorJoint.h" + +// Module +#include "Body.h" +#include "World.h" +#include "Physics.h" + +namespace love +{ +namespace physics +{ +namespace box2d +{ + +MotorJoint::MotorJoint(Body *body1, Body *body2) + : Joint(body1, body2) + , joint(NULL) +{ + b2MotorJointDef def; + + def.Initialize(body1->body, body2->body); + joint = (b2MotorJoint *) createJoint(&def); +} + +MotorJoint::MotorJoint(Body *body1, Body *body2, float correctionFactor) + : Joint(body1, body2) + , joint(NULL) +{ + b2MotorJointDef def; + + def.Initialize(body1->body, body2->body); + def.correctionFactor = correctionFactor; + + joint = (b2MotorJoint *) createJoint(&def); +} + +MotorJoint::~MotorJoint() +{ +} + +void MotorJoint::setLinearOffset(float x, float y) +{ + joint->SetLinearOffset(Physics::scaleDown(b2Vec2(x, y))); +} + +int MotorJoint::getLinearOffset(lua_State *L) const +{ + lua_pushnumber(L, Physics::scaleUp(joint->GetLinearOffset().x)); + lua_pushnumber(L, Physics::scaleUp(joint->GetLinearOffset().y)); + return 2; +} + +void MotorJoint::setAngularOffset(float angularOffset) +{ + joint->SetAngularOffset(angularOffset); +} + +float MotorJoint::getAngularOffset() const +{ + return joint->GetAngularOffset(); +} + +void MotorJoint::setMaxForce(float force) +{ + joint->SetMaxForce(Physics::scaleDown(force)); +} + +float MotorJoint::getMaxForce() const +{ + return Physics::scaleUp(joint->GetMaxForce()); +} + +void MotorJoint::setMaxTorque(float torque) +{ + joint->SetMaxTorque(Physics::scaleDown(Physics::scaleDown(torque))); +} + +float MotorJoint::getMaxTorque() const +{ + return Physics::scaleUp(Physics::scaleUp(joint->GetMaxTorque())); +} + +void MotorJoint::setCorrectionFactor(float factor) +{ + joint->SetCorrectionFactor(factor); +} + +float MotorJoint::getCorrectionFactor() const +{ + return joint->GetCorrectionFactor(); +} + +} // box2d +} // physics +} // love diff --git a/src/modules/physics/box2d/MotorJoint.h b/src/modules/physics/box2d/MotorJoint.h new file mode 100644 index 000000000..6e54d8f2a --- /dev/null +++ b/src/modules/physics/box2d/MotorJoint.h @@ -0,0 +1,85 @@ +/** + * Copyright (c) 2006-2013 LOVE Development Team + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + **/ + +#ifndef LOVE_PHYSICS_BOX2D_MOTOR_JOINT_H +#define LOVE_PHYSICS_BOX2D_MOTOR_JOINT_H + +// Module +#include "Joint.h" + +namespace love +{ +namespace physics +{ +namespace box2d +{ + +/** + * A motor joint is used to control the relative motion + * between two bodies. A typical usage is to control the movement + * of a dynamic body with respect to the ground. + */ +class MotorJoint : public Joint +{ +public: + + MotorJoint(Body *body1, Body* body2); + MotorJoint(Body *body1, Body* body2, float correctionFactor); + virtual ~MotorJoint(); + + /// Set/get the target linear offset, in frame A, in meters. + void setLinearOffset(float x, float y); + int getLinearOffset(lua_State *L) const; + + /// Set/get the target angular offset, in radians. + void setAngularOffset(float angularOffset); + float getAngularOffset() const; + + /// Set the maximum friction force. + void setMaxForce(float force); + + /// Get the maximum friction force. + float getMaxForce() const; + + /// Set the maximum friction torque. + void setMaxTorque(float torque); + + /// Get the maximum friction torque. + float getMaxTorque() const; + + /// Set the position correction factor in the range [0,1]. + void setCorrectionFactor(float factor); + + /// Get the position correction factor in the range [0,1]. + float getCorrectionFactor() const; + +private: + + // The Box2D MotorJoint object. + b2MotorJoint *joint; + +}; // MotorJoint + + +} // box2d +} // physics +} // love + +#endif // LOVE_PHYSICS_BOX2D_MOTOR_JOINT_H diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index 18b527ce3..24830e686 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -104,53 +104,25 @@ int Physics::newPolygonShape(lua_State *L) b2PolygonShape *s = new b2PolygonShape(); - bool reverse = false; - b2Vec2 edge1; b2Vec2 vecs[b2_maxPolygonVertices]; for (int i = 0; i < vcount; i++) { - float x = (float)luaL_checknumber(L, -2); - float y = (float)luaL_checknumber(L, -1); + float x = (float)luaL_checknumber(L, 1 + i * 2); + float y = (float)luaL_checknumber(L, 2 + i * 2); vecs[i] = Physics::scaleDown(b2Vec2(x, y)); - lua_pop(L, 2); - - if (!reverse) - { - // Detect clockwise winding. - if (i == 1) - { - edge1 = vecs[1] - vecs[0]; - } - else if (i == vcount - 1) - { - b2Vec2 edge2 = vecs[i] - vecs[i-1]; - // Also check the edge from the last and first point. - b2Vec2 edge3 = vecs[0] - vecs[i]; - if (b2Cross(edge1, edge2) < 0.0f || b2Cross(edge2, edge3) < 0.0f) - reverse = true; - } - else if (i > 1) - { - b2Vec2 edge2 = vecs[i] - vecs[i-1]; - if (b2Cross(edge1, edge2) < 0.0f) - reverse = true; - edge1 = edge2; - } - } } - if (reverse) + try { - for (int i = 0, j = vcount-1; i < j; ++i, --j) - { - b2Vec2 swap = vecs[i]; - vecs[i] = vecs[j]; - vecs[j] = swap; - } + s->Set(vecs, vcount); + } + catch (love::Exception &) + { + delete s; + throw; } - s->Set(vecs, vcount); PolygonShape *p = new PolygonShape(s); luax_pushtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, p); @@ -240,6 +212,17 @@ RopeJoint *Physics::newRopeJoint(Body *body1, Body *body2, float x1, float y1, f return new RopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected); } +MotorJoint *Physics::newMotorJoint(Body *body1, Body *body2) +{ + return new MotorJoint(body1, body2); +} + +MotorJoint *Physics::newMotorJoint(Body *body1, Body *body2, float correctionFactor) +{ + return new MotorJoint(body1, body2, correctionFactor); +} + + Fixture *Physics::newFixture(Body *body, Shape *shape, float density) { return new Fixture(body, shape, density); diff --git a/src/modules/physics/box2d/Physics.h b/src/modules/physics/box2d/Physics.h index 4e5f6317b..c82f9a53c 100644 --- a/src/modules/physics/box2d/Physics.h +++ b/src/modules/physics/box2d/Physics.h @@ -43,6 +43,7 @@ #include "WeldJoint.h" #include "WheelJoint.h" #include "RopeJoint.h" +#include "MotorJoint.h" namespace love { @@ -250,6 +251,13 @@ public: **/ RopeJoint *newRopeJoint(Body *body1, Body *body2, float x1, float y1, float x2, float y2, float maxLength, bool collideConnected); + /** + * Creates a new MotorJoint controlling the relative motion between body1 + * and body2. + **/ + MotorJoint *newMotorJoint(Body *body1, Body *body2); + MotorJoint *newMotorJoint(Body *body1, Body *body2, float correctionFactor); + /** * Creates a new Fixture attaching shape to body. * @param body The body to attach the Fixture to. diff --git a/src/modules/physics/box2d/PolygonShape.cpp b/src/modules/physics/box2d/PolygonShape.cpp index fd5ead1e7..5743008ef 100644 --- a/src/modules/physics/box2d/PolygonShape.cpp +++ b/src/modules/physics/box2d/PolygonShape.cpp @@ -57,6 +57,12 @@ int PolygonShape::getPoints(lua_State *L) return count*2; } +bool PolygonShape::validate() const +{ + b2PolygonShape *p = (b2PolygonShape *)shape; + return p->Validate(); +} + } // box2d } // physics } // love diff --git a/src/modules/physics/box2d/PolygonShape.h b/src/modules/physics/box2d/PolygonShape.h index edb276fa7..d6a546a45 100644 --- a/src/modules/physics/box2d/PolygonShape.h +++ b/src/modules/physics/box2d/PolygonShape.h @@ -57,6 +57,11 @@ public: * The result can be directly passed into love.graphics.polygon(). **/ int getPoints(lua_State *L); + + /** + * Validate convexity. + **/ + bool validate() const; }; } // box2d diff --git a/src/modules/physics/box2d/World.cpp b/src/modules/physics/box2d/World.cpp index f70893fe8..d870c9dbf 100644 --- a/src/modules/physics/box2d/World.cpp +++ b/src/modules/physics/box2d/World.cpp @@ -396,6 +396,11 @@ int World::getGravity(lua_State *L) return 2; } +void World::translateOrigin(float x, float y) +{ + world->ShiftOrigin(Physics::scaleDown(b2Vec2(x, y))); +} + void World::setSleepingAllowed(bool allow) { world->SetAllowSleeping(allow); diff --git a/src/modules/physics/box2d/World.h b/src/modules/physics/box2d/World.h index f86b94467..c1f8483ea 100644 --- a/src/modules/physics/box2d/World.h +++ b/src/modules/physics/box2d/World.h @@ -181,6 +181,13 @@ public: **/ int getGravity(lua_State *L); + /** + * Translate the world origin. + * @param x The new world origin's x-coordinate relative to the old origin. + * @param y The new world origin's y-coordinate relative to the old origin. + **/ + void translateOrigin(float x, float y); + /** * Sets whether this World allows sleep. * @param allow True to allow, false to disallow. diff --git a/src/modules/physics/box2d/wrap_Body.cpp b/src/modules/physics/box2d/wrap_Body.cpp index e78b849fd..4f1826555 100644 --- a/src/modules/physics/box2d/wrap_Body.cpp +++ b/src/modules/physics/box2d/wrap_Body.cpp @@ -169,15 +169,19 @@ int w_Body_applyLinearImpulse(lua_State *L) float jx = (float)luaL_checknumber(L, 2); float jy = (float)luaL_checknumber(L, 3); - if (lua_gettop(L) == 3) + int nargs = lua_gettop(L); + + if (nargs <= 3 || (nargs == 4 && lua_type(L, 4) == LUA_TBOOLEAN)) { - t->applyLinearImpulse(jx, jy); + bool awake = luax_optboolean(L, 4, true); + t->applyLinearImpulse(jx, jy, awake); } - else if (lua_gettop(L) == 5) + else if (nargs >= 5) { float rx = (float)luaL_checknumber(L, 4); float ry = (float)luaL_checknumber(L, 5); - t->applyLinearImpulse(jx, jy, rx, ry); + bool awake = luax_optboolean(L, 6, true); + t->applyLinearImpulse(jx, jy, rx, ry, awake); } else { @@ -191,7 +195,8 @@ int w_Body_applyAngularImpulse(lua_State *L) { Body *t = luax_checkbody(L, 1); float i = (float)luaL_checknumber(L, 2); - t->applyAngularImpulse(i); + bool awake = luax_optboolean(L, 3, true); + t->applyAngularImpulse(i, awake); return 0; } @@ -199,7 +204,8 @@ int w_Body_applyTorque(lua_State *L) { Body *t = luax_checkbody(L, 1); float arg = (float)luaL_checknumber(L, 2); - t->applyTorque(arg); + bool awake = luax_optboolean(L, 3, true); + t->applyTorque(arg, awake); return 0; } @@ -209,16 +215,19 @@ int w_Body_applyForce(lua_State *L) float fx = (float)luaL_checknumber(L, 2); float fy = (float)luaL_checknumber(L, 3); + int nargs = lua_gettop(L); - if (lua_gettop(L) == 3) + if (nargs <= 3 || (nargs == 4 && lua_type(L, 4) == LUA_TBOOLEAN)) { - t->applyForce(fx, fy); + bool awake = luax_optboolean(L, 4, true); + t->applyForce(fx, fy, awake); } - else if (lua_gettop(L) == 5) + else if (lua_gettop(L) >= 5) { float rx = (float)luaL_checknumber(L, 4); float ry = (float)luaL_checknumber(L, 5); - t->applyForce(fx, fy, rx, ry); + bool awake = luax_optboolean(L, 6, true); + t->applyForce(fx, fy, rx, ry, awake); } else { diff --git a/src/modules/physics/box2d/wrap_Contact.cpp b/src/modules/physics/box2d/wrap_Contact.cpp index cdaafc32d..8aef81412 100644 --- a/src/modules/physics/box2d/wrap_Contact.cpp +++ b/src/modules/physics/box2d/wrap_Contact.cpp @@ -113,6 +113,21 @@ int w_Contact_resetRestitution(lua_State *L) return 0; } +int w_Contact_setTangentSpeed(lua_State *L) +{ + Contact *t = luax_checkcontact(L, 1); + float speed = (float) luaL_checknumber(L, 2); + t->setTangentSpeed(speed); + return 0; +} + +int w_Contact_getTangentSpeed(lua_State *L) +{ + Contact *t = luax_checkcontact(L, 1); + lua_pushnumber(L, t->getTangentSpeed()); + return 1; +} + int w_Contact_getChildren(lua_State *L) { Contact *t = luax_checkcontact(L, 1); @@ -138,6 +153,8 @@ extern "C" int luaopen_contact(lua_State *L) { "setEnabled", w_Contact_setEnabled }, { "resetFriction", w_Contact_resetFriction }, { "resetRestitution", w_Contact_resetRestitution }, + { "setTangentSpeed", w_Contact_setTangentSpeed }, + { "getTangentSpeed", w_Contact_getTangentSpeed }, { "getChildren", w_Contact_getChildren }, { 0, 0 } }; diff --git a/src/modules/physics/box2d/wrap_Contact.h b/src/modules/physics/box2d/wrap_Contact.h index 448456135..38bf54bd2 100644 --- a/src/modules/physics/box2d/wrap_Contact.h +++ b/src/modules/physics/box2d/wrap_Contact.h @@ -44,6 +44,8 @@ int w_Contact_setRestitution(lua_State *L); int w_Contact_setEnabled(lua_State *L); int w_Contact_resetFriction(lua_State *L); int w_Contact_resetRestitution(lua_State *L); +int w_Contact_setTangentSpeed(lua_State *L); +int w_Contact_getTangentSpeed(lua_State *L); int w_Contact_getChildren(lua_State *L); extern "C" int luaopen_contact(lua_State *L); diff --git a/src/modules/physics/box2d/wrap_MotorJoint.cpp b/src/modules/physics/box2d/wrap_MotorJoint.cpp new file mode 100644 index 000000000..f85cbec46 --- /dev/null +++ b/src/modules/physics/box2d/wrap_MotorJoint.cpp @@ -0,0 +1,143 @@ +/** + * Copyright (c) 2006-2013 LOVE Development Team + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + **/ + +#include "wrap_MotorJoint.h" + +namespace love +{ +namespace physics +{ +namespace box2d +{ + +MotorJoint *luax_checkmotorjoint(lua_State *L, int idx) +{ + MotorJoint *j = luax_checktype(L, idx, "MotorJoint", PHYSICS_MOTOR_JOINT_T); + if (!j->isValid()) + luaL_error(L, "Attempt to use destroyed joint."); + return j; +} + +int w_MotorJoint_setLinearOffset(lua_State *L) +{ + MotorJoint *t = luax_checkmotorjoint(L, 1); + float x = (float) luaL_checknumber(L, 2); + float y = (float) luaL_checknumber(L, 3); + t->setLinearOffset(x, y); + return 0; +} + +int w_MotorJoint_getLinearOffset(lua_State *L) +{ + MotorJoint *t = luax_checkmotorjoint(L, 1); + return t->getLinearOffset(L); +} + +int w_MotorJoint_setAngularOffset(lua_State *L) +{ + MotorJoint *t = luax_checkmotorjoint(L, 1); + float arg1 = (float) luaL_checknumber(L, 2); + t->setAngularOffset(arg1); + return 0; +} + +int w_MotorJoint_getAngularOffset(lua_State *L) +{ + MotorJoint *t = luax_checkmotorjoint(L, 1); + lua_pushnumber(L, t->getAngularOffset()); + return 1; +} + +int w_MotorJoint_setMaxForce(lua_State *L) +{ + MotorJoint *t = luax_checkmotorjoint(L, 1); + float arg1 = (float) luaL_checknumber(L, 2); + EXCEPT_GUARD(t->setMaxForce(arg1);) + return 0; +} + +int w_MotorJoint_getMaxForce(lua_State *L) +{ + MotorJoint *t = luax_checkmotorjoint(L, 1); + lua_pushnumber(L, t->getMaxForce()); + return 1; +} + +int w_MotorJoint_setMaxTorque(lua_State *L) +{ + MotorJoint *t = luax_checkmotorjoint(L, 1); + float arg1 = (float) luaL_checknumber(L, 2); + EXCEPT_GUARD(t->setMaxTorque(arg1);) + return 0; +} + +int w_MotorJoint_getMaxTorque(lua_State *L) +{ + MotorJoint *t = luax_checkmotorjoint(L, 1); + lua_pushnumber(L, t->getMaxTorque()); + return 1; +} + +int w_MotorJoint_setCorrectionFactor(lua_State *L) +{ + MotorJoint *t = luax_checkmotorjoint(L, 1); + float arg1 = (float) luaL_checknumber(L, 2); + EXCEPT_GUARD(t->setCorrectionFactor(arg1);) + return 0; +} + +int w_MotorJoint_getCorrectionFactor(lua_State *L) +{ + MotorJoint *t = luax_checkmotorjoint(L, 1); + lua_pushnumber(L, t->getCorrectionFactor()); + return 1; +} + +static const luaL_Reg functions[] = +{ + { "setLinearOffset", w_MotorJoint_setLinearOffset }, + { "getLinearOffset", w_MotorJoint_getLinearOffset }, + { "setAngularOffset", w_MotorJoint_setAngularOffset }, + { "getAngularOffset", w_MotorJoint_getAngularOffset }, + { "setMaxForce", w_MotorJoint_setMaxForce }, + { "getMaxForce", w_MotorJoint_getMaxForce }, + { "setMaxTorque", w_MotorJoint_setMaxTorque }, + { "getMaxTorque", w_MotorJoint_getMaxTorque }, + { "setCorrectionFactor", w_MotorJoint_setCorrectionFactor }, + { "getCorrectionFactor", w_MotorJoint_getCorrectionFactor }, + // From Joint. + { "getType", w_Joint_getType }, + { "getAnchors", w_Joint_getAnchors }, + { "getReactionForce", w_Joint_getReactionForce }, + { "getReactionTorque", w_Joint_getReactionTorque }, + { "getCollideConnected", w_Joint_getCollideConnected }, + { "destroy", w_Joint_destroy }, + { 0, 0 } +}; + +extern "C" int luaopen_motorjoint(lua_State *L) +{ + return luax_register_type(L, "MotorJoint", functions); +} + + +} // box2d +} // phyics +} // love \ No newline at end of file diff --git a/src/modules/physics/box2d/wrap_MotorJoint.h b/src/modules/physics/box2d/wrap_MotorJoint.h new file mode 100644 index 000000000..fad2bf8a4 --- /dev/null +++ b/src/modules/physics/box2d/wrap_MotorJoint.h @@ -0,0 +1,53 @@ +/** + * Copyright (c) 2006-2013 LOVE Development Team + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + **/ + +#ifndef LOVE_PHYSICS_BOX2D_WRAP_MOTOR_JOINT_H +#define LOVE_PHYSICS_BOX2D_WRAP_MOTOR_JOINT_H + +// LOVE +#include "common/runtime.h" +#include "wrap_Joint.h" +#include "MotorJoint.h" + +namespace love +{ +namespace physics +{ +namespace box2d +{ + +MotorJoint *luax_checkmotorjoint(lua_State *L, int idx); +int w_MotorJoint_setLinearOffset(lua_State *L); +int w_MotorJoint_getLinearOffset(lua_State *L); +int w_MotorJoint_setAngularOffset(lua_State *L); +int w_MotorJoint_getAngularOffset(lua_State *L); +int w_MotorJoint_setMaxForce(lua_State *L); +int w_MotorJoint_getMaxForce(lua_State *L); +int w_MotorJoint_setMaxTorque(lua_State *L); +int w_MotorJoint_getMaxTorque(lua_State *L); +int w_MotorJoint_setCorrectionFactor(lua_State *L); +int w_MotorJoint_getCorrectionFactor(lua_State *L); +extern "C" int luaopen_motorjoint(lua_State *L); + +} // box2d +} // physics +} // love + +#endif // LOVE_PHYSICS_BOX2D_WRAP_MOTOR_JOINT_H diff --git a/src/modules/physics/box2d/wrap_Physics.cpp b/src/modules/physics/box2d/wrap_Physics.cpp index cf2495efd..f5df282e7 100644 --- a/src/modules/physics/box2d/wrap_Physics.cpp +++ b/src/modules/physics/box2d/wrap_Physics.cpp @@ -40,6 +40,7 @@ #include "wrap_WeldJoint.h" #include "wrap_WheelJoint.h" #include "wrap_RopeJoint.h" +#include "wrap_MotorJoint.h" namespace love { @@ -374,6 +375,24 @@ int w_newRopeJoint(lua_State *L) return 1; } +int w_newMotorJoint(lua_State *L) +{ + Body *body1 = luax_checktype(L, 1, "Body", PHYSICS_BODY_T); + Body *body2 = luax_checktype(L, 2, "Body", PHYSICS_BODY_T); + MotorJoint *j = 0; + if (!lua_isnoneornil(L, 3)) + { + float correctionFactor = (float)luaL_checknumber(L, 3); + EXCEPT_GUARD(j = instance->newMotorJoint(body1, body2, correctionFactor);) + } + else + { + EXCEPT_GUARD(j = instance->newMotorJoint(body1, body2);) + } + luax_pushtype(L, "MotorJoint", PHYSICS_MOTOR_JOINT_T, j); + return 1; +} + int w_getDistance(lua_State *L) { return instance->getDistance(L); @@ -413,6 +432,7 @@ static const luaL_Reg functions[] = { "newWeldJoint", w_newWeldJoint }, { "newWheelJoint", w_newWheelJoint }, { "newRopeJoint", w_newRopeJoint }, + { "newMotorJoint", w_newMotorJoint }, { "getDistance", w_getDistance }, { "getMeter", w_getMeter }, { "setMeter", w_setMeter }, @@ -441,6 +461,7 @@ static const lua_CFunction types[] = luaopen_weldjoint, luaopen_wheeljoint, luaopen_ropejoint, + luaopen_motorjoint, 0 }; diff --git a/src/modules/physics/box2d/wrap_Physics.h b/src/modules/physics/box2d/wrap_Physics.h index 61829840a..900ca8e3a 100644 --- a/src/modules/physics/box2d/wrap_Physics.h +++ b/src/modules/physics/box2d/wrap_Physics.h @@ -50,6 +50,7 @@ int w_newFrictionJoint(lua_State *L); int w_newWeldJoint(lua_State *L); int w_newWheelJoint(lua_State *L); int w_newRopeJoint(lua_State *L); +int w_newMotorJoint(lua_State *L); int w_getDistance(lua_State *L); int w_setMeter(lua_State *L); int w_getMeter(lua_State *L); diff --git a/src/modules/physics/box2d/wrap_PolygonShape.cpp b/src/modules/physics/box2d/wrap_PolygonShape.cpp index 948d39ef0..96436857a 100644 --- a/src/modules/physics/box2d/wrap_PolygonShape.cpp +++ b/src/modules/physics/box2d/wrap_PolygonShape.cpp @@ -39,9 +39,17 @@ int w_PolygonShape_getPoints(lua_State *L) return t->getPoints(L); } +int w_PolygonShape_validate(lua_State *L) +{ + PolygonShape *t = luax_checkpolygonshape(L, 1); + luax_pushboolean(L, t->validate()); + return 1; +} + static const luaL_Reg functions[] = { { "getPoints", w_PolygonShape_getPoints }, + { "validate", w_PolygonShape_validate }, // From Shape. { "getType", w_Shape_getType }, { "getRadius", w_Shape_getRadius }, diff --git a/src/modules/physics/box2d/wrap_PolygonShape.h b/src/modules/physics/box2d/wrap_PolygonShape.h index 46ebee91b..a745a2652 100644 --- a/src/modules/physics/box2d/wrap_PolygonShape.h +++ b/src/modules/physics/box2d/wrap_PolygonShape.h @@ -35,6 +35,7 @@ namespace box2d PolygonShape *luax_checkpolygonshape(lua_State *L, int idx); int w_PolygonShape_getPoints(lua_State *L); +int w_PolygonShape_validate(lua_State *L); extern "C" int luaopen_polygonshape(lua_State *L); } // box2d diff --git a/src/modules/physics/box2d/wrap_World.cpp b/src/modules/physics/box2d/wrap_World.cpp index 13deee20b..ea7e2b293 100644 --- a/src/modules/physics/box2d/wrap_World.cpp +++ b/src/modules/physics/box2d/wrap_World.cpp @@ -87,6 +87,15 @@ int w_World_getGravity(lua_State *L) return t->getGravity(L); } +int w_World_translateOrigin(lua_State *L) +{ + World *t = luax_checkworld(L, 1); + float arg1 = (float)luaL_checknumber(L, 2); + float arg2 = (float)luaL_checknumber(L, 3); + EXCEPT_GUARD(t->translateOrigin(arg1, arg2);) + return 0; +} + int w_World_setSleepingAllowed(lua_State *L) { World *t = luax_checkworld(L, 1); @@ -184,6 +193,7 @@ static const luaL_Reg functions[] = { "getContactFilter", w_World_getContactFilter }, { "setGravity", w_World_setGravity }, { "getGravity", w_World_getGravity }, + { "translateOrigin", w_World_translateOrigin }, { "setSleepingAllowed", w_World_setSleepingAllowed }, { "isSleepingAllowed", w_World_isSleepingAllowed }, { "isLocked", w_World_isLocked }, diff --git a/src/modules/physics/box2d/wrap_World.h b/src/modules/physics/box2d/wrap_World.h index aa642c306..2e060cd0a 100644 --- a/src/modules/physics/box2d/wrap_World.h +++ b/src/modules/physics/box2d/wrap_World.h @@ -42,6 +42,7 @@ int w_World_setContactFilter(lua_State *L); int w_World_getContactFilter(lua_State *L); int w_World_setGravity(lua_State *L); int w_World_getGravity(lua_State *L); +int w_World_translateOrigin(lua_State *L); int w_World_setSleepingAllowed(lua_State *L); int w_World_isSleepingAllowed(lua_State *L); int w_World_isLocked(lua_State *L);