From e69cafe86889a57e270937d895c0d9437b4762e8 Mon Sep 17 00:00:00 2001 From: Bill Meltsner Date: Thu, 15 Sep 2011 16:13:54 -0400 Subject: [PATCH] Switched get/setMeter from being a World method to a love.physics function --HG-- branch : box2d-update --- src/modules/physics/box2d/Body.cpp | 41 +++++------ src/modules/physics/box2d/Contact.cpp | 15 ++-- src/modules/physics/box2d/DistanceJoint.cpp | 7 +- src/modules/physics/box2d/FrictionJoint.cpp | 3 +- src/modules/physics/box2d/Joint.cpp | 9 +-- src/modules/physics/box2d/MouseJoint.cpp | 9 +-- src/modules/physics/box2d/Physics.cpp | 64 +++++++++++++++++ src/modules/physics/box2d/Physics.h | 73 ++++++++++++++++++++ src/modules/physics/box2d/PrismaticJoint.cpp | 29 ++++---- src/modules/physics/box2d/PulleyJoint.cpp | 17 ++--- src/modules/physics/box2d/RevoluteJoint.cpp | 3 +- src/modules/physics/box2d/RopeJoint.cpp | 9 +-- src/modules/physics/box2d/Shape.cpp | 37 +++++----- src/modules/physics/box2d/Shape.h | 4 +- src/modules/physics/box2d/WeldJoint.cpp | 3 +- src/modules/physics/box2d/WheelJoint.cpp | 25 +++---- src/modules/physics/box2d/World.cpp | 71 ++----------------- src/modules/physics/box2d/World.h | 71 ------------------- src/modules/physics/box2d/wrap_Physics.cpp | 15 ++++ src/modules/physics/box2d/wrap_Physics.h | 2 + src/modules/physics/box2d/wrap_World.cpp | 17 ----- src/modules/physics/box2d/wrap_World.h | 2 - 22 files changed, 270 insertions(+), 256 deletions(-) diff --git a/src/modules/physics/box2d/Body.cpp b/src/modules/physics/box2d/Body.cpp index 5487414c8..4d51f12c3 100644 --- a/src/modules/physics/box2d/Body.cpp +++ b/src/modules/physics/box2d/Body.cpp @@ -23,6 +23,7 @@ #include #include "World.h" +#include "Physics.h" namespace love { @@ -35,7 +36,7 @@ namespace box2d { world->retain(); b2BodyDef def; - def.position = world->scaleDown(p); + def.position = Physics::scaleDown(p); def.massData.mass = m; def.massData.I = i; body = world->world->CreateBody(&def); @@ -50,24 +51,24 @@ namespace box2d float Body::getX() { - return world->scaleUp(body->GetPosition().x); + return Physics::scaleUp(body->GetPosition().x); } float Body::getY() { - return world->scaleUp(body->GetPosition().y); + return Physics::scaleUp(body->GetPosition().y); } void Body::getPosition(float & x_o, float & y_o) { - b2Vec2 v = world->scaleUp(body->GetPosition()); + b2Vec2 v = Physics::scaleUp(body->GetPosition()); x_o = v.x; y_o = v.y; } void Body::getLinearVelocity(float & x_o, float & y_o) { - b2Vec2 v = world->scaleUp(body->GetLinearVelocity()); + b2Vec2 v = Physics::scaleUp(body->GetLinearVelocity()); x_o = v.x; y_o = v.y; } @@ -79,14 +80,14 @@ namespace box2d void Body::getWorldCenter(float & x_o, float & y_o) { - b2Vec2 v = world->scaleUp(body->GetWorldCenter()); + b2Vec2 v = Physics::scaleUp(body->GetWorldCenter()); x_o = v.x; y_o = v.y; } void Body::getLocalCenter(float & x_o, float & y_o) { - b2Vec2 v = world->scaleUp(body->GetLocalCenter()); + b2Vec2 v = Physics::scaleUp(body->GetLocalCenter()); x_o = v.x; y_o = v.y; } @@ -123,7 +124,7 @@ namespace box2d void Body::applyImpulse(float jx, float jy, float rx, float ry) { - body->ApplyImpulse(b2Vec2(jx, jy), world->scaleDown(b2Vec2(rx, ry))); + body->ApplyImpulse(b2Vec2(jx, jy), Physics::scaleDown(b2Vec2(rx, ry))); } void Body::applyTorque(float t) @@ -133,7 +134,7 @@ namespace box2d void Body::applyForce(float fx, float fy, float rx, float ry) { - body->ApplyForce(b2Vec2(fx, fy), world->scaleDown(b2Vec2(rx, ry))); + body->ApplyForce(b2Vec2(fx, fy), Physics::scaleDown(b2Vec2(rx, ry))); } void Body::applyForce(float fx, float fy) @@ -143,17 +144,17 @@ namespace box2d void Body::setX(float x) { - body->SetXForm(world->scaleDown(b2Vec2(x, getY())), getAngle()); + body->SetXForm(Physics::scaleDown(b2Vec2(x, getY())), getAngle()); } void Body::setY(float y) { - body->SetXForm(world->scaleDown(b2Vec2(getX(), y)), getAngle()); + body->SetXForm(Physics::scaleDown(b2Vec2(getX(), y)), getAngle()); } void Body::setLinearVelocity(float x, float y) { - body->SetLinearVelocity(world->scaleDown(b2Vec2(x, y))); + body->SetLinearVelocity(Physics::scaleDown(b2Vec2(x, y))); } void Body::setAngle(float d) @@ -168,7 +169,7 @@ namespace box2d void Body::setPosition(float x, float y) { - body->SetXForm(world->scaleDown(b2Vec2(x, y)), body->GetAngle()); + body->SetXForm(Physics::scaleDown(b2Vec2(x, y)), body->GetAngle()); } void Body::setAngularDamping(float d) @@ -189,7 +190,7 @@ namespace box2d void Body::setMass(float x, float y, float m, float i) { b2MassData massData; - massData.center = world->scaleDown(b2Vec2(x, y)); + massData.center = Physics::scaleDown(b2Vec2(x, y)); massData.mass = m; massData.I = i; body->SetMass(&massData); @@ -206,42 +207,42 @@ namespace box2d void Body::getWorldPoint(float x, float y, float & x_o, float & y_o) { - b2Vec2 v = world->scaleUp(body->GetWorldPoint(world->scaleDown(b2Vec2(x, y)))); + b2Vec2 v = Physics::scaleUp(body->GetWorldPoint(Physics::scaleDown(b2Vec2(x, y)))); x_o = v.x; y_o = v.y; } void Body::getWorldVector(float x, float y, float & x_o, float & y_o) { - b2Vec2 v = world->scaleUp(body->GetWorldVector(world->scaleDown(b2Vec2(x, y)))); + b2Vec2 v = Physics::scaleUp(body->GetWorldVector(Physics::scaleDown(b2Vec2(x, y)))); x_o = v.x; y_o = v.y; } void Body::getLocalPoint(float x, float y, float & x_o, float & y_o) { - b2Vec2 v = world->scaleUp(body->GetLocalPoint(world->scaleDown(b2Vec2(x, y)))); + b2Vec2 v = Physics::scaleUp(body->GetLocalPoint(Physics::scaleDown(b2Vec2(x, y)))); x_o = v.x; y_o = v.y; } void Body::getLocalVector(float x, float y, float & x_o, float & y_o) { - b2Vec2 v = world->scaleUp(body->GetLocalVector(world->scaleDown(b2Vec2(x, y)))); + b2Vec2 v = Physics::scaleUp(body->GetLocalVector(Physics::scaleDown(b2Vec2(x, y)))); x_o = v.x; y_o = v.y; } void Body::getLinearVelocityFromWorldPoint(float x, float y, float & x_o, float & y_o) { - b2Vec2 v = world->scaleUp(body->GetLinearVelocityFromWorldPoint(world->scaleDown(b2Vec2(x, y)))); + b2Vec2 v = Physics::scaleUp(body->GetLinearVelocityFromWorldPoint(Physics::scaleDown(b2Vec2(x, y)))); x_o = v.x; y_o = v.y; } void Body::getLinearVelocityFromLocalPoint(float x, float y, float & x_o, float & y_o) { - b2Vec2 v = world->scaleUp(body->GetLinearVelocityFromLocalPoint(world->scaleDown(b2Vec2(x, y)))); + b2Vec2 v = Physics::scaleUp(body->GetLinearVelocityFromLocalPoint(Physics::scaleDown(b2Vec2(x, y)))); x_o = v.x; y_o = v.y; } diff --git a/src/modules/physics/box2d/Contact.cpp b/src/modules/physics/box2d/Contact.cpp index 8495920be..f048e8c62 100644 --- a/src/modules/physics/box2d/Contact.cpp +++ b/src/modules/physics/box2d/Contact.cpp @@ -20,6 +20,7 @@ #include "Contact.h" #include "World.h" +#include "Physics.h" namespace love { @@ -41,30 +42,30 @@ namespace box2d int Contact::getPosition(lua_State * L) { love::luax_assert_argc(L, 1, 1); - lua_pushnumber(L, world->scaleUp(point.position.x)); - lua_pushnumber(L, world->scaleUp(point.position.y)); + lua_pushnumber(L, Physics::scaleUp(point.position.x)); + lua_pushnumber(L, Physics::scaleUp(point.position.y)); return 2; } int Contact::getVelocity(lua_State * L) { love::luax_assert_argc(L, 1, 1); - lua_pushnumber(L, world->scaleUp(point.velocity.x)); - lua_pushnumber(L, world->scaleUp(point.velocity.y)); + lua_pushnumber(L, Physics::scaleUp(point.velocity.x)); + lua_pushnumber(L, Physics::scaleUp(point.velocity.y)); return 2; } int Contact::getNormal(lua_State * L) { love::luax_assert_argc(L, 1, 1); - lua_pushnumber(L, world->scaleUp(point.normal.x)); - lua_pushnumber(L, world->scaleUp(point.normal.y)); + lua_pushnumber(L, Physics::scaleUp(point.normal.x)); + lua_pushnumber(L, Physics::scaleUp(point.normal.y)); return 2; } float Contact::getSeparation() const { - return world->scaleUp(point.separation); + return Physics::scaleUp(point.separation); } float Contact::getFriction() const diff --git a/src/modules/physics/box2d/DistanceJoint.cpp b/src/modules/physics/box2d/DistanceJoint.cpp index 8ffa8ca9c..3ecd450d8 100644 --- a/src/modules/physics/box2d/DistanceJoint.cpp +++ b/src/modules/physics/box2d/DistanceJoint.cpp @@ -23,6 +23,7 @@ // Module #include "Body.h" #include "World.h" +#include "Physics.h" namespace love { @@ -34,7 +35,7 @@ namespace box2d : Joint(body1, body2), joint(NULL) { b2DistanceJointDef def; - def.Initialize(body1->body, body2->body, world->scaleDown(b2Vec2(x1,y1)), world->scaleDown(b2Vec2(x2,y2))); + def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(x1,y1)), Physics::scaleDown(b2Vec2(x2,y2))); def.collideConnected = collideConnected; joint = (b2DistanceJoint*)createJoint(&def); } @@ -47,12 +48,12 @@ namespace box2d void DistanceJoint::setLength(float length) { - joint->SetLength(world->scaleDown(length)); + joint->SetLength(Physics::scaleDown(length)); } float DistanceJoint::getLength() const { - return world->scaleUp(joint->GetLength()); + return Physics::scaleUp(joint->GetLength()); } void DistanceJoint::setFrequency(float hz) diff --git a/src/modules/physics/box2d/FrictionJoint.cpp b/src/modules/physics/box2d/FrictionJoint.cpp index f606199dd..699bd478a 100644 --- a/src/modules/physics/box2d/FrictionJoint.cpp +++ b/src/modules/physics/box2d/FrictionJoint.cpp @@ -25,6 +25,7 @@ // Module #include "Body.h" #include "World.h" +#include "Physics.h" namespace love { @@ -36,7 +37,7 @@ namespace box2d : Joint(body1, body2), joint(NULL) { b2FrictionJointDef def; - def.Initialize(body1->body, body2->body, world->scaleDown(b2Vec2(x,y))); + def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(x,y))); def.collideConnected = collideConnected; joint = (b2FrictionJoint*)createJoint(&def); } diff --git a/src/modules/physics/box2d/Joint.cpp b/src/modules/physics/box2d/Joint.cpp index 79cf039f0..1aa48f00d 100644 --- a/src/modules/physics/box2d/Joint.cpp +++ b/src/modules/physics/box2d/Joint.cpp @@ -26,6 +26,7 @@ // Module #include "Body.h" #include "World.h" +#include "Physics.h" namespace love { @@ -87,10 +88,10 @@ namespace box2d int Joint::getAnchors(lua_State * L) { - 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)); + lua_pushnumber(L, Physics::scaleUp(joint->GetAnchorA().x)); + lua_pushnumber(L, Physics::scaleUp(joint->GetAnchorA().y)); + lua_pushnumber(L, Physics::scaleUp(joint->GetAnchorB().x)); + lua_pushnumber(L, Physics::scaleUp(joint->GetAnchorB().y)); return 4; } diff --git a/src/modules/physics/box2d/MouseJoint.cpp b/src/modules/physics/box2d/MouseJoint.cpp index 5fee8b23f..9ea217b6f 100644 --- a/src/modules/physics/box2d/MouseJoint.cpp +++ b/src/modules/physics/box2d/MouseJoint.cpp @@ -23,6 +23,7 @@ // Module #include "Body.h" #include "World.h" +#include "Physics.h" namespace love { @@ -38,7 +39,7 @@ namespace box2d def.bodyA = body1->world->getGroundBody(); def.bodyB = body1->body; def.maxForce = 1000.0f * body1->body->GetMass(); - def.target = body1->world->scaleDown(b2Vec2(x,y)); + def.target = Physics::scaleDown(b2Vec2(x,y)); joint = (b2MouseJoint*)createJoint(&def); } @@ -50,13 +51,13 @@ namespace box2d void MouseJoint::setTarget(float x, float y) { - joint->SetTarget(world->scaleDown(b2Vec2(x, y))); + joint->SetTarget(Physics::scaleDown(b2Vec2(x, y))); } int MouseJoint::getTarget(lua_State * L) { - lua_pushnumber(L, world->scaleUp(joint->GetTarget().x)); - lua_pushnumber(L, world->scaleUp(joint->GetTarget().y)); + lua_pushnumber(L, Physics::scaleUp(joint->GetTarget().x)); + lua_pushnumber(L, Physics::scaleUp(joint->GetTarget().y)); return 2; } diff --git a/src/modules/physics/box2d/Physics.cpp b/src/modules/physics/box2d/Physics.cpp index b8d781eed..6a270036e 100644 --- a/src/modules/physics/box2d/Physics.cpp +++ b/src/modules/physics/box2d/Physics.cpp @@ -193,6 +193,70 @@ namespace box2d { return new RopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected); } + + + + void Physics::setMeter(int meter) + { + Physics::meter = meter; + } + + int Physics::getMeter() + { + return meter; + } + + void Physics::scaleDown(float & x, float & y) + { + x /= (float)meter; + y /= (float)meter; + } + + void Physics::scaleUp(float & x, float & y) + { + x *= (float)meter; + y *= (float)meter; + } + + float Physics::scaleDown(float f) + { + return f/(float)meter; + } + + float Physics::scaleUp(float f) + { + return f*(float)meter; + } + + b2Vec2 Physics::scaleDown(const b2Vec2 & v) + { + b2Vec2 t = v; + scaleDown(t.x, t.y); + return t; + } + + b2Vec2 Physics::scaleUp(const b2Vec2 & v) + { + b2Vec2 t = v; + scaleUp(t.x, t.y); + return t; + } + + b2AABB Physics::scaleDown(const b2AABB & aabb) + { + b2AABB t; + t.lowerBound = scaleDown(aabb.lowerBound); + t.upperBound = scaleDown(aabb.upperBound); + return t; + } + + b2AABB Physics::scaleUp(const b2AABB & aabb) + { + b2AABB t; + t.lowerBound = scaleUp(aabb.lowerBound); + t.upperBound = scaleUp(aabb.upperBound); + return t; + } } // box2d } // physics diff --git a/src/modules/physics/box2d/Physics.h b/src/modules/physics/box2d/Physics.h index ed2ca3d54..7dd2c6322 100644 --- a/src/modules/physics/box2d/Physics.h +++ b/src/modules/physics/box2d/Physics.h @@ -50,6 +50,11 @@ namespace box2d { class Physics : public Module { + private: + + // The length of one meter in pixels. + static int meter; + public: // Implements Module. @@ -242,6 +247,74 @@ namespace box2d * @param collideConnected Whether the connected bodies should collide with each other. Defaults to false. **/ RopeJoint * newRopeJoint(Body * body1, Body * body2, float x1, float y1, float x2, float y2, float maxLength, bool collideConnected); + + /** + * Sets the number of pixels in one meter. + * @param pixels The number of pixels in one meter. (1m ~= 3.3ft). + **/ + static void setMeter(int meter); + + /** + * Gets the number of pixels in one meter. + * @param pixels The number of pixels in one meter. (1m ~= 3.3ft). + **/ + static int getMeter(); + + /** + * Scales a value down according to the current meter in pixels. + * @param f The unscaled input value. + **/ + static float scaleDown(float f); + + /** + * Scales a value up according to the current meter in pixels. + * @param f The unscaled input value. + **/ + static float scaleUp(float f); + + /** + * Scales a point down according to the current meter + * in pixels, for instance x = x0/meter, y = x0/meter. + * @param x The x-coordinate of the point to scale. + * @param y The y-coordinate of the point to scale. + **/ + static void scaleDown(float & x, float & y); + + /** + * Scales a point up according to the current meter + * in pixels, for instance x = x0/meter, y = x0/meter. + * @param x The x-coordinate of the point to scale. + * @param y The y-coordinate of the point to scale. + **/ + static void scaleUp(float & x, float & y); + + /** + * Scales a b2Vec2 down according to the current meter in pixels. + * @param v The unscaled input vector. + * @return The scaled vector. + **/ + static b2Vec2 scaleDown(const b2Vec2 & v); + + /** + * Scales a b2Vec up according to the current meter in pixels. + * @param v The unscaled input vector. + * @return The scaled vector. + **/ + static b2Vec2 scaleUp(const b2Vec2 & v); + + /** + * Scales a b2AABB down according to the current meter in pixels. + * @param v The unscaled input AABB. + * @return The scaled AABB. + **/ + static b2AABB scaleDown(const b2AABB & aabb); + + /** + * Scales a b2AABB up according to the current meter in pixels. + * @param v The unscaled input AABB. + * @return The scaled AABB. + **/ + static b2AABB scaleUp(const b2AABB & aabb); }; // Physics diff --git a/src/modules/physics/box2d/PrismaticJoint.cpp b/src/modules/physics/box2d/PrismaticJoint.cpp index 57ae56b4b..17b6b1c7e 100644 --- a/src/modules/physics/box2d/PrismaticJoint.cpp +++ b/src/modules/physics/box2d/PrismaticJoint.cpp @@ -23,6 +23,7 @@ // Module #include "Body.h" #include "World.h" +#include "Physics.h" namespace love { @@ -35,7 +36,7 @@ namespace box2d { b2PrismaticJointDef def; - def.Initialize(body1->body, body2->body, world->scaleDown(b2Vec2(x,y)), b2Vec2(ax,ay)); + def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(x,y)), b2Vec2(ax,ay)); def.lowerTranslation = 0.0f; def.upperTranslation = 100.0f; def.enableLimit = true; @@ -51,12 +52,12 @@ namespace box2d float PrismaticJoint::getJointTranslation() const { - return world->scaleDown(joint->GetJointTranslation()); + return Physics::scaleDown(joint->GetJointTranslation()); } float PrismaticJoint::getJointSpeed() const { - return world->scaleDown(joint->GetJointSpeed()); + return Physics::scaleDown(joint->GetJointSpeed()); } void PrismaticJoint::enableMotor(bool motor) @@ -71,22 +72,22 @@ namespace box2d void PrismaticJoint::setMaxMotorForce(float force) { - joint->SetMaxMotorForce(world->scaleDown(force)); + joint->SetMaxMotorForce(Physics::scaleDown(force)); } void PrismaticJoint::setMotorSpeed(float speed) { - joint->SetMotorSpeed(world->scaleDown(speed)); + joint->SetMotorSpeed(Physics::scaleDown(speed)); } float PrismaticJoint::getMotorSpeed() const { - return world->scaleUp(joint->GetMotorSpeed()); + return Physics::scaleUp(joint->GetMotorSpeed()); } float PrismaticJoint::getMotorForce(float inv_dt) const { - return world->scaleUp(joint->GetMotorForce(inv_dt)); + return Physics::scaleUp(joint->GetMotorForce(inv_dt)); } void PrismaticJoint::enableLimit(bool limit) @@ -101,33 +102,33 @@ namespace box2d void PrismaticJoint::setUpperLimit(float limit) { - joint->SetLimits(joint->GetLowerLimit(), world->scaleDown(limit)); + joint->SetLimits(joint->GetLowerLimit(), Physics::scaleDown(limit)); } void PrismaticJoint::setLowerLimit(float limit) { - joint->SetLimits(world->scaleDown(limit), joint->GetUpperLimit()); + joint->SetLimits(Physics::scaleDown(limit), joint->GetUpperLimit()); } void PrismaticJoint::setLimits(float lower, float upper) { - joint->SetLimits(world->scaleDown(lower), world->scaleDown(upper)); + joint->SetLimits(Physics::scaleDown(lower), Physics::scaleDown(upper)); } float PrismaticJoint::getLowerLimit() const { - return world->scaleUp(joint->GetLowerLimit()); + return Physics::scaleUp(joint->GetLowerLimit()); } float PrismaticJoint::getUpperLimit() const { - return world->scaleUp(joint->GetUpperLimit()); + return Physics::scaleUp(joint->GetUpperLimit()); } int PrismaticJoint::getLimits(lua_State * L) { - lua_pushnumber(L, world->scaleUp(joint->GetLowerLimit())); - lua_pushnumber(L, world->scaleUp(joint->GetUpperLimit())); + lua_pushnumber(L, Physics::scaleUp(joint->GetLowerLimit())); + lua_pushnumber(L, Physics::scaleUp(joint->GetUpperLimit())); return 2; } diff --git a/src/modules/physics/box2d/PulleyJoint.cpp b/src/modules/physics/box2d/PulleyJoint.cpp index 387a75d41..84e449eaf 100644 --- a/src/modules/physics/box2d/PulleyJoint.cpp +++ b/src/modules/physics/box2d/PulleyJoint.cpp @@ -23,6 +23,7 @@ // Module #include "Body.h" #include "World.h" +#include "Physics.h" namespace love { @@ -34,8 +35,8 @@ namespace box2d : Joint(body1, body2), joint(NULL) { b2PulleyJointDef def; - def.Initialize(body1->body, body2->body, world->scaleDown(groundAnchor1), world->scaleDown(groundAnchor2), \ - world->scaleDown(anchor1), world->scaleDown(anchor2), ratio); + def.Initialize(body1->body, body2->body, Physics::scaleDown(groundAnchor1), Physics::scaleDown(groundAnchor2), \ + Physics::scaleDown(anchor1), Physics::scaleDown(anchor2), ratio); def.collideConnected = collideConnected; joint = (b2PulleyJoint*)createJoint(&def); @@ -49,21 +50,21 @@ namespace box2d int PulleyJoint::getGroundAnchors(lua_State * L) { - 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)); + lua_pushnumber(L, Physics::scaleUp(joint->GetGroundAnchorA().x)); + lua_pushnumber(L, Physics::scaleUp(joint->GetGroundAnchorA().y)); + lua_pushnumber(L, Physics::scaleUp(joint->GetGroundAnchorB().x)); + lua_pushnumber(L, Physics::scaleUp(joint->GetGroundAnchorB().y)); return 4; } float PulleyJoint::getLength1() const { - return world->scaleUp(joint->GetLength1()); + return Physics::scaleUp(joint->GetLength1()); } float PulleyJoint::getLength2() const { - return world->scaleUp(joint->GetLength2()); + return Physics::scaleUp(joint->GetLength2()); } float PulleyJoint::getRatio() const diff --git a/src/modules/physics/box2d/RevoluteJoint.cpp b/src/modules/physics/box2d/RevoluteJoint.cpp index cf22a195a..1a6251c8d 100644 --- a/src/modules/physics/box2d/RevoluteJoint.cpp +++ b/src/modules/physics/box2d/RevoluteJoint.cpp @@ -25,6 +25,7 @@ // Module #include "Body.h" #include "World.h" +#include "Physics.h" namespace love { @@ -36,7 +37,7 @@ namespace box2d : Joint(body1, body2), joint(NULL) { b2RevoluteJointDef def; - def.Initialize(body1->body, body2->body, world->scaleDown(b2Vec2(x,y))); + def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(x,y))); def.collideConnected = collideConnected; joint = (b2RevoluteJoint*)createJoint(&def); } diff --git a/src/modules/physics/box2d/RopeJoint.cpp b/src/modules/physics/box2d/RopeJoint.cpp index 6becef476..7516d7311 100644 --- a/src/modules/physics/box2d/RopeJoint.cpp +++ b/src/modules/physics/box2d/RopeJoint.cpp @@ -23,6 +23,7 @@ // Module #include "Body.h" #include "World.h" +#include "Physics.h" namespace love { @@ -36,9 +37,9 @@ namespace box2d 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); + def.localAnchorA = Physics::scaleDown(b2Vec2(x1,y1)); + def.localAnchorB = Physics::scaleDown(b2Vec2(x2,y2)); + def.maxLength = Physics::scaleDown(maxLength); def.collideConnected = collideConnected; joint = (b2RopeJoint*)createJoint(&def); } @@ -51,7 +52,7 @@ namespace box2d float RopeJoint::getMaxLength() const { - return world->scaleUp(joint->GetMaxLength()); + return Physics::scaleUp(joint->GetMaxLength()); } diff --git a/src/modules/physics/box2d/Shape.cpp b/src/modules/physics/box2d/Shape.cpp index 177ebfad7..6b6b99e4e 100644 --- a/src/modules/physics/box2d/Shape.cpp +++ b/src/modules/physics/box2d/Shape.cpp @@ -23,6 +23,7 @@ // Module #include "Body.h" #include "World.h" +#include "Physics.h" // STD #include @@ -33,8 +34,8 @@ namespace physics { namespace box2d { - Shape::Shape(World * world) - : world(world), shape(NULL) + Shape::Shape() + : shape(NULL) { } @@ -62,7 +63,7 @@ namespace box2d float Shape::getRadius() const { - return world->scaleUp(shape->m_radius); + return Physics::scaleUp(shape->m_radius); } int Shape::getChildCount() const @@ -73,19 +74,19 @@ namespace box2d bool Shape::testPoint(float x, float y, float r, float px, float py) const { b2Vec2 point(px, py); - b2Transform transform(world->scaleDown(b2Vec2(x, y)), b2Rot(r)); - return shape->TestPoint(transform, world->scaleDown(point)); + b2Transform transform(Physics::scaleDown(b2Vec2(x, y)), b2Rot(r)); + return shape->TestPoint(transform, Physics::scaleDown(point)); } int Shape::rayCast(lua_State * L) const { - float p1x = world->scaleDown((float)luaL_checknumber(L, 1)); - float p1y = world->scaleDown((float)luaL_checknumber(L, 2)); - float p2x = world->scaleDown((float)luaL_checknumber(L, 3)); - float p2y = world->scaleDown((float)luaL_checknumber(L, 4)); + float p1x = Physics::scaleDown((float)luaL_checknumber(L, 1)); + float p1y = Physics::scaleDown((float)luaL_checknumber(L, 2)); + float p2x = Physics::scaleDown((float)luaL_checknumber(L, 3)); + float p2y = Physics::scaleDown((float)luaL_checknumber(L, 4)); float maxFraction = (float)luaL_checknumber(L, 5); - float x = world->scaleDown((float)luaL_checknumber(L, 6)); - float y = world->scaleDown((float)luaL_checknumber(L, 7)); + float x = Physics::scaleDown((float)luaL_checknumber(L, 6)); + float y = Physics::scaleDown((float)luaL_checknumber(L, 7)); float r = (float)luaL_checknumber(L, 8); int childIndex = (int)luaL_optint(L, 9, 0); b2RayCastInput input; @@ -95,22 +96,22 @@ namespace box2d b2Transform transform(b2Vec2(x, y), b2Rot(r)); b2RayCastOutput output; shape->RayCast(&output, input, transform, childIndex); - lua_pushnumber(L, world->scaleUp(output.normal.x)); - lua_pushnumber(L, world->scaleUp(output.normal.y)); + lua_pushnumber(L, Physics::scaleUp(output.normal.x)); + lua_pushnumber(L, Physics::scaleUp(output.normal.y)); lua_pushnumber(L, output.fraction); return 3; } int Shape::computeAABB(lua_State * L) const { - float x = world->scaleDown((float)luaL_checknumber(L, 1)); - float y = world->scaleDown((float)luaL_checknumber(L, 2)); + float x = Physics::scaleDown((float)luaL_checknumber(L, 1)); + float y = Physics::scaleDown((float)luaL_checknumber(L, 2)); float r = (float)luaL_checknumber(L, 3); int childIndex = (int)luaL_optint(L, 4, 0); b2Transform transform(b2Vec2(x, y), b2Rot(r)); b2AABB box; shape->ComputeAABB(&box, transform, childIndex); - box = world->scaleUp(box); + box = Physics::scaleUp(box); lua_pushnumber(L, box.lowerBound.x); lua_pushnumber(L, box.lowerBound.y); lua_pushnumber(L, box.upperBound.x); @@ -120,10 +121,10 @@ namespace box2d int Shape::computeMass(lua_State * L) const { - float density = world->scaleDown((float)luaL_checknumber(L, 1)); + float density = Physics::scaleDown((float)luaL_checknumber(L, 1)); b2MassData data; shape->ComputeMass(&data, density); - b2Vec2 center = world->scaleUp(data.center); + b2Vec2 center = Physics::scaleUp(data.center); lua_pushnumber(L, center.x); lua_pushnumber(L, center.y); lua_pushnumber(L, data.mass); diff --git a/src/modules/physics/box2d/Shape.h b/src/modules/physics/box2d/Shape.h index e89e10d2e..d388088c3 100644 --- a/src/modules/physics/box2d/Shape.h +++ b/src/modules/physics/box2d/Shape.h @@ -44,8 +44,6 @@ namespace box2d **/ class Shape : public love::physics::Shape { - private: - World * world; // We need a reference for scaling up/down protected: @@ -57,7 +55,7 @@ namespace box2d /** * Creates a Shape. **/ - Shape(World * world); + Shape(); virtual ~Shape(); diff --git a/src/modules/physics/box2d/WeldJoint.cpp b/src/modules/physics/box2d/WeldJoint.cpp index 1b07e04b9..1d1f39494 100644 --- a/src/modules/physics/box2d/WeldJoint.cpp +++ b/src/modules/physics/box2d/WeldJoint.cpp @@ -25,6 +25,7 @@ // Module #include "Body.h" #include "World.h" +#include "Physics.h" namespace love { @@ -36,7 +37,7 @@ namespace box2d : Joint(body1, body2), joint(NULL) { b2WeldJointDef def; - def.Initialize(body1->body, body2->body, world->scaleDown(b2Vec2(x,y))); + def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(x,y))); def.collideConnected = collideConnected; joint = (b2WeldJoint*)createJoint(&def); } diff --git a/src/modules/physics/box2d/WheelJoint.cpp b/src/modules/physics/box2d/WheelJoint.cpp index 5ccd7099a..93e59a910 100644 --- a/src/modules/physics/box2d/WheelJoint.cpp +++ b/src/modules/physics/box2d/WheelJoint.cpp @@ -23,6 +23,7 @@ // Module #include "Body.h" #include "World.h" +#include "Physics.h" namespace love { @@ -35,7 +36,7 @@ namespace box2d { b2WheelJointDef def; - def.Initialize(body1->body, body2->body, world->scaleDown(b2Vec2(x,y)), b2Vec2(ax,ay)); + def.Initialize(body1->body, body2->body, Physics::scaleDown(b2Vec2(x,y)), b2Vec2(ax,ay)); def.collideConnected = collideConnected; joint = (b2WheelJoint*)createJoint(&def); } @@ -48,12 +49,12 @@ namespace box2d float WheelJoint::getJointTranslation() const { - return world->scaleDown(joint->GetJointTranslation()); + return Physics::scaleDown(joint->GetJointTranslation()); } float WheelJoint::getJointSpeed() const { - return world->scaleDown(joint->GetJointSpeed()); + return Physics::scaleDown(joint->GetJointSpeed()); } void WheelJoint::enableMotor(bool motor) @@ -68,47 +69,47 @@ namespace box2d void WheelJoint::setMotorSpeed(float speed) { - joint->SetMotorSpeed(world->scaleDown(speed)); + joint->SetMotorSpeed(Physics::scaleDown(speed)); } float WheelJoint::getMotorSpeed() const { - return world->scaleUp(joint->GetMotorSpeed()); + return Physics::scaleUp(joint->GetMotorSpeed()); } void WheelJoint::setMaxMotorTorque(float torque) { - joint->SetMaxMotorTorque(world->scaleDown(torque)); + joint->SetMaxMotorTorque(Physics::scaleDown(torque)); } float WheelJoint::getMaxMotorTorque() const { - return world->scaleUp(joint->GetMaxMotorTorque()); + return Physics::scaleUp(joint->GetMaxMotorTorque()); } float WheelJoint::getMotorTorque(float inv_dt) const { - return world->scaleUp(joint->getMotorTorque(inv_dt)); + return Physics::scaleUp(joint->GetMotorTorque(inv_dt)); } void WheelJoint::setSpringFrequencyHz(float hz) { - joint->SetSpringFrequencyHz(world->scaleDown(hz)); + joint->SetSpringFrequencyHz(Physics::scaleDown(hz)); } float WheelJoint::getSpringFrequencyHz() const { - return world->scaleUp(joint->GetSpringFrequencyHz()); + return Physics::scaleUp(joint->GetSpringFrequencyHz()); } void WheelJoint::setSpringDampingRatio(float ratio) { - joint->SetSpringDampingRatio(world->scaleDown(ratio)); + joint->SetSpringDampingRatio(Physics::scaleDown(ratio)); } float WheelJoint::getSpringDampingRatio() const { - return world->scaleUp(joint->GetSpringDampingRatio()); + return Physics::scaleUp(joint->GetSpringDampingRatio()); } diff --git a/src/modules/physics/box2d/World.cpp b/src/modules/physics/box2d/World.cpp index bcb8582a7..d6de02db0 100644 --- a/src/modules/physics/box2d/World.cpp +++ b/src/modules/physics/box2d/World.cpp @@ -22,6 +22,7 @@ #include "Shape.h" #include "Contact.h" +#include "Physics.h" #include namespace love @@ -98,7 +99,7 @@ namespace box2d World::World(b2AABB aabb) : world(NULL), meter(DEFAULT_METER) { - world = new b2World(scaleDown(aabb), b2Vec2(0,0), true); + world = new b2World(Physics::scaleDown(aabb), b2Vec2(0,0), true); world->SetContactListener(this); b2BodyDef def; groundBody = world->CreateBody(def); @@ -107,7 +108,7 @@ namespace box2d World::World(b2AABB aabb, b2Vec2 gravity, bool sleep, int meter) : world(NULL), meter(meter) { - world = new b2World(scaleDown(aabb), scaleDown(gravity), sleep); + world = new b2World(Physics::scaleDown(aabb), Physics::scaleDown(gravity), sleep); world->SetContactListener(this); } @@ -183,12 +184,12 @@ namespace box2d void World::setGravity(float x, float y) { - world->SetGravity(scaleDown(b2Vec2(x, y))); + world->SetGravity(Physics::scaleDown(b2Vec2(x, y))); } int World::getGravity(lua_State * L) { - b2Vec2 v = scaleUp(world->m_gravity); + b2Vec2 v = Physics::scaleUp(world->m_gravity); lua_pushnumber(L, v.x); lua_pushnumber(L, v.y); return 2; @@ -213,68 +214,6 @@ namespace box2d { return world->GetJointCount(); } - - void World::setMeter(int meter) - { - this->meter = meter; - } - - int World::getMeter() const - { - return this->meter; - } - - void World::scaleDown(float & x, float & y) - { - x /= (float)meter; - y /= (float)meter; - } - - void World::scaleUp(float & x, float & y) - { - x *= (float)meter; - y *= (float)meter; - } - - float World::scaleDown(float f) - { - return f/(float)meter; - } - - float World::scaleUp(float f) - { - return f*(float)meter; - } - - b2Vec2 World::scaleDown(const b2Vec2 & v) - { - b2Vec2 t = v; - scaleDown(t.x, t.y); - return t; - } - - b2Vec2 World::scaleUp(const b2Vec2 & v) - { - b2Vec2 t = v; - scaleUp(t.x, t.y); - return t; - } - - b2AABB World::scaleDown(const b2AABB & aabb) - { - b2AABB t; - t.lowerBound = scaleDown(aabb.lowerBound); - t.upperBound = scaleDown(aabb.upperBound); - return t; - } - - b2AABB World::scaleUp(const b2AABB & aabb) - { - b2AABB t; - t.lowerBound = scaleUp(aabb.lowerBound); - t.upperBound = scaleUp(aabb.upperBound); - return t; - } b2Body * World::getGroundBody() { diff --git a/src/modules/physics/box2d/World.h b/src/modules/physics/box2d/World.h index 68ed12af1..238f19b7f 100644 --- a/src/modules/physics/box2d/World.h +++ b/src/modules/physics/box2d/World.h @@ -84,9 +84,6 @@ namespace box2d // Contact callbacks. ContactCallback add, persist, remove, result; - // The length of one meter in pixels. - int meter; - public: /** @@ -176,74 +173,6 @@ namespace box2d * @return The number of joints. **/ int getJointCount(); - - /** - * Sets the number of pixels in one meter. - * @param pixels The number of pixels in one meter. (1m ~= 3.3ft). - **/ - void setMeter(int meter); - - /** - * Gets the number of pixels in one meter. - * @param pixels The number of pixels in one meter. (1m ~= 3.3ft). - **/ - int getMeter() const; - - /** - * Scales a value down according to the current meter in pixels. - * @param f The unscaled input value. - **/ - float scaleDown(float f); - - /** - * Scales a value up according to the current meter in pixels. - * @param f The unscaled input value. - **/ - float scaleUp(float f); - - /** - * Scales a point down according to the current meter - * in pixels, for instance x = x0/meter, y = x0/meter. - * @param x The x-coordinate of the point to scale. - * @param y The y-coordinate of the point to scale. - **/ - void scaleDown(float & x, float & y); - - /** - * Scales a point up according to the current meter - * in pixels, for instance x = x0/meter, y = x0/meter. - * @param x The x-coordinate of the point to scale. - * @param y The y-coordinate of the point to scale. - **/ - void scaleUp(float & x, float & y); - - /** - * Scales a b2Vec2 down according to the current meter in pixels. - * @param v The unscaled input vector. - * @return The scaled vector. - **/ - b2Vec2 scaleDown(const b2Vec2 & v); - - /** - * Scales a b2Vec up according to the current meter in pixels. - * @param v The unscaled input vector. - * @return The scaled vector. - **/ - b2Vec2 scaleUp(const b2Vec2 & v); - - /** - * Scales a b2AABB down according to the current meter in pixels. - * @param v The unscaled input AABB. - * @return The scaled AABB. - **/ - b2AABB scaleDown(const b2AABB & aabb); - - /** - * Scales a b2AABB up according to the current meter in pixels. - * @param v The unscaled input AABB. - * @return The scaled AABB. - **/ - b2AABB scaleUp(const b2AABB & aabb); /** * Gets the ground body. diff --git a/src/modules/physics/box2d/wrap_Physics.cpp b/src/modules/physics/box2d/wrap_Physics.cpp index 90c75150e..ac39f6837 100644 --- a/src/modules/physics/box2d/wrap_Physics.cpp +++ b/src/modules/physics/box2d/wrap_Physics.cpp @@ -262,6 +262,19 @@ namespace box2d luax_newtype(L, "RopeJoint", PHYSICS_ROPE_JOINT_T, (void*)j); return 1; } + + int w_setMeter(lua_State * L) + { + int arg1 = luaL_checkint(L, 1); + Physics::setMeter(arg1); + return 0; + + } + int w_getMeter(lua_State * L) + { + lua_pushinteger(L, Physics::getMeter()); + return 1; + } // List of functions to wrap. static const luaL_Reg functions[] = { @@ -281,6 +294,8 @@ namespace box2d { "newWeldJoint", w_newWeldJoint }, { "newWheelJoint", w_newWheelJoint }, { "newRopeJoint", w_newRopeJoint }, + { "getMeter", w_getMeter }, + { "setMeter", w_setMeter }, { 0, 0 }, }; diff --git a/src/modules/physics/box2d/wrap_Physics.h b/src/modules/physics/box2d/wrap_Physics.h index c87684796..47e291807 100644 --- a/src/modules/physics/box2d/wrap_Physics.h +++ b/src/modules/physics/box2d/wrap_Physics.h @@ -65,6 +65,8 @@ namespace box2d int w_newWeldJoint(lua_State * L); int w_newWheelJoint(lua_State * L); int w_newRopeJoint(lua_State * L); + int w_setMeter(lua_State * L); + int w_getMeter(lua_State * L); extern "C" LOVE_EXPORT int luaopen_love_physics(lua_State * L); } // box2d diff --git a/src/modules/physics/box2d/wrap_World.cpp b/src/modules/physics/box2d/wrap_World.cpp index 457626c4c..7ab9e76de 100644 --- a/src/modules/physics/box2d/wrap_World.cpp +++ b/src/modules/physics/box2d/wrap_World.cpp @@ -98,21 +98,6 @@ namespace box2d lua_pushinteger(L, t->getJointCount()); return 1; } - - int w_World_setMeter(lua_State * L) - { - World * t = luax_checkworld(L, 1); - int arg1 = luaL_checkint(L, 2); - t->setMeter(arg1); - return 0; - - } - int w_World_getMeter(lua_State * L) - { - World * t = luax_checkworld(L, 1); - lua_pushinteger(L, t->getMeter()); - return 1; - } static const luaL_Reg functions[] = { { "update", w_World_update }, @@ -124,8 +109,6 @@ namespace box2d { "isAllowSleep", w_World_isAllowSleep }, { "getBodyCount", w_World_getBodyCount }, { "getJointCount", w_World_getJointCount }, - { "setMeter", w_World_setMeter }, - { "getMeter", w_World_getMeter }, { 0, 0 } }; diff --git a/src/modules/physics/box2d/wrap_World.h b/src/modules/physics/box2d/wrap_World.h index dbe64d913..0ba044d84 100644 --- a/src/modules/physics/box2d/wrap_World.h +++ b/src/modules/physics/box2d/wrap_World.h @@ -41,8 +41,6 @@ namespace box2d int w_World_isAllowSleep(lua_State * L); int w_World_getBodyCount(lua_State * L); int w_World_getJointCount(lua_State * L); - int w_World_setMeter(lua_State * L); - int w_World_getMeter(lua_State * L); int luaopen_world(lua_State * L); } // box2d