physics.lua 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. -- love.physics
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. ----------------------------------OBJECTS---------------------------------------
  5. --------------------------------------------------------------------------------
  6. --------------------------------------------------------------------------------
  7. -- Body (love.physics.newBody)
  8. love.test.physics.Body = function(test)
  9. -- create body
  10. local world = love.physics.newWorld(1, 1, true)
  11. local body1 = love.physics.newBody(world, 0, 0, 'static')
  12. local body2 = love.physics.newBody(world, 30, 30, 'dynamic')
  13. love.physics.newRectangleShape(body1, 5, 5, 10, 10)
  14. love.physics.newRectangleShape(body2, 5, 5, 10, 10)
  15. test:assertObject(body1)
  16. -- check state properties
  17. test:assertEquals(true, body1:isActive(), 'check active by def')
  18. test:assertEquals(false, body1:isBullet(), 'check not bullet by def')
  19. body1:setBullet(true)
  20. test:assertEquals(true, body1:isBullet(), 'check set bullet')
  21. test:assertEquals(false, body1:isFixedRotation(), 'check fix rot def')
  22. body1:setFixedRotation(true)
  23. test:assertEquals(true, body1:isFixedRotation(), 'check set fix rot')
  24. test:assertEquals(true, body1:isSleepingAllowed(), 'check sleep def')
  25. body1:setSleepingAllowed(false)
  26. test:assertEquals(false, body1:isSleepingAllowed(), 'check set sleep')
  27. body1:setSleepingAllowed(true)
  28. world:update(1)
  29. test:assertEquals(false, body1:isAwake(), 'check fell asleep')
  30. body1:setSleepingAllowed(false)
  31. body1:setType('dynamic')
  32. test:assertEquals(true, body1:isAwake(), 'check waking up')
  33. test:assertEquals(false, body1:isTouching(body2))
  34. body2:setPosition(5, 5)
  35. world:update(1)
  36. test:assertEquals(true, body1:isTouching(body2))
  37. -- check body properties
  38. test:assertEquals(1, #body1:getContacts(), 'check contact list')
  39. test:assertEquals(0, #body1:getJoints(), 'check joints list')
  40. love.physics.newDistanceJoint(body1, body2, 5, 5, 10, 10, true)
  41. test:assertEquals(1, #body1:getJoints(), 'check joints list')
  42. local x, y = body1:getLocalCenter()
  43. test:assertEquals(5, math.floor(x), 'check local center x')
  44. test:assertEquals(5, math.floor(y), 'check local center y')
  45. local lx, ly = body1:getLocalPoint(10, 10)
  46. test:assertEquals(10, math.floor(lx), 'check local point x')
  47. test:assertEquals(9, math.floor(ly), 'check local point y')
  48. local lx1, ly1, lx2, ly2 = body1:getLocalPoints(0, 5, 5, 10)
  49. test:assertEquals(0, math.floor(lx1), 'check local points x 1')
  50. test:assertEquals(4, math.floor(ly1), 'check local points y 1')
  51. test:assertEquals(5, math.floor(lx2), 'check local points x 2')
  52. test:assertEquals(9, math.floor(ly2), 'check local points y 2')
  53. local wx, wy = body1:getWorldPoint(10.4, 9)
  54. test:assertEquals(10, math.floor(wx), 'check world point x')
  55. test:assertEquals(10, math.floor(wy), 'check world point y')
  56. local wx1, wy1, wx2, wy2 = body1:getWorldPoints(0.4, 4, 5.4, 9)
  57. test:assertEquals(0, math.floor(wx1), 'check world points x 1')
  58. test:assertEquals(5, math.floor(wy1), 'check world points y 1')
  59. test:assertEquals(5, math.floor(wx2), 'check world points x 2')
  60. test:assertEquals(10, math.floor(wy2), 'check world points y 2')
  61. test:assertEquals(0, body1:getAngularDamping(), 'check angular damping')
  62. test:assertEquals(0, body1:getAngularVelocity(), 'check angular velocity')
  63. test:assertObject(body1:getWorld())
  64. test:assertEquals(2, body1:getWorld():getBodyCount(), 'check world count')
  65. local cx, cy = body1:getWorldCenter()
  66. test:assertEquals(46, math.floor(cx*10), 'check world center x')
  67. test:assertEquals(60, math.floor(cy*10), 'check world center y')
  68. local vx, vy = body1:getWorldVector(5, 10)
  69. test:assertEquals(5, vx, 'check vector x')
  70. test:assertEquals(10, vy, 'check vector y')
  71. test:assertEquals(555, math.floor(body1:getInertia()*100), 'check inertia')
  72. -- check get/set properties
  73. test:assertEquals(0, body1:getAngle(), 'check def angle')
  74. body1:setAngle(90 * (math.pi/180))
  75. test:assertEquals(math.floor(math.pi/2*100), math.floor(body1:getAngle()*100), 'check set angle')
  76. test:assertEquals(1, body1:getGravityScale(), 'check def grav')
  77. body1:setGravityScale(2)
  78. test:assertEquals(2, body1:getGravityScale(), 'check change grav')
  79. test:assertEquals(0, body1:getLinearDamping(), 'check def lin damping')
  80. body1:setLinearDamping(0.1)
  81. test:assertEquals(1, math.floor(body1:getLinearDamping()*10), 'check change lin damping')
  82. x, y = body1:getLinearVelocity()
  83. test:assertEquals(1, x, 'check def lin velocity x')
  84. test:assertEquals(1, y, 'check def lin velocity y')
  85. body1:setLinearVelocity(4, 5)
  86. x, y = body1:getLinearVelocity()
  87. test:assertEquals(4, x, 'check change lin velocity x')
  88. test:assertEquals(5, y, 'check change lin velocity y')
  89. test:assertEquals(1, math.floor(body1:getMass()*10), 'check def mass')
  90. body1:setMass(10)
  91. test:assertEquals(10, body1:getMass(), 'check change mass')
  92. body1:setMassData(3, 5, 10, 1)
  93. local x, y, mass, inertia = body1:getMassData()
  94. test:assertEquals(3, x, 'check mass data change x')
  95. test:assertEquals(5, y, 'check mass data change y')
  96. test:assertEquals(10, mass, 'check mass data change mass')
  97. test:assertEquals(340, math.floor(inertia), 'check mass data change inertia')
  98. body1:resetMassData()
  99. x, y, mass, inertia = body1:getMassData()
  100. test:assertEquals(5, math.floor(x), 'check mass data reset x')
  101. test:assertEquals(5, math.floor(y), 'check mass data reset y')
  102. test:assertEquals(1, math.floor(mass*10), 'check mass data reset mass')
  103. test:assertEquals(5, math.floor(inertia), 'check mass data reset inertia')
  104. x, y = body1:getPosition()
  105. test:assertEquals(-1, math.floor(x), 'check position x')
  106. test:assertEquals(0, math.floor(y), 'check position y')
  107. body1:setPosition(10, 4)
  108. x, y = body1:getPosition()
  109. test:assertEquals(10, math.floor(x), 'check set position x')
  110. test:assertEquals(4, math.floor(y), 'check set position y')
  111. test:assertEquals('dynamic', body1:getType(), 'check type match')
  112. body1:setType('kinematic')
  113. body1:setType('static')
  114. test:assertEquals('static', body1:getType(), 'check type change')
  115. test:assertEquals(nil, body1:getUserData(), 'check user data')
  116. body1:setUserData({ love = 'cool' })
  117. test:assertEquals('cool', body1:getUserData().love, 'check set user data')
  118. test:assertEquals(10, body1:getX(), 'check get x')
  119. test:assertEquals(4, body1:getY(), 'check get y')
  120. body1:setX(0)
  121. body1:setY(0)
  122. test:assertEquals(0, body1:getX(), 'check get x')
  123. test:assertEquals(0, body1:getY(), 'check get y')
  124. -- apply some force
  125. local vel = body2:getAngularVelocity()
  126. test:assertEquals(0, math.floor(vel), 'check velocity before')
  127. body2:applyAngularImpulse(10)
  128. vel = body2:getAngularVelocity()
  129. test:assertEquals(54, math.floor(vel*10), 'check velocity after 1')
  130. local ang = body2:getAngle()
  131. test:assertEquals(149, math.floor(ang*1000), 'check initial angle')
  132. body2:applyForce(2, 3)
  133. world:update(2)
  134. vel = body2:getAngularVelocity()
  135. ang = body2:getAngle()
  136. test:assertEquals(-84, math.floor(ang*1000), 'check angle after')
  137. test:assertEquals(124, math.floor(vel*100), 'check velocity after 2')
  138. body2:applyLinearImpulse(-4, -59)
  139. world:update(1)
  140. ang = body2:getAngle()
  141. vel = body2:getAngularVelocity()
  142. test:assertEquals(-1572, math.floor(ang*1000), 'check angle after 2')
  143. test:assertEquals(9, math.floor(vel*100000000), 'check velocity after 3')
  144. body2:applyTorque(4)
  145. world:update(2)
  146. ang = body2:getAngle()
  147. vel = body2:getAngularVelocity()
  148. test:assertEquals(-912, math.floor(ang*1000), 'check angle after 3')
  149. test:assertEquals(321, math.floor(vel*1000), 'check velocity after 4')
  150. test:assertEquals(false, body1:isDestroyed(), 'check not destroyed')
  151. body1:destroy()
  152. test:assertEquals(true, body1:isDestroyed(), 'check destroyed')
  153. end
  154. -- Contact (love.physics.World:getContacts)
  155. love.test.physics.Contact = function(test)
  156. local world = love.physics.newWorld(1, 1, true)
  157. local body1 = love.physics.newBody(world, 0, 0, 'dynamic')
  158. local body2 = love.physics.newBody(world, 10, 10, 'dynamic')
  159. local rectangle1 = love.physics.newRectangleShape(body1, 0, 0, 10, 10)
  160. local rectangle2 = love.physics.newRectangleShape(body2, 0, 0, 10, 10)
  161. rectangle1:setUserData('rec1')
  162. rectangle2:setUserData('rec2')
  163. local collided = false
  164. local pass = 1
  165. world:setCallbacks(
  166. function(shape_a, shape_b, contact)
  167. collided = true
  168. test:assertObject(contact)
  169. local indexA, indexB = contact:getChildren()
  170. test:assertEquals(1, indexA, 'check child indice a')
  171. test:assertEquals(1, indexB, 'check child indice b')
  172. local shapeA, shapeB = contact:getShapes()
  173. test:assertEquals(shape_a:getUserData(), shapeA:getUserData(), 'check shape a matches')
  174. test:assertEquals(shape_b:getUserData(), shapeB:getUserData(), 'check shape b matches')
  175. local nx, ny = contact:getNormal()
  176. test:assertEquals(1, nx, 'check normal x')
  177. test:assertEquals(0, ny, 'check normal y')
  178. local px1, py1, px2, py2 = contact:getPositions()
  179. test:assertEquals(5, math.floor(px1), 'check collide x 1')
  180. test:assertEquals(5, math.floor(py1), 'check collide y 1')
  181. test:assertEquals(5, math.floor(px2), 'check collide x 2')
  182. test:assertEquals(5, math.floor(py2), 'check collide y 2')
  183. test:assertEquals(true, contact:isTouching(), 'check touching')
  184. test:assertEquals(pass == 1, contact:isEnabled(), 'check enabled for pass ' .. tostring(pass))
  185. test:assertEquals(2, math.floor(contact:getFriction()*10), 'check def friction')
  186. contact:setFriction(0.1)
  187. test:assertEquals(1, math.floor(contact:getFriction()*10), 'check set friction')
  188. contact:resetFriction()
  189. test:assertEquals(2, math.floor(contact:getFriction()*10), 'check reset friction')
  190. test:assertEquals(0, contact:getRestitution(), 'check def restitution')
  191. contact:setRestitution(1)
  192. test:assertEquals(1, contact:getRestitution(), 'check set restitution')
  193. contact:resetRestitution()
  194. test:assertEquals(0, contact:getRestitution(), 'check reset restitution')
  195. pass = pass + 1
  196. end, function() end, function(shape_a, shape_b, contact)
  197. if pass > 2 then
  198. contact:setEnabled(false)
  199. end
  200. end, function() end
  201. )
  202. world:update(1)
  203. test:assertEquals(true, collided, 'check bodies collided')
  204. -- update again for enabled check
  205. world:update(1)
  206. test:assertEquals(2, pass, 'check ran twice')
  207. end
  208. -- Joint (love.physics.newDistanceJoint)
  209. love.test.physics.Joint = function(test)
  210. -- make joint
  211. local world = love.physics.newWorld(1, 1, true)
  212. local body1 = love.physics.newBody(world, 10, 10, 'dynamic')
  213. local body2 = love.physics.newBody(world, 20, 20, 'dynamic')
  214. local joint = love.physics.newDistanceJoint(body1, body2, 10, 10, 20, 20, true)
  215. test:assertObject(joint)
  216. -- check props
  217. test:assertEquals('distance', joint:getType(), 'check joint type')
  218. test:assertEquals(false, joint:isDestroyed(), 'check not destroyed')
  219. test:assertEquals(0, joint:getReactionForce(1), 'check reaction force')
  220. test:assertEquals(0, joint:getReactionTorque(1), 'check reaction torque')
  221. local b1, b2 = joint:getBodies()
  222. test:assertEquals(body1:getX(), b1:getX(), 'check body 1')
  223. test:assertEquals(body2:getX(), b2:getX(), 'check body 2')
  224. local x1, y1, x2, y2 = joint:getAnchors()
  225. test:assertEquals(10, math.floor(x1), 'check anchor x1')
  226. test:assertEquals(10, math.floor(y1), 'check anchor y1')
  227. test:assertEquals(20, math.floor(x2), 'check anchor x2')
  228. test:assertEquals(20, math.floor(y2), 'check anchor y2')
  229. test:assertEquals(true, joint:getCollideConnected(), 'check not colliding')
  230. -- test userdata
  231. test:assertEquals(nil, joint:getUserData(), 'check no data by def')
  232. joint:setUserData('hello')
  233. test:assertEquals('hello', joint:getUserData(), 'check set userdata')
  234. -- destroy
  235. joint:destroy()
  236. test:assertEquals(true, joint:isDestroyed(), 'check destroyed')
  237. end
  238. --love.test.physics.Test1 = function(test)
  239. -- local world = love.physics.newWorld(0, 0, false)
  240. -- local body1 = love.physics.newBody(world, 0, 0, 'dynamic')
  241. -- local shape1 = love.physics.newRectangleShape(body1, 5, 5, 10, 10)
  242. -- local tlx, tly, brx, bry = shape1:getBoundingBox(1)
  243. -- print('position:', tlx, tly, brx, bry) -- (-0.3, -0.3, 10.3, 10.3)
  244. -- test:assertEquals(true, shape1:testPoint(5, 5), 'check point 1') -- returns false
  245. --end
  246. -- Shape (love.physics.newCircleShape)
  247. -- @NOTE in 12.0 fixtures have been merged into shapes
  248. love.test.physics.Shape = function(test)
  249. -- create shape
  250. local world = love.physics.newWorld(0, 0, false)
  251. local body1 = love.physics.newBody(world, 0, 0, 'dynamic')
  252. local shape1 = love.physics.newRectangleShape(body1, 5, 5, 10, 10)
  253. test:assertObject(shape1)
  254. -- check base properties
  255. test:assertEquals(1, shape1:getChildCount(), 'check child count')
  256. test:assertEquals(0, math.floor(shape1:getRadius()), 'check radius')
  257. test:assertEquals('polygon', shape1:getType(), 'check rectangle type')
  258. test:assertEquals(0, shape1:getBody():getX(), 'check body link')
  259. test:assertEquals(1, shape1:getCategory(), 'check def category')
  260. shape1:setCategory(3, 5, 6)
  261. local categories = {shape1:getCategory()}
  262. test:assertEquals(14, categories[1] + categories[2] + categories[3], 'check set category')
  263. test:assertEquals(false, shape1:isSensor(), 'check sensor def')
  264. shape1:setSensor(true)
  265. test:assertEquals(true, shape1:isSensor(), 'check set sensor')
  266. shape1:setSensor(false)
  267. test:assertEquals(false, shape1:isDestroyed(), 'check not destroyed')
  268. test:assertEquals(nil, shape1:getUserData(), 'check no user data')
  269. shape1:setUserData({ test = 14 })
  270. test:assertEquals(14, shape1:getUserData().test, 'check user data set')
  271. -- check bounding box
  272. -- polygons have an additional skin radius to help with collisions
  273. -- so this wont be 0, 0, 10, 10 as you'd think but has an additional 0.3 padding
  274. local topLeftX, topLeftY, bottomRightX, bottomRightY = shape1:computeAABB(0, 0, 0, 1)
  275. local tlx, tly, brx, bry = shape1:getBoundingBox(1)
  276. test:assertEquals(topLeftX, tlx, 'check bbox methods match tlx')
  277. test:assertEquals(topLeftY, tly, 'check bbox methods match tly')
  278. test:assertEquals(bottomRightX, brx, 'check bbox methods match brx')
  279. test:assertEquals(bottomRightY, bry, 'check bbox methods match bry')
  280. test:assertEquals(topLeftX, topLeftY, 'check bbox tl 1')
  281. test:assertEquals(-3, math.floor(topLeftY*10), 'check bbox tl 2')
  282. test:assertEquals(bottomRightX, bottomRightY, 'check bbox br 1')
  283. test:assertEquals(10, math.floor(bottomRightX), 'check bbox br 2')
  284. -- check physics props
  285. test:assertEquals(1, shape1:getDensity(), 'check def density')
  286. shape1:setDensity(5)
  287. test:assertEquals(5, shape1:getDensity(), 'check set density')
  288. local x, y, mass, inertia = shape1:getMassData()
  289. test:assertEquals(5, math.floor(x), 'check shape mass pos x')
  290. test:assertEquals(5, math.floor(y), 'check shape mass pos y')
  291. test:assertEquals(5, math.floor(mass*10), 'check mass at 1 density')
  292. test:assertEquals(0, math.floor(inertia*10), 'check intertia at 1 density')
  293. x, y, mass, inertia = shape1:computeMass(1000)
  294. test:assertEquals(111, math.floor(mass), 'check mass at 1000 density')
  295. test:assertEquals(7407, math.floor(inertia), 'check intertia at 1000 density')
  296. test:assertEquals(2, math.floor(shape1:getFriction()*10), 'check def friction')
  297. shape1:setFriction(1)
  298. test:assertEquals(1, shape1:getFriction(), 'check set friction')
  299. test:assertEquals(0, shape1:getRestitution(), 'check def restitution')
  300. shape1:setRestitution(0.5)
  301. test:assertEquals(5, math.floor(shape1:getRestitution()*10), 'check set restitution')
  302. -- check points
  303. local shape2 = love.physics.newRectangleShape(body1, 5, 5, 10, 10)
  304. tlx, tly, brx, bry = shape2:getBoundingBox(1)
  305. test:assertEquals(true, shape2:testPoint(5, 5), 'check point 5,5')
  306. test:assertEquals(true, shape2:testPoint(15, 15, 10, 10, 0), 'check point 15,15 after translate 10,10')
  307. test:assertEquals(true, shape2:testPoint(15, 15, 10, 10, 90), 'check point 15,15 after translate 10,10,90')
  308. test:assertEquals(false, shape2:testPoint(5, 5, 10, 10, 90), 'check point 5,5 after translate 10,10,90')
  309. test:assertEquals(false, shape2:testPoint(15, 15), 'check point 15,15')
  310. local xn, yn, fraction = shape2:rayCast(-20, -20, 20, 20, 100, 0, 0, 0, 1)
  311. test:assertNotEquals(nil, xn, 'check ray 1 x')
  312. test:assertNotEquals(nil, xn, 'check ray 1 y')
  313. xn, yn, fraction = shape2:rayCast(10, 10, -150, -150, 100, 0, 0, 0, 1)
  314. test:assertEquals(nil, xn, 'check ray 2 x')
  315. test:assertEquals(nil, xn, 'check ray 2 y')
  316. -- check filtering
  317. test:assertEquals(nil, shape2:getMask(), 'check no mask')
  318. shape2:setMask(1, 2, 3)
  319. test:assertEquals(3, #{shape2:getMask()}, 'check set mask')
  320. test:assertEquals(0, shape2:getGroupIndex(), 'check no index')
  321. shape2:setGroupIndex(-1)
  322. test:assertEquals(-1, shape2:getGroupIndex(), 'check set index')
  323. local cat, mask, group = shape2:getFilterData()
  324. test:assertEquals(1, cat, 'check filter cat')
  325. test:assertEquals(65528, mask, 'check filter mask')
  326. test:assertEquals(-1, group, 'check filter group')
  327. -- run some collision checks using filters
  328. shape1:destroy()
  329. test:assertEquals(true, shape1:isDestroyed(), 'check destroyed')
  330. shape2:destroy()
  331. local body2 = love.physics.newBody(world, 5, 5, 'dynamic')
  332. local shape3 = love.physics.newRectangleShape(body1, 0, 0, 10, 10)
  333. local shape4 = love.physics.newRectangleShape(body2, 0, 0, 10, 10)
  334. local collisions = 0
  335. world:setCallbacks(
  336. function() collisions = collisions + 1 end,
  337. function() end,
  338. function() end,
  339. function() end
  340. )
  341. -- same group will always collide if the group is positive or never collide if it's negative
  342. shape3:setGroupIndex(1)
  343. shape4:setGroupIndex(1)
  344. world:update(1)
  345. test:assertEquals(1, collisions, 'check positive group collide')
  346. shape3:setGroupIndex(-1)
  347. shape4:setGroupIndex(-1)
  348. body2:setPosition(20, 20); world:update(1); body2:setPosition(0, 0); world:update(1)
  349. test:assertEquals(1, collisions, 'check negative group collide')
  350. -- mask sets which categories this fixture should NOT collide with.
  351. shape3:setGroupIndex(0)
  352. shape4:setGroupIndex(0)
  353. shape3:setCategory(2)
  354. shape4:setMask(3)
  355. body2:setPosition(20, 20); world:update(1); body2:setPosition(0, 0); world:update(1)
  356. test:assertEquals(2, collisions, 'check mask collide')
  357. shape3:setCategory(2)
  358. shape4:setMask(2, 4, 6)
  359. body2:setPosition(20, 20); world:update(1); body2:setPosition(0, 0); world:update(1)
  360. test:assertEquals(2, collisions, 'check mask not collide')
  361. end
  362. -- World (love.physics.newWorld)
  363. love.test.physics.World = function(test)
  364. -- create new world
  365. local world = love.physics.newWorld(0, 0, false)
  366. local body1 = love.physics.newBody(world, 0, 0, 'dynamic')
  367. local rectangle1 = love.physics.newRectangleShape(body1, 0, 0, 10, 10)
  368. test:assertObject(world)
  369. -- check defaults
  370. test:assertEquals(1, #world:getBodies(), 'check 1 body')
  371. test:assertEquals(0, world:getBodies()[1]:getX(), 'check body prop x')
  372. test:assertEquals(0, world:getBodies()[1]:getY(), 'check body prop y')
  373. world:translateOrigin(-10, -10)
  374. test:assertEquals(10, math.floor(world:getBodies()[1]:getX()), 'check body prop change x')
  375. test:assertEquals(10, math.floor(world:getBodies()[1]:getY()), 'check body prop change y')
  376. test:assertEquals(1, world:getBodyCount(), 'check 1 body count')
  377. test:assertEquals(false, world:isDestroyed(), 'check not destroyed')
  378. test:assertEquals(false, world:isLocked(), 'check not updating')
  379. test:assertEquals(0, #world:getJoints(), 'check no joints')
  380. test:assertEquals(0, world:getJointCount(), 'check no joints count')
  381. test:assertEquals(0, world:getGravity(), 'check def gravity')
  382. test:assertEquals(0, #world:getContacts(), 'check no contacts')
  383. test:assertEquals(0, world:getContactCount(), 'check no contact count')
  384. test:assertEquals(false, world:isSleepingAllowed(), 'check no sleep (till brooklyn)')
  385. world:setSleepingAllowed(true)
  386. test:assertEquals(true, world:isSleepingAllowed(), 'check can sleep')
  387. -- check callbacks are called
  388. local beginContact, endContact, preSolve, postSolve = world:getCallbacks()
  389. test:assertEquals(nil, beginContact, 'check no begin contact callback')
  390. test:assertEquals(nil, endContact, 'check no end contact callback')
  391. test:assertEquals(nil, preSolve, 'check no pre solve callback')
  392. test:assertEquals(nil, postSolve, 'check no post solve callback')
  393. local beginContactCheck = false
  394. local endContactCheck = false
  395. local preSolveCheck = false
  396. local postSolveCheck = false
  397. local collisions = 0
  398. world:setCallbacks(
  399. function() beginContactCheck = true; collisions = collisions + 1 end,
  400. function() endContactCheck = true end,
  401. function() preSolveCheck = true end,
  402. function() postSolveCheck = true end
  403. )
  404. local body2 = love.physics.newBody(world, 10, 10, 'dynamic')
  405. local rectangle2 = love.physics.newRectangleShape(body2, 0, 0, 10, 10)
  406. test:assertEquals(false, beginContactCheck, 'check world didnt update after adding body')
  407. world:update(1)
  408. test:assertEquals(true, beginContactCheck, 'check contact start')
  409. test:assertEquals(true, preSolveCheck, 'check pre solve')
  410. test:assertEquals(true, postSolveCheck, 'check post solve')
  411. body2:setPosition(100, 100)
  412. world:update(1)
  413. test:assertEquals(true, endContactCheck, 'check contact end')
  414. -- check point checking
  415. local shapes = 0
  416. world:queryShapesInArea(0, 0, 10, 10, function(x)
  417. shapes = shapes + 1
  418. end)
  419. test:assertEquals(1, shapes, 'check shapes in area')
  420. world:rayCast(0, 0, 200, 200, function(x)
  421. shapes = shapes + 1
  422. return 1
  423. end)
  424. test:assertEquals(3, shapes, 'check shapes in raycast')
  425. -- change collision logic
  426. test:assertEquals(nil, world:getContactFilter(), 'check def filter')
  427. world:update(1)
  428. world:setContactFilter(function(s1, s2)
  429. return false -- nothing collides
  430. end)
  431. body2:setPosition(10, 10)
  432. world:update(1)
  433. test:assertEquals(1, collisions, 'check collision logic change')
  434. -- final bits
  435. world:setGravity(1, 1)
  436. test:assertEquals(1, world:getGravity(), 'check grav change')
  437. world:destroy()
  438. test:assertEquals(true, world:isDestroyed(), 'check world destroyed')
  439. end
  440. --------------------------------------------------------------------------------
  441. --------------------------------------------------------------------------------
  442. ------------------------------------METHODS-------------------------------------
  443. --------------------------------------------------------------------------------
  444. --------------------------------------------------------------------------------
  445. -- love.physics.getDistance
  446. love.test.physics.getDistance = function(test)
  447. -- setup two fixtues to check
  448. local world = love.physics.newWorld(0, 0, false)
  449. local body = love.physics.newBody(world, 10, 10, 'static')
  450. local shape1 = love.physics.newEdgeShape(body, 0, 0, 5, 5)
  451. local shape2 = love.physics.newEdgeShape(body, 10, 10, 15, 15)
  452. -- check distance between them
  453. test:assertEquals(647106, math.floor(love.physics.getDistance(shape1, shape2)*100000), 'check distance matches')
  454. end
  455. -- love.physics.getMeter
  456. love.test.physics.getMeter = function(test)
  457. -- check value set is returned
  458. love.physics.setMeter(30)
  459. test:assertEquals(30, love.physics.getMeter(), 'check meter matches')
  460. end
  461. -- love.physics.newBody
  462. -- @NOTE this is just basic nil checking, objs have their own test method
  463. love.test.physics.newBody = function(test)
  464. local world = love.physics.newWorld(1, 1, true)
  465. local body = love.physics.newBody(world, 10, 10, 'static')
  466. test:assertObject(body)
  467. end
  468. -- love.physics.newChainShape
  469. -- @NOTE this is just basic nil checking, objs have their own test method
  470. love.test.physics.newChainShape = function(test)
  471. local world = love.physics.newWorld(1, 1, true)
  472. local body = love.physics.newBody(world, 10, 10, 'static')
  473. test:assertObject(love.physics.newChainShape(body, true, 0, 0, 1, 0, 1, 1, 0, 1))
  474. end
  475. -- love.physics.newCircleShape
  476. -- @NOTE this is just basic nil checking, objs have their own test method
  477. love.test.physics.newCircleShape = function(test)
  478. local world = love.physics.newWorld(1, 1, true)
  479. local body = love.physics.newBody(world, 10, 10, 'static')
  480. test:assertObject(love.physics.newCircleShape(body, 10))
  481. end
  482. -- love.physics.newDistanceJoint
  483. -- @NOTE this is just basic nil checking, objs have their own test method
  484. love.test.physics.newDistanceJoint = function(test)
  485. local world = love.physics.newWorld(1, 1, true)
  486. local body1 = love.physics.newBody(world, 10, 10, 'static')
  487. local body2 = love.physics.newBody(world, 20, 20, 'static')
  488. local obj = love.physics.newDistanceJoint(body1, body2, 10, 10, 20, 20, true)
  489. test:assertObject(obj)
  490. end
  491. -- love.physics.newEdgeShape
  492. -- @NOTE this is just basic nil checking, objs have their own test method
  493. love.test.physics.newEdgeShape = function(test)
  494. local world = love.physics.newWorld(1, 1, true)
  495. local body = love.physics.newBody(world, 10, 10, 'static')
  496. local obj = love.physics.newEdgeShape(body, 0, 0, 10, 10)
  497. test:assertObject(obj)
  498. end
  499. -- love.physics.newFrictionJoint
  500. -- @NOTE this is just basic nil checking, objs have their own test method
  501. love.test.physics.newFrictionJoint = function(test)
  502. local world = love.physics.newWorld(1, 1, true)
  503. local body1 = love.physics.newBody(world, 10, 10, 'static')
  504. local body2 = love.physics.newBody(world, 20, 20, 'static')
  505. local obj = love.physics.newFrictionJoint(body1, body2, 15, 15, true)
  506. test:assertObject(obj)
  507. end
  508. -- love.physics.newGearJoint
  509. -- @NOTE this is just basic nil checking, objs have their own test method
  510. love.test.physics.newGearJoint = function(test)
  511. local world = love.physics.newWorld(1, 1, true)
  512. local body1 = love.physics.newBody(world, 10, 10, 'dynamic')
  513. local body2 = love.physics.newBody(world, 20, 20, 'dynamic')
  514. local body3 = love.physics.newBody(world, 30, 30, 'dynamic')
  515. local body4 = love.physics.newBody(world, 40, 40, 'dynamic')
  516. local joint1 = love.physics.newPrismaticJoint(body1, body2, 10, 10, 20, 20, true)
  517. local joint2 = love.physics.newPrismaticJoint(body3, body4, 30, 30, 40, 40, true)
  518. local obj = love.physics.newGearJoint(joint1, joint2, 1, true)
  519. test:assertObject(obj)
  520. end
  521. -- love.physics.newMotorJoint
  522. -- @NOTE this is just basic nil checking, objs have their own test method
  523. love.test.physics.newMotorJoint = function(test)
  524. local world = love.physics.newWorld(1, 1, true)
  525. local body1 = love.physics.newBody(world, 10, 10, 'static')
  526. local body2 = love.physics.newBody(world, 20, 20, 'static')
  527. local obj = love.physics.newMotorJoint(body1, body2, 1)
  528. test:assertObject(obj)
  529. end
  530. -- love.physics.newMouseJoint
  531. -- @NOTE this is just basic nil checking, objs have their own test method
  532. love.test.physics.newMouseJoint = function(test)
  533. local world = love.physics.newWorld(1, 1, true)
  534. local body = love.physics.newBody(world, 10, 10, 'static')
  535. local obj = love.physics.newMouseJoint(body, 10, 10)
  536. test:assertObject(obj)
  537. end
  538. -- love.physics.newPolygonShape
  539. -- @NOTE this is just basic nil checking, objs have their own test method
  540. love.test.physics.newPolygonShape = function(test)
  541. local world = love.physics.newWorld(1, 1, true)
  542. local body = love.physics.newBody(world, 10, 10, 'static')
  543. local obj = love.physics.newPolygonShape(body, {0, 0, 2, 3, 2, 1, 3, 1, 5, 1})
  544. test:assertObject(obj)
  545. end
  546. -- love.physics.newPrismaticJoint
  547. -- @NOTE this is just basic nil checking, objs have their own test method
  548. love.test.physics.newPrismaticJoint = function(test)
  549. local world = love.physics.newWorld(1, 1, true)
  550. local body1 = love.physics.newBody(world, 10, 10, 'static')
  551. local body2 = love.physics.newBody(world, 20, 20, 'static')
  552. local obj = love.physics.newPrismaticJoint(body1, body2, 10, 10, 20, 20, true)
  553. test:assertObject(obj)
  554. end
  555. -- love.physics.newPulleyJoint
  556. -- @NOTE this is just basic nil checking, objs have their own test method
  557. love.test.physics.newPulleyJoint = function(test)
  558. local world = love.physics.newWorld(1, 1, true)
  559. local body1 = love.physics.newBody(world, 10, 10, 'static')
  560. local body2 = love.physics.newBody(world, 20, 20, 'static')
  561. local obj = love.physics.newPulleyJoint(body1, body2, 10, 10, 20, 20, 15, 15, 25, 25, 1, true)
  562. test:assertObject(obj)
  563. end
  564. -- love.physics.newRectangleShape
  565. -- @NOTE this is just basic nil checking, objs have their own test method
  566. love.test.physics.newRectangleShape = function(test)
  567. local world = love.physics.newWorld(1, 1, true)
  568. local body = love.physics.newBody(world, 10, 10, 'static')
  569. local shape1 = love.physics.newRectangleShape(body, 10, 20)
  570. local shape2 = love.physics.newRectangleShape(body, 10, 10, 40, 30, 10)
  571. test:assertObject(shape1)
  572. test:assertObject(shape2)
  573. end
  574. -- love.physics.newRevoluteJoint
  575. -- @NOTE this is just basic nil checking, objs have their own test method
  576. love.test.physics.newRevoluteJoint = function(test)
  577. local world = love.physics.newWorld(1, 1, true)
  578. local body1 = love.physics.newBody(world, 10, 10, 'static')
  579. local body2 = love.physics.newBody(world, 20, 20, 'static')
  580. local obj = love.physics.newRevoluteJoint(body1, body2, 10, 10, true)
  581. test:assertObject(obj)
  582. end
  583. -- love.physics.newRopeJoint
  584. -- @NOTE this is just basic nil checking, objs have their own test method
  585. love.test.physics.newRopeJoint = function(test)
  586. local world = love.physics.newWorld(1, 1, true)
  587. local body1 = love.physics.newBody(world, 10, 10, 'static')
  588. local body2 = love.physics.newBody(world, 20, 20, 'static')
  589. local obj = love.physics.newRopeJoint(body1, body2, 10, 10, 20, 20, 50, true)
  590. test:assertObject(obj)
  591. end
  592. -- love.physics.newWeldJoint
  593. -- @NOTE this is just basic nil checking, objs have their own test method
  594. love.test.physics.newWeldJoint = function(test)
  595. local world = love.physics.newWorld(1, 1, true)
  596. local body1 = love.physics.newBody(world, 10, 10, 'static')
  597. local body2 = love.physics.newBody(world, 20, 20, 'static')
  598. local obj = love.physics.newWeldJoint(body1, body2, 10, 10, true)
  599. test:assertObject(obj)
  600. end
  601. -- love.physics.newWheelJoint
  602. -- @NOTE this is just basic nil checking, objs have their own test method
  603. love.test.physics.newWheelJoint = function(test)
  604. local world = love.physics.newWorld(1, 1, true)
  605. local body1 = love.physics.newBody(world, 10, 10, 'static')
  606. local body2 = love.physics.newBody(world, 20, 20, 'static')
  607. local obj = love.physics.newWheelJoint(body1, body2, 10, 10, 5, 5, true)
  608. test:assertObject(obj)
  609. end
  610. -- love.physics.newWorld
  611. -- @NOTE this is just basic nil checking, objs have their own test method
  612. love.test.physics.newWorld = function(test)
  613. local world = love.physics.newWorld(1, 1, true)
  614. test:assertObject(world)
  615. end
  616. -- love.physics.setMeter
  617. love.test.physics.setMeter = function(test)
  618. -- set initial meter
  619. local world = love.physics.newWorld(1, 1, true)
  620. love.physics.setMeter(30)
  621. local body = love.physics.newBody(world, 300, 300, "dynamic")
  622. -- check changing meter changes pos value relatively
  623. love.physics.setMeter(10)
  624. local x, y = body:getPosition()
  625. test:assertEquals(100, x, 'check pos x')
  626. test:assertEquals(100, y, 'check pos y')
  627. end