physics.lua 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. -- Fixture (love.physics.newFixture)
  16. love.test.physics.Fixture = function(test)
  17. test:skipTest('test class needs writing')
  18. end
  19. -- Joint (love.physics.newDistanceJoint)
  20. love.test.physics.Joint = function(test)
  21. test:skipTest('test class needs writing')
  22. end
  23. -- Shape (love.physics.newCircleShape)
  24. love.test.physics.Shape = function(test)
  25. test:skipTest('test class needs writing')
  26. end
  27. -- World (love.physics.newWorld)
  28. love.test.physics.World = function(test)
  29. test:skipTest('test class needs writing')
  30. end
  31. --------------------------------------------------------------------------------
  32. --------------------------------------------------------------------------------
  33. ------------------------------------METHODS-------------------------------------
  34. --------------------------------------------------------------------------------
  35. --------------------------------------------------------------------------------
  36. -- love.physics.getDistance
  37. love.test.physics.getDistance = function(test)
  38. -- setup two fixtues to check
  39. local shape1 = love.physics.newEdgeShape(0, 0, 5, 5)
  40. local shape2 = love.physics.newEdgeShape(10, 10, 15, 15)
  41. local world = love.physics.newWorld(0, 0, false)
  42. local body = love.physics.newBody(world, 10, 10, 'static')
  43. local fixture1 = love.physics.newFixture(body, shape1, 1)
  44. local fixture2 = love.physics.newFixture(body, shape2, 1)
  45. -- check distance between them
  46. test:assertEquals(647106, math.floor(love.physics.getDistance(fixture1, fixture2)*100000), 'check distance matches')
  47. end
  48. -- love.physics.getMeter
  49. love.test.physics.getMeter = function(test)
  50. -- check value set is returned
  51. love.physics.setMeter(30)
  52. test:assertEquals(30, love.physics.getMeter(), 'check meter matches')
  53. end
  54. -- love.physics.newBody
  55. -- @NOTE this is just basic nil checking, objs have their own test method
  56. love.test.physics.newBody = function(test)
  57. local world = love.physics.newWorld(1, 1, true)
  58. local body = love.physics.newBody(world, 10, 10, 'static')
  59. test:assertObject(body)
  60. end
  61. -- love.physics.newChainShape
  62. -- @NOTE this is just basic nil checking, objs have their own test method
  63. love.test.physics.newChainShape = function(test)
  64. test:assertObject(love.physics.newChainShape(true, 0, 0, 1, 0, 1, 1, 0, 1))
  65. end
  66. -- love.physics.newCircleShape
  67. -- @NOTE this is just basic nil checking, objs have their own test method
  68. love.test.physics.newCircleShape = function(test)
  69. test:assertObject(love.physics.newCircleShape(10))
  70. end
  71. -- love.physics.newDistanceJoint
  72. -- @NOTE this is just basic nil checking, objs have their own test method
  73. love.test.physics.newDistanceJoint = function(test)
  74. local world = love.physics.newWorld(1, 1, true)
  75. local body1 = love.physics.newBody(world, 10, 10, 'static')
  76. local body2 = love.physics.newBody(world, 20, 20, 'static')
  77. local obj = love.physics.newDistanceJoint(body1, body2, 10, 10, 20, 20, true)
  78. test:assertObject(obj)
  79. end
  80. -- love.physics.newEdgeShape
  81. -- @NOTE this is just basic nil checking, objs have their own test method
  82. love.test.physics.newEdgeShape = function(test)
  83. local obj = love.physics.newEdgeShape(0, 0, 10, 10)
  84. test:assertObject(obj)
  85. end
  86. -- love.physics.newFixture
  87. -- @NOTE this is just basic nil checking, objs have their own test method
  88. love.test.physics.newFixture = function(test)
  89. local world = love.physics.newWorld(1, 1, true)
  90. local body = love.physics.newBody(world, 10, 10, 'static')
  91. local shape = love.physics.newCircleShape(10)
  92. local obj = love.physics.newFixture(body, shape, 1)
  93. test:assertObject(obj)
  94. end
  95. -- love.physics.newFrictionJoint
  96. -- @NOTE this is just basic nil checking, objs have their own test method
  97. love.test.physics.newFrictionJoint = function(test)
  98. local world = love.physics.newWorld(1, 1, true)
  99. local body1 = love.physics.newBody(world, 10, 10, 'static')
  100. local body2 = love.physics.newBody(world, 20, 20, 'static')
  101. local obj = love.physics.newFrictionJoint(body1, body2, 15, 15, true)
  102. test:assertObject(obj)
  103. end
  104. -- love.physics.newGearJoint
  105. -- @NOTE this is just basic nil checking, objs have their own test method
  106. love.test.physics.newGearJoint = function(test)
  107. local world = love.physics.newWorld(1, 1, true)
  108. local body1 = love.physics.newBody(world, 10, 10, 'dynamic')
  109. local body2 = love.physics.newBody(world, 20, 20, 'dynamic')
  110. local body3 = love.physics.newBody(world, 30, 30, 'dynamic')
  111. local body4 = love.physics.newBody(world, 40, 40, 'dynamic')
  112. local joint1 = love.physics.newPrismaticJoint(body1, body2, 10, 10, 20, 20, true)
  113. local joint2 = love.physics.newPrismaticJoint(body3, body4, 30, 30, 40, 40, true)
  114. local obj = love.physics.newGearJoint(joint1, joint2, 1, true)
  115. test:assertObject(obj)
  116. end
  117. -- love.physics.newMotorJoint
  118. -- @NOTE this is just basic nil checking, objs have their own test method
  119. love.test.physics.newMotorJoint = function(test)
  120. local world = love.physics.newWorld(1, 1, true)
  121. local body1 = love.physics.newBody(world, 10, 10, 'static')
  122. local body2 = love.physics.newBody(world, 20, 20, 'static')
  123. local obj = love.physics.newMotorJoint(body1, body2, 1)
  124. test:assertObject(obj)
  125. end
  126. -- love.physics.newMouseJoint
  127. -- @NOTE this is just basic nil checking, objs have their own test method
  128. love.test.physics.newMouseJoint = function(test)
  129. local world = love.physics.newWorld(1, 1, true)
  130. local body = love.physics.newBody(world, 10, 10, 'static')
  131. local obj = love.physics.newMouseJoint(body, 10, 10)
  132. test:assertObject(obj)
  133. end
  134. -- love.physics.newPolygonShape
  135. -- @NOTE this is just basic nil checking, objs have their own test method
  136. love.test.physics.newPolygonShape = function(test)
  137. local obj = love.physics.newPolygonShape({0, 0, 2, 3, 2, 1, 3, 1, 5, 1})
  138. test:assertObject(obj)
  139. end
  140. -- love.physics.newPrismaticJoint
  141. -- @NOTE this is just basic nil checking, objs have their own test method
  142. love.test.physics.newPrismaticJoint = function(test)
  143. local world = love.physics.newWorld(1, 1, true)
  144. local body1 = love.physics.newBody(world, 10, 10, 'static')
  145. local body2 = love.physics.newBody(world, 20, 20, 'static')
  146. local obj = love.physics.newPrismaticJoint(body1, body2, 10, 10, 20, 20, true)
  147. test:assertObject(obj)
  148. end
  149. -- love.physics.newPulleyJoint
  150. -- @NOTE this is just basic nil checking, objs have their own test method
  151. love.test.physics.newPulleyJoint = function(test)
  152. local world = love.physics.newWorld(1, 1, true)
  153. local body1 = love.physics.newBody(world, 10, 10, 'static')
  154. local body2 = love.physics.newBody(world, 20, 20, 'static')
  155. local obj = love.physics.newPulleyJoint(body1, body2, 10, 10, 20, 20, 15, 15, 25, 25, 1, true)
  156. test:assertObject(obj)
  157. end
  158. -- love.physics.newRectangleShape
  159. -- @NOTE this is just basic nil checking, objs have their own test method
  160. love.test.physics.newRectangleShape = function(test)
  161. local shape1 = love.physics.newRectangleShape(10, 20)
  162. local shape2 = love.physics.newRectangleShape(10, 10, 40, 30, 10)
  163. test:assertObject(shape1)
  164. test:assertObject(shape2)
  165. end
  166. -- love.physics.newRevoluteJoint
  167. -- @NOTE this is just basic nil checking, objs have their own test method
  168. love.test.physics.newRevoluteJoint = function(test)
  169. local world = love.physics.newWorld(1, 1, true)
  170. local body1 = love.physics.newBody(world, 10, 10, 'static')
  171. local body2 = love.physics.newBody(world, 20, 20, 'static')
  172. local obj = love.physics.newRevoluteJoint(body1, body2, 10, 10, true)
  173. test:assertObject(obj)
  174. end
  175. -- love.physics.newRopeJoint
  176. -- @NOTE this is just basic nil checking, objs have their own test method
  177. love.test.physics.newRopeJoint = function(test)
  178. local world = love.physics.newWorld(1, 1, true)
  179. local body1 = love.physics.newBody(world, 10, 10, 'static')
  180. local body2 = love.physics.newBody(world, 20, 20, 'static')
  181. local obj = love.physics.newRopeJoint(body1, body2, 10, 10, 20, 20, 50, true)
  182. test:assertObject(obj)
  183. end
  184. -- love.physics.newWeldJoint
  185. -- @NOTE this is just basic nil checking, objs have their own test method
  186. love.test.physics.newWeldJoint = function(test)
  187. local world = love.physics.newWorld(1, 1, true)
  188. local body1 = love.physics.newBody(world, 10, 10, 'static')
  189. local body2 = love.physics.newBody(world, 20, 20, 'static')
  190. local obj = love.physics.newWeldJoint(body1, body2, 10, 10, true)
  191. test:assertObject(obj)
  192. end
  193. -- love.physics.newWheelJoint
  194. -- @NOTE this is just basic nil checking, objs have their own test method
  195. love.test.physics.newWheelJoint = function(test)
  196. local world = love.physics.newWorld(1, 1, true)
  197. local body1 = love.physics.newBody(world, 10, 10, 'static')
  198. local body2 = love.physics.newBody(world, 20, 20, 'static')
  199. local obj = love.physics.newWheelJoint(body1, body2, 10, 10, 5, 5, true)
  200. test:assertObject(obj)
  201. end
  202. -- love.physics.newWorld
  203. -- @NOTE this is just basic nil checking, objs have their own test method
  204. love.test.physics.newWorld = function(test)
  205. local world = love.physics.newWorld(1, 1, true)
  206. test:assertObject(world)
  207. end
  208. -- love.physics.setMeter
  209. love.test.physics.setMeter = function(test)
  210. -- set initial meter
  211. local world = love.physics.newWorld(1, 1, true)
  212. love.physics.setMeter(30)
  213. local body = love.physics.newBody(world, 300, 300, "dynamic")
  214. -- check changing meter changes pos value relatively
  215. love.physics.setMeter(10)
  216. local x, y = body:getPosition()
  217. test:assertEquals(100, x, 'check pos x')
  218. test:assertEquals(100, y, 'check pos y')
  219. end