Browse Source

Added meter parameter to newWorld

Bart van Strien 15 years ago
parent
commit
908df65bdb

+ 2 - 1
changes.txt

@@ -7,7 +7,8 @@ LOVE 0.7.0
   * Added get/setGroupIndex to CircleShape and PolygonShape.
   * Added Font:getWrap.
   * Added identity field to love.conf.
-  * Added love.quit callback.
+  * Added love.quit callback.
+  * Added extra meter parameter to love.physics.newWorld.
   * Enabled love.filesystem.FileData.
   * Fixed bug where the debug module was not an upvalue of the error handlers. (you can now override debug)
   * Fixed bug where love.audio.pause and friends were acting on everything, not just the passed Source.

+ 13 - 5
src/modules/physics/box2d/Physics.cpp

@@ -1,14 +1,14 @@
 /**
 * Copyright (c) 2006-2010 LOVE Development Team
-* 
+*
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
-* 
+*
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
-* 
+*
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
@@ -33,10 +33,18 @@ namespace physics
 {
 namespace box2d
 {
-	
+
 	const char * Physics::getName() const
 	{
 		return "love.physics.box2d";
+	}
+
+	World * Physics::newWorld(float lx, float ly, float ux, float uy, float gx, float gy, bool sleep, int meter)
+	{
+		b2AABB aabb;
+		aabb.lowerBound.Set(lx, ly);
+		aabb.upperBound.Set(ux, uy);
+		return new World(aabb, b2Vec2(gx, gy), sleep, meter);
 	}
 
 	World * Physics::newWorld(float lx, float ly, float ux, float uy, float gx, float gy, bool sleep)
@@ -166,7 +174,7 @@ namespace box2d
 	{
 		return new PrismaticJoint(body1, body2, x, y, ax, ay);
 	}
-	
+
 	PulleyJoint * Physics::newPulleyJoint(Body * body1, Body * body2, b2Vec2 groundAnchor1, b2Vec2 groundAnchor2, b2Vec2 anchor1, b2Vec2 anchor2, float ratio)
 	{
 		return new PulleyJoint(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, ratio);

+ 17 - 4
src/modules/physics/box2d/Physics.h

@@ -1,14 +1,14 @@
 /**
 * Copyright (c) 2006-2010 LOVE Development Team
-* 
+*
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
-* 
+*
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
-* 
+*
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
@@ -50,6 +50,19 @@ namespace box2d
 		// Implements Module.
 		const char * getName() const;
 
+		/**
+		* Creates a new World.
+		* @param lx Lower bound on the x-axis.
+		* @param ly Lower bound on the y-axis.
+		* @param ux Upper bound on the x-axis.
+		* @param uy Upper bound on the y-axis.
+		* @param gx Gravity along x-axis.
+		* @param gy Gravity along y-axis.
+		* @param sleep Whether the World allows sleep.
+		* @param meter The scale in px/m.
+		**/
+		World * newWorld(float lx, float ly, float ux, float uy, float gx, float gy, bool sleep, int meter);
+
 		/**
 		* Creates a new World.
 		* @param lx Lower bound on the x-axis.
@@ -176,7 +189,7 @@ namespace box2d
 		* @param ay The y-component of the world-axis.
 		**/
 		PrismaticJoint * newPrismaticJoint(Body * body1, Body * body2, float x, float y, float ax, float ay);
