wrap_Physics.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * Copyright (c) 2006-2009 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "wrap_Physics.h"
  22. namespace love
  23. {
  24. namespace physics
  25. {
  26. namespace box2d
  27. {
  28. static Physics * instance = 0;
  29. int w_newWorld(lua_State * L)
  30. {
  31. if(lua_gettop(L) == 2)
  32. {
  33. float x = (float)luaL_checknumber(L, 1);
  34. float y = (float)luaL_checknumber(L, 2);
  35. World * w = instance->newWorld(x, y);
  36. luax_newtype(L, "World", PHYSICS_WORLD_T, (void*)w);
  37. return 1;
  38. }
  39. else if(lua_gettop(L) == 6)
  40. {
  41. float lx = (float)luaL_checknumber(L, 1);
  42. float ly = (float)luaL_checknumber(L, 2);
  43. float ux = (float)luaL_checknumber(L, 3);
  44. float uy = (float)luaL_checknumber(L, 4);
  45. float gx = (float)luaL_checknumber(L, 5);
  46. float gy = (float)luaL_checknumber(L, 6);
  47. bool sleep = luax_toboolean(L, 7);
  48. World * w = instance->newWorld(lx, ly, ux, uy, gx, gy, sleep);
  49. luax_newtype(L, "World", PHYSICS_WORLD_T, (void*)w);
  50. return 1;
  51. }
  52. else
  53. return luaL_error(L, "Incorrect number of parameters");
  54. }
  55. int w_newBody(lua_State * L)
  56. {
  57. World * world = luax_checktype<World>(L, 1, "World", PHYSICS_WORLD_T);
  58. float x = (float)luaL_optnumber(L, 2, 0.0);
  59. float y = (float)luaL_optnumber(L, 3, 0.0);
  60. float m = (float)luaL_optnumber(L, 4, 1.0);
  61. float i = (float)luaL_optnumber(L, 5, 1.0);
  62. Body * body = instance->newBody(world, x, y, m, i);
  63. luax_newtype(L, "Body", PHYSICS_BODY_T, (void*)body);
  64. return 1;
  65. }
  66. int w_newCircleShape(lua_State * L)
  67. {
  68. Body * body = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  69. int top = lua_gettop(L);
  70. if(top == 2)
  71. {
  72. float radius = (float)luaL_checknumber(L, 2);
  73. CircleShape * shape = instance->newCircleShape(body, radius);
  74. luax_newtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, (void*)shape);
  75. return 1;
  76. }
  77. else if(top == 4)
  78. {
  79. float x = (float)luaL_checknumber(L, 2);
  80. float y = (float)luaL_checknumber(L, 3);
  81. float radius = (float)luaL_checknumber(L, 4);
  82. CircleShape * shape = instance->newCircleShape(body, x, y, radius);
  83. luax_newtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, (void*)shape);
  84. return 1;
  85. }
  86. else
  87. return luaL_error(L, "Incorrect number of parameters");
  88. }
  89. int w_newRectangleShape(lua_State * L)
  90. {
  91. Body * body = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  92. int top = lua_gettop(L);
  93. if(top == 3)
  94. {
  95. float w = (float)luaL_checknumber(L, 2);
  96. float h = (float)luaL_checknumber(L, 3);
  97. PolygonShape * shape = instance->newRectangleShape(body, w, h);
  98. luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)shape);
  99. return 1;
  100. }
  101. else if(top == 5 || top == 6)
  102. {
  103. float x = (float)luaL_checknumber(L, 2);
  104. float y = (float)luaL_checknumber(L, 3);
  105. float w = (float)luaL_checknumber(L, 4);
  106. float h = (float)luaL_checknumber(L, 5);
  107. float angle = (float)luaL_optnumber(L, 6, 0);
  108. PolygonShape * shape = instance->newRectangleShape(body, x, y, w, h, angle);
  109. luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)shape);
  110. return 1;
  111. }
  112. else
  113. return luaL_error(L, "Incorrect number of parameters");
  114. }
  115. int w_newPolygonShape(lua_State * L)
  116. {
  117. return instance->newPolygonShape(L);
  118. }
  119. int w_newDistanceJoint(lua_State * L)
  120. {
  121. Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  122. Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
  123. float x1 = (float)luaL_checknumber(L, 3);
  124. float y1 = (float)luaL_checknumber(L, 4);
  125. float x2 = (float)luaL_checknumber(L, 5);
  126. float y2 = (float)luaL_checknumber(L, 6);
  127. DistanceJoint * j = instance->newDistanceJoint(body1, body2, x1, y1, x2, y2);
  128. luax_newtype(L, "DistanceJoint", PHYSICS_DISTANCE_JOINT_T, (void*)j);
  129. return 1;
  130. }
  131. int w_newMouseJoint(lua_State * L)
  132. {
  133. Body * body = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  134. float x = (float)luaL_checknumber(L, 2);
  135. float y = (float)luaL_checknumber(L, 3);
  136. MouseJoint * j = instance->newMouseJoint(body, x, y);
  137. luax_newtype(L, "MouseJoint", PHYSICS_MOUSE_JOINT_T, (void*)j);
  138. return 1;
  139. }
  140. int w_newRevoluteJoint(lua_State * L)
  141. {
  142. Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  143. Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
  144. float x = (float)luaL_checknumber(L, 3);
  145. float y = (float)luaL_checknumber(L, 4);
  146. RevoluteJoint * j = instance->newRevoluteJoint(body1, body2, x, y);
  147. luax_newtype(L, "RevoluteJoint", PHYSICS_REVOLUTE_JOINT_T, (void*)j);
  148. return 1;
  149. }
  150. int w_newPrismaticJoint(lua_State * L)
  151. {
  152. Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  153. Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
  154. float x = (float)luaL_checknumber(L, 3);
  155. float y = (float)luaL_checknumber(L, 4);
  156. float ax = (float)luaL_checknumber(L, 5);
  157. float ay = (float)luaL_checknumber(L, 6);
  158. PrismaticJoint * j = instance->newPrismaticJoint(body1, body2, x, y, ax, ay);
  159. luax_newtype(L, "PrismaticJoint", PHYSICS_PRISMATIC_JOINT_T, (void*)j);
  160. return 1;
  161. }
  162. int w_newPulleyJoint(lua_State * L)
  163. {
  164. Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  165. Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
  166. float gx1 = (float)luaL_checknumber(L, 3);
  167. float gy1 = (float)luaL_checknumber(L, 4);
  168. float gx2 = (float)luaL_checknumber(L, 5);
  169. float gy2 = (float)luaL_checknumber(L, 6);
  170. float x1 = (float)luaL_checknumber(L, 7);
  171. float y1 = (float)luaL_checknumber(L, 8);
  172. float x2 = (float)luaL_checknumber(L, 9);
  173. float y2 = (float)luaL_checknumber(L, 10);
  174. float ratio = (float)luaL_optnumber(L, 11, 1.0);
  175. PulleyJoint * j = instance->newPulleyJoint(body1, body2, b2Vec2(gx1,gy1), b2Vec2(gx2,gy2), b2Vec2(x1,y1), b2Vec2(x2,y2), ratio);
  176. luax_newtype(L, "PulleyJoint", PHYSICS_PULLEY_JOINT_T, (void*)j);
  177. return 1;
  178. }
  179. int w_newGearJoint(lua_State * L)
  180. {
  181. Joint * joint1 = luax_checktype<Joint>(L, 1, "Joint", PHYSICS_JOINT_T);
  182. Joint * joint2 = luax_checktype<Joint>(L, 2, "Joint", PHYSICS_JOINT_T);
  183. float ratio = (float)luaL_optnumber(L, 3, 1.0);
  184. GearJoint * j = instance->newGearJoint(joint1, joint2, ratio);
  185. luax_newtype(L, "GearJoint", PHYSICS_GEAR_JOINT_T, (void*)j);
  186. return 1;
  187. }
  188. // List of functions to wrap.
  189. static const luaL_Reg functions[] = {
  190. { "newWorld", w_newWorld },
  191. { "newBody", w_newBody },
  192. { "newCircleShape", w_newCircleShape },
  193. { "newRectangleShape", w_newRectangleShape },
  194. { "newPolygonShape", w_newPolygonShape },
  195. { "newDistanceJoint", w_newDistanceJoint },
  196. { "newMouseJoint", w_newMouseJoint },
  197. { "newRevoluteJoint", w_newRevoluteJoint },
  198. { "newPrismaticJoint", w_newPrismaticJoint },
  199. { "newPulleyJoint", w_newPulleyJoint },
  200. { "newGearJoint", w_newGearJoint },
  201. { 0, 0 },
  202. };
  203. static const lua_CFunction types[] = {
  204. luaopen_world,
  205. luaopen_contact,
  206. luaopen_body,
  207. luaopen_shape,
  208. luaopen_circleshape,
  209. luaopen_polygonshape,
  210. luaopen_joint,
  211. luaopen_mousejoint,
  212. luaopen_distancejoint,
  213. luaopen_prismaticjoint,
  214. luaopen_revolutejoint,
  215. luaopen_pulleyjoint,
  216. luaopen_gearjoint,
  217. 0
  218. };
  219. // List of constants.
  220. static const LuaConstant constants[] = {
  221. { "shape_circle", Shape::SHAPE_CIRCLE },
  222. { "shape_polygon", Shape::SHAPE_POLYGON },
  223. { "joint_distance", Joint::JOINT_DISTANCE },
  224. { "joint_revolute", Joint::JOINT_REVOLUTE },
  225. { "joint_prismatic", Joint::JOINT_PRISMATIC },
  226. { "joint_mouse", Joint::JOINT_MOUSE },
  227. { "joint_pulley", Joint::JOINT_PULLEY },
  228. { "joint_gear", Joint::JOINT_GEAR },
  229. { 0, 0 }
  230. };
  231. int luaopen_love_physics(lua_State * L)
  232. {
  233. if(instance == 0)
  234. {
  235. try
  236. {
  237. instance = new Physics();
  238. }
  239. catch(Exception & e)
  240. {
  241. return luaL_error(L, e.what());
  242. }
  243. }
  244. return luax_register_module(L, functions, types, constants, "physics");
  245. }
  246. } // box2d
  247. } // physics
  248. } // love