wrap_Physics.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /**
  2. * Copyright (c) 2006-2011 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) < 4)
  32. return luaL_error(L, "Incorrect number of parameters");
  33. float lx = (float)luaL_checknumber(L, 1);
  34. float ly = (float)luaL_checknumber(L, 2);
  35. float ux = (float)luaL_checknumber(L, 3);
  36. float uy = (float)luaL_checknumber(L, 4);
  37. float gx = (float)luaL_optnumber(L, 5, 0);
  38. float gy = (float)luaL_optnumber(L, 6, 0);
  39. bool sleep = luax_optboolean(L, 7, true);
  40. int meter = (int)luaL_optnumber(L, 8, World::DEFAULT_METER);
  41. World * w = instance->newWorld(lx, ly, ux, uy, gx, gy, sleep, meter);
  42. luax_newtype(L, "World", PHYSICS_WORLD_T, (void*)w);
  43. return 1;
  44. }
  45. int w_newBody(lua_State * L)
  46. {
  47. World * world = luax_checktype<World>(L, 1, "World", PHYSICS_WORLD_T);
  48. float x = (float)luaL_optnumber(L, 2, 0.0);
  49. float y = (float)luaL_optnumber(L, 3, 0.0);
  50. float m = (float)luaL_optnumber(L, 4, 0.0);
  51. float i = (float)luaL_optnumber(L, 5, 0.0);
  52. Body * body = instance->newBody(world, x, y, m, i);
  53. luax_newtype(L, "Body", PHYSICS_BODY_T, (void*)body);
  54. return 1;
  55. }
  56. int w_newCircleShape(lua_State * L)
  57. {
  58. int top = lua_gettop(L);
  59. if(top == 1)
  60. {
  61. float radius = (float)luaL_checknumber(L, 1);
  62. CircleShape * shape = instance->newCircleShape(radius);
  63. luax_newtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, (void*)shape);
  64. return 1;
  65. }
  66. else if(top == 3)
  67. {
  68. float x = (float)luaL_checknumber(L, 1);
  69. float y = (float)luaL_checknumber(L, 2);
  70. float radius = (float)luaL_checknumber(L, 3);
  71. CircleShape * shape = instance->newCircleShape(x, y, radius);
  72. luax_newtype(L, "CircleShape", PHYSICS_CIRCLE_SHAPE_T, (void*)shape);
  73. return 1;
  74. }
  75. else
  76. return luaL_error(L, "Incorrect number of parameters");
  77. }
  78. int w_newRectangleShape(lua_State * L)
  79. {
  80. int top = lua_gettop(L);
  81. if(top == 2)
  82. {
  83. float w = (float)luaL_checknumber(L, 1);
  84. float h = (float)luaL_checknumber(L, 2);
  85. PolygonShape * shape = instance->newRectangleShape(w, h);
  86. luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)shape);
  87. return 1;
  88. }
  89. else if(top == 4 || top == 5)
  90. {
  91. float x = (float)luaL_checknumber(L, 1);
  92. float y = (float)luaL_checknumber(L, 2);
  93. float w = (float)luaL_checknumber(L, 3);
  94. float h = (float)luaL_checknumber(L, 4);
  95. float angle = (float)luaL_optnumber(L, 5, 0);
  96. PolygonShape * shape = instance->newRectangleShape(x, y, w, h, angle);
  97. luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)shape);
  98. return 1;
  99. }
  100. else
  101. return luaL_error(L, "Incorrect number of parameters");
  102. }
  103. int w_newEdgeShape(lua_State * L)
  104. {
  105. float x1 = (float)luaL_checknumber(L, 1);
  106. float y1 = (float)luaL_checknumber(L, 2);
  107. float x2 = (float)luaL_checknumber(L, 3);
  108. float y2 = (float)luaL_checknumber(L, 4);
  109. EdgeShape * shape = instance->newEdgeShape(x1, y1, x2, y2);
  110. luax_newtype(L, "EdgeShape", PHYSICS_EDGE_SHAPE_T, (void*)shape);
  111. return 1;
  112. }
  113. int w_newPolygonShape(lua_State * L)
  114. {
  115. return instance->newPolygonShape(L);
  116. }
  117. int w_newDistanceJoint(lua_State * L)
  118. {
  119. Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  120. Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
  121. float x1 = (float)luaL_checknumber(L, 3);
  122. float y1 = (float)luaL_checknumber(L, 4);
  123. float x2 = (float)luaL_checknumber(L, 5);
  124. float y2 = (float)luaL_checknumber(L, 6);
  125. DistanceJoint * j = instance->newDistanceJoint(body1, body2, x1, y1, x2, y2);
  126. luax_newtype(L, "DistanceJoint", PHYSICS_DISTANCE_JOINT_T, (void*)j);
  127. return 1;
  128. }
  129. int w_newMouseJoint(lua_State * L)
  130. {
  131. Body * body = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  132. float x = (float)luaL_checknumber(L, 2);
  133. float y = (float)luaL_checknumber(L, 3);
  134. MouseJoint * j = instance->newMouseJoint(body, x, y);
  135. luax_newtype(L, "MouseJoint", PHYSICS_MOUSE_JOINT_T, (void*)j);
  136. return 1;
  137. }
  138. int w_newRevoluteJoint(lua_State * L)
  139. {
  140. Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  141. Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
  142. float x = (float)luaL_checknumber(L, 3);
  143. float y = (float)luaL_checknumber(L, 4);
  144. RevoluteJoint * j = instance->newRevoluteJoint(body1, body2, x, y);
  145. luax_newtype(L, "RevoluteJoint", PHYSICS_REVOLUTE_JOINT_T, (void*)j);
  146. return 1;
  147. }
  148. int w_newPrismaticJoint(lua_State * L)
  149. {
  150. Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  151. Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
  152. float x = (float)luaL_checknumber(L, 3);
  153. float y = (float)luaL_checknumber(L, 4);
  154. float ax = (float)luaL_checknumber(L, 5);
  155. float ay = (float)luaL_checknumber(L, 6);
  156. PrismaticJoint * j = instance->newPrismaticJoint(body1, body2, x, y, ax, ay);
  157. luax_newtype(L, "PrismaticJoint", PHYSICS_PRISMATIC_JOINT_T, (void*)j);
  158. return 1;
  159. }
  160. int w_newPulleyJoint(lua_State * L)
  161. {
  162. Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  163. Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
  164. float gx1 = (float)luaL_checknumber(L, 3);
  165. float gy1 = (float)luaL_checknumber(L, 4);
  166. float gx2 = (float)luaL_checknumber(L, 5);
  167. float gy2 = (float)luaL_checknumber(L, 6);
  168. float x1 = (float)luaL_checknumber(L, 7);
  169. float y1 = (float)luaL_checknumber(L, 8);
  170. float x2 = (float)luaL_checknumber(L, 9);
  171. float y2 = (float)luaL_checknumber(L, 10);
  172. float ratio = (float)luaL_optnumber(L, 11, 1.0);
  173. PulleyJoint * j = instance->newPulleyJoint(body1, body2, b2Vec2(gx1,gy1), b2Vec2(gx2,gy2), b2Vec2(x1,y1), b2Vec2(x2,y2), ratio);
  174. luax_newtype(L, "PulleyJoint", PHYSICS_PULLEY_JOINT_T, (void*)j);
  175. return 1;
  176. }
  177. int w_newGearJoint(lua_State * L)
  178. {
  179. Joint * joint1 = luax_checktype<Joint>(L, 1, "Joint", PHYSICS_JOINT_T);
  180. Joint * joint2 = luax_checktype<Joint>(L, 2, "Joint", PHYSICS_JOINT_T);
  181. float ratio = (float)luaL_optnumber(L, 3, 1.0);
  182. GearJoint * j = instance->newGearJoint(joint1, joint2, ratio);
  183. luax_newtype(L, "GearJoint", PHYSICS_GEAR_JOINT_T, (void*)j);
  184. return 1;
  185. }
  186. int w_newFrictionJoint(lua_State * L)
  187. {
  188. Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  189. Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
  190. float x = (float)luaL_checknumber(L, 3);
  191. float y = (float)luaL_checknumber(L, 4);
  192. FrictionJoint * j = instance->newFrictionJoint(body1, body2, x, y);
  193. luax_newtype(L, "FrictionJoint", PHYSICS_FRICTION_JOINT_T, (void*)j);
  194. return 1;
  195. }
  196. int w_newWeldJoint(lua_State * L)
  197. {
  198. Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  199. Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
  200. float x = (float)luaL_checknumber(L, 3);
  201. float y = (float)luaL_checknumber(L, 4);
  202. WeldJoint * j = instance->newWeldJoint(body1, body2, x, y);
  203. luax_newtype(L, "WeldJoint", PHYSICS_WELD_JOINT_T, (void*)j);
  204. return 1;
  205. }
  206. int w_newWheelJoint(lua_State * L)
  207. {
  208. Body * body1 = luax_checktype<Body>(L, 1, "Body", PHYSICS_BODY_T);
  209. Body * body2 = luax_checktype<Body>(L, 2, "Body", PHYSICS_BODY_T);
  210. float x = (float)luaL_checknumber(L, 3);
  211. float y = (float)luaL_checknumber(L, 4);
  212. float ax = (float)luaL_checknumber(L, 5);
  213. float ay = (float)luaL_checknumber(L, 6);
  214. WheelJoint * j = instance->newWheelJoint(body1, body2, x, y, ax, ay);
  215. luax_newtype(L, "WheelJoint", PHYSICS_WHEEL_JOINT_T, (void*)j);
  216. return 1;
  217. }
  218. // List of functions to wrap.
  219. static const luaL_Reg functions[] = {
  220. { "newWorld", w_newWorld },
  221. { "newBody", w_newBody },
  222. { "newCircleShape", w_newCircleShape },
  223. { "newRectangleShape", w_newRectangleShape },
  224. { "newPolygonShape", w_newPolygonShape },
  225. { "newEdgeShape", w_newEdgeShape },
  226. { "newDistanceJoint", w_newDistanceJoint },
  227. { "newMouseJoint", w_newMouseJoint },
  228. { "newRevoluteJoint", w_newRevoluteJoint },
  229. { "newPrismaticJoint", w_newPrismaticJoint },
  230. { "newPulleyJoint", w_newPulleyJoint },
  231. { "newGearJoint", w_newGearJoint },
  232. { "newFrictionJoint", w_newFrictionJoint },
  233. { "newWeldJoint", w_newWeldJoint },
  234. { "newWheelJoint", w_newWheelJoint },
  235. { 0, 0 },
  236. };
  237. static const lua_CFunction types[] = {
  238. luaopen_world,
  239. luaopen_contact,
  240. luaopen_body,
  241. luaopen_shape,
  242. luaopen_circleshape,
  243. luaopen_polygonshape,
  244. luaopen_edgeshape,
  245. luaopen_joint,
  246. luaopen_mousejoint,
  247. luaopen_distancejoint,
  248. luaopen_prismaticjoint,
  249. luaopen_revolutejoint,
  250. luaopen_pulleyjoint,
  251. luaopen_gearjoint,
  252. luaopen_frictionjoint,
  253. luaopen_weldjoint,
  254. 0
  255. };
  256. int luaopen_love_physics(lua_State * L)
  257. {
  258. if(instance == 0)
  259. {
  260. try
  261. {
  262. instance = new Physics();
  263. }
  264. catch(Exception & e)
  265. {
  266. return luaL_error(L, e.what());
  267. }
  268. }
  269. else
  270. instance->retain();
  271. WrappedModule w;
  272. w.module = instance;
  273. w.name = "physics";
  274. w.flags = MODULE_T;
  275. w.functions = functions;
  276. w.types = types;
  277. return luax_register_module(L, w);
  278. }
  279. } // box2d
  280. } // physics
  281. } // love