-		
+
 		/**
 		* Creates a new PulleyJoint connecting body1 with body2.
 		* @param groundAnchor1 World ground-anchor for body1.

+ 12 - 12
src/modules/physics/box2d/World.cpp

@@ -1,14 +1,14 @@
 /**
 * Copyright (c) 2006-2010 LOVE Development Team
-* 
+*
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
-* 
+*
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
-* 
+*
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
@@ -64,22 +64,22 @@ namespace box2d
 			{
 				// Push the function.
 				ref->push();
-				
+
 				// Push first userdata.
 				{
 					shapeudata * d = (shapeudata *)(contacts[i]->point.shape1->GetUserData());
-					if(d->ref != 0) 
-						d->ref->push(); 
-					else 
+					if(d->ref != 0)
+						d->ref->push();
+					else
 						lua_pushnil(L);
 				}
 
 				// Push first userdata.
 				{
 					shapeudata * d = (shapeudata *)(contacts[i]->point.shape2->GetUserData());
-					if(d->ref != 0) 
-						d->ref->push(); 
-					else 
+					if(d->ref != 0)
+						d->ref->push();
+					else
 						lua_pushnil(L);
 				}
 
@@ -102,8 +102,8 @@ namespace box2d
 		world->SetContactListener(this);
 	}
 
-	World::World(b2AABB aabb, b2Vec2 gravity, bool sleep)
-		: meter(DEFAULT_METER)
+	World::World(b2AABB aabb, b2Vec2 gravity, bool sleep, int meter)
+		: meter(meter)
 	{
 		world = new b2World(scaleDown(aabb), scaleDown(gravity), sleep);
 		world->SetContactListener(this);

+ 13 - 13
src/modules/physics/box2d/World.h

@@ -1,14 +1,14 @@
 /**
 * Copyright (c) 2006-2010 LOVE Development Team
-* 
+*
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
-* 
+*
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
-* 
+*
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
@@ -38,18 +38,18 @@ namespace physics
 {
 namespace box2d
 {
-	
+
 	class Contact;
 
 	/**
-	* The World is the "God" container class, 
+	* The World is the "God" container class,
 	* which contains all Bodies and Joints. Shapes
-	* are contained in their associated Body. 
-	* 
+	* are contained in their associated Body.
+	*
 	* Bodies in different worlds can obviously not
 	* collide.
-	* 
-	* The world also controls global parameters, like 
+	*
+	* The world also controls global parameters, like
 	* gravity.
 	**/
 	class World : public Object, public b2ContactListener
@@ -105,7 +105,7 @@ namespace box2d
 		* @param sleep True if the bodies should be able to sleep,
 		* false otherwise.
 		**/
-		World(b2AABB aabb, b2Vec2 gravity, bool sleep);
+		World(b2AABB aabb, b2Vec2 gravity, bool sleep, int meter = DEFAULT_METER);
 
 		virtual ~World();
 
@@ -127,7 +127,7 @@ namespace box2d
 		* Receives up to four Lua functions as arguments. Each function is
 		* collision callback for the four events (in order): add, persist,
 		* remove and result. The value "nil" is accepted if one or more events
-		* are uninteresting. 
+		* are uninteresting.
 		**/
 		int setCallbacks(lua_State * L);
 
@@ -135,7 +135,7 @@ namespace box2d
 		* Returns the functions previously set by setCallbacks.
 		**/
 		int getCallbacks(lua_State * L);
-		
+
 		/**
 		* Sets the current gravity of the World.
 		* @param x Gravity in the x-direction.
@@ -175,7 +175,7 @@ namespace box2d
 		int getJointCount();
 
 		/**
-		* Sets the number of pixels in one meter. 
+		* Sets the number of pixels in one meter.
 		* @param pixels The number of pixels in one meter. (1m ~= 3.3ft).
 		**/
 		void setMeter(int meter);

+ 14 - 0
src/modules/physics/box2d/wrap_Physics.cpp

@@ -53,6 +53,20 @@ namespace box2d
 			luax_newtype(L, "World", PHYSICS_WORLD_T, (void*)w);
 			return 1;
 		}
+		else if (top == 8)
+		{
+			float lx = (float)luaL_checknumber(L, 1);
+			float ly = (float)luaL_checknumber(L, 2);
+			float ux = (float)luaL_checknumber(L, 3);
+			float uy = (float)luaL_checknumber(L, 4);
+			float gx = (float)luaL_checknumber(L, 5);
+			float gy = (float)luaL_checknumber(L, 6);
+			bool sleep = lua_toboolean(L, 7);
+			int meter = (int)luaL_checknumber(L, 8);
+			World * w = instance->newWorld(lx, ly, ux, uy, gx, gy, sleep, meter);
+			luax_newtype(L, "World", PHYSICS_WORLD_T, (void*)w);
+			return 1;
+		}
 		else
 			return luaL_error(L, "Incorrect number of parameters");
 	}