Physics.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include "Physics.h"
  21. // Convex Hull Scan
  22. #include "graham/GrahamScanConvexHull.h"
  23. // LOVE
  24. #include <common/math.h>
  25. #include "wrap_Body.h"
  26. namespace love
  27. {
  28. namespace physics
  29. {
  30. namespace box2d
  31. {
  32. const char * Physics::getName() const
  33. {
  34. return "love.physics.box2d";
  35. }
  36. World * Physics::newWorld(float lx, float ly, float ux, float uy, float gx, float gy, bool sleep, int meter)
  37. {
  38. b2AABB aabb;
  39. aabb.lowerBound.Set(lx, ly);
  40. aabb.upperBound.Set(ux, uy);
  41. return new World(aabb, b2Vec2(gx, gy), sleep, meter);
  42. }
  43. Body * Physics::newBody(World * world, float x, float y, float mass, float i)
  44. {
  45. return new Body(world, b2Vec2(x, y), mass, i);
  46. }
  47. Body * Physics::newBody(World * world, float x, float y)
  48. {
  49. return new Body(world, b2Vec2(x, y), 1, 1);
  50. }
  51. Body * Physics::newBody(World * world)
  52. {
  53. return new Body(world, b2Vec2(0, 0), 1, 1);
  54. }
  55. CircleShape * Physics::newCircleShape(float radius)
  56. {
  57. return newCircleShape(0, 0, radius);
  58. }
  59. CircleShape * Physics::newCircleShape(float x, float y, float radius)
  60. {
  61. b2CircleShape s;
  62. s.m_p = b2Vec2(x, y);
  63. s.m_radius = radius;
  64. return new CircleShape(&s);
  65. }
  66. PolygonShape * Physics::newRectangleShape(float w, float h)
  67. {
  68. return newRectangleShape(0, 0, w, h, 0);
  69. }
  70. PolygonShape * Physics::newRectangleShape(float x, float y, float w, float h)
  71. {
  72. return newRectangleShape(x, y, w, h, 0);
  73. }
  74. PolygonShape * Physics::newRectangleShape(float x, float y, float w, float h, float angle)
  75. {
  76. b2PolygonShape s;
  77. s.SetAsBox(w/2.0f, h/2.0f, b2Vec2(x, y), angle);
  78. return new PolygonShape(&s);
  79. }
  80. EdgeShape * Physics::newEdgeShape(float x1, float y1, float x2, float y2)
  81. {
  82. b2EdgeShape s;
  83. s.Set(b2Vec2(x1, y1), b2Vec2(x2, y2));
  84. return new EdgeShape(&s);
  85. }
  86. int Physics::newPolygonShape(lua_State * L)
  87. {
  88. int argc = lua_gettop(L);
  89. int vcount = (int)argc/2;
  90. // 3 vertices
  91. love::luax_assert_argc(L, 2 * 3);
  92. b2PolygonShape s;
  93. std::vector<point2d> points(s.m_vertexCount);
  94. std::vector<point2d> convex_hull;
  95. for(int i = 0;i<vcount;i++)
  96. {
  97. float x = (float)lua_tonumber(L, -2);
  98. float y = (float)lua_tonumber(L, -1);
  99. point2d tmp(x, y);
  100. points.push_back(tmp);
  101. lua_pop(L, 2);
  102. }
  103. // Compute convex hull.
  104. GrahamScanConvexHull()(points, convex_hull);
  105. int count = convex_hull.size();
  106. if(count < 3)
  107. return luaL_error(L, "Polygon degenerated to less than three points.");
  108. b2Vec2 * vecs = new b2Vec2[count];
  109. for (int i = 0; i < count; i++) {
  110. vecs[i].Set(convex_hull[i].x, convex_hull[i].y);
  111. }
  112. s.Set(vecs, count);
  113. PolygonShape * p = new PolygonShape(&s);
  114. delete[] vecs;
  115. luax_newtype(L, "PolygonShape", PHYSICS_POLYGON_SHAPE_T, (void*)p);
  116. return 1;
  117. }
  118. int Physics::newChainShape(lua_State * L)
  119. {
  120. int argc = lua_gettop(L)-1; // first argument is looping
  121. int vcount = (int)argc/2;
  122. b2ChainShape s;
  123. bool loop = luax_toboolean(L, 1);
  124. b2Vec2 * vecs = new b2Vec2[vcount];
  125. for(int i = 0;i<vcount;i++)
  126. {
  127. float x = (float)lua_tonumber(L, -2);
  128. float y = (float)lua_tonumber(L, -1);
  129. vecs[i].Set(x, y);
  130. lua_pop(L, 2);
  131. }
  132. if (loop)
  133. s.CreateLoop(vecs, vcount);
  134. else
  135. s.CreateChain(vecs, vcount);
  136. ChainShape * c = new ChainShape(&s);
  137. delete[] vecs;
  138. luax_newtype(L, "ChainShape", PHYSICS_CHAIN_SHAPE_T, (void*)c);
  139. return 1;
  140. }
  141. DistanceJoint * Physics::newDistanceJoint(Body * body1, Body * body2, float x1, float y1, float x2, float y2, bool collideConnected)
  142. {
  143. return new DistanceJoint(body1, body2, x1, y1, x2, y2, collideConnected);
  144. }
  145. MouseJoint * Physics::newMouseJoint(Body * body, float x, float y)
  146. {
  147. return new MouseJoint(body, x, y);
  148. }
  149. RevoluteJoint * Physics::newRevoluteJoint(Body * body1, Body * body2, float x, float y, bool collideConnected)
  150. {
  151. return new RevoluteJoint(body1, body2, x, y, collideConnected);
  152. }
  153. PrismaticJoint * Physics::newPrismaticJoint(Body * body1, Body * body2, float x, float y, float ax, float ay, bool collideConnected)
  154. {
  155. return new PrismaticJoint(body1, body2, x, y, ax, ay, collideConnected);
  156. }
  157. PulleyJoint * Physics::newPulleyJoint(Body * body1, Body * body2, b2Vec2 groundAnchor1, b2Vec2 groundAnchor2, b2Vec2 anchor1, b2Vec2 anchor2, float ratio, bool collideConnected)
  158. {
  159. return new PulleyJoint(body1, body2, groundAnchor1, groundAnchor2, anchor1, anchor2, ratio, collideConnected);
  160. }
  161. GearJoint * Physics::newGearJoint(Joint * joint1, Joint * joint2, float ratio, bool collideConnected)
  162. {
  163. return new GearJoint(joint1, joint2, ratio, collideConnected);
  164. }
  165. FrictionJoint * Physics::newFrictionJoint(Body * body1, Body * body2, float x, float y, bool collideConnected)
  166. {
  167. return new FrictionJoint(body1, body2, x, y, collideConnected);
  168. }
  169. WeldJoint * Physics::newWeldJoint(Body * body1, Body * body2, float x, float y, bool collideConnected)
  170. {
  171. return new WeldJoint(body1, body2, x, y, collideConnected);
  172. }
  173. WheelJoint * Physics::newWheelJoint(Body * body1, Body * body2, float x, float y, float ax, float ay, bool collideConnected)
  174. {
  175. return new WheelJoint(body1, body2, x, y, ax, ay, collideConnected);
  176. }
  177. RopeJoint * Physics::newRopeJoint(Body * body1, Body * body2, float x1, float y1, float x2, float y2, float maxLength, bool collideConnected)
  178. {
  179. return new RopeJoint(body1, body2, x1, y1, x2, y2, maxLength, collideConnected);
  180. }
  181. void Physics::setMeter(int meter)
  182. {
  183. Physics::meter = meter;
  184. }
  185. int Physics::getMeter()
  186. {
  187. return meter;
  188. }
  189. void Physics::scaleDown(float & x, float & y)
  190. {
  191. x /= (float)meter;
  192. y /= (float)meter;
  193. }
  194. void Physics::scaleUp(float & x, float & y)
  195. {
  196. x *= (float)meter;
  197. y *= (float)meter;
  198. }
  199. float Physics::scaleDown(float f)
  200. {
  201. return f/(float)meter;
  202. }
  203. float Physics::scaleUp(float f)
  204. {
  205. return f*(float)meter;
  206. }
  207. b2Vec2 Physics::scaleDown(const b2Vec2 & v)
  208. {
  209. b2Vec2 t = v;
  210. scaleDown(t.x, t.y);
  211. return t;
  212. }
  213. b2Vec2 Physics::scaleUp(const b2Vec2 & v)
  214. {
  215. b2Vec2 t = v;
  216. scaleUp(t.x, t.y);
  217. return t;
  218. }
  219. b2AABB Physics::scaleDown(const b2AABB & aabb)
  220. {
  221. b2AABB t;
  222. t.lowerBound = scaleDown(aabb.lowerBound);
  223. t.upperBound = scaleDown(aabb.upperBound);
  224. return t;
  225. }
  226. b2AABB Physics::scaleUp(const b2AABB & aabb)
  227. {
  228. b2AABB t;
  229. t.lowerBound = scaleUp(aabb.lowerBound);
  230. t.upperBound = scaleUp(aabb.upperBound);
  231. return t;
  232. }
  233. } // box2d
  234. } // physics
  235. } // love