Browse Source

Implmented a few 'missing methods' in love.physics.

rude 15 years ago
parent
commit
67e10b64f9

+ 9 - 0
src/modules/physics/box2d/Body.cpp

@@ -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))));

+ 7 - 0
src/modules/physics/box2d/Body.h

@@ -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.
 		**/

+ 17 - 1
src/modules/physics/box2d/CircleShape.cpp

@@ -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

+ 8 - 0
src/modules/physics/box2d/CircleShape.h

@@ -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

+ 9 - 0
src/modules/physics/box2d/wrap_Body.cpp

@@ -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 },

+ 1 - 0
src/modules/physics/box2d/wrap_Body.h

@@ -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);

+ 22 - 0
src/modules/physics/box2d/wrap_CircleShape.cpp

@@ -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 },

+ 2 - 0
src/modules/physics/box2d/wrap_CircleShape.h

@@ -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