mirror of
https://github.com/love2d/love.git
synced 2026-08-15 15:51:12 +02:00
Implmented a few 'missing methods' in love.physics.
This commit is contained in:
@@ -195,6 +195,15 @@ namespace box2d
|
||||
body->SetMass(&massData);
|
||||
}
|
||||
|
||||
void Body::setInertia(float i)
|
||||
{
|
||||
b2MassData massData;
|
||||
massData.center = body->GetLocalCenter();
|
||||
massData.mass = body->GetMass();
|
||||
massData.I = i;
|
||||
body->SetMass(&massData);
|
||||
}
|
||||
|
||||
void Body::getWorldPoint(float x, float y, float & x_o, float & y_o)
|
||||
{
|
||||
b2Vec2 v = world->scaleUp(body->GetWorldPoint(world->scaleDown(b2Vec2(x, y))));
|
||||
|
||||
@@ -214,6 +214,13 @@ namespace box2d
|
||||
**/
|
||||
void setMass(float x, float y, float m, float i);
|
||||
|
||||
/**
|
||||
* Sets the inertia while keeping the other properties
|
||||
* (mass and local center).
|
||||
* @param i The inertia.
|
||||
**/
|
||||
void setInertia(float i);
|
||||
|
||||
/**
|
||||
* Sets the Body's angular damping.
|
||||
**/
|
||||
|
||||
@@ -31,11 +31,12 @@ namespace physics
|
||||
namespace box2d
|
||||
{
|
||||
CircleShape::CircleShape(Body * body, b2CircleDef * def)
|
||||
: Shape(body), radius(def->radius)
|
||||
: Shape(body)
|
||||
{
|
||||
def->localPosition = body->world->scaleDown(def->localPosition);
|
||||
def->radius = body->world->scaleDown(def->radius);
|
||||
radius = def->radius;
|
||||
this->localPosition = def->localPosition;
|
||||
shape = body->body->CreateShape(def);
|
||||
shape->SetUserData((void*)data);
|
||||
}
|
||||
@@ -49,6 +50,21 @@ namespace box2d
|
||||
return body->world->scaleUp(radius);
|
||||
}
|
||||
|
||||
void CircleShape::getLocalCenter(float & x, float & y) const
|
||||
{
|
||||
x = localPosition.x;
|
||||
y = localPosition.y;
|
||||
body->world->scaleUp(x, y);
|
||||
}
|
||||
|
||||
void CircleShape::getWorldCenter(float & x, float & y) const
|
||||
{
|
||||
b2Vec2 worldCenter = body->body->GetWorldPoint(localPosition);
|
||||
worldCenter = body->world->scaleUp(worldCenter);
|
||||
x = worldCenter.x;
|
||||
y = worldCenter.y;
|
||||
}
|
||||
|
||||
} // box2d
|
||||
} // physics
|
||||
} // love
|
||||
|
||||
@@ -42,9 +42,14 @@ namespace box2d
|
||||
class CircleShape : public Shape
|
||||
{
|
||||
private:
|
||||
|
||||
// The radius of the circle. We need to store this because
|
||||
// Box2D has no built-in method for getting the radius.
|
||||
float radius;
|
||||
|
||||
// Local offset.
|
||||
b2Vec2 localPosition;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
@@ -63,6 +68,9 @@ namespace box2d
|
||||
float getRadius() const;
|
||||
|
||||
// There is no support for setting the radius.
|
||||
|
||||
void getLocalCenter(float & x, float & y) const;
|
||||
void getWorldCenter(float & x, float & y) const;
|
||||
};
|
||||
|
||||
} // box2d
|
||||
|
||||
@@ -260,6 +260,14 @@ namespace box2d
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_Body_setInertia(lua_State * L)
|
||||
{
|
||||
Body * t = luax_checkbody(L, 1);
|
||||
float i = (float)luaL_checknumber(L, 2);
|
||||
t->setInertia(i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int w_Body_setAngularDamping(lua_State * L)
|
||||
{
|
||||
Body * t = luax_checkbody(L, 1);
|
||||
@@ -476,6 +484,7 @@ namespace box2d
|
||||
{ "setPosition", w_Body_setPosition },
|
||||
{ "setMassFromShapes", w_Body_setMassFromShapes },
|
||||
{ "setMass", w_Body_setMass },
|
||||
{ "setInertia", w_Body_setInertia },
|
||||
{ "setAngularDamping", w_Body_setAngularDamping },
|
||||
{ "setLinearDamping", w_Body_setLinearDamping },
|
||||
{ "getWorldPoint", w_Body_getWorldPoint },
|
||||
|
||||
@@ -56,6 +56,7 @@ namespace box2d
|
||||
int w_Body_setPosition(lua_State * L);
|
||||
int w_Body_setMassFromShapes(lua_State * L);
|
||||
int w_Body_setMass(lua_State * L);
|
||||
int w_Body_setInertia(lua_State * L);
|
||||
int w_Body_setAngularDamping(lua_State * L);
|
||||
int w_Body_setLinearDamping(lua_State * L);
|
||||
int w_Body_getWorldPoint(lua_State * L);
|
||||
|
||||
@@ -38,8 +38,30 @@ namespace box2d
|
||||
return 1;
|
||||
}
|
||||
|
||||
int w_CircleShape_getLocalCenter(lua_State * L)
|
||||
{
|
||||
CircleShape * c = luax_checkcircleshape(L, 1);
|
||||
float x, y;
|
||||
c->getLocalCenter(x, y);
|
||||
lua_pushnumber(L, x);
|
||||
lua_pushnumber(L, y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int w_CircleShape_getWorldCenter(lua_State * L)
|
||||
{
|
||||
CircleShape * c = luax_checkcircleshape(L, 1);
|
||||
float x, y;
|
||||
c->getWorldCenter(x, y);
|
||||
lua_pushnumber(L, x);
|
||||
lua_pushnumber(L, y);
|
||||
return 2;
|
||||
}
|
||||
|
||||
static const luaL_Reg functions[] = {
|
||||
{ "getRadius", w_CircleShape_getRadius },
|
||||
{ "getLocalCenter", w_CircleShape_getLocalCenter },
|
||||
{ "getWorldCenter", w_CircleShape_getWorldCenter },
|
||||
// From Shape.
|
||||
{ "getType", w_Shape_getType },
|
||||
{ "setFriction", w_Shape_setFriction },
|
||||
|
||||
@@ -34,6 +34,8 @@ namespace box2d
|
||||
{
|
||||
CircleShape * luax_checkcircleshape(lua_State * L, int idx);
|
||||
int w_CircleShape_getRadius(lua_State * L);
|
||||
int w_CircleShape_getLocalCenter(lua_State * L);
|
||||
int w_CircleShape_getWorldCenter(lua_State * L);
|
||||
int luaopen_circleshape(lua_State * L);
|
||||
|
||||
} // box2d
|
||||
|
||||
Reference in New Issue
Block a user