physics.lua 31 KB

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