physics.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. -- love.physics
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. ----------------------------------OBJECTS---------------------------------------
  5. --------------------------------------------------------------------------------
  6. --------------------------------------------------------------------------------
  7. -- Body (love.physics.newBody)
  8. love.test.physics.Body = function(test)
  9. test:skipTest('test class needs writing')
  10. end
  11. -- Contact (love.physics.World:getContacts)
  12. love.test.physics.Contact = function(test)
  13. test:skipTest('test class needs writing')
  14. end
  15. -- Joint (love.physics.newDistanceJoint)
  16. love.test.physics.Joint = function(test)
  17. -- make joint
  18. local world = love.physics.newWorld(1, 1, true)
  19. local body1 = love.physics.newBody(world, 10, 10, 'dynamic')
  20. local body2 = love.physics.newBody(world, 20, 20, 'dynamic')
  21. local joint = love.physics.newDistanceJoint(body1, body2, 10, 10, 20, 20, true)
  22. test:assertObject(joint)
  23. -- check props
  24. test:assertEquals('distance', joint:getType(), 'check joint type')
  25. test:assertEquals(false, joint:isDestroyed(), 'check not destroyed')
  26. test:assertEquals(0, joint:getReactionForce(1), 'check reaction force')
  27. test:assertEquals(0, joint:getReactionTorque(1), 'check reaction torque')
  28. local b1, b2 = joint:getBodies()
  29. test:assertEquals(body1:getX(), b1:getX(), 'check body 1')
  30. test:assertEquals(body2:getX(), b2:getX(), 'check body 2')
  31. local x1, y1, x2, y2 = joint:getAnchors()
  32. test:assertEquals(10, math.floor(x1), 'check anchor x1')
  33. test:assertEquals(10, math.floor(y1), 'check anchor y1')
  34. test:assertEquals(20, math.floor(x2), 'check anchor x2')
  35. test:assertEquals(20, math.floor(y2), 'check anchor y2')
  36. test:assertEquals(true, joint:getCollideConnected(), 'check not colliding')
  37. -- test userdata
  38. test:assertEquals(nil, joint:getUserData(), 'check no data by def')
  39. joint:setUserData('hello')
  40. test:assertEquals('hello', joint:getUserData(), 'check set userdata')
  41. -- destroy
  42. joint:destroy()
  43. test:assertEquals(true, joint:isDestroyed(), 'check destroyed')
  44. end
  45. -- Shape (love.physics.newCircleShape)
  46. -- @NOTE includes Fixture methods too now so enjoy
  47. love.test.physics.Shape = function(test)
  48. test:skipTest('test class needs writing')
  49. end
  50. -- World (love.physics.newWorld)
  51. love.test.physics.World = function(test)
  52. -- create new world
  53. local world = love.physics.newWorld(0, 0, false)
  54. local body1 = love.physics.newBody(world, 0, 0, 'dynamic')
  55. local rectangle1 = love.physics.newRectangleShape(body1, 0, 0, 10, 10)
  56. test:assertObject(world)
  57. -- check defaults
  58. test:assertEquals(1, #world:getBodies(), 'check 1 body')
  59. test:assertEquals(0, world:getBodies()[1]:getX(), 'check body prop x')
  60. test:assertEquals(0, world:getBodies()[1]:getY(), 'check body prop y')
  61. world:translateOrigin(-10, -10)
  62. test:assertEquals(10, math.floor(world:getBodies()[1]:getX()), 'check body prop change x')
  63. test:assertEquals(10, math.floor(world:getBodies()[1]:getY()), 'check body prop change y')
  64. test:assertEquals(1, world:getBodyCount(), 'check 1 body count')
  65. test:assertEquals(false, world:isDestroyed(), 'check not destroyed')
  66. test:assertEquals(false, world:isLocked(), 'check not updating')
  67. test:assertEquals(0, #world:getJoints(), 'check no joints')
  68. test:assertEquals(0, world:getJointCount(), 'check no joints count')
  69. test:assertEquals(0, world:getGravity(), 'check def gravity')
  70. test:assertEquals(0, #world:getContacts(), 'check no contacts')
  71. test:assertEquals(0, world:getContactCount(), 'check no contact count')
  72. test:assertEquals(false, world:isSleepingAllowed(), 'check no sleep (till brooklyn)')
  73. world:setSleepingAllowed(true)
  74. test:assertEquals(true, world:isSleepingAllowed(), 'check can sleep')
  75. -- check callbacks are called
  76. local beginContact, endContact, preSolve, postSolve = world:getCallbacks()
  77. test:assertEquals(nil, beginContact, 'check no begin contact callback')
  78. test:assertEquals(nil, endContact, 'check no end contact callback')
  79. test:assertEquals(nil, preSolve, 'check no pre solve callback')
  80. test:assertEquals(nil, postSolve, 'check no post solve callback')
  81. local beginContactCheck = false
  82. local endContactCheck = false
  83. local preSolveCheck = false
  84. local postSolveCheck = false
  85. local collisions = 0
  86. world:setCallbacks(
  87. function() beginContactCheck = true; collisions = collisions + 1 end,
  88. function() endContactCheck = true end,
  89. function() preSolveCheck = true end,
  90. function() postSolveCheck = true end
  91. )
  92. local body2 = love.physics.newBody(world, 10, 10, 'dynamic')
  93. local rectangle2 = love.physics.newRectangleShape(body2, 0, 0, 10, 10)
  94. test:assertEquals(false, beginContactCheck, 'check world didnt update after adding body')
  95. world:update(1)
  96. test:assertEquals(true, beginContactCheck, 'check contact start')
  97. test:assertEquals(true, preSolveCheck, 'check pre solve')
  98. test:assertEquals(true, postSolveCheck, 'check post solve')
  99. body2:setPosition(100, 100)
  100. world:update(1)
  101. test:assertEquals(true, endContactCheck, 'check contact end')
  102. -- check point checking
  103. local shapes = 0
  104. world:queryShapesInArea(0, 0, 10, 10, function(x)
  105. shapes = shapes + 1
  106. end)
  107. test:assertEquals(1, shapes, 'check shapes in area')
  108. world:rayCast(0, 0, 200, 200, function(x)
  109. shapes = shapes + 1
  110. return 1
  111. end)
  112. test:assertEquals(3, shapes, 'check shapes in raycast')
  113. -- change collision logic
  114. test:assertEquals(nil, world:getContactFilter(), 'check def filter')
  115. world:update(1)
  116. world:setContactFilter(function(s1, s2)
  117. return false -- nothing collides
  118. end)
  119. body2:setPosition(10, 10)
  120. world:update(1)
  121. test:assertEquals(1, collisions, 'check collision logic change')
  122. -- final bits
  123. world:setGravity(1, 1)
  124. test:assertEquals(1, world:getGravity(), 'check grav change')
  125. world:destroy()
  126. test:assertEquals(true, world:isDestroyed(), 'check world destroyed')
  127. end
  128. --------------------------------------------------------------------------------
  129. --------------------------------------------------------------------------------
  130. ------------------------------------METHODS-------------------------------------
  131. --------------------------------------------------------------------------------
  132. --------------------------------------------------------------------------------
  133. -- love.physics.getDistance
  134. love.test.physics.getDistance = function(test)
  135. -- setup two fixtues to check
  136. local world = love.physics.newWorld(0, 0, false)
  137. local body = love.physics.newBody(world, 10, 10, 'static')
  138. local shape1 = love.physics.newEdgeShape(body, 0, 0, 5, 5)
  139. local shape2 = love.physics.newEdgeShape(body, 10, 10, 15, 15)
  140. -- check distance between them
  141. test:assertEquals(647106, math.floor(love.physics.getDistance(shape1, shape2)*100000), 'check distance matches')
  142. end
  143. -- love.physics.getMeter
  144. love.test.physics.getMeter = function(test)
  145. -- check value set is returned
  146. love.physics.setMeter(30)
  147. test:assertEquals(30, love.physics.getMeter(), 'check meter matches')
  148. end
  149. -- love.physics.newBody
  150. -- @NOTE this is just basic nil checking, objs have their own test method
  151. love.test.physics.newBody = function(test)
  152. local world = love.physics.newWorld(1, 1, true)
  153. local body = love.physics.newBody(world, 10, 10, 'static')
  154. test:assertObject(body)
  155. end
  156. -- love.physics.newChainShape
  157. -- @NOTE this is just basic nil checking, objs have their own test method
  158. love.test.physics.newChainShape = function(test)
  159. local world = love.physics.newWorld(1, 1, true)
  160. local body = love.physics.newBody(world, 10, 10, 'static')
  161. test:assertObject(love.physics.newChainShape(body, true, 0, 0, 1, 0, 1, 1, 0, 1))
  162. end
  163. -- love.physics.newCircleShape
  164. -- @NOTE this is just basic nil checking, objs have their own test method
  165. love.test.physics.newCircleShape = function(test)
  166. local world = love.physics.newWorld(1, 1, true)
  167. local body = love.physics.newBody(world, 10, 10, 'static')
  168. test:assertObject(love.physics.newCircleShape(body, 10))
  169. end
  170. -- love.physics.newDistanceJoint
  171. -- @NOTE this is just basic nil checking, objs have their own test method
  172. love.test.physics.newDistanceJoint = function(test)
  173. local world = love.physics.newWorld(1, 1, true)
  174. local body1 = love.physics.newBody(world, 10, 10, 'static')
  175. local body2 = love.physics.newBody(world, 20, 20, 'static')
  176. local obj = love.physics.newDistanceJoint(body1, body2, 10, 10, 20, 20, true)
  177. test:assertObject(obj)
  178. end
  179. -- love.physics.newEdgeShape
  180. -- @NOTE this is just basic nil checking, objs have their own test method
  181. love.test.physics.newEdgeShape = function(test)
  182. local world = love.physics.newWorld(1, 1, true)
  183. local body = love.physics.newBody(world, 10, 10, 'static')
  184. local obj = love.physics.newEdgeShape(body, 0, 0, 10, 10)
  185. test:assertObject(obj)
  186. end
  187. -- love.physics.newFrictionJoint
  188. -- @NOTE this is just basic nil checking, objs have their own test method
  189. love.test.physics.newFrictionJoint = function(test)
  190. local world = love.physics.newWorld(1, 1, true)
  191. local body1 = love.physics.newBody(world, 10, 10, 'static')
  192. local body2 = love.physics.newBody(world, 20, 20, 'static')
  193. local obj = love.physics.newFrictionJoint(body1, body2, 15, 15, true)
  194. test:assertObject(obj)
  195. end
  196. -- love.physics.newGearJoint
  197. -- @NOTE this is just basic nil checking, objs have their own test method
  198. love.test.physics.newGearJoint = function(test)
  199. local world = love.physics.newWorld(1, 1, true)
  200. local body1 = love.physics.newBody(world, 10, 10, 'dynamic')
  201. local body2 = love.physics.newBody(world, 20, 20, 'dynamic')
  202. local body3 = love.physics.newBody(world, 30, 30, 'dynamic')
  203. local body4 = love.physics.newBody(world, 40, 40, 'dynamic')
  204. local joint1 = love.physics.newPrismaticJoint(body1, body2, 10, 10, 20, 20, true)
  205. local joint2 = love.physics.newPrismaticJoint(body3, body4, 30, 30, 40, 40, true)
  206. local obj = love.physics.newGearJoint(joint1, joint2, 1, true)
  207. test:assertObject(obj)
  208. end
  209. -- love.physics.newMotorJoint
  210. -- @NOTE this is just basic nil checking, objs have their own test method
  211. love.test.physics.newMotorJoint = function(test)
  212. local world = love.physics.newWorld(1, 1, true)
  213. local body1 = love.physics.newBody(world, 10, 10, 'static')
  214. local body2 = love.physics.newBody(world, 20, 20, 'static')
  215. local obj = love.physics.newMotorJoint(body1, body2, 1)
  216. test:assertObject(obj)
  217. end
  218. -- love.physics.newMouseJoint
  219. -- @NOTE this is just basic nil checking, objs have their own test method
  220. love.test.physics.newMouseJoint = function(test)
  221. local world = love.physics.newWorld(1, 1, true)
  222. local body = love.physics.newBody(world, 10, 10, 'static')
  223. local obj = love.physics.newMouseJoint(body, 10, 10)
  224. test:assertObject(obj)
  225. end
  226. -- love.physics.newPolygonShape
  227. -- @NOTE this is just basic nil checking, objs have their own test method
  228. love.test.physics.newPolygonShape = function(test)
  229. local world = love.physics.newWorld(1, 1, true)
  230. local body = love.physics.newBody(world, 10, 10, 'static')
  231. local obj = love.physics.newPolygonShape(body, {0, 0, 2, 3, 2, 1, 3, 1, 5, 1})
  232. test:assertObject(obj)
  233. end
  234. -- love.physics.newPrismaticJoint
  235. -- @NOTE this is just basic nil checking, objs have their own test method
  236. love.test.physics.newPrismaticJoint = function(test)
  237. local world = love.physics.newWorld(1, 1, true)
  238. local body1 = love.physics.newBody(world, 10, 10, 'static')
  239. local body2 = love.physics.newBody(world, 20, 20, 'static')
  240. local obj = love.physics.newPrismaticJoint(body1, body2, 10, 10, 20, 20, true)
  241. test:assertObject(obj)
  242. end
  243. -- love.physics.newPulleyJoint
  244. -- @NOTE this is just basic nil checking, objs have their own test method
  245. love.test.physics.newPulleyJoint = function(test)
  246. local world = love.physics.newWorld(1, 1, true)
  247. local body1 = love.physics.newBody(world, 10, 10, 'static')
  248. local body2 = love.physics.newBody(world, 20, 20, 'static')
  249. local obj = love.physics.newPulleyJoint(body1, body2, 10, 10, 20, 20, 15, 15, 25, 25, 1, true)
  250. test:assertObject(obj)
  251. end
  252. -- love.physics.newRectangleShape
  253. -- @NOTE this is just basic nil checking, objs have their own test method
  254. love.test.physics.newRectangleShape = function(test)
  255. local world = love.physics.newWorld(1, 1, true)
  256. local body = love.physics.newBody(world, 10, 10, 'static')
  257. local shape1 = love.physics.newRectangleShape(body, 10, 20)
  258. local shape2 = love.physics.newRectangleShape(body, 10, 10, 40, 30, 10)
  259. test:assertObject(shape1)
  260. test:assertObject(shape2)
  261. end
  262. -- love.physics.newRevoluteJoint
  263. -- @NOTE this is just basic nil checking, objs have their own test method
  264. love.test.physics.newRevoluteJoint = function(test)
  265. local world = love.physics.newWorld(1, 1, true)
  266. local body1 = love.physics.newBody(world, 10, 10, 'static')
  267. local body2 = love.physics.newBody(world, 20, 20, 'static')
  268. local obj = love.physics.newRevoluteJoint(body1, body2, 10, 10, true)
  269. test:assertObject(obj)
  270. end
  271. -- love.physics.newRopeJoint
  272. -- @NOTE this is just basic nil checking, objs have their own test method
  273. love.test.physics.newRopeJoint = function(test)
  274. local world = love.physics.newWorld(1, 1, true)
  275. local body1 = love.physics.newBody(world, 10, 10, 'static')
  276. local body2 = love.physics.newBody(world, 20, 20, 'static')
  277. local obj = love.physics.newRopeJoint(body1, body2, 10, 10, 20, 20, 50, true)
  278. test:assertObject(obj)
  279. end
  280. -- love.physics.newWeldJoint
  281. -- @NOTE this is just basic nil checking, objs have their own test method
  282. love.test.physics.newWeldJoint = function(test)
  283. local world = love.physics.newWorld(1, 1, true)
  284. local body1 = love.physics.newBody(world, 10, 10, 'static')
  285. local body2 = love.physics.newBody(world, 20, 20, 'static')
  286. local obj = love.physics.newWeldJoint(body1, body2, 10, 10, true)
  287. test:assertObject(obj)
  288. end
  289. -- love.physics.newWheelJoint
  290. -- @NOTE this is just basic nil checking, objs have their own test method
  291. love.test.physics.newWheelJoint = function(test)
  292. local world = love.physics.newWorld(1, 1, true)
  293. local body1 = love.physics.newBody(world, 10, 10, 'static')
  294. local body2 = love.physics.newBody(world, 20, 20, 'static')
  295. local obj = love.physics.newWheelJoint(body1, body2, 10, 10, 5, 5, true)
  296. test:assertObject(obj)
  297. end
  298. -- love.physics.newWorld
  299. -- @NOTE this is just basic nil checking, objs have their own test method
  300. love.test.physics.newWorld = function(test)
  301. local world = love.physics.newWorld(1, 1, true)
  302. test:assertObject(world)
  303. end
  304. -- love.physics.setMeter
  305. love.test.physics.setMeter = function(test)
  306. -- set initial meter
  307. local world = love.physics.newWorld(1, 1, true)
  308. love.physics.setMeter(30)
  309. local body = love.physics.newBody(world, 300, 300, "dynamic")
  310. -- check changing meter changes pos value relatively
  311. love.physics.setMeter(10)
  312. local x, y = body:getPosition()
  313. test:assertEquals(100, x, 'check pos x')
  314. test:assertEquals(100, y, 'check pos y')
  315. end