lua_PhysicsRigidBodyParameters.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "PhysicsRigidBody.h"
  4. #include "lua_PhysicsRigidBodyParameters.h"
  5. #include "lua_Global.h"
  6. namespace gameplay
  7. {
  8. void luaRegister_PhysicsRigidBodyParameters()
  9. {
  10. ScriptController* sc = ScriptController::getInstance();
  11. const luaL_Reg lua_members[] =
  12. {
  13. {"angularDamping", lua_PhysicsRigidBodyParameters_angularDamping},
  14. {"anisotropicFriction", lua_PhysicsRigidBodyParameters_anisotropicFriction},
  15. {"friction", lua_PhysicsRigidBodyParameters_friction},
  16. {"kinematic", lua_PhysicsRigidBodyParameters_kinematic},
  17. {"linearDamping", lua_PhysicsRigidBodyParameters_linearDamping},
  18. {"mass", lua_PhysicsRigidBodyParameters_mass},
  19. {"restitution", lua_PhysicsRigidBodyParameters_restitution},
  20. {NULL, NULL}
  21. };
  22. const luaL_Reg* lua_statics = NULL;
  23. std::vector<std::string> scopePath;
  24. scopePath.push_back("PhysicsRigidBody");
  25. sc->registerClass("PhysicsRigidBodyParameters", lua_members, lua_PhysicsRigidBodyParameters__init, lua_PhysicsRigidBodyParameters__gc, lua_statics, scopePath);
  26. }
  27. static PhysicsRigidBody::Parameters* getInstance(lua_State* state)
  28. {
  29. void* userdata = luaL_checkudata(state, 1, "PhysicsRigidBodyParameters");
  30. luaL_argcheck(state, userdata != NULL, 1, "'PhysicsRigidBodyParameters' expected.");
  31. return (PhysicsRigidBody::Parameters*)((ScriptController::LuaObject*)userdata)->instance;
  32. }
  33. int lua_PhysicsRigidBodyParameters__gc(lua_State* state)
  34. {
  35. // Get the number of parameters.
  36. int paramCount = lua_gettop(state);
  37. // Attempt to match the parameters to a valid binding.
  38. switch (paramCount)
  39. {
  40. case 1:
  41. {
  42. if (lua_type(state, 1) == LUA_TUSERDATA)
  43. {
  44. void* userdata = luaL_checkudata(state, 1, "PhysicsRigidBodyParameters");
  45. luaL_argcheck(state, userdata != NULL, 1, "'PhysicsRigidBodyParameters' expected.");
  46. ScriptController::LuaObject* object = (ScriptController::LuaObject*)userdata;
  47. if (object->owns)
  48. {
  49. PhysicsRigidBody::Parameters* instance = (PhysicsRigidBody::Parameters*)object->instance;
  50. SAFE_DELETE(instance);
  51. }
  52. return 0;
  53. }
  54. else
  55. {
  56. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  57. lua_error(state);
  58. }
  59. break;
  60. }
  61. default:
  62. {
  63. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  64. lua_error(state);
  65. break;
  66. }
  67. }
  68. return 0;
  69. }
  70. int lua_PhysicsRigidBodyParameters__init(lua_State* state)
  71. {
  72. // Get the number of parameters.
  73. int paramCount = lua_gettop(state);
  74. // Attempt to match the parameters to a valid binding.
  75. switch (paramCount)
  76. {
  77. case 0:
  78. {
  79. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  80. object->instance = (void*)new PhysicsRigidBody::Parameters();
  81. object->owns = true;
  82. luaL_getmetatable(state, "PhysicsRigidBodyParameters");
  83. lua_setmetatable(state, -2);
  84. return 1;
  85. break;
  86. }
  87. case 1:
  88. {
  89. if (lua_type(state, 1) == LUA_TNUMBER)
  90. {
  91. // Get parameter 1 off the stack.
  92. float param1 = (float)luaL_checknumber(state, 1);
  93. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  94. object->instance = (void*)new PhysicsRigidBody::Parameters(param1);
  95. object->owns = true;
  96. luaL_getmetatable(state, "PhysicsRigidBodyParameters");
  97. lua_setmetatable(state, -2);
  98. return 1;
  99. }
  100. else
  101. {
  102. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  103. lua_error(state);
  104. }
  105. break;
  106. }
  107. case 2:
  108. {
  109. if (lua_type(state, 1) == LUA_TNUMBER &&
  110. lua_type(state, 2) == LUA_TNUMBER)
  111. {
  112. // Get parameter 1 off the stack.
  113. float param1 = (float)luaL_checknumber(state, 1);
  114. // Get parameter 2 off the stack.
  115. float param2 = (float)luaL_checknumber(state, 2);
  116. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  117. object->instance = (void*)new PhysicsRigidBody::Parameters(param1, param2);
  118. object->owns = true;
  119. luaL_getmetatable(state, "PhysicsRigidBodyParameters");
  120. lua_setmetatable(state, -2);
  121. return 1;
  122. }
  123. else
  124. {
  125. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  126. lua_error(state);
  127. }
  128. break;
  129. }
  130. case 3:
  131. {
  132. if (lua_type(state, 1) == LUA_TNUMBER &&
  133. lua_type(state, 2) == LUA_TNUMBER &&
  134. lua_type(state, 3) == LUA_TNUMBER)
  135. {
  136. // Get parameter 1 off the stack.
  137. float param1 = (float)luaL_checknumber(state, 1);
  138. // Get parameter 2 off the stack.
  139. float param2 = (float)luaL_checknumber(state, 2);
  140. // Get parameter 3 off the stack.
  141. float param3 = (float)luaL_checknumber(state, 3);
  142. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  143. object->instance = (void*)new PhysicsRigidBody::Parameters(param1, param2, param3);
  144. object->owns = true;
  145. luaL_getmetatable(state, "PhysicsRigidBodyParameters");
  146. lua_setmetatable(state, -2);
  147. return 1;
  148. }
  149. else
  150. {
  151. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  152. lua_error(state);
  153. }
  154. break;
  155. }
  156. case 4:
  157. {
  158. if (lua_type(state, 1) == LUA_TNUMBER &&
  159. lua_type(state, 2) == LUA_TNUMBER &&
  160. lua_type(state, 3) == LUA_TNUMBER &&
  161. lua_type(state, 4) == LUA_TNUMBER)
  162. {
  163. // Get parameter 1 off the stack.
  164. float param1 = (float)luaL_checknumber(state, 1);
  165. // Get parameter 2 off the stack.
  166. float param2 = (float)luaL_checknumber(state, 2);
  167. // Get parameter 3 off the stack.
  168. float param3 = (float)luaL_checknumber(state, 3);
  169. // Get parameter 4 off the stack.
  170. float param4 = (float)luaL_checknumber(state, 4);
  171. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  172. object->instance = (void*)new PhysicsRigidBody::Parameters(param1, param2, param3, param4);
  173. object->owns = true;
  174. luaL_getmetatable(state, "PhysicsRigidBodyParameters");
  175. lua_setmetatable(state, -2);
  176. return 1;
  177. }
  178. else
  179. {
  180. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  181. lua_error(state);
  182. }
  183. break;
  184. }
  185. case 5:
  186. {
  187. if (lua_type(state, 1) == LUA_TNUMBER &&
  188. lua_type(state, 2) == LUA_TNUMBER &&
  189. lua_type(state, 3) == LUA_TNUMBER &&
  190. lua_type(state, 4) == LUA_TNUMBER &&
  191. lua_type(state, 5) == LUA_TNUMBER)
  192. {
  193. // Get parameter 1 off the stack.
  194. float param1 = (float)luaL_checknumber(state, 1);
  195. // Get parameter 2 off the stack.
  196. float param2 = (float)luaL_checknumber(state, 2);
  197. // Get parameter 3 off the stack.
  198. float param3 = (float)luaL_checknumber(state, 3);
  199. // Get parameter 4 off the stack.
  200. float param4 = (float)luaL_checknumber(state, 4);
  201. // Get parameter 5 off the stack.
  202. float param5 = (float)luaL_checknumber(state, 5);
  203. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  204. object->instance = (void*)new PhysicsRigidBody::Parameters(param1, param2, param3, param4, param5);
  205. object->owns = true;
  206. luaL_getmetatable(state, "PhysicsRigidBodyParameters");
  207. lua_setmetatable(state, -2);
  208. return 1;
  209. }
  210. else
  211. {
  212. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  213. lua_error(state);
  214. }
  215. break;
  216. }
  217. case 6:
  218. {
  219. if (lua_type(state, 1) == LUA_TNUMBER &&
  220. lua_type(state, 2) == LUA_TNUMBER &&
  221. lua_type(state, 3) == LUA_TNUMBER &&
  222. lua_type(state, 4) == LUA_TNUMBER &&
  223. lua_type(state, 5) == LUA_TNUMBER &&
  224. lua_type(state, 6) == LUA_TBOOLEAN)
  225. {
  226. // Get parameter 1 off the stack.
  227. float param1 = (float)luaL_checknumber(state, 1);
  228. // Get parameter 2 off the stack.
  229. float param2 = (float)luaL_checknumber(state, 2);
  230. // Get parameter 3 off the stack.
  231. float param3 = (float)luaL_checknumber(state, 3);
  232. // Get parameter 4 off the stack.
  233. float param4 = (float)luaL_checknumber(state, 4);
  234. // Get parameter 5 off the stack.
  235. float param5 = (float)luaL_checknumber(state, 5);
  236. // Get parameter 6 off the stack.
  237. bool param6 = (luaL_checkint(state, 6) != 0);
  238. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  239. object->instance = (void*)new PhysicsRigidBody::Parameters(param1, param2, param3, param4, param5, param6);
  240. object->owns = true;
  241. luaL_getmetatable(state, "PhysicsRigidBodyParameters");
  242. lua_setmetatable(state, -2);
  243. return 1;
  244. }
  245. else
  246. {
  247. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  248. lua_error(state);
  249. }
  250. break;
  251. }
  252. case 7:
  253. {
  254. if (lua_type(state, 1) == LUA_TNUMBER &&
  255. lua_type(state, 2) == LUA_TNUMBER &&
  256. lua_type(state, 3) == LUA_TNUMBER &&
  257. lua_type(state, 4) == LUA_TNUMBER &&
  258. lua_type(state, 5) == LUA_TNUMBER &&
  259. lua_type(state, 6) == LUA_TBOOLEAN &&
  260. lua_type(state, 7) == LUA_TUSERDATA)
  261. {
  262. // Get parameter 1 off the stack.
  263. float param1 = (float)luaL_checknumber(state, 1);
  264. // Get parameter 2 off the stack.
  265. float param2 = (float)luaL_checknumber(state, 2);
  266. // Get parameter 3 off the stack.
  267. float param3 = (float)luaL_checknumber(state, 3);
  268. // Get parameter 4 off the stack.
  269. float param4 = (float)luaL_checknumber(state, 4);
  270. // Get parameter 5 off the stack.
  271. float param5 = (float)luaL_checknumber(state, 5);
  272. // Get parameter 6 off the stack.
  273. bool param6 = (luaL_checkint(state, 6) != 0);
  274. // Get parameter 7 off the stack.
  275. void* userdata7 = ScriptController::getInstance()->getObjectPointer(7, "Vector3");
  276. if (!userdata7)
  277. {
  278. lua_pushstring(state, "Failed to retrieve a valid object pointer of type 'Vector3' for parameter 7.");
  279. lua_error(state);
  280. }
  281. Vector3* param7 = (Vector3*)((ScriptController::LuaObject*)userdata7)->instance;
  282. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  283. object->instance = (void*)new PhysicsRigidBody::Parameters(param1, param2, param3, param4, param5, param6, *param7);
  284. object->owns = true;
  285. luaL_getmetatable(state, "PhysicsRigidBodyParameters");
  286. lua_setmetatable(state, -2);
  287. return 1;
  288. }
  289. else
  290. {
  291. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  292. lua_error(state);
  293. }
  294. break;
  295. }
  296. default:
  297. {
  298. lua_pushstring(state, "Invalid number of parameters (expected 0, 1, 2, 3, 4, 5, 6 or 7).");
  299. lua_error(state);
  300. break;
  301. }
  302. }
  303. return 0;
  304. }
  305. int lua_PhysicsRigidBodyParameters_angularDamping(lua_State* state)
  306. {
  307. // Validate the number of parameters.
  308. if (lua_gettop(state) > 2)
  309. {
  310. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  311. lua_error(state);
  312. }
  313. PhysicsRigidBody::Parameters* instance = getInstance(state);
  314. if (lua_gettop(state) == 2)
  315. {
  316. // Get parameter 2 off the stack.
  317. float param2 = (float)luaL_checknumber(state, 2);
  318. instance->angularDamping = param2;
  319. return 0;
  320. }
  321. else
  322. {
  323. float result = instance->angularDamping;
  324. // Push the return value onto the stack.
  325. lua_pushnumber(state, result);
  326. return 1;
  327. }
  328. }
  329. int lua_PhysicsRigidBodyParameters_anisotropicFriction(lua_State* state)
  330. {
  331. // Validate the number of parameters.
  332. if (lua_gettop(state) > 2)
  333. {
  334. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  335. lua_error(state);
  336. }
  337. PhysicsRigidBody::Parameters* instance = getInstance(state);
  338. if (lua_gettop(state) == 2)
  339. {
  340. // Get parameter 2 off the stack.
  341. void* userdata2 = ScriptController::getInstance()->getObjectPointer(2, "Vector3");
  342. if (!userdata2)
  343. {
  344. lua_pushstring(state, "Failed to retrieve a valid object pointer of type 'Vector3' for parameter 2.");
  345. lua_error(state);
  346. }
  347. Vector3* param2 = (Vector3*)((ScriptController::LuaObject*)userdata2)->instance;
  348. instance->anisotropicFriction = *param2;
  349. return 0;
  350. }
  351. else
  352. {
  353. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  354. object->instance = (void*)new Vector3(instance->anisotropicFriction);
  355. object->owns = true;
  356. luaL_getmetatable(state, "Vector3");
  357. lua_setmetatable(state, -2);
  358. return 1;
  359. }
  360. }
  361. int lua_PhysicsRigidBodyParameters_friction(lua_State* state)
  362. {
  363. // Validate the number of parameters.
  364. if (lua_gettop(state) > 2)
  365. {
  366. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  367. lua_error(state);
  368. }
  369. PhysicsRigidBody::Parameters* instance = getInstance(state);
  370. if (lua_gettop(state) == 2)
  371. {
  372. // Get parameter 2 off the stack.
  373. float param2 = (float)luaL_checknumber(state, 2);
  374. instance->friction = param2;
  375. return 0;
  376. }
  377. else
  378. {
  379. float result = instance->friction;
  380. // Push the return value onto the stack.
  381. lua_pushnumber(state, result);
  382. return 1;
  383. }
  384. }
  385. int lua_PhysicsRigidBodyParameters_kinematic(lua_State* state)
  386. {
  387. // Validate the number of parameters.
  388. if (lua_gettop(state) > 2)
  389. {
  390. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  391. lua_error(state);
  392. }
  393. PhysicsRigidBody::Parameters* instance = getInstance(state);
  394. if (lua_gettop(state) == 2)
  395. {
  396. // Get parameter 2 off the stack.
  397. bool param2 = (luaL_checkint(state, 2) != 0);
  398. instance->kinematic = param2;
  399. return 0;
  400. }
  401. else
  402. {
  403. bool result = instance->kinematic;
  404. // Push the return value onto the stack.
  405. lua_pushboolean(state, result);
  406. return 1;
  407. }
  408. }
  409. int lua_PhysicsRigidBodyParameters_linearDamping(lua_State* state)
  410. {
  411. // Validate the number of parameters.
  412. if (lua_gettop(state) > 2)
  413. {
  414. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  415. lua_error(state);
  416. }
  417. PhysicsRigidBody::Parameters* instance = getInstance(state);
  418. if (lua_gettop(state) == 2)
  419. {
  420. // Get parameter 2 off the stack.
  421. float param2 = (float)luaL_checknumber(state, 2);
  422. instance->linearDamping = param2;
  423. return 0;
  424. }
  425. else
  426. {
  427. float result = instance->linearDamping;
  428. // Push the return value onto the stack.
  429. lua_pushnumber(state, result);
  430. return 1;
  431. }
  432. }
  433. int lua_PhysicsRigidBodyParameters_mass(lua_State* state)
  434. {
  435. // Validate the number of parameters.
  436. if (lua_gettop(state) > 2)
  437. {
  438. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  439. lua_error(state);
  440. }
  441. PhysicsRigidBody::Parameters* instance = getInstance(state);
  442. if (lua_gettop(state) == 2)
  443. {
  444. // Get parameter 2 off the stack.
  445. float param2 = (float)luaL_checknumber(state, 2);
  446. instance->mass = param2;
  447. return 0;
  448. }
  449. else
  450. {
  451. float result = instance->mass;
  452. // Push the return value onto the stack.
  453. lua_pushnumber(state, result);
  454. return 1;
  455. }
  456. }
  457. int lua_PhysicsRigidBodyParameters_restitution(lua_State* state)
  458. {
  459. // Validate the number of parameters.
  460. if (lua_gettop(state) > 2)
  461. {
  462. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  463. lua_error(state);
  464. }
  465. PhysicsRigidBody::Parameters* instance = getInstance(state);
  466. if (lua_gettop(state) == 2)
  467. {
  468. // Get parameter 2 off the stack.
  469. float param2 = (float)luaL_checknumber(state, 2);
  470. instance->restitution = param2;
  471. return 0;
  472. }
  473. else
  474. {
  475. float result = instance->restitution;
  476. // Push the return value onto the stack.
  477. lua_pushnumber(state, result);
  478. return 1;
  479. }
  480. }
  481. }