Physics.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * Copyright (c) 2006-2016 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 "Physics.h"
  21. // LOVE
  22. #include "common/math.h"
  23. #include "wrap_Body.h"
  24. namespace love
  25. {
  26. namespace physics
  27. {
  28. namespace box2d
  29. {
  30. int Physics::meter = Physics::DEFAULT_METER;
  31. const char *Physics::getName() const
  32. {
  33. return "love.physics.box2d";
  34. }
  35. World *Physics::newWorld(float gx, float gy, bool sleep)
  36. {
  37. return new World(b2Vec2(gx, gy), sleep);
  38. }
  39. Body *Physics::newBody(World *world, float x, float y, Body::Type type)
  40. {
  41. return new Body(world, b2Vec2(x, y), type);
  42. }
  43. Body *Physics::newBody(World *world, Body::Type type)
  44. {
  45. return new Body(world, b2Vec2(0, 0), type);
  46. }
  47. CircleShape *Physics::newCircleShape(float radius)
  48. {
  49. return newCircleShape(0, 0, radius);
  50. }
  51. CircleShape *Physics::newCircleShape(float x, float y, float radius)
  52. {
  53. b2CircleShape *s = new b2CircleShape();
  54. s->m_p = Physics::scaleDown(b2Vec2(x, y));
  55. s->m_radius = Physics::scaleDown(radius);
  56. return new CircleShape(s);
  57. }
  58. PolygonShape *Physics::newRectangleShape(float w, float h)
  59. {
  60. return newRectangleShape(0, 0, w, h, 0);
  61. }
  62. PolygonShape *Physics::newRectangleShape(float x, float y, float w, float h)
  63. {
  64. return newRectangleShape(x, y, w, h, 0);
  65. }
  66. PolygonShape *Physics::newRectangleShape(float x, float y, float w, float h, float angle)
  67. {
  68. b2PolygonShape *s = new b2PolygonShape();
  69. s->SetAsBox(Physics::scaleDown(w/2.0f), Physics::scaleDown(h/2.0f), Physics::scaleDown(b2Vec2(x, y)), angle);
  70. return new PolygonShape(s);
  71. }
  72. EdgeShape *Physics::newEdgeShape(float x1, float y1, float x2, float y2)
  73. {
  74. b2EdgeShape *s = new b2EdgeShape();
  75. s->Set(Physics::scaleDown(b2Vec2(x1, y1)), Physics::scaleDown(b2Vec2(x2, y2)));
  76. return new EdgeShape(s);
  77. }
  78. int Physics::newPolygonShape(lua_State *L)
  79. {
  80. int argc = lua_gettop(L);
  81. bool istable = lua_istable(L, 1);
  82. if (istable)
  83. argc = (int) luax_objlen(L, 1);
  84. if (argc % 2 != 0)
  85. return luaL_error(L, "Number of vertex components must be a multiple of two.");
  86. // 3 to 8 (b2_maxPolygonVertices) vertices
  87. int vcount = argc / 2;
  88. if (vcount < 3)
  89. return luaL_error(L, "Expected a minimum of 3 vertices, got %d.", vcount);
  90. else if (vcount > b2_maxPolygonVertices)
  91. return luaL_error(L, "Expected a maximum of %d vertices, got %d.", b2_maxPolygonVertices, vcount);
  92. b2Vec2 vecs[b2_maxPolygonVertices];
  93. if (istable)
  94. {
  95. for (int i = 0; i < vcount; i++)
  96. {
  97. lua_rawgeti(L, 1, 1 + i * 2);
  98. lua_rawgeti(L, 1, 2 + i * 2);
  99. float x = (float)luaL_checknumber(L, -2);
  100. float y = (float)luaL_checknumber(L, -1);
  101. vecs[i] = Physics::scaleDown(b2Vec2(x, y));
  102. lua_pop(L, 2);
  103. }
  104. }
  105. else
  106. {
  107. for (int i = 0; i < vcount; i++)
  108. {
  109. float x = (float)luaL_checknumber(L, 1 + i * 2);
  110. float y = (float)luaL_checknumber(L, 2 + i * 2);
  111. vecs[i] = Physics::scaleDown(b2Vec2(x, y));
  112. }
  113. }
  114. b2PolygonShape *s = new b2PolygonShape();
  115. try
  116. {
  117. s->Set(vecs, vcount);
  118. }
  119. catch (love::Exception &)
  120. {
  121. delete s;
  122. throw;
  123. }
  124. PolygonShape *p = new PolygonShape(s);
  125. luax_pushtype(L, PHYSICS_POLYGON_SHAPE_ID, p);
  126. p->release();
  127. return 1;
  128. }
  129. int Physics::newChainShape(lua_State *L)
  130. {
  131. int argc = lua_gettop(L)-1; // first argument is looping
  132. bool istable = lua_istable(L, 2);
  133. if (istable)
  134. argc = (int) luax_objlen(L, 2);
  135. if (argc % 2 != 0)
  136. return luaL_error(L, "Number of vertex components must be a multiple of two.");
  137. int vcount = argc/2;
  138. bool loop = luax_toboolean(L, 1);
  139. b2Vec2 *vecs = new b2Vec2[vcount];
  140. if (istable)
  141. {
  142. for (int i = 0; i < vcount; i++)
  143. {
  144. lua_rawgeti(L, 2, 1 + i * 2);
  145. lua_rawgeti(L, 2, 2 + i * 2);
  146. float x = (float)lua_tonumber(L, -2);
  147. float y = (float)lua_tonumber(L, -1);
  148. vecs[i] = Physics::scaleDown(b2Vec2(x, y));
  149. lua_pop(L, 2);
  150. }
  151. }
  152. else
  153. {
  154. for (int i = 0; i < vcount; i++)
  155. {
  156. float x = (float)luaL_checknumber(L, 2 + i * 2);
  157. float y = (float)luaL_checknumber(L, 3 + i * 2);
  158. vecs[i] = Physics::scaleDown(b2Vec2(x, y));
  159. }
  160. }
  161. b2ChainShape *s = new b2ChainShape();
  162. try
  163. {
  164. if (loop)
  165. s->CreateLoop(vecs, vcount);
  166. else
  167. s->CreateChain(vecs, vcount);
  168. }
  169. catch (love::Exception &)
  170. {
  171. delete[] vecs;
  172. delete s;
  173. throw;
  174. }
  175. delete[] vecs;
  176. ChainShape *c = new ChainShape(s);
  177. luax_pushtype(L, PHYSICS_CHAIN_SHAPE_ID, c);
  178. c->release();
  179. return 1;
  180. }
  181. DistanceJoint *Physics::newDistanceJoint(Body *body1, Body *body2, float x1, float y1, float x2, float y2, bool collideConnected)
  182. {
  183. return new DistanceJoint(body1, body2, x1, y1, x2, y2, collideConnected);
  184. }
  185. MouseJoint *Physics::newMouseJoint(Body *body, float x, float y)
  186. {
  187. return new MouseJoint(body, x, y);
  188. }
  189. RevoluteJoint *Physics::newRevoluteJoint(Body *body1, Body *body2, float x, float y, bool collideConnected)
  190. {
  191. return new RevoluteJoint(body1, body2, x, y, collideConnected);
  192. }
  193. PrismaticJoint *Physics::newPrismaticJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected)
  194. {
  195. return new PrismaticJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);
  196. }
  197. PulleyJoint *Physics::newPulleyJoint(Body *body1, Body *body2, b2Vec2 groundAnchor1, b2Vec2 groundAnchor2, b2Vec2 anchor1, b2Vec2 anchor2, float ratio, bool collideConnected)
  198. {
  199. return new PulleyJoint(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, ratio, collideConnected);
  200. }
  201. GearJoint *Physics::newGearJoint(Joint *joint1, Joint *joint2, float ratio, bool collideConnected)
  202. {
  203. return new GearJoint(joint1, joint2, ratio, collideConnected);
  204. }
  205. FrictionJoint *Physics::newFrictionJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected)
  206. {
  207. return new FrictionJoint(body1, body2, xA, yA, xB, yB, collideConnected);
  208. }
  209. WeldJoint *Physics::newWeldJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, bool collideConnected)
  210. {
  211. return new WeldJoint(body1, body2, xA, yA, xB, yB, collideConnected);
  212. }
  213. WheelJoint *Physics::newWheelJoint(Body *body1, Body *body2, float xA, float yA, float xB, float yB, float ax, float ay, bool collideConnected)
  214. {
  215. return new WheelJoint(body1, body2, xA, yA, xB, yB, ax, ay, collideConnected);
  216. }
  217. RopeJoint *Physics::newRopeJoint(Body *body1, Body *body2, float x1, float y1, float x2, float y2, float maxLength, bool collideConnected)
  218. {
  219. return new RopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected);
  220. }
  221. MotorJoint *Physics::newMotorJoint(Body *body1, Body *body2)
  222. {
  223. return new MotorJoint(body1, body2);
  224. }
  225. MotorJoint *Physics::newMotorJoint(Body *body1, Body *body2, float correctionFactor, bool collideConnected)
  226. {
  227. return new MotorJoint(body1, body2, correctionFactor, collideConnected);
  228. }
  229. Fixture *Physics::newFixture(Body *body, Shape *shape, float density)
  230. {
  231. return new Fixture(body, shape, density);
  232. }
  233. int Physics::getDistance(lua_State *L)
  234. {
  235. Fixture *fixtureA = luax_checktype<Fixture>(L, 1, PHYSICS_FIXTURE_ID);
  236. Fixture *fixtureB = luax_checktype<Fixture>(L, 2, PHYSICS_FIXTURE_ID);
  237. b2DistanceProxy pA, pB;
  238. b2DistanceInput i;
  239. b2DistanceOutput o;
  240. b2SimplexCache c;
  241. c.count = 0;
  242. luax_catchexcept(L, [&]() {
  243. pA.Set(fixtureA->fixture->GetShape(), 0);
  244. pB.Set(fixtureB->fixture->GetShape(), 0);
  245. i.proxyA = pA;
  246. i.proxyB = pB;
  247. i.transformA = fixtureA->fixture->GetBody()->GetTransform();
  248. i.transformB = fixtureB->fixture->GetBody()->GetTransform();
  249. i.useRadii = true;
  250. b2Distance(&o, &c, &i);
  251. });
  252. lua_pushnumber(L, Physics::scaleUp(o.distance));
  253. lua_pushnumber(L, Physics::scaleUp(o.pointA.x));
  254. lua_pushnumber(L, Physics::scaleUp(o.pointA.y));
  255. lua_pushnumber(L, Physics::scaleUp(o.pointB.x));
  256. lua_pushnumber(L, Physics::scaleUp(o.pointB.y));
  257. return 5;
  258. }
  259. void Physics::setMeter(int scale)
  260. {
  261. if (scale < 1) throw love::Exception("Physics error: invalid meter");
  262. Physics::meter = scale;
  263. }
  264. int Physics::getMeter()
  265. {
  266. return meter;
  267. }
  268. void Physics::scaleDown(float &x, float &y)
  269. {
  270. x /= (float)meter;
  271. y /= (float)meter;
  272. }
  273. void Physics::scaleUp(float &x, float &y)
  274. {
  275. x *= (float)meter;
  276. y *= (float)meter;
  277. }
  278. float Physics::scaleDown(float f)
  279. {
  280. return f/(float)meter;
  281. }
  282. float Physics::scaleUp(float f)
  283. {
  284. return f*(float)meter;
  285. }
  286. b2Vec2 Physics::scaleDown(const b2Vec2 &v)
  287. {
  288. b2Vec2 t = v;
  289. scaleDown(t.x, t.y);
  290. return t;
  291. }
  292. b2Vec2 Physics::scaleUp(const b2Vec2 &v)
  293. {
  294. b2Vec2 t = v;
  295. scaleUp(t.x, t.y);
  296. return t;
  297. }
  298. b2AABB Physics::scaleDown(const b2AABB &aabb)
  299. {
  300. b2AABB t;
  301. t.lowerBound = scaleDown(aabb.lowerBound);
  302. t.upperBound = scaleDown(aabb.upperBound);
  303. return t;
  304. }
  305. b2AABB Physics::scaleUp(const b2AABB &aabb)
  306. {
  307. b2AABB t;
  308. t.lowerBound = scaleUp(aabb.lowerBound);
  309. t.upperBound = scaleUp(aabb.upperBound);
  310. return t;
  311. }
  312. } // box2d
  313. } // physics
  314. } // love