From 99660f39ab7e932769f83384eeb3cbace6239af0 Mon Sep 17 00:00:00 2001 From: Bill Meltsner Date: Thu, 15 Sep 2011 12:40:16 -0400 Subject: [PATCH] Added RopeJoint --HG-- branch : box2d-update --- .../macosx/love.xcodeproj/project.pbxproj | 12 ++++ src/modules/physics/box2d/Physics.cpp | 5 ++ src/modules/physics/box2d/Physics.h | 11 ++++ src/modules/physics/box2d/RopeJoint.cpp | 59 ++++++++++++++++++ src/modules/physics/box2d/RopeJoint.h | 62 +++++++++++++++++++ src/modules/physics/box2d/wrap_Physics.cpp | 17 +++++ src/modules/physics/box2d/wrap_Physics.h | 2 + src/modules/physics/box2d/wrap_RopeJoint.cpp | 60 ++++++++++++++++++ src/modules/physics/box2d/wrap_RopeJoint.h | 43 +++++++++++++ 9 files changed, 271 insertions(+) create mode 100644 src/modules/physics/box2d/RopeJoint.cpp create mode 100644 src/modules/physics/box2d/RopeJoint.h create mode 100644 src/modules/physics/box2d/wrap_RopeJoint.cpp create mode 100644 src/modules/physics/box2d/wrap_RopeJoint.h diff --git a/platform/macosx/love.xcodeproj/project.pbxproj b/platform/macosx/love.xcodeproj/project.pbxproj index 0b351b637..70382453f 100644 --- a/platform/macosx/love.xcodeproj/project.pbxproj +++ b/platform/macosx/love.xcodeproj/project.pbxproj @@ -209,6 +209,8 @@ A9BD60741226C988007DEC63 /* Framebuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9BD60701226C988007DEC63 /* Framebuffer.cpp */; }; A9BD60751226C988007DEC63 /* wrap_Framebuffer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9BD60721226C988007DEC63 /* wrap_Framebuffer.cpp */; }; A9CF0E8610B9EB1000E6F37E /* utf8.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9CF0E8510B9EB1000E6F37E /* utf8.cpp */; }; + A9D1D20214224E1300A8BC2F /* RopeJoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9D1D20114224E1300A8BC2F /* RopeJoint.cpp */; }; + A9D1D2041422578C00A8BC2F /* wrap_RopeJoint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = A9D1D2031422578A00A8BC2F /* wrap_RopeJoint.cpp */; }; A9D307EA106635C3004FEDF8 /* physfs.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A9D307E9106635C3004FEDF8 /* physfs.framework */; }; A9D307F2106635D3004FEDF8 /* physfs.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = A9D307E9106635C3004FEDF8 /* physfs.framework */; }; A9DEC1C11046EFA70049C70C /* Love.icns in Resources */ = {isa = PBXBuildFile; fileRef = A9DEC1BF1046EFA60049C70C /* Love.icns */; }; @@ -669,6 +671,10 @@ A9BFAA851137C1CE005FE0AD /* ThreadModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThreadModule.h; sourceTree = ""; }; A9CF0E8410B9EB1000E6F37E /* utf8.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = utf8.h; sourceTree = ""; }; A9CF0E8510B9EB1000E6F37E /* utf8.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = utf8.cpp; sourceTree = ""; }; + A9D1D20014224DFF00A8BC2F /* RopeJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RopeJoint.h; sourceTree = ""; }; + A9D1D20114224E1300A8BC2F /* RopeJoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RopeJoint.cpp; sourceTree = ""; }; + A9D1D2031422578A00A8BC2F /* wrap_RopeJoint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wrap_RopeJoint.cpp; sourceTree = ""; }; + A9D1D2051422579500A8BC2F /* wrap_RopeJoint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wrap_RopeJoint.h; sourceTree = ""; }; A9D307E9106635C3004FEDF8 /* physfs.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = physfs.framework; path = /Library/Frameworks/physfs.framework; sourceTree = ""; }; A9DEC1BF1046EFA60049C70C /* Love.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = Love.icns; path = icons/Love.icns; sourceTree = ""; }; A9DEC1C01046EFA70049C70C /* LoveDocument.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; name = LoveDocument.icns; path = icons/LoveDocument.icns; sourceTree = ""; }; @@ -1222,6 +1228,8 @@ A93E6B0310420AC8007D418B /* PrismaticJoint.h */, A93E6B0410420AC8007D418B /* PulleyJoint.cpp */, A93E6B0510420AC8007D418B /* PulleyJoint.h */, + A9D1D20114224E1300A8BC2F /* RopeJoint.cpp */, + A9D1D20014224DFF00A8BC2F /* RopeJoint.h */, A93E6B0610420AC8007D418B /* RevoluteJoint.cpp */, A93E6B0710420AC8007D418B /* RevoluteJoint.h */, A93E6B0810420AC8007D418B /* Shape.cpp */, @@ -1260,6 +1268,8 @@ A93E6B6510420ACA007D418B /* wrap_PulleyJoint.h */, A93E6B6610420ACA007D418B /* wrap_RevoluteJoint.cpp */, A93E6B6710420ACA007D418B /* wrap_RevoluteJoint.h */, + A9D1D2031422578A00A8BC2F /* wrap_RopeJoint.cpp */, + A9D1D2051422579500A8BC2F /* wrap_RopeJoint.h */, A93E6B6810420ACA007D418B /* wrap_Shape.cpp */, A93E6B6910420ACA007D418B /* wrap_Shape.h */, A96F41811412AFB70067FE9A /* wrap_WeldJoint.cpp */, @@ -1798,6 +1808,8 @@ A96F418C1412B3AD0067FE9A /* WheelJoint.cpp in Sources */, A96F41A41412C91F0067FE9A /* EdgeShape.cpp in Sources */, A96F41A71412C92B0067FE9A /* wrap_EdgeShape.cpp in Sources */, + A9D1D20214224E1300A8BC2F /* RopeJoint.cpp in Sources */, + A9D1D2041422578C00A8BC2F /* wrap_RopeJoint.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index 20a584021..bf43ddb22 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -188,6 +188,11 @@ namespace box2d { return new WheelJoint(body1, body2, x, y, ax, ay); } + + RopeJoint * Physics::newRopeJoint(Body * body1, Body * body2, float x1, float y1, float x2, float y2, float maxLength) + { + return new RopeJoint(body1, body2, x1, y1, x2, y2, maxLength); + } } // box2d } // physics diff --git a/src/modules/physics/box2d/Physics.h b/src/modules/physics/box2d/Physics.h index 9e78e14de..94839e865 100644 --- a/src/modules/physics/box2d/Physics.h +++ b/src/modules/physics/box2d/Physics.h @@ -40,6 +40,7 @@ #include "FrictionJoint.h" #include "WeldJoint.h" #include "WheelJoint.h" +#include "RopeJoint.h" namespace love { @@ -222,6 +223,16 @@ namespace box2d * @param ay The y-component of the world-axis. **/ WheelJoint * newWheelJoint(Body * body1, Body * body2, float x, float y, float ax, float ay); + + /** + * Creates a new RopeJoint connecting body1 with body2. + * @param x1 Anchor1 along the x-axis. (Local coordinates) + * @param y1 Anchor1 along the y-axis. (Local coordinates) + * @param x2 Anchor2 along the x-axis. (Local coordinates) + * @param y2 Anchor2 along the y-axis. (Local coordinates) + * @param maxLength The maximum distance for the bodies. + **/ + RopeJoint * newRopeJoint(Body * body1, Body * body2, float x1, float y1, float x2, float y2, float maxLength); }; // Physics diff --git a/src/modules/physics/box2d/RopeJoint.cpp b/src/modules/physics/box2d/RopeJoint.cpp new file mode 100644 index 000000000..5cbd26698 --- /dev/null +++ b/src/modules/physics/box2d/RopeJoint.cpp @@ -0,0 +1,59 @@ +/** +* Copyright (c) 2006-2011 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 "RopeJoint.h" + +// Module +#include "Body.h" +#include "World.h" + +namespace love +{ +namespace physics +{ +namespace box2d +{ + RopeJoint::RopeJoint(Body * body1, Body * body2, float x1, float y1, float x2, float y2, float maxLength) + : Joint(body1, body2), joint(NULL) + { + b2RopeJointDef def; + def.bodyA = body1->body; + def.bodyB = body2->body; + def.localAnchorA = world->scaleDown(b2Vec2(x1,y1)); + def.localAnchorB = world->scaleDown(b2Vec2(x2,y2)); + def.maxLength = world->scaleDown(maxLength); + joint = (b2RopeJoint*)createJoint(&def); + } + + RopeJoint::~RopeJoint() + { + destroyJoint(joint); + joint = 0; + } + + float RopeJoint::getMaxLength() const + { + return world->scaleUp(joint->GetMaxLength()); + } + + +} // box2d +} // physics +} // love diff --git a/src/modules/physics/box2d/RopeJoint.h b/src/modules/physics/box2d/RopeJoint.h new file mode 100644 index 000000000..886f459cd --- /dev/null +++ b/src/modules/physics/box2d/RopeJoint.h @@ -0,0 +1,62 @@ +/** +* Copyright (c) 2006-2011 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_ROPE_JOINT_H +#define LOVE_PHYSICS_BOX2D_ROPE_JOINT_H + +// Module +#include "Joint.h" + +namespace love +{ +namespace physics +{ +namespace box2d +{ + /** + * The RopeJoint enforces a maximum distance between two points + * on two bodies. It has no other effect. + **/ + class RopeJoint : public Joint + { + private: + // The Box2D RopeJoint object. + b2RopeJoint * joint; + public: + + /** + * Creates a RopeJoint connecting body1 to body2. + **/ + RopeJoint(Body * body1, Body * body2, float x1, float y1, float x2, float y2, float maxLength); + + virtual ~RopeJoint(); + + /** + * Gets the maximum length of the rope. + **/ + float getMaxLength() const; + + }; + +} // box2d +} // physics +} // love + +#endif // LOVE_PHYSICS_BOX2D_ROPE_JOINT_H diff --git a/src/modules/physics/box2d/wrap_Physics.cpp b/src/modules/physics/box2d/wrap_Physics.cpp index 78508fe3c..f72f6f6bd 100644 --- a/src/modules/physics/box2d/wrap_Physics.cpp +++ b/src/modules/physics/box2d/wrap_Physics.cpp @@ -239,6 +239,20 @@ namespace box2d luax_newtype(L, "WheelJoint", PHYSICS_WHEEL_JOINT_T, (void*)j); return 1; } + + int w_newRopeJoint(lua_State * L) + { + Body * body1 = luax_checktype(L, 1, "Body", PHYSICS_BODY_T); + Body * body2 = luax_checktype(L, 2, "Body", PHYSICS_BODY_T); + float x1 = (float)luaL_checknumber(L, 3); + float y1 = (float)luaL_checknumber(L, 4); + float x2 = (float)luaL_checknumber(L, 5); + float y2 = (float)luaL_checknumber(L, 6); + float maxLength = (float)luaL_checknumber(L, 7); + RopeJoint * j = instance->newRopeJoint(body1, body2, x1, y1, x2, y2, maxLength); + luax_newtype(L, "RopeJoint", PHYSICS_ROPE_JOINT_T, (void*)j); + return 1; + } // List of functions to wrap. static const luaL_Reg functions[] = { @@ -257,6 +271,7 @@ namespace box2d { "newFrictionJoint", w_newFrictionJoint }, { "newWeldJoint", w_newWeldJoint }, { "newWheelJoint", w_newWheelJoint }, + { "newRopeJoint", w_newRopeJoint }, { 0, 0 }, }; @@ -277,6 +292,8 @@ namespace box2d luaopen_gearjoint, luaopen_frictionjoint, luaopen_weldjoint, + luaopen_wheeljoint, + luaopen_ropejoint, 0 }; diff --git a/src/modules/physics/box2d/wrap_Physics.h b/src/modules/physics/box2d/wrap_Physics.h index fb715d32b..c87684796 100644 --- a/src/modules/physics/box2d/wrap_Physics.h +++ b/src/modules/physics/box2d/wrap_Physics.h @@ -41,6 +41,7 @@ #include "wrap_FrictionJoint.h" #include "wrap_WeldJoint.h" #include "wrap_WheelJoint.h" +#include "wrap_RopeJoint.h" namespace love { @@ -63,6 +64,7 @@ namespace box2d int w_newFrictionJoint(lua_State * L); int w_newWeldJoint(lua_State * L); int w_newWheelJoint(lua_State * L); + int w_newRopeJoint(lua_State * L); extern "C" LOVE_EXPORT int luaopen_love_physics(lua_State * L); } // box2d diff --git a/src/modules/physics/box2d/wrap_RopeJoint.cpp b/src/modules/physics/box2d/wrap_RopeJoint.cpp new file mode 100644 index 000000000..a86ba666a --- /dev/null +++ b/src/modules/physics/box2d/wrap_RopeJoint.cpp @@ -0,0 +1,60 @@ +/** +* Copyright (c) 2006-2011 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_RopeJoint.h" + +namespace love +{ +namespace physics +{ +namespace box2d +{ + RopeJoint * luax_checkropejoint(lua_State * L, int idx) + { + return luax_checktype(L, idx, "RopeJoint", PHYSICS_ROPE_JOINT_T); + } + + int w_RopeJoint_getMaxLength(lua_State * L) + { + RopeJoint * t = luax_checkropejoint(L, 1); + lua_pushnumber(L, t->getMaxLength()); + return 1; + } + + static const luaL_Reg functions[] = { + { "getMaxLength", w_RopeJoint_getMaxLength }, + // 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 } + }; + + int luaopen_ropejoint(lua_State * L) + { + return luax_register_type(L, "RopeJoint", functions); + } + +} // box2d +} // physics +} // love diff --git a/src/modules/physics/box2d/wrap_RopeJoint.h b/src/modules/physics/box2d/wrap_RopeJoint.h new file mode 100644 index 000000000..d1cc17ce4 --- /dev/null +++ b/src/modules/physics/box2d/wrap_RopeJoint.h @@ -0,0 +1,43 @@ +/** +* Copyright (c) 2006-2011 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_ROPE_JOINT_H +#define LOVE_PHYSICS_BOX2D_WRAP_ROPE_JOINT_H + +// LOVE +#include +#include "wrap_Joint.h" +#include "RopeJoint.h" + +namespace love +{ +namespace physics +{ +namespace box2d +{ + RopeJoint * luax_checkropejoint(lua_State * L, int idx); + int w_RopeJoint_getMaxLength(lua_State * L); + int luaopen_ropejoint(lua_State * L); + +} // box2d +} // physics +} // love + +#endif // LOVE_PHYSICS_BOX2D_WRAP_ROPE_JOINT_H