Switched get/setMeter from being a World method to a love.physics function

--HG--
branch : box2d-update
This commit is contained in:
Bill Meltsner
2011-09-15 16:13:54 -04:00
parent 1fa99d022a
commit e69cafe868
22 changed files with 270 additions and 256 deletions
+21 -20
View File
@@ -23,6 +23,7 @@
#include <common/math.h> #include <common/math.h>
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -35,7 +36,7 @@ namespace box2d
{ {
world->retain(); world->retain();
b2BodyDef def; b2BodyDef def;
def.position = world->scaleDown(p); def.position = Physics::scaleDown(p);
def.massData.mass = m; def.massData.mass = m;
def.massData.I = i; def.massData.I = i;
body = world->world->CreateBody(&def); body = world->world->CreateBody(&def);
@@ -50,24 +51,24 @@ namespace box2d
float Body::getX() float Body::getX()
{ {
return world->scaleUp(body->GetPosition().x); return Physics::scaleUp(body->GetPosition().x);
} }
float Body::getY() float Body::getY()
{ {
return world->scaleUp(body->GetPosition().y); return Physics::scaleUp(body->GetPosition().y);
} }
void Body::getPosition(float & x_o, float & y_o) 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; x_o = v.x;
y_o = v.y; y_o = v.y;
} }
void Body::getLinearVelocity(float & x_o, float & y_o) 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; x_o = v.x;
y_o = v.y; y_o = v.y;
} }
@@ -79,14 +80,14 @@ namespace box2d
void Body::getWorldCenter(float & x_o, float & y_o) 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; x_o = v.x;
y_o = v.y; y_o = v.y;
} }
void Body::getLocalCenter(float & x_o, float & y_o) 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; x_o = v.x;
y_o = v.y; y_o = v.y;
} }
@@ -123,7 +124,7 @@ namespace box2d
void Body::applyImpulse(float jx, float jy, float rx, float ry) 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) void Body::applyTorque(float t)
@@ -133,7 +134,7 @@ namespace box2d
void Body::applyForce(float fx, float fy, float rx, float ry) 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) void Body::applyForce(float fx, float fy)
@@ -143,17 +144,17 @@ namespace box2d
void Body::setX(float x) 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) 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) 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) void Body::setAngle(float d)
@@ -168,7 +169,7 @@ namespace box2d
void Body::setPosition(float x, float y) 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) void Body::setAngularDamping(float d)
@@ -189,7 +190,7 @@ namespace box2d
void Body::setMass(float x, float y, float m, float i) void Body::setMass(float x, float y, float m, float i)
{ {
b2MassData massData; b2MassData massData;
massData.center = world->scaleDown(b2Vec2(x, y)); massData.center = Physics::scaleDown(b2Vec2(x, y));
massData.mass = m; massData.mass = m;
massData.I = i; massData.I = i;
body->SetMass(&massData); body->SetMass(&massData);
@@ -206,42 +207,42 @@ namespace box2d
void Body::getWorldPoint(float x, float y, float & x_o, float & y_o) 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; x_o = v.x;
y_o = v.y; y_o = v.y;
} }
void Body::getWorldVector(float x, float y, float & x_o, float & y_o) 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; x_o = v.x;
y_o = v.y; y_o = v.y;
} }
void Body::getLocalPoint(float x, float y, float & x_o, float & y_o) 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; x_o = v.x;
y_o = v.y; y_o = v.y;
} }
void Body::getLocalVector(float x, float y, float & x_o, float & y_o) 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; x_o = v.x;
y_o = v.y; y_o = v.y;
} }
void Body::getLinearVelocityFromWorldPoint(float x, float y, float & x_o, float & y_o) 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; x_o = v.x;
y_o = v.y; y_o = v.y;
} }
void Body::getLinearVelocityFromLocalPoint(float x, float y, float & x_o, float & y_o) 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; x_o = v.x;
y_o = v.y; y_o = v.y;
} }
+8 -7
View File
@@ -20,6 +20,7 @@
#include "Contact.h" #include "Contact.h"
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -41,30 +42,30 @@ namespace box2d
int Contact::getPosition(lua_State * L) int Contact::getPosition(lua_State * L)
{ {
love::luax_assert_argc(L, 1, 1); love::luax_assert_argc(L, 1, 1);
lua_pushnumber(L, world->scaleUp(point.position.x)); lua_pushnumber(L, Physics::scaleUp(point.position.x));
lua_pushnumber(L, world->scaleUp(point.position.y)); lua_pushnumber(L, Physics::scaleUp(point.position.y));
return 2; return 2;
} }
int Contact::getVelocity(lua_State * L) int Contact::getVelocity(lua_State * L)
{ {
love::luax_assert_argc(L, 1, 1); love::luax_assert_argc(L, 1, 1);
lua_pushnumber(L, world->scaleUp(point.velocity.x)); lua_pushnumber(L, Physics::scaleUp(point.velocity.x));
lua_pushnumber(L, world->scaleUp(point.velocity.y)); lua_pushnumber(L, Physics::scaleUp(point.velocity.y));
return 2; return 2;
} }
int Contact::getNormal(lua_State * L) int Contact::getNormal(lua_State * L)
{ {
love::luax_assert_argc(L, 1, 1); love::luax_assert_argc(L, 1, 1);
lua_pushnumber(L, world->scaleUp(point.normal.x)); lua_pushnumber(L, Physics::scaleUp(point.normal.x));
lua_pushnumber(L, world->scaleUp(point.normal.y)); lua_pushnumber(L, Physics::scaleUp(point.normal.y));
return 2; return 2;
} }
float Contact::getSeparation() const float Contact::getSeparation() const
{ {
return world->scaleUp(point.separation); return Physics::scaleUp(point.separation);
} }
float Contact::getFriction() const float Contact::getFriction() const
+4 -3
View File
@@ -23,6 +23,7 @@
// Module // Module
#include "Body.h" #include "Body.h"
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -34,7 +35,7 @@ namespace box2d
: Joint(body1, body2), joint(NULL) : Joint(body1, body2), joint(NULL)
{ {
b2DistanceJointDef def; 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; def.collideConnected = collideConnected;
joint = (b2DistanceJoint*)createJoint(&def); joint = (b2DistanceJoint*)createJoint(&def);
} }
@@ -47,12 +48,12 @@ namespace box2d
void DistanceJoint::setLength(float length) void DistanceJoint::setLength(float length)
{ {
joint->SetLength(world->scaleDown(length)); joint->SetLength(Physics::scaleDown(length));
} }
float DistanceJoint::getLength() const float DistanceJoint::getLength() const
{ {
return world->scaleUp(joint->GetLength()); return Physics::scaleUp(joint->GetLength());
} }
void DistanceJoint::setFrequency(float hz) void DistanceJoint::setFrequency(float hz)
+2 -1
View File
@@ -25,6 +25,7 @@
// Module // Module
#include "Body.h" #include "Body.h"
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -36,7 +37,7 @@ namespace box2d
: Joint(body1, body2), joint(NULL) : Joint(body1, body2), joint(NULL)
{ {
b2FrictionJointDef def; 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; def.collideConnected = collideConnected;
joint = (b2FrictionJoint*)createJoint(&def); joint = (b2FrictionJoint*)createJoint(&def);
} }
+5 -4
View File
@@ -26,6 +26,7 @@
// Module // Module
#include "Body.h" #include "Body.h"
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -87,10 +88,10 @@ namespace box2d
int Joint::getAnchors(lua_State * L) int Joint::getAnchors(lua_State * L)
{ {
lua_pushnumber(L, world->scaleUp(joint->GetAnchorA().x)); lua_pushnumber(L, Physics::scaleUp(joint->GetAnchorA().x));
lua_pushnumber(L, world->scaleUp(joint->GetAnchorA().y)); lua_pushnumber(L, Physics::scaleUp(joint->GetAnchorA().y));
lua_pushnumber(L, world->scaleUp(joint->GetAnchorB().x)); lua_pushnumber(L, Physics::scaleUp(joint->GetAnchorB().x));
lua_pushnumber(L, world->scaleUp(joint->GetAnchorB().y)); lua_pushnumber(L, Physics::scaleUp(joint->GetAnchorB().y));
return 4; return 4;
} }
+5 -4
View File
@@ -23,6 +23,7 @@
// Module // Module
#include "Body.h" #include "Body.h"
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -38,7 +39,7 @@ namespace box2d
def.bodyA = body1->world->getGroundBody(); def.bodyA = body1->world->getGroundBody();
def.bodyB = body1->body; def.bodyB = body1->body;
def.maxForce = 1000.0f * body1->body->GetMass(); 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); joint = (b2MouseJoint*)createJoint(&def);
} }
@@ -50,13 +51,13 @@ namespace box2d
void MouseJoint::setTarget(float x, float y) 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) int MouseJoint::getTarget(lua_State * L)
{ {
lua_pushnumber(L, world->scaleUp(joint->GetTarget().x)); lua_pushnumber(L, Physics::scaleUp(joint->GetTarget().x));
lua_pushnumber(L, world->scaleUp(joint->GetTarget().y)); lua_pushnumber(L, Physics::scaleUp(joint->GetTarget().y));
return 2; return 2;
} }
+64
View File
@@ -194,6 +194,70 @@ namespace box2d
return new RopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected); 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 } // box2d
} // physics } // physics
} // love } // love
+73
View File
@@ -50,6 +50,11 @@ namespace box2d
{ {
class Physics : public Module class Physics : public Module
{ {
private:
// The length of one meter in pixels.
static int meter;
public: public:
// Implements Module. // Implements Module.
@@ -243,6 +248,74 @@ namespace box2d
**/ **/
RopeJoint * newRopeJoint(Body * body1, Body * body2, float x1, float y1, float x2, float y2, float maxLength, bool collideConnected); 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 }; // Physics
+15 -14
View File
@@ -23,6 +23,7 @@
// Module // Module
#include "Body.h" #include "Body.h"
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -35,7 +36,7 @@ namespace box2d
{ {
b2PrismaticJointDef def; 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.lowerTranslation = 0.0f;
def.upperTranslation = 100.0f; def.upperTranslation = 100.0f;
def.enableLimit = true; def.enableLimit = true;
@@ -51,12 +52,12 @@ namespace box2d
float PrismaticJoint::getJointTranslation() const float PrismaticJoint::getJointTranslation() const
{ {
return world->scaleDown(joint->GetJointTranslation()); return Physics::scaleDown(joint->GetJointTranslation());
} }
float PrismaticJoint::getJointSpeed() const float PrismaticJoint::getJointSpeed() const
{ {
return world->scaleDown(joint->GetJointSpeed()); return Physics::scaleDown(joint->GetJointSpeed());
} }
void PrismaticJoint::enableMotor(bool motor) void PrismaticJoint::enableMotor(bool motor)
@@ -71,22 +72,22 @@ namespace box2d
void PrismaticJoint::setMaxMotorForce(float force) void PrismaticJoint::setMaxMotorForce(float force)
{ {
joint->SetMaxMotorForce(world->scaleDown(force)); joint->SetMaxMotorForce(Physics::scaleDown(force));
} }
void PrismaticJoint::setMotorSpeed(float speed) void PrismaticJoint::setMotorSpeed(float speed)
{ {
joint->SetMotorSpeed(world->scaleDown(speed)); joint->SetMotorSpeed(Physics::scaleDown(speed));
} }
float PrismaticJoint::getMotorSpeed() const float PrismaticJoint::getMotorSpeed() const
{ {
return world->scaleUp(joint->GetMotorSpeed()); return Physics::scaleUp(joint->GetMotorSpeed());
} }
float PrismaticJoint::getMotorForce(float inv_dt) const 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) void PrismaticJoint::enableLimit(bool limit)
@@ -101,33 +102,33 @@ namespace box2d
void PrismaticJoint::setUpperLimit(float limit) void PrismaticJoint::setUpperLimit(float limit)
{ {
joint->SetLimits(joint->GetLowerLimit(), world->scaleDown(limit)); joint->SetLimits(joint->GetLowerLimit(), Physics::scaleDown(limit));
} }
void PrismaticJoint::setLowerLimit(float 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) 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 float PrismaticJoint::getLowerLimit() const
{ {
return world->scaleUp(joint->GetLowerLimit()); return Physics::scaleUp(joint->GetLowerLimit());
} }
float PrismaticJoint::getUpperLimit() const float PrismaticJoint::getUpperLimit() const
{ {
return world->scaleUp(joint->GetUpperLimit()); return Physics::scaleUp(joint->GetUpperLimit());
} }
int PrismaticJoint::getLimits(lua_State * L) int PrismaticJoint::getLimits(lua_State * L)
{ {
lua_pushnumber(L, world->scaleUp(joint->GetLowerLimit())); lua_pushnumber(L, Physics::scaleUp(joint->GetLowerLimit()));
lua_pushnumber(L, world->scaleUp(joint->GetUpperLimit())); lua_pushnumber(L, Physics::scaleUp(joint->GetUpperLimit()));
return 2; return 2;
} }
+9 -8
View File
@@ -23,6 +23,7 @@
// Module // Module
#include "Body.h" #include "Body.h"
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -34,8 +35,8 @@ namespace box2d
: Joint(body1, body2), joint(NULL) : Joint(body1, body2), joint(NULL)
{ {
b2PulleyJointDef def; b2PulleyJointDef def;
def.Initialize(body1->body, body2->body, world->scaleDown(groundAnchor1), world->scaleDown(groundAnchor2), \ def.Initialize(body1->body, body2->body, Physics::scaleDown(groundAnchor1), Physics::scaleDown(groundAnchor2), \
world->scaleDown(anchor1), world->scaleDown(anchor2), ratio); Physics::scaleDown(anchor1), Physics::scaleDown(anchor2), ratio);
def.collideConnected = collideConnected; def.collideConnected = collideConnected;
joint = (b2PulleyJoint*)createJoint(&def); joint = (b2PulleyJoint*)createJoint(&def);
@@ -49,21 +50,21 @@ namespace box2d
int PulleyJoint::getGroundAnchors(lua_State * L) int PulleyJoint::getGroundAnchors(lua_State * L)
{ {
lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchorA().x)); lua_pushnumber(L, Physics::scaleUp(joint->GetGroundAnchorA().x));
lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchorA().y)); lua_pushnumber(L, Physics::scaleUp(joint->GetGroundAnchorA().y));
lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchorB().x)); lua_pushnumber(L, Physics::scaleUp(joint->GetGroundAnchorB().x));
lua_pushnumber(L, world->scaleUp(joint->GetGroundAnchorB().y)); lua_pushnumber(L, Physics::scaleUp(joint->GetGroundAnchorB().y));
return 4; return 4;
} }
float PulleyJoint::getLength1() const float PulleyJoint::getLength1() const
{ {
return world->scaleUp(joint->GetLength1()); return Physics::scaleUp(joint->GetLength1());
} }
float PulleyJoint::getLength2() const float PulleyJoint::getLength2() const
{ {
return world->scaleUp(joint->GetLength2()); return Physics::scaleUp(joint->GetLength2());
} }
float PulleyJoint::getRatio() const float PulleyJoint::getRatio() const
+2 -1
View File
@@ -25,6 +25,7 @@
// Module // Module
#include "Body.h" #include "Body.h"
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -36,7 +37,7 @@ namespace box2d
: Joint(body1, body2), joint(NULL) : Joint(body1, body2), joint(NULL)
{ {
b2RevoluteJointDef def; 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; def.collideConnected = collideConnected;
joint = (b2RevoluteJoint*)createJoint(&def); joint = (b2RevoluteJoint*)createJoint(&def);
} }
+5 -4
View File
@@ -23,6 +23,7 @@
// Module // Module
#include "Body.h" #include "Body.h"
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -36,9 +37,9 @@ namespace box2d
b2RopeJointDef def; b2RopeJointDef def;
def.bodyA = body1->body; def.bodyA = body1->body;
def.bodyB = body2->body; def.bodyB = body2->body;
def.localAnchorA = world->scaleDown(b2Vec2(x1,y1)); def.localAnchorA = Physics::scaleDown(b2Vec2(x1,y1));
def.localAnchorB = world->scaleDown(b2Vec2(x2,y2)); def.localAnchorB = Physics::scaleDown(b2Vec2(x2,y2));
def.maxLength = world->scaleDown(maxLength); def.maxLength = Physics::scaleDown(maxLength);
def.collideConnected = collideConnected; def.collideConnected = collideConnected;
joint = (b2RopeJoint*)createJoint(&def); joint = (b2RopeJoint*)createJoint(&def);
} }
@@ -51,7 +52,7 @@ namespace box2d
float RopeJoint::getMaxLength() const float RopeJoint::getMaxLength() const
{ {
return world->scaleUp(joint->GetMaxLength()); return Physics::scaleUp(joint->GetMaxLength());
} }
+19 -18
View File
@@ -23,6 +23,7 @@
// Module // Module
#include "Body.h" #include "Body.h"
#include "World.h" #include "World.h"
#include "Physics.h"
// STD // STD
#include <bitset> #include <bitset>
@@ -33,8 +34,8 @@ namespace physics
{ {
namespace box2d namespace box2d
{ {
Shape::Shape(World * world) Shape::Shape()
: world(world), shape(NULL) : shape(NULL)
{ {
} }
@@ -62,7 +63,7 @@ namespace box2d
float Shape::getRadius() const float Shape::getRadius() const
{ {
return world->scaleUp(shape->m_radius); return Physics::scaleUp(shape->m_radius);
} }
int Shape::getChildCount() const int Shape::getChildCount() const
@@ -73,19 +74,19 @@ namespace box2d
bool Shape::testPoint(float x, float y, float r, float px, float py) const bool Shape::testPoint(float x, float y, float r, float px, float py) const
{ {
b2Vec2 point(px, py); b2Vec2 point(px, py);
b2Transform transform(world->scaleDown(b2Vec2(x, y)), b2Rot(r)); b2Transform transform(Physics::scaleDown(b2Vec2(x, y)), b2Rot(r));
return shape->TestPoint(transform, world->scaleDown(point)); return shape->TestPoint(transform, Physics::scaleDown(point));
} }
int Shape::rayCast(lua_State * L) const int Shape::rayCast(lua_State * L) const
{ {
float p1x = world->scaleDown((float)luaL_checknumber(L, 1)); float p1x = Physics::scaleDown((float)luaL_checknumber(L, 1));
float p1y = world->scaleDown((float)luaL_checknumber(L, 2)); float p1y = Physics::scaleDown((float)luaL_checknumber(L, 2));
float p2x = world->scaleDown((float)luaL_checknumber(L, 3)); float p2x = Physics::scaleDown((float)luaL_checknumber(L, 3));
float p2y = world->scaleDown((float)luaL_checknumber(L, 4)); float p2y = Physics::scaleDown((float)luaL_checknumber(L, 4));
float maxFraction = (float)luaL_checknumber(L, 5); float maxFraction = (float)luaL_checknumber(L, 5);
float x = world->scaleDown((float)luaL_checknumber(L, 6)); float x = Physics::scaleDown((float)luaL_checknumber(L, 6));
float y = world->scaleDown((float)luaL_checknumber(L, 7)); float y = Physics::scaleDown((float)luaL_checknumber(L, 7));
float r = (float)luaL_checknumber(L, 8); float r = (float)luaL_checknumber(L, 8);
int childIndex = (int)luaL_optint(L, 9, 0); int childIndex = (int)luaL_optint(L, 9, 0);
b2RayCastInput input; b2RayCastInput input;
@@ -95,22 +96,22 @@ namespace box2d
b2Transform transform(b2Vec2(x, y), b2Rot(r)); b2Transform transform(b2Vec2(x, y), b2Rot(r));
b2RayCastOutput output; b2RayCastOutput output;
shape->RayCast(&output, input, transform, childIndex); shape->RayCast(&output, input, transform, childIndex);
lua_pushnumber(L, world->scaleUp(output.normal.x)); lua_pushnumber(L, Physics::scaleUp(output.normal.x));
lua_pushnumber(L, world->scaleUp(output.normal.y)); lua_pushnumber(L, Physics::scaleUp(output.normal.y));
lua_pushnumber(L, output.fraction); lua_pushnumber(L, output.fraction);
return 3; return 3;
} }
int Shape::computeAABB(lua_State * L) const int Shape::computeAABB(lua_State * L) const
{ {
float x = world->scaleDown((float)luaL_checknumber(L, 1)); float x = Physics::scaleDown((float)luaL_checknumber(L, 1));
float y = world->scaleDown((float)luaL_checknumber(L, 2)); float y = Physics::scaleDown((float)luaL_checknumber(L, 2));
float r = (float)luaL_checknumber(L, 3); float r = (float)luaL_checknumber(L, 3);
int childIndex = (int)luaL_optint(L, 4, 0); int childIndex = (int)luaL_optint(L, 4, 0);
b2Transform transform(b2Vec2(x, y), b2Rot(r)); b2Transform transform(b2Vec2(x, y), b2Rot(r));
b2AABB box; b2AABB box;
shape->ComputeAABB(&box, transform, childIndex); shape->ComputeAABB(&box, transform, childIndex);
box = world->scaleUp(box); box = Physics::scaleUp(box);
lua_pushnumber(L, box.lowerBound.x); lua_pushnumber(L, box.lowerBound.x);
lua_pushnumber(L, box.lowerBound.y); lua_pushnumber(L, box.lowerBound.y);
lua_pushnumber(L, box.upperBound.x); lua_pushnumber(L, box.upperBound.x);
@@ -120,10 +121,10 @@ namespace box2d
int Shape::computeMass(lua_State * L) const 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; b2MassData data;
shape->ComputeMass(&data, density); 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.x);
lua_pushnumber(L, center.y); lua_pushnumber(L, center.y);
lua_pushnumber(L, data.mass); lua_pushnumber(L, data.mass);
+1 -3
View File
@@ -44,8 +44,6 @@ namespace box2d
**/ **/
class Shape : public love::physics::Shape class Shape : public love::physics::Shape
{ {
private:
World * world; // We need a reference for scaling up/down
protected: protected:
@@ -57,7 +55,7 @@ namespace box2d
/** /**
* Creates a Shape. * Creates a Shape.
**/ **/
Shape(World * world); Shape();
virtual ~Shape(); virtual ~Shape();
+2 -1
View File
@@ -25,6 +25,7 @@
// Module // Module
#include "Body.h" #include "Body.h"
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -36,7 +37,7 @@ namespace box2d
: Joint(body1, body2), joint(NULL) : Joint(body1, body2), joint(NULL)
{ {
b2WeldJointDef def; 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; def.collideConnected = collideConnected;
joint = (b2WeldJoint*)createJoint(&def); joint = (b2WeldJoint*)createJoint(&def);
} }
+13 -12
View File
@@ -23,6 +23,7 @@
// Module // Module
#include "Body.h" #include "Body.h"
#include "World.h" #include "World.h"
#include "Physics.h"
namespace love namespace love
{ {
@@ -35,7 +36,7 @@ namespace box2d
{ {
b2WheelJointDef def; 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; def.collideConnected = collideConnected;
joint = (b2WheelJoint*)createJoint(&def); joint = (b2WheelJoint*)createJoint(&def);
} }
@@ -48,12 +49,12 @@ namespace box2d
float WheelJoint::getJointTranslation() const float WheelJoint::getJointTranslation() const
{ {
return world->scaleDown(joint->GetJointTranslation()); return Physics::scaleDown(joint->GetJointTranslation());
} }
float WheelJoint::getJointSpeed() const float WheelJoint::getJointSpeed() const
{ {
return world->scaleDown(joint->GetJointSpeed()); return Physics::scaleDown(joint->GetJointSpeed());
} }
void WheelJoint::enableMotor(bool motor) void WheelJoint::enableMotor(bool motor)
@@ -68,47 +69,47 @@ namespace box2d
void WheelJoint::setMotorSpeed(float speed) void WheelJoint::setMotorSpeed(float speed)
{ {
joint->SetMotorSpeed(world->scaleDown(speed)); joint->SetMotorSpeed(Physics::scaleDown(speed));
} }
float WheelJoint::getMotorSpeed() const float WheelJoint::getMotorSpeed() const
{ {
return world->scaleUp(joint->GetMotorSpeed()); return Physics::scaleUp(joint->GetMotorSpeed());
} }
void WheelJoint::setMaxMotorTorque(float torque) void WheelJoint::setMaxMotorTorque(float torque)
{ {
joint->SetMaxMotorTorque(world->scaleDown(torque)); joint->SetMaxMotorTorque(Physics::scaleDown(torque));
} }
float WheelJoint::getMaxMotorTorque() const float WheelJoint::getMaxMotorTorque() const
{ {
return world->scaleUp(joint->GetMaxMotorTorque()); return Physics::scaleUp(joint->GetMaxMotorTorque());
} }
float WheelJoint::getMotorTorque(float inv_dt) const 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) void WheelJoint::setSpringFrequencyHz(float hz)
{ {
joint->SetSpringFrequencyHz(world->scaleDown(hz)); joint->SetSpringFrequencyHz(Physics::scaleDown(hz));
} }
float WheelJoint::getSpringFrequencyHz() const float WheelJoint::getSpringFrequencyHz() const
{ {
return world->scaleUp(joint->GetSpringFrequencyHz()); return Physics::scaleUp(joint->GetSpringFrequencyHz());
} }
void WheelJoint::setSpringDampingRatio(float ratio) void WheelJoint::setSpringDampingRatio(float ratio)
{ {
joint->SetSpringDampingRatio(world->scaleDown(ratio)); joint->SetSpringDampingRatio(Physics::scaleDown(ratio));
} }
float WheelJoint::getSpringDampingRatio() const float WheelJoint::getSpringDampingRatio() const
{ {
return world->scaleUp(joint->GetSpringDampingRatio()); return Physics::scaleUp(joint->GetSpringDampingRatio());
} }
+5 -66
View File
@@ -22,6 +22,7 @@
#include "Shape.h" #include "Shape.h"
#include "Contact.h" #include "Contact.h"
#include "Physics.h"
#include <common/Reference.h> #include <common/Reference.h>
namespace love namespace love
@@ -98,7 +99,7 @@ namespace box2d
World::World(b2AABB aabb) World::World(b2AABB aabb)
: world(NULL), meter(DEFAULT_METER) : 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); world->SetContactListener(this);
b2BodyDef def; b2BodyDef def;
groundBody = world->CreateBody(def); groundBody = world->CreateBody(def);
@@ -107,7 +108,7 @@ namespace box2d
World::World(b2AABB aabb, b2Vec2 gravity, bool sleep, int meter) World::World(b2AABB aabb, b2Vec2 gravity, bool sleep, int meter)
: world(NULL), meter(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); world->SetContactListener(this);
} }
@@ -183,12 +184,12 @@ namespace box2d
void World::setGravity(float x, float y) 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) 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.x);
lua_pushnumber(L, v.y); lua_pushnumber(L, v.y);
return 2; return 2;
@@ -214,68 +215,6 @@ namespace box2d
return world->GetJointCount(); 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() b2Body * World::getGroundBody()
{ {
return groundBody; return groundBody;
-71
View File
@@ -84,9 +84,6 @@ namespace box2d
// Contact callbacks. // Contact callbacks.
ContactCallback add, persist, remove, result; ContactCallback add, persist, remove, result;
// The length of one meter in pixels.
int meter;
public: public:
/** /**
@@ -177,74 +174,6 @@ namespace box2d
**/ **/
int getJointCount(); 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. * Gets the ground body.
* @return The ground body. * @return The ground body.
@@ -263,6 +263,19 @@ namespace box2d
return 1; 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. // List of functions to wrap.
static const luaL_Reg functions[] = { static const luaL_Reg functions[] = {
{ "newWorld", w_newWorld }, { "newWorld", w_newWorld },
@@ -281,6 +294,8 @@ namespace box2d
{ "newWeldJoint", w_newWeldJoint }, { "newWeldJoint", w_newWeldJoint },
{ "newWheelJoint", w_newWheelJoint }, { "newWheelJoint", w_newWheelJoint },
{ "newRopeJoint", w_newRopeJoint }, { "newRopeJoint", w_newRopeJoint },
{ "getMeter", w_getMeter },
{ "setMeter", w_setMeter },
{ 0, 0 }, { 0, 0 },
}; };
+2
View File
@@ -65,6 +65,8 @@ namespace box2d
int w_newWeldJoint(lua_State * L); int w_newWeldJoint(lua_State * L);
int w_newWheelJoint(lua_State * L); int w_newWheelJoint(lua_State * L);
int w_newRopeJoint(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); extern "C" LOVE_EXPORT int luaopen_love_physics(lua_State * L);
} // box2d } // box2d
-17
View File
@@ -99,21 +99,6 @@ namespace box2d
return 1; 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[] = { static const luaL_Reg functions[] = {
{ "update", w_World_update }, { "update", w_World_update },
{ "setCallbacks", w_World_setCallbacks }, { "setCallbacks", w_World_setCallbacks },
@@ -124,8 +109,6 @@ namespace box2d
{ "isAllowSleep", w_World_isAllowSleep }, { "isAllowSleep", w_World_isAllowSleep },
{ "getBodyCount", w_World_getBodyCount }, { "getBodyCount", w_World_getBodyCount },
{ "getJointCount", w_World_getJointCount }, { "getJointCount", w_World_getJointCount },
{ "setMeter", w_World_setMeter },
{ "getMeter", w_World_getMeter },
{ 0, 0 } { 0, 0 }
}; };
-2
View File
@@ -41,8 +41,6 @@ namespace box2d
int w_World_isAllowSleep(lua_State * L); int w_World_isAllowSleep(lua_State * L);
int w_World_getBodyCount(lua_State * L); int w_World_getBodyCount(lua_State * L);
int w_World_getJointCount(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); int luaopen_world(lua_State * L);
} // box2d } // box2d