love.physics.newWorld now acts like it's supposed to and adheres to the documentation

This commit is contained in:
bill@Ixion
2009-12-03 19:13:35 -06:00
parent 2515c697e5
commit c120a1570c
+267 -265
View File
@@ -1,265 +1,267 @@
/** /**
* Copyright (c) 2006-2009 LOVE Development Team * Copyright (c) 2006-2009 LOVE Development Team
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
* arising from the use of this software. * arising from the use of this software.
* *
* Permission is granted to anyone to use this software for any purpose, * Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it * including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions: * freely, subject to the following restrictions:
* *
* 1. The origin of this software must not be misrepresented; you must not * 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 * claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be * in a product, an acknowledgment in the product documentation would be
* appreciated but is not required. * appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be * 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software. * misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution. * 3. This notice may not be removed or altered from any source distribution.
**/ **/
// LOVE // LOVE
#include "wrap_Physics.h" #include "wrap_Physics.h"
namespace love namespace love
{ {
namespace physics namespace physics
{ {
namespace box2d namespace box2d
{ {
static Physics * instance = 0; static Physics * instance = 0;
int w_newWorld(lua_State * L) int w_newWorld(lua_State * L)
{ {
if(lua_gettop(L) == 2) switch(lua_gettop(L))
{ {
float x = (float)luaL_checknumber(L, 1); case 2:
float y = (float)luaL_checknumber(L, 2); float x = (float)luaL_checknumber(L, 1);
World * w = instance->newWorld(x, y); float y = (float)luaL_checknumber(L, 2);
luax_newtype(L, "World", PHYSICS_WORLD_T, (void*)w); World * w = instance->newWorld(x, y);
return 1; luax_newtype(L, "World", PHYSICS_WORLD_T, (void*)w);
} return 1;
else if(lua_gettop(L) == 6)
{ case 6:
float lx = (float)luaL_checknumber(L, 1); case 7:
float ly = (float)luaL_checknumber(L, 2); float lx = (float)luaL_checknumber(L, 1);
float ux = (float)luaL_checknumber(L, 3); float ly = (float)luaL_checknumber(L, 2);
float uy = (float)luaL_checknumber(L, 4); float ux = (float)luaL_checknumber(L, 3);
float gx = (float)luaL_checknumber(L, 5); float uy = (float)luaL_checknumber(L, 4);
float gy = (float)luaL_checknumber(L, 6); float gx = (float)luaL_checknumber(L, 5);
bool sleep = luax_toboolean(L, 7); float gy = (float)luaL_checknumber(L, 6);
World * w = instance->newWorld(lx, ly, ux, uy, gx, gy, sleep); bool sleep = luax_optboolean(L, 7, true);
luax_newtype(L, "World", PHYSICS_WORLD_T, (void*)w); World * w = instance->newWorld(lx, ly, ux, uy, gx, gy, sleep);
return 1; luax_newtype(L, "World", PHYSICS_WORLD_T, (void*)w);
} return 1;
else
return luaL_error(L, "Incorrect number of parameters"); default:
} return luaL_error(L, "Incorrect number of parameters");
}
int w_newBody(lua_State * L) }
{
World * world = luax_checktype<World>(L, 1, "World", PHYSICS_WORLD_T); int w_newBody(lua_State * L)
float x = (float)luaL_optnumber(L, 2, 0.0); {
float y = (float)luaL_optnumber(L, 3, 0.0); World * world = luax_checktype<World>(L, 1, "World", PHYSICS_WORLD_T);
float m = (float)luaL_optnumber(L, 4, 0.0); float x = (float)luaL_optnumber(L, 2, 0.0);
float i = (float)luaL_optnumber(L, 5, 0.0); float y = (float)luaL_optnumber(L, 3, 0.0);
Body * body = instance->newBody(world, x, y, m, i); float m = (float)luaL_optnumber(L, 4, 0.0);
luax_newtype(L, "Body", PHYSICS_BODY_T, (void*)body); float i = (float)luaL_optnumber(L, 5, 0.0);
return 1; Body * body = instance->newBody(world, x, y, m, i);
} luax_newtype(L, "Body", PHYSICS_BODY_T, (void*)body);
return 1;
int w_newCircleShape(lua_State * L) }
{
Body * body = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T); int w_newCircleShape(lua_State * L)
int top = lua_gettop(L); {
Body * body = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
if(top == 2) int top = lua_gettop(L);
{
float radius = (float)luaL_checknumber(L, 2); if(top == 2)
CircleShape * shape = instance->newCircleShape(body, radius); {
luax_newtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, (void*)shape); float radius = (float)luaL_checknumber(L, 2);
return 1; CircleShape * shape = instance->newCircleShape(body, radius);
} luax_newtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, (void*)shape);
else if(top == 4) return 1;
{ }
float x = (float)luaL_checknumber(L, 2); else if(top == 4)
float y = (float)luaL_checknumber(L, 3); {
float radius = (float)luaL_checknumber(L, 4); float x = (float)luaL_checknumber(L, 2);
CircleShape * shape = instance->newCircleShape(body, x, y, radius); float y = (float)luaL_checknumber(L, 3);
luax_newtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, (void*)shape); float radius = (float)luaL_checknumber(L, 4);
return 1; CircleShape * shape = instance->newCircleShape(body, x, y, radius);
} luax_newtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, (void*)shape);
else return 1;
return luaL_error(L, "Incorrect number of parameters"); }
} else
return luaL_error(L, "Incorrect number of parameters");
int w_newRectangleShape(lua_State * L) }
{
Body * body = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T); int w_newRectangleShape(lua_State * L)
int top = lua_gettop(L); {
Body * body = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
if(top == 3) int top = lua_gettop(L);
{
float w = (float)luaL_checknumber(L, 2); if(top == 3)
float h = (float)luaL_checknumber(L, 3); {
PolygonShape * shape = instance->newRectangleShape(body, w, h); float w = (float)luaL_checknumber(L, 2);
luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)shape); float h = (float)luaL_checknumber(L, 3);
return 1; PolygonShape * shape = instance->newRectangleShape(body, w, h);
} luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)shape);
else if(top == 5 || top == 6) return 1;
{ }
float x = (float)luaL_checknumber(L, 2); else if(top == 5 || top == 6)
float y = (float)luaL_checknumber(L, 3); {
float w = (float)luaL_checknumber(L, 4); float x = (float)luaL_checknumber(L, 2);
float h = (float)luaL_checknumber(L, 5); float y = (float)luaL_checknumber(L, 3);
float angle = (float)luaL_optnumber(L, 6, 0); float w = (float)luaL_checknumber(L, 4);
PolygonShape * shape = instance->newRectangleShape(body, x, y, w, h, angle); float h = (float)luaL_checknumber(L, 5);
luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)shape); float angle = (float)luaL_optnumber(L, 6, 0);
return 1; PolygonShape * shape = instance->newRectangleShape(body, x, y, w, h, angle);
} luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)shape);
else return 1;
return luaL_error(L, "Incorrect number of parameters"); }
} else
return luaL_error(L, "Incorrect number of parameters");
int w_newPolygonShape(lua_State * L) }
{
return instance->newPolygonShape(L); int w_newPolygonShape(lua_State * L)
} {
return instance->newPolygonShape(L);
int w_newDistanceJoint(lua_State * L) }
{
Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T); int w_newDistanceJoint(lua_State * L)
Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T); {
float x1 = (float)luaL_checknumber(L, 3); Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
float y1 = (float)luaL_checknumber(L, 4); Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
float x2 = (float)luaL_checknumber(L, 5); float x1 = (float)luaL_checknumber(L, 3);
float y2 = (float)luaL_checknumber(L, 6); float y1 = (float)luaL_checknumber(L, 4);
DistanceJoint * j = instance->newDistanceJoint(body1, body2, x1, y1, x2, y2); float x2 = (float)luaL_checknumber(L, 5);
luax_newtype(L, "DistanceJoint", PHYSICS_DISTANCE_JOINT_T, (void*)j); float y2 = (float)luaL_checknumber(L, 6);
return 1; DistanceJoint * j = instance->newDistanceJoint(body1, body2, x1, y1, x2, y2);
} luax_newtype(L, "DistanceJoint", PHYSICS_DISTANCE_JOINT_T, (void*)j);
return 1;
int w_newMouseJoint(lua_State * L) }
{
Body * body = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T); int w_newMouseJoint(lua_State * L)
float x = (float)luaL_checknumber(L, 2); {
float y = (float)luaL_checknumber(L, 3); Body * body = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
MouseJoint * j = instance->newMouseJoint(body, x, y); float x = (float)luaL_checknumber(L, 2);
luax_newtype(L, "MouseJoint", PHYSICS_MOUSE_JOINT_T, (void*)j); float y = (float)luaL_checknumber(L, 3);
return 1; MouseJoint * j = instance->newMouseJoint(body, x, y);
} luax_newtype(L, "MouseJoint", PHYSICS_MOUSE_JOINT_T, (void*)j);
return 1;
int w_newRevoluteJoint(lua_State * L) }
{
Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T); int w_newRevoluteJoint(lua_State * L)
Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T); {
float x = (float)luaL_checknumber(L, 3); Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
float y = (float)luaL_checknumber(L, 4); Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
RevoluteJoint * j = instance->newRevoluteJoint(body1, body2, x, y); float x = (float)luaL_checknumber(L, 3);
luax_newtype(L, "RevoluteJoint", PHYSICS_REVOLUTE_JOINT_T, (void*)j); float y = (float)luaL_checknumber(L, 4);
return 1; RevoluteJoint * j = instance->newRevoluteJoint(body1, body2, x, y);
} luax_newtype(L, "RevoluteJoint", PHYSICS_REVOLUTE_JOINT_T, (void*)j);
return 1;
int w_newPrismaticJoint(lua_State * L) }
{
Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T); int w_newPrismaticJoint(lua_State * L)
Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T); {
float x = (float)luaL_checknumber(L, 3); Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
float y = (float)luaL_checknumber(L, 4); Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
float ax = (float)luaL_checknumber(L, 5); float x = (float)luaL_checknumber(L, 3);
float ay = (float)luaL_checknumber(L, 6); float y = (float)luaL_checknumber(L, 4);
PrismaticJoint * j = instance->newPrismaticJoint(body1, body2, x, y, ax, ay); float ax = (float)luaL_checknumber(L, 5);
luax_newtype(L, "PrismaticJoint", PHYSICS_PRISMATIC_JOINT_T, (void*)j); float ay = (float)luaL_checknumber(L, 6);
return 1; PrismaticJoint * j = instance->newPrismaticJoint(body1, body2, x, y, ax, ay);
} luax_newtype(L, "PrismaticJoint", PHYSICS_PRISMATIC_JOINT_T, (void*)j);
return 1;
int w_newPulleyJoint(lua_State * L) }
{
Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T); int w_newPulleyJoint(lua_State * L)
Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T); {
float gx1 = (float)luaL_checknumber(L, 3); Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
float gy1 = (float)luaL_checknumber(L, 4); Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
float gx2 = (float)luaL_checknumber(L, 5); float gx1 = (float)luaL_checknumber(L, 3);
float gy2 = (float)luaL_checknumber(L, 6); float gy1 = (float)luaL_checknumber(L, 4);
float x1 = (float)luaL_checknumber(L, 7); float gx2 = (float)luaL_checknumber(L, 5);
float y1 = (float)luaL_checknumber(L, 8); float gy2 = (float)luaL_checknumber(L, 6);
float x2 = (float)luaL_checknumber(L, 9); float x1 = (float)luaL_checknumber(L, 7);
float y2 = (float)luaL_checknumber(L, 10); float y1 = (float)luaL_checknumber(L, 8);
float ratio = (float)luaL_optnumber(L, 11, 1.0); float x2 = (float)luaL_checknumber(L, 9);
float y2 = (float)luaL_checknumber(L, 10);
PulleyJoint * j = instance->newPulleyJoint(body1, body2, b2Vec2(gx1,gy1), b2Vec2(gx2,gy2), b2Vec2(x1,y1), b2Vec2(x2,y2), ratio); float ratio = (float)luaL_optnumber(L, 11, 1.0);
luax_newtype(L, "PulleyJoint", PHYSICS_PULLEY_JOINT_T, (void*)j);
return 1; PulleyJoint * j = instance->newPulleyJoint(body1, body2, b2Vec2(gx1,gy1), b2Vec2(gx2,gy2), b2Vec2(x1,y1), b2Vec2(x2,y2), ratio);
} luax_newtype(L, "PulleyJoint", PHYSICS_PULLEY_JOINT_T, (void*)j);
return 1;
int w_newGearJoint(lua_State * L) }
{
Joint * joint1 = luax_checktype<Joint>(L, 1, "Joint", PHYSICS_JOINT_T); int w_newGearJoint(lua_State * L)
Joint * joint2 = luax_checktype<Joint>(L, 2, "Joint", PHYSICS_JOINT_T); {
float ratio = (float)luaL_optnumber(L, 3, 1.0); Joint * joint1 = luax_checktype<Joint>(L, 1, "Joint", PHYSICS_JOINT_T);
Joint * joint2 = luax_checktype<Joint>(L, 2, "Joint", PHYSICS_JOINT_T);
GearJoint * j = instance->newGearJoint(joint1, joint2, ratio); float ratio = (float)luaL_optnumber(L, 3, 1.0);
luax_newtype(L, "GearJoint", PHYSICS_GEAR_JOINT_T, (void*)j);
return 1; GearJoint * j = instance->newGearJoint(joint1, joint2, ratio);
} luax_newtype(L, "GearJoint", PHYSICS_GEAR_JOINT_T, (void*)j);
return 1;
// List of functions to wrap. }
static const luaL_Reg functions[] = {
{ "newWorld", w_newWorld }, // List of functions to wrap.
{ "newBody", w_newBody }, static const luaL_Reg functions[] = {
{ "newCircleShape", w_newCircleShape }, { "newWorld", w_newWorld },
{ "newRectangleShape", w_newRectangleShape }, { "newBody", w_newBody },
{ "newPolygonShape", w_newPolygonShape }, { "newCircleShape", w_newCircleShape },
{ "newDistanceJoint", w_newDistanceJoint }, { "newRectangleShape", w_newRectangleShape },
{ "newMouseJoint", w_newMouseJoint }, { "newPolygonShape", w_newPolygonShape },
{ "newRevoluteJoint", w_newRevoluteJoint }, { "newDistanceJoint", w_newDistanceJoint },
{ "newPrismaticJoint", w_newPrismaticJoint }, { "newMouseJoint", w_newMouseJoint },
{ "newPulleyJoint", w_newPulleyJoint }, { "newRevoluteJoint", w_newRevoluteJoint },
{ "newGearJoint", w_newGearJoint }, { "newPrismaticJoint", w_newPrismaticJoint },
{ 0, 0 }, { "newPulleyJoint", w_newPulleyJoint },
}; { "newGearJoint", w_newGearJoint },
{ 0, 0 },
static const lua_CFunction types[] = { };
luaopen_world,
luaopen_contact, static const lua_CFunction types[] = {
luaopen_body, luaopen_world,
luaopen_shape, luaopen_contact,
luaopen_circleshape, luaopen_body,
luaopen_polygonshape, luaopen_shape,
luaopen_joint, luaopen_circleshape,
luaopen_mousejoint, luaopen_polygonshape,
luaopen_distancejoint, luaopen_joint,
luaopen_prismaticjoint, luaopen_mousejoint,
luaopen_revolutejoint, luaopen_distancejoint,
luaopen_pulleyjoint, luaopen_prismaticjoint,
luaopen_gearjoint, luaopen_revolutejoint,
0 luaopen_pulleyjoint,
}; luaopen_gearjoint,
0
int luaopen_love_physics(lua_State * L) };
{
if(instance == 0) int luaopen_love_physics(lua_State * L)
{ {
try if(instance == 0)
{ {
instance = new Physics(); try
} {
catch(Exception & e) instance = new Physics();
{ }
return luaL_error(L, e.what()); catch(Exception & e)
} {
} return luaL_error(L, e.what());
}
WrappedModule w; }
w.module = instance;
w.name = "physics"; WrappedModule w;
w.flags = MODULE_T; w.module = instance;
w.functions = functions; w.name = "physics";
w.types = types; w.flags = MODULE_T;
w.functions = functions;
return luax_register_module(L, w); w.types = types;
}
return luax_register_module(L, w);
} // box2d }
} // physics
} // love } // box2d
} // physics
} // love