wrap_Body.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /**
  2. * Copyright (c) 2006-2012 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. #include "wrap_Body.h"
  21. namespace love
  22. {
  23. namespace physics
  24. {
  25. namespace box2d
  26. {
  27. Body * luax_checkbody(lua_State * L, int idx)
  28. {
  29. Body * b = luax_checktype<Body>(L, idx, "Body", PHYSICS_BODY_T);
  30. if (b->body == 0)
  31. luaL_error(L, "Attempt to use destroyed body.");
  32. return b;
  33. }
  34. int w_Body_getX(lua_State * L)
  35. {
  36. Body * t = luax_checkbody(L, 1);
  37. lua_pushnumber(L, t->getX());
  38. return 1;
  39. }
  40. int w_Body_getY(lua_State * L)
  41. {
  42. Body * t = luax_checkbody(L, 1);
  43. lua_pushnumber(L, t->getY());
  44. return 1;
  45. }
  46. int w_Body_getAngle(lua_State * L)
  47. {
  48. Body * t = luax_checkbody(L, 1);
  49. lua_pushnumber(L, t->getAngle());
  50. return 1;
  51. }
  52. int w_Body_getPosition(lua_State * L)
  53. {
  54. Body * t = luax_checkbody(L, 1);
  55. float x_o, y_o;
  56. t->getPosition(x_o, y_o);
  57. lua_pushnumber(L, x_o);
  58. lua_pushnumber(L, y_o);
  59. return 2;
  60. }
  61. int w_Body_getLinearVelocity(lua_State * L)
  62. {
  63. Body * t = luax_checkbody(L, 1);
  64. float x_o, y_o;
  65. t->getLinearVelocity(x_o, y_o);
  66. lua_pushnumber(L, x_o);
  67. lua_pushnumber(L, y_o);
  68. return 2;
  69. }
  70. int w_Body_getWorldCenter(lua_State * L)
  71. {
  72. Body * t = luax_checkbody(L, 1);
  73. float x_o, y_o;
  74. t->getWorldCenter(x_o, y_o);
  75. lua_pushnumber(L, x_o);
  76. lua_pushnumber(L, y_o);
  77. return 2;
  78. }
  79. int w_Body_getLocalCenter(lua_State * L)
  80. {
  81. Body * t = luax_checkbody(L, 1);
  82. float x_o, y_o;
  83. t->getLocalCenter(x_o, y_o);
  84. lua_pushnumber(L, x_o);
  85. lua_pushnumber(L, y_o);
  86. return 2;
  87. }
  88. int w_Body_getAngularVelocity(lua_State * L)
  89. {
  90. Body * t = luax_checkbody(L, 1);
  91. lua_pushnumber(L, t->getAngularVelocity());
  92. return 1;
  93. }
  94. int w_Body_getMass(lua_State * L)
  95. {
  96. Body * t = luax_checkbody(L, 1);
  97. lua_pushnumber(L, t->getMass());
  98. return 1;
  99. }
  100. int w_Body_getInertia(lua_State * L)
  101. {
  102. Body * t = luax_checkbody(L, 1);
  103. lua_pushnumber(L, t->getInertia());
  104. return 1;
  105. }
  106. int w_Body_getMassData(lua_State * L)
  107. {
  108. Body * t = luax_checkbody(L, 1);
  109. lua_remove(L, 1);
  110. return t->getMassData(L);
  111. }
  112. int w_Body_getAngularDamping(lua_State * L)
  113. {
  114. Body * t = luax_checkbody(L, 1);
  115. lua_pushnumber(L, t->getAngularDamping());
  116. return 1;
  117. }
  118. int w_Body_getLinearDamping(lua_State * L)
  119. {
  120. Body * t = luax_checkbody(L, 1);
  121. lua_pushnumber(L, t->getLinearDamping());
  122. return 1;
  123. }
  124. int w_Body_getGravityScale(lua_State * L)
  125. {
  126. Body * t = luax_checkbody(L, 1);
  127. lua_pushnumber(L, t->getGravityScale());
  128. return 1;
  129. }
  130. int w_Body_getType(lua_State * L)
  131. {
  132. Body * t = luax_checkbody(L, 1);
  133. const char * type = "";
  134. Body::getConstant(t->getType(), type);
  135. lua_pushstring(L, type);
  136. return 1;
  137. }
  138. int w_Body_applyLinearImpulse(lua_State * L)
  139. {
  140. Body * t = luax_checkbody(L, 1);
  141. float jx = (float)luaL_checknumber(L, 2);
  142. float jy = (float)luaL_checknumber(L, 3);
  143. if (lua_gettop(L) == 3)
  144. {
  145. t->applyLinearImpulse(jx, jy);
  146. }
  147. else if (lua_gettop(L) == 5)
  148. {
  149. float rx = (float)luaL_checknumber(L, 4);
  150. float ry = (float)luaL_checknumber(L, 5);
  151. t->applyLinearImpulse(jx, jy, rx, ry);
  152. }
  153. else
  154. {
  155. return luaL_error(L, "Wrong number of parameters.");
  156. }
  157. return 0;
  158. }
  159. int w_Body_applyAngularImpulse(lua_State * L)
  160. {
  161. Body * t = luax_checkbody(L, 1);
  162. float i = (float)luaL_checknumber(L, 2);
  163. t->applyAngularImpulse(i);
  164. return 0;
  165. }
  166. int w_Body_applyTorque(lua_State * L)
  167. {
  168. Body * t = luax_checkbody(L, 1);
  169. float arg = (float)luaL_checknumber(L, 2);
  170. t->applyTorque(arg);
  171. return 0;
  172. }
  173. int w_Body_applyForce(lua_State * L)
  174. {
  175. Body * t = luax_checkbody(L, 1);
  176. float fx = (float)luaL_checknumber(L, 2);
  177. float fy = (float)luaL_checknumber(L, 3);
  178. if (lua_gettop(L) == 3)
  179. {
  180. t->applyForce(fx, fy);
  181. }
  182. else if (lua_gettop(L) == 5)
  183. {
  184. float rx = (float)luaL_checknumber(L, 4);
  185. float ry = (float)luaL_checknumber(L, 5);
  186. t->applyForce(fx, fy, rx, ry);
  187. }
  188. else
  189. {
  190. return luaL_error(L, "Wrong number of parameters.");
  191. }
  192. return 0;
  193. }
  194. int w_Body_setX(lua_State * L)
  195. {
  196. Body * t = luax_checkbody(L, 1);
  197. float arg1 = (float)luaL_checknumber(L, 2);
  198. t->setX(arg1);
  199. return 0;
  200. }
  201. int w_Body_setY(lua_State * L)
  202. {
  203. Body * t = luax_checkbody(L, 1);
  204. float arg1 = (float)luaL_checknumber(L, 2);
  205. t->setY(arg1);
  206. return 0;
  207. }
  208. int w_Body_setLinearVelocity(lua_State * L)
  209. {
  210. Body * t = luax_checkbody(L, 1);
  211. float arg1 = (float)luaL_checknumber(L, 2);
  212. float arg2 = (float)luaL_checknumber(L, 3);
  213. t->setLinearVelocity(arg1, arg2);
  214. return 0;
  215. }
  216. int w_Body_setAngle(lua_State * L)
  217. {
  218. Body * t = luax_checkbody(L, 1);
  219. float arg1 = (float)luaL_checknumber(L, 2);
  220. t->setAngle(arg1);
  221. return 0;
  222. }
  223. int w_Body_setAngularVelocity(lua_State * L)
  224. {
  225. Body * t = luax_checkbody(L, 1);
  226. float arg1 = (float)luaL_checknumber(L, 2);
  227. t->setAngularVelocity(arg1);
  228. return 0;
  229. }
  230. int w_Body_setPosition(lua_State * L)
  231. {
  232. Body * t = luax_checkbody(L, 1);
  233. float arg1 = (float)luaL_checknumber(L, 2);
  234. float arg2 = (float)luaL_checknumber(L, 3);
  235. t->setPosition(arg1, arg2);
  236. return 0;
  237. }
  238. int w_Body_resetMassData(lua_State * L)
  239. {
  240. Body * t = luax_checkbody(L, 1);
  241. t->resetMassData();
  242. return 0;
  243. }
  244. int w_Body_setMassData(lua_State * L)
  245. {
  246. Body * t = luax_checkbody(L, 1);
  247. float x = (float)luaL_checknumber(L, 2);
  248. float y = (float)luaL_checknumber(L, 3);
  249. float m = (float)luaL_checknumber(L, 4);
  250. float i = (float)luaL_checknumber(L, 5);
  251. t->setMassData(x, y, m, i);
  252. return 0;
  253. }
  254. int w_Body_setMass(lua_State * L)
  255. {
  256. Body * t = luax_checkbody(L, 1);
  257. float m = (float)luaL_checknumber(L, 2);
  258. t->setMass(m);
  259. return 0;
  260. }
  261. int w_Body_setInertia(lua_State * L)
  262. {
  263. Body * t = luax_checkbody(L, 1);
  264. float i = (float)luaL_checknumber(L, 2);
  265. t->setInertia(i);
  266. return 0;
  267. }
  268. int w_Body_setAngularDamping(lua_State * L)
  269. {
  270. Body * t = luax_checkbody(L, 1);
  271. float arg1 = (float)luaL_checknumber(L, 2);
  272. t->setAngularDamping(arg1);
  273. return 0;
  274. }
  275. int w_Body_setLinearDamping(lua_State * L)
  276. {
  277. Body * t = luax_checkbody(L, 1);
  278. float arg1 = (float)luaL_checknumber(L, 2);
  279. t->setLinearDamping(arg1);
  280. return 0;
  281. }
  282. int w_Body_setGravityScale(lua_State * L)
  283. {
  284. Body * t = luax_checkbody(L, 1);
  285. float arg1 = (float)luaL_checknumber(L, 2);
  286. t->setGravityScale(arg1);
  287. return 0;
  288. }
  289. int w_Body_setType(lua_State * L)
  290. {
  291. Body * t = luax_checkbody(L, 1);
  292. const char * typeStr = luaL_checkstring(L, 2);
  293. Body::Type type;
  294. Body::getConstant(typeStr, type);
  295. t->setType(type);
  296. return 0;
  297. }
  298. int w_Body_getWorldPoint(lua_State * L)
  299. {
  300. Body * t = luax_checkbody(L, 1);
  301. float x = (float)luaL_checknumber(L, 2);
  302. float y = (float)luaL_checknumber(L, 3);
  303. float x_o, y_o;
  304. t->getWorldPoint(x, y, x_o, y_o);
  305. lua_pushnumber(L, x_o);
  306. lua_pushnumber(L, y_o);
  307. return 2;
  308. }
  309. int w_Body_getWorldVector(lua_State * L)
  310. {
  311. Body * t = luax_checkbody(L, 1);
  312. float x = (float)luaL_checknumber(L, 2);
  313. float y = (float)luaL_checknumber(L, 3);
  314. float x_o, y_o;
  315. t->getWorldVector(x, y, x_o, y_o);
  316. lua_pushnumber(L, x_o);
  317. lua_pushnumber(L, y_o);
  318. return 2;
  319. }
  320. int w_Body_getWorldPoints(lua_State * L)
  321. {
  322. Body * t = luax_checkbody(L, 1);
  323. lua_remove(L, 1);
  324. return t->getWorldPoints(L);
  325. }
  326. int w_Body_getLocalPoint(lua_State * L)
  327. {
  328. Body * t = luax_checkbody(L, 1);
  329. float x = (float)luaL_checknumber(L, 2);
  330. float y = (float)luaL_checknumber(L, 3);
  331. float x_o, y_o;
  332. t->getLocalPoint(x, y, x_o, y_o);
  333. lua_pushnumber(L, x_o);
  334. lua_pushnumber(L, y_o);
  335. return 2;
  336. }
  337. int w_Body_getLocalVector(lua_State * L)
  338. {
  339. Body * t = luax_checkbody(L, 1);
  340. float x = (float)luaL_checknumber(L, 2);
  341. float y = (float)luaL_checknumber(L, 3);
  342. float x_o, y_o;
  343. t->getLocalVector(x, y, x_o, y_o);
  344. lua_pushnumber(L, x_o);
  345. lua_pushnumber(L, y_o);
  346. return 2;
  347. }
  348. int w_Body_getLinearVelocityFromWorldPoint(lua_State * L)
  349. {
  350. Body * t = luax_checkbody(L, 1);
  351. float x = (float)luaL_checknumber(L, 2);
  352. float y = (float)luaL_checknumber(L, 3);
  353. float x_o, y_o;
  354. t->getLinearVelocityFromWorldPoint(x, y, x_o, y_o);
  355. lua_pushnumber(L, x_o);
  356. lua_pushnumber(L, y_o);
  357. return 2;
  358. }
  359. int w_Body_getLinearVelocityFromLocalPoint(lua_State * L)
  360. {
  361. Body * t = luax_checkbody(L, 1);
  362. float x = (float)luaL_checknumber(L, 2);
  363. float y = (float)luaL_checknumber(L, 3);
  364. float x_o, y_o;
  365. t->getLinearVelocityFromLocalPoint(x, y, x_o, y_o);
  366. lua_pushnumber(L, x_o);
  367. lua_pushnumber(L, y_o);
  368. return 2;
  369. }
  370. int w_Body_isBullet(lua_State * L)
  371. {
  372. Body * t = luax_checkbody(L, 1);
  373. luax_pushboolean(L, t->isBullet());
  374. return 1;
  375. }
  376. int w_Body_setBullet(lua_State * L)
  377. {
  378. Body * t = luax_checkbody(L, 1);
  379. bool b = luax_toboolean(L, 2);
  380. t->setBullet(b);
  381. return 0;
  382. }
  383. int w_Body_isActive(lua_State * L)
  384. {
  385. Body * t = luax_checkbody(L, 1);
  386. luax_pushboolean(L, t->isActive());
  387. return 1;
  388. }
  389. int w_Body_isAwake(lua_State * L)
  390. {
  391. Body * t = luax_checkbody(L, 1);
  392. luax_pushboolean(L, t->isAwake());
  393. return 1;
  394. }
  395. int w_Body_setSleepingAllowed(lua_State * L)
  396. {
  397. Body * t = luax_checkbody(L, 1);
  398. bool b = luax_toboolean(L, 2);
  399. t->setSleepingAllowed(b);
  400. return 0;
  401. }
  402. int w_Body_isSleepingAllowed(lua_State * L)
  403. {
  404. Body * t = luax_checkbody(L, 1);
  405. lua_pushboolean(L, t->isSleepingAllowed());
  406. return 1;
  407. }
  408. int w_Body_setActive(lua_State * L)
  409. {
  410. Body * t = luax_checkbody(L, 1);
  411. bool b = luax_toboolean(L, 2);
  412. t->setActive(b);
  413. return 0;
  414. }
  415. int w_Body_setAwake(lua_State * L)
  416. {
  417. Body * t = luax_checkbody(L, 1);
  418. bool b = luax_toboolean(L, 2);
  419. t->setAwake(b);
  420. return 0;
  421. }
  422. int w_Body_setFixedRotation(lua_State * L)
  423. {
  424. Body * t = luax_checkbody(L, 1);
  425. bool b = luax_toboolean(L, 2);
  426. t->setFixedRotation(b);
  427. return 0;
  428. }
  429. int w_Body_isFixedRotation(lua_State * L)
  430. {
  431. Body * t = luax_checkbody(L, 1);
  432. bool b = t->isFixedRotation();
  433. luax_pushboolean(L, b);
  434. return 1;
  435. }
  436. int w_Body_getFixtureList(lua_State * L)
  437. {
  438. Body * t = luax_checkbody(L, 1);
  439. lua_remove(L, 1);
  440. return t->getFixtureList(L);
  441. }
  442. int w_Body_destroy(lua_State * L)
  443. {
  444. Body * t = luax_checkbody(L, 1);
  445. try
  446. {
  447. t->destroy();
  448. }
  449. catch (love::Exception & e)
  450. {
  451. luaL_error(L, "%s", e.what());
  452. }
  453. return 0;
  454. }
  455. static const luaL_Reg functions[] = {
  456. { "getX", w_Body_getX },
  457. { "getY", w_Body_getY },
  458. { "getAngle", w_Body_getAngle },
  459. { "getPosition", w_Body_getPosition },
  460. { "getLinearVelocity", w_Body_getLinearVelocity },
  461. { "getWorldCenter", w_Body_getWorldCenter },
  462. { "getLocalCenter", w_Body_getLocalCenter },
  463. { "getAngularVelocity", w_Body_getAngularVelocity },
  464. { "getMass", w_Body_getMass },
  465. { "getInertia", w_Body_getInertia },
  466. { "getMassData", w_Body_getMassData },
  467. { "getAngularDamping", w_Body_getAngularDamping },
  468. { "getLinearDamping", w_Body_getLinearDamping },
  469. { "getGravityScale", w_Body_getGravityScale },
  470. { "getType", w_Body_getType },
  471. { "applyLinearImpulse", w_Body_applyLinearImpulse },
  472. { "applyAngularImpulse", w_Body_applyAngularImpulse },
  473. { "applyTorque", w_Body_applyTorque },
  474. { "applyForce", w_Body_applyForce },
  475. { "setX", w_Body_setX },
  476. { "setY", w_Body_setY },
  477. { "setLinearVelocity", w_Body_setLinearVelocity },
  478. { "setAngle", w_Body_setAngle },
  479. { "setAngularVelocity", w_Body_setAngularVelocity },
  480. { "setPosition", w_Body_setPosition },
  481. { "resetMassData", w_Body_resetMassData },
  482. { "setMassData", w_Body_setMassData },
  483. { "setMass", w_Body_setMass },
  484. { "setInertia", w_Body_setInertia },
  485. { "setAngularDamping", w_Body_setAngularDamping },
  486. { "setLinearDamping", w_Body_setLinearDamping },
  487. { "setGravityScale", w_Body_setGravityScale },
  488. { "setType", w_Body_setType },
  489. { "getWorldPoint", w_Body_getWorldPoint },
  490. { "getWorldVector", w_Body_getWorldVector },
  491. { "getWorldPoints", w_Body_getWorldPoints },
  492. { "getLocalPoint", w_Body_getLocalPoint },
  493. { "getLocalVector", w_Body_getLocalVector },
  494. { "getLinearVelocityFromWorldPoint", w_Body_getLinearVelocityFromWorldPoint },
  495. { "getLinearVelocityFromLocalPoint", w_Body_getLinearVelocityFromLocalPoint },
  496. { "isBullet", w_Body_isBullet },
  497. { "setBullet", w_Body_setBullet },
  498. { "isActive", w_Body_isActive },
  499. { "isAwake", w_Body_isAwake },
  500. { "setSleepingAllowed", w_Body_setSleepingAllowed },
  501. { "isSleepingAllowed", w_Body_isSleepingAllowed },
  502. { "setActive", w_Body_setActive },
  503. { "setAwake", w_Body_setAwake },
  504. { "setFixedRotation", w_Body_setFixedRotation },
  505. { "isFixedRotation", w_Body_isFixedRotation },
  506. { "getFixtureList", w_Body_getFixtureList },
  507. { "destroy", w_Body_destroy },
  508. { 0, 0 }
  509. };
  510. extern "C" int luaopen_body(lua_State * L)
  511. {
  512. return luax_register_type(L, "Body", functions);
  513. }
  514. } // box2d
  515. } // physics
  516. } // love