From b4fae893fd54227bf20696a34d0a7a9d9cfa7f36 Mon Sep 17 00:00:00 2001 From: Bill Meltsner Date: Sun, 13 Mar 2011 13:16:02 -0400 Subject: [PATCH] updating the Joints --HG-- branch : box2d-update --- src/modules/physics/box2d/DistanceJoint.cpp | 12 ++--- src/modules/physics/box2d/GearJoint.cpp | 8 +-- src/modules/physics/box2d/Joint.cpp | 25 +++------ src/modules/physics/box2d/Joint.h | 14 +---- src/modules/physics/box2d/MouseJoint.cpp | 12 ++--- src/modules/physics/box2d/Physics.cpp | 2 +- src/modules/physics/box2d/PrismaticJoint.cpp | 7 +-- src/modules/physics/box2d/PrismaticJoint.h | 5 -- src/modules/physics/box2d/PulleyJoint.cpp | 51 ++----------------- src/modules/physics/box2d/PulleyJoint.h | 23 --------- src/modules/physics/box2d/RevoluteJoint.cpp | 7 +-- src/modules/physics/box2d/RevoluteJoint.h | 5 -- src/modules/physics/box2d/World.cpp | 8 +++ src/modules/physics/box2d/World.h | 9 ++++ .../physics/box2d/wrap_PrismaticJoint.cpp | 8 --- .../physics/box2d/wrap_PrismaticJoint.h | 1 - .../physics/box2d/wrap_PulleyJoint.cpp | 44 ---------------- src/modules/physics/box2d/wrap_PulleyJoint.h | 5 -- .../physics/box2d/wrap_RevoluteJoint.cpp | 8 --- .../physics/box2d/wrap_RevoluteJoint.h | 1 - 20 files changed, 50 insertions(+), 205 deletions(-) diff --git a/src/modules/physics/box2d/DistanceJoint.cpp b/src/modules/physics/box2d/DistanceJoint.cpp index 3089139e4..d05ee3883 100644 --- a/src/modules/physics/box2d/DistanceJoint.cpp +++ b/src/modules/physics/box2d/DistanceJoint.cpp @@ -46,32 +46,32 @@ namespace box2d void DistanceJoint::setLength(float length) { - joint->m_length = world->scaleDown(length); + joint->SetLength(world->scaleDown(length)); } float DistanceJoint::getLength() const { - return world->scaleUp(joint->m_length); + return world->scaleUp(joint->GetLength()); } void DistanceJoint::setFrequency(float hz) { - joint->m_frequencyHz = hz; + joint->SetFrequency(hz); } float DistanceJoint::getFrequency() const { - return joint->m_frequencyHz; + return joint->GetFrequency(); } void DistanceJoint::setDampingRatio(float d) { - joint->m_dampingRatio = d; + joint->SetDampingRatio(d); } float DistanceJoint::getDampingRatio() const { - return joint->m_dampingRatio; + return joint->GetDampingRatio(); } diff --git a/src/modules/physics/box2d/GearJoint.cpp b/src/modules/physics/box2d/GearJoint.cpp index 6dba58348..aad347f45 100644 --- a/src/modules/physics/box2d/GearJoint.cpp +++ b/src/modules/physics/box2d/GearJoint.cpp @@ -36,8 +36,8 @@ namespace box2d b2GearJointDef def; def.joint1 = joint1->joint; def.joint2 = joint2->joint; - def.body1 = joint1->body2->body; - def.body2 = joint2->body2->body; + def.bodyA = joint1->body2->body; + def.bodyB = joint2->body2->body; def.ratio = ratio; joint = (b2GearJoint*)createJoint(&def); @@ -51,12 +51,12 @@ namespace box2d void GearJoint::setRatio(float ratio) { - joint->m_ratio = ratio; + joint->SetRatio(ratio); } float GearJoint::getRatio() const { - return joint->m_ratio; + return joint->GetRatio(); } } // box2d diff --git a/src/modules/physics/box2d/Joint.cpp b/src/modules/physics/box2d/Joint.cpp index 4e7f0368e..a32f97372 100644 --- a/src/modules/physics/box2d/Joint.cpp +++ b/src/modules/physics/box2d/Joint.cpp @@ -79,34 +79,25 @@ namespace box2d int Joint::getAnchors(lua_State * L) { - lua_pushnumber(L, world->scaleUp(joint->GetAnchor1().x)); - lua_pushnumber(L, world->scaleUp(joint->GetAnchor1().y)); - lua_pushnumber(L, world->scaleUp(joint->GetAnchor2().x)); - lua_pushnumber(L, world->scaleUp(joint->GetAnchor2().y)); + lua_pushnumber(L, world->scaleUp(joint->GetAnchorA().x)); + lua_pushnumber(L, world->scaleUp(joint->GetAnchorA().y)); + lua_pushnumber(L, world->scaleUp(joint->GetAnchorB().x)); + lua_pushnumber(L, world->scaleUp(joint->GetAnchorB().y)); return 4; } int Joint::getReactionForce(lua_State * L) { - b2Vec2 v = joint->GetReactionForce(); + float dt = (float)luaL_checknumber(L, 2); + b2Vec2 v = joint->GetReactionForce(dt); lua_pushnumber(L, v.x); lua_pushnumber(L, v.y); return 2; } - float Joint::getReactionTorque() + float Joint::getReactionTorque(float dt) { - return joint->GetReactionTorque(); - } - - void Joint::setCollideConnected(bool collide) - { - joint->m_collideConnected = collide; - } - - bool Joint::getCollideConnected() const - { - return joint->m_collideConnected; + return joint->GetReactionTorque(dt); } b2Joint * Joint::createJoint(b2JointDef * def) diff --git a/src/modules/physics/box2d/Joint.h b/src/modules/physics/box2d/Joint.h index 6393f0dcb..f05beb735 100644 --- a/src/modules/physics/box2d/Joint.h +++ b/src/modules/physics/box2d/Joint.h @@ -97,19 +97,7 @@ namespace box2d /** * Gets the reaction torque on body2. **/ - float getReactionTorque(); - - /** - * Sets whether connected bodies should collide - * or not. Default is false. - **/ - void setCollideConnected(bool collide); - - /** - * Gets whether connected bodies should collide - * or not. - **/ - bool getCollideConnected() const; + float getReactionTorque(float dt); protected: diff --git a/src/modules/physics/box2d/MouseJoint.cpp b/src/modules/physics/box2d/MouseJoint.cpp index e35f37445..e3f239b8b 100644 --- a/src/modules/physics/box2d/MouseJoint.cpp +++ b/src/modules/physics/box2d/MouseJoint.cpp @@ -35,8 +35,8 @@ namespace box2d { b2MouseJointDef def; - def.body1 = body1->world->world->GetGroundBody(); - def.body2 = body1->body; + def.bodyA = body1->world->getGroundBody(); + def.bodyB = body1->body; def.maxForce = 1000.0f * body1->body->GetMass(); def.target = body1->world->scaleDown(b2Vec2(x,y)); joint = (b2MouseJoint*)createJoint(&def); @@ -55,19 +55,19 @@ namespace box2d int MouseJoint::getTarget(lua_State * L) { - lua_pushnumber(L, world->scaleUp(joint->m_target.x)); - lua_pushnumber(L, world->scaleUp(joint->m_target.y)); + lua_pushnumber(L, world->scaleUp(joint->GetTarget().x)); + lua_pushnumber(L, world->scaleUp(joint->GetTarget().y)); return 2; } void MouseJoint::setMaxForce(float force) { - joint->m_maxForce = force; + joint->SetMaxForce(force); } float MouseJoint::getMaxForce() const { - return joint->m_maxForce; + return joint->GetMaxForce(); } } // box2d diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index 7829a1ec9..f76d9c9d1 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -131,7 +131,7 @@ namespace box2d if(count < 3) return luaL_error(L, "Polygon degenerated to less than three points."); - b2vec2 vecs[] = new b2vec2[count]; + b2Vec2 vecs[] = new b2vec2[count]; for (int i = 0; i < count; i++) { vecs[i].Set(convex_hull[i].x, convex_hull[i].y); diff --git a/src/modules/physics/box2d/PrismaticJoint.cpp b/src/modules/physics/box2d/PrismaticJoint.cpp index b613cbe6f..f97eba491 100644 --- a/src/modules/physics/box2d/PrismaticJoint.cpp +++ b/src/modules/physics/box2d/PrismaticJoint.cpp @@ -72,12 +72,7 @@ namespace box2d { joint->SetMaxMotorForce(force); } - - float PrismaticJoint::getMaxMotorForce() const - { - return joint->m_maxMotorForce; - } - + void PrismaticJoint::setMotorSpeed(float speed) { joint->SetMotorSpeed(speed); diff --git a/src/modules/physics/box2d/PrismaticJoint.h b/src/modules/physics/box2d/PrismaticJoint.h index 01baf40e1..6a0fb3b1b 100644 --- a/src/modules/physics/box2d/PrismaticJoint.h +++ b/src/modules/physics/box2d/PrismaticJoint.h @@ -75,11 +75,6 @@ namespace box2d **/ void setMaxMotorForce(float force); - /** - * Get the current motor force, usually in N. - **/ - float getMaxMotorForce() const; - /** * Set the motor speed, usually in meters per second. **/ diff --git a/src/modules/physics/box2d/PulleyJoint.cpp b/src/modules/physics/box2d/PulleyJoint.cpp index e4193902b..d45c850da 100644 --- a/src/modules/physics/box2d/PulleyJoint.cpp +++ b/src/modules/physics/box2d/PulleyJoint.cpp @@ -48,49 +48,13 @@ namespace box2d int PulleyJoint::getGroundAnchors(lua_State * L) { - lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchor1().x)); - lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchor1().y)); - lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchor2().x)); - lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchor2().y)); + lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchorA().x)); + lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchorA().y)); + lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchorB().x)); + lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchorB().y)); return 4; } - void PulleyJoint::setMaxLengths(float maxlength1, float maxlength2) - { - // Apply Box2D's maximum lengths too. They know better. - - if (maxlength1 > 0) - { - joint->m_maxLength1 = b2Min(world->scaleDown(maxlength1), joint->m_constant - joint->m_ratio * b2_minPulleyLength); - } - - if (maxlength2 > 0) - { - joint->m_maxLength2 = b2Min(world->scaleDown(maxlength2), (joint->m_constant - b2_minPulleyLength) / joint->m_ratio); - } - } - - int PulleyJoint::getMaxLengths(lua_State * L) - { - lua_pushnumber(L, world->scaleUp(joint->m_maxLength1)); - lua_pushnumber(L, world->scaleUp(joint->m_maxLength2)); - return 2; - } - - void PulleyJoint::setConstant(float constant) - { - joint->m_constant = world->scaleDown(constant); - - // Update the max lengths, as does Box2D - joint->m_maxLength1 = joint->m_constant - joint->m_ratio * b2_minPulleyLength; - joint->m_maxLength2 = (joint->m_constant - b2_minPulleyLength) / joint->m_ratio; - } - - float PulleyJoint::getConstant() const - { - return world->scaleUp(joint->m_constant); - } - float PulleyJoint::getLength1() const { return world->scaleUp(joint->GetLength1()); @@ -101,14 +65,9 @@ namespace box2d return world->scaleUp(joint->GetLength2()); } - void PulleyJoint::setRatio(float ratio) - { - joint->m_ratio = ratio; - } - float PulleyJoint::getRatio() const { - return joint->m_ratio; + return joint->GetRatio(); } } // box2d diff --git a/src/modules/physics/box2d/PulleyJoint.h b/src/modules/physics/box2d/PulleyJoint.h index 8e5154c66..9e16e8c33 100644 --- a/src/modules/physics/box2d/PulleyJoint.h +++ b/src/modules/physics/box2d/PulleyJoint.h @@ -56,29 +56,6 @@ namespace box2d **/ int getGroundAnchors(lua_State * L); - /** - * Sets the max rope lengths (a value of zero keeps it - * unchanged). Beware that Box2D also imposes a maximum, - * so the smallest of these is actually set. - **/ - void setMaxLengths(float maxlength1, float maxlength2); - - /** - * Gets the max rope lengths. - **/ - int getMaxLengths(lua_State * L); - - /** - * Sets the total length of the rope and updates the - * MaxLengths values accordingly. - **/ - void setConstant(float length); - - /** - * Gets the total length of the rope. - **/ - float getConstant() const; - /** * Gets the current length of the segment attached to body1. **/ diff --git a/src/modules/physics/box2d/RevoluteJoint.cpp b/src/modules/physics/box2d/RevoluteJoint.cpp index a3003f900..01276ca80 100644 --- a/src/modules/physics/box2d/RevoluteJoint.cpp +++ b/src/modules/physics/box2d/RevoluteJoint.cpp @@ -70,12 +70,7 @@ namespace box2d { joint->SetMaxMotorTorque(torque); } - - float RevoluteJoint::getMaxMotorTorque() const - { - return joint->m_maxMotorTorque; - } - + void RevoluteJoint::setMotorSpeed(float speed) { joint->SetMotorSpeed(speed); diff --git a/src/modules/physics/box2d/RevoluteJoint.h b/src/modules/physics/box2d/RevoluteJoint.h index 1d9685299..359caa7fb 100644 --- a/src/modules/physics/box2d/RevoluteJoint.h +++ b/src/modules/physics/box2d/RevoluteJoint.h @@ -75,11 +75,6 @@ namespace box2d **/ void setMaxMotorTorque(float torque); - /** - * Gets the maximum motor torque, usually in N-m. - **/ - float getMaxMotorTorque() const; - /** * Sets the motor speed in degrees per second. **/ diff --git a/src/modules/physics/box2d/World.cpp b/src/modules/physics/box2d/World.cpp index 87594530f..bcb8582a7 100644 --- a/src/modules/physics/box2d/World.cpp +++ b/src/modules/physics/box2d/World.cpp @@ -100,6 +100,8 @@ namespace box2d { world = new b2World(scaleDown(aabb), b2Vec2(0,0), true); world->SetContactListener(this); + b2BodyDef def; + groundBody = world->CreateBody(def); } World::World(b2AABB aabb, b2Vec2 gravity, bool sleep, int meter) @@ -111,6 +113,7 @@ namespace box2d World::~World() { + world->DestroyBody(groundBody); delete world; } @@ -272,6 +275,11 @@ namespace box2d t.upperBound = scaleUp(aabb.upperBound); return t; } + + b2Body * World::getGroundBody() + { + return groundBody; + } } // box2d } // physics diff --git a/src/modules/physics/box2d/World.h b/src/modules/physics/box2d/World.h index 7cdfc8ba2..68ed12af1 100644 --- a/src/modules/physics/box2d/World.h +++ b/src/modules/physics/box2d/World.h @@ -77,6 +77,9 @@ namespace box2d // Pointer to the Box2D world. b2World * world; + + // Ground body + b2Body * groundBody; // Contact callbacks. ContactCallback add, persist, remove, result; @@ -241,6 +244,12 @@ namespace box2d * @return The scaled AABB. **/ b2AABB scaleUp(const b2AABB & aabb); + + /** + * Gets the ground body. + * @return The ground body. + **/ + b2Body * getGroundBody(); }; diff --git a/src/modules/physics/box2d/wrap_PrismaticJoint.cpp b/src/modules/physics/box2d/wrap_PrismaticJoint.cpp index 86b335508..80146caac 100644 --- a/src/modules/physics/box2d/wrap_PrismaticJoint.cpp +++ b/src/modules/physics/box2d/wrap_PrismaticJoint.cpp @@ -68,13 +68,6 @@ namespace box2d return 0; } - int w_PrismaticJoint_getMaxMotorForce(lua_State * L) - { - PrismaticJoint * t = luax_checkprismaticjoint(L, 1); - lua_pushnumber(L, t->getMaxMotorForce()); - return 1; - } - int w_PrismaticJoint_setMotorSpeed(lua_State * L) { PrismaticJoint * t = luax_checkprismaticjoint(L, 1); @@ -164,7 +157,6 @@ namespace box2d { "setMotorEnabled", w_PrismaticJoint_setMotorEnabled }, { "isMotorEnabled", w_PrismaticJoint_isMotorEnabled }, { "setMaxMotorForce", w_PrismaticJoint_setMaxMotorForce }, - { "getMaxMotorForce", w_PrismaticJoint_getMaxMotorForce }, { "setMotorSpeed", w_PrismaticJoint_setMotorSpeed }, { "getMotorSpeed", w_PrismaticJoint_getMotorSpeed }, { "getMotorForce", w_PrismaticJoint_getMotorForce }, diff --git a/src/modules/physics/box2d/wrap_PrismaticJoint.h b/src/modules/physics/box2d/wrap_PrismaticJoint.h index 1044d1aec..d420e26cb 100644 --- a/src/modules/physics/box2d/wrap_PrismaticJoint.h +++ b/src/modules/physics/box2d/wrap_PrismaticJoint.h @@ -38,7 +38,6 @@ namespace box2d int w_PrismaticJoint_setMotorEnabled(lua_State * L); int w_PrismaticJoint_isMotorEnabled(lua_State * L); int w_PrismaticJoint_setMaxMotorForce(lua_State * L); - int w_PrismaticJoint_getMaxMotorForce(lua_State * L); int w_PrismaticJoint_setMotorSpeed(lua_State * L); int w_PrismaticJoint_getMotorSpeed(lua_State * L); int w_PrismaticJoint_getMotorForce(lua_State * L); diff --git a/src/modules/physics/box2d/wrap_PulleyJoint.cpp b/src/modules/physics/box2d/wrap_PulleyJoint.cpp index ac45af23f..aa68acda3 100644 --- a/src/modules/physics/box2d/wrap_PulleyJoint.cpp +++ b/src/modules/physics/box2d/wrap_PulleyJoint.cpp @@ -37,37 +37,6 @@ namespace box2d lua_remove(L, 1); return t->getGroundAnchors(L); } - - int w_PulleyJoint_setMaxLengths(lua_State * L) - { - PulleyJoint * t = luax_checkpulleyjoint(L, 1); - float arg1 = (float)luaL_optnumber(L, 2, 0.0); - float arg2 = (float)luaL_optnumber(L, 3, 0.0); - t->setMaxLengths(arg1, arg2); - return 0; - } - - int w_PulleyJoint_getMaxLengths(lua_State * L) - { - PulleyJoint * t = luax_checkpulleyjoint(L, 1); - lua_remove(L, 1); - return t->getMaxLengths(L); - } - - int w_PulleyJoint_setConstant(lua_State * L) - { - PulleyJoint * t = luax_checkpulleyjoint(L, 1); - float arg1 = (float)luaL_checknumber(L, 2); - t->setConstant(arg1); - return 0; - } - - int w_PulleyJoint_getConstant(lua_State * L) - { - PulleyJoint * t = luax_checkpulleyjoint(L, 1); - lua_pushnumber(L, t->getConstant()); - return 1; - } int w_PulleyJoint_getLength1(lua_State * L) { @@ -83,14 +52,6 @@ namespace box2d return 1; } - int w_PulleyJoint_setRatio(lua_State * L) - { - PulleyJoint * t = luax_checkpulleyjoint(L, 1); - float arg1 = (float)luaL_checknumber(L, 2); - t->setRatio(arg1); - return 0; - } - int w_PulleyJoint_getRatio(lua_State * L) { PulleyJoint * t = luax_checkpulleyjoint(L, 1); @@ -100,13 +61,8 @@ namespace box2d static const luaL_Reg functions[] = { { "getGroundAnchors", w_PulleyJoint_getGroundAnchors }, - { "setMaxLengths", w_PulleyJoint_setMaxLengths }, - { "getMaxLengths", w_PulleyJoint_getMaxLengths }, - { "setConstant", w_PulleyJoint_setConstant }, - { "getConstant", w_PulleyJoint_getConstant }, { "getLength1", w_PulleyJoint_getLength1 }, { "getLength2", w_PulleyJoint_getLength2 }, - { "setRatio", w_PulleyJoint_setRatio }, { "getRatio", w_PulleyJoint_getRatio }, // From Joint. { "getType", w_Joint_getType }, diff --git a/src/modules/physics/box2d/wrap_PulleyJoint.h b/src/modules/physics/box2d/wrap_PulleyJoint.h index 3ccd33995..6001a5f85 100644 --- a/src/modules/physics/box2d/wrap_PulleyJoint.h +++ b/src/modules/physics/box2d/wrap_PulleyJoint.h @@ -34,13 +34,8 @@ namespace box2d { PulleyJoint * luax_checkpulleyjoint(lua_State * L, int idx); int w_PulleyJoint_getGroundAnchors(lua_State * L); - int w_PulleyJoint_setMaxLengths(lua_State * L); - int w_PulleyJoint_getMaxLengths(lua_State * L); - int w_PulleyJoint_setConstant(lua_State * L); - int w_PulleyJoint_getConstant(lua_State * L); int w_PulleyJoint_getLength1(lua_State * L); int w_PulleyJoint_getLength2(lua_State * L); - int w_PulleyJoint_setRatio(lua_State * L); int w_PulleyJoint_getRatio(lua_State * L); int luaopen_pulleyjoint(lua_State * L); diff --git a/src/modules/physics/box2d/wrap_RevoluteJoint.cpp b/src/modules/physics/box2d/wrap_RevoluteJoint.cpp index 58e2ab6ee..6545d125e 100644 --- a/src/modules/physics/box2d/wrap_RevoluteJoint.cpp +++ b/src/modules/physics/box2d/wrap_RevoluteJoint.cpp @@ -68,13 +68,6 @@ namespace box2d return 0; } - int w_RevoluteJoint_getMaxMotorTorque(lua_State * L) - { - RevoluteJoint * t = luax_checkrevolutejoint(L, 1); - lua_pushnumber(L, t->getMaxMotorTorque()); - return 1; - } - int w_RevoluteJoint_setMotorSpeed(lua_State * L) { RevoluteJoint * t = luax_checkrevolutejoint(L, 1); @@ -164,7 +157,6 @@ namespace box2d { "setMotorEnabled", w_RevoluteJoint_setMotorEnabled }, { "isMotorEnabled", w_RevoluteJoint_isMotorEnabled }, { "setMaxMotorTorque", w_RevoluteJoint_setMaxMotorTorque }, - { "getMaxMotorTorque", w_RevoluteJoint_getMaxMotorTorque }, { "setMotorSpeed", w_RevoluteJoint_setMotorSpeed }, { "getMotorSpeed", w_RevoluteJoint_getMotorSpeed }, { "getMotorTorque", w_RevoluteJoint_getMotorTorque }, diff --git a/src/modules/physics/box2d/wrap_RevoluteJoint.h b/src/modules/physics/box2d/wrap_RevoluteJoint.h index 89c856b62..ad03ce4e1 100644 --- a/src/modules/physics/box2d/wrap_RevoluteJoint.h +++ b/src/modules/physics/box2d/wrap_RevoluteJoint.h @@ -38,7 +38,6 @@ namespace box2d int w_RevoluteJoint_setMotorEnabled(lua_State * L); int w_RevoluteJoint_isMotorEnabled(lua_State * L); int w_RevoluteJoint_setMaxMotorTorque(lua_State * L); - int w_RevoluteJoint_getMaxMotorTorque(lua_State * L); int w_RevoluteJoint_setMotorSpeed(lua_State * L); int w_RevoluteJoint_getMotorSpeed(lua_State * L); int w_RevoluteJoint_getMotorTorque(lua_State * L);