physics.lua 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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 (sometimes nil on linux runners)
  255. if joint:getReactionForce(1) ~= nil and joint:getReactionForce(1) ~= 0/0 then
  256. test:assertEquals(0, joint:getReactionForce(1), 'check reaction force')
  257. end
  258. if joint:getReactionTorque(1) ~= nil and joint:getReactionTorque(1) ~= 0/0 then
  259. test:assertEquals(0, joint:getReactionTorque(1), 'check reaction torque')
  260. end
  261. -- check body pointer
  262. local b1, b2 = joint:getBodies()
  263. test:assertEquals(body1:getX(), b1:getX(), 'check body 1')
  264. test:assertEquals(body2:getX(), b2:getX(), 'check body 2')
  265. -- check joint anchors
  266. local x1, y1, x2, y2 = joint:getAnchors()
  267. test:assertRange(x1, 10, 11, 'check anchor x1')
  268. test:assertRange(y1, 10, 11, 'check anchor y1')
  269. test:assertRange(x2, 20, 21, 'check anchor x2')
  270. test:assertRange(y2, 20, 21, 'check anchor y2')
  271. test:assertTrue(joint:getCollideConnected(), 'check not colliding')
  272. -- test userdata
  273. test:assertEquals(nil, joint:getUserData(), 'check no data by def')
  274. joint:setUserData('hello')
  275. test:assertEquals('hello', joint:getUserData(), 'check set userdata')
  276. -- destroy
  277. joint:destroy()
  278. test:assertTrue(joint:isDestroyed(), 'check destroyed')
  279. end
  280. -- Shape (love.physics.newCircleShape)
  281. -- @NOTE in 12.0 fixtures have been merged into shapes
  282. love.test.physics.Shape = function(test)
  283. -- create shape
  284. local world = love.physics.newWorld(0, 0, false)
  285. local body1 = love.physics.newBody(world, 0, 0, 'dynamic')
  286. local shape1 = love.physics.newRectangleShape(body1, 5, 5, 10, 10)
  287. test:assertObject(shape1)
  288. -- check child count
  289. test:assertEquals(1, shape1:getChildCount(), 'check child count')
  290. -- check radius
  291. test:assertRange(shape1:getRadius(), 0, 0.4, 'check radius')
  292. -- check type match
  293. test:assertEquals('polygon', shape1:getType(), 'check rectangle type')
  294. -- check body pointer
  295. test:assertEquals(0, shape1:getBody():getX(), 'check body link')
  296. -- check category
  297. test:assertEquals(1, shape1:getCategory(), 'check def category')
  298. shape1:setCategory(3, 5, 6)
  299. local categories = {shape1:getCategory()}
  300. test:assertEquals(14, categories[1] + categories[2] + categories[3], 'check set category')
  301. -- check sensor prop
  302. test:assertFalse(shape1:isSensor(), 'check sensor def')
  303. shape1:setSensor(true)
  304. test:assertTrue(shape1:isSensor(), 'check set sensor')
  305. shape1:setSensor(false)
  306. -- check not destroyed
  307. test:assertFalse(shape1:isDestroyed(), 'check not destroyed')
  308. -- check user data
  309. test:assertEquals(nil, shape1:getUserData(), 'check no user data')
  310. shape1:setUserData({ test = 14 })
  311. test:assertEquals(14, shape1:getUserData().test, 'check user data set')
  312. -- check bounding box
  313. -- polygons have an additional skin radius to help with collisions
  314. -- so this wont be 0, 0, 10, 10 as you'd think but has an additional 0.3 padding
  315. local topLeftX, topLeftY, bottomRightX, bottomRightY = shape1:computeAABB(0, 0, 0, 1)
  316. local tlx, tly, brx, bry = shape1:getBoundingBox(1)
  317. test:assertEquals(topLeftX, tlx, 'check bbox methods match tlx')
  318. test:assertEquals(topLeftY, tly, 'check bbox methods match tly')
  319. test:assertEquals(bottomRightX, brx, 'check bbox methods match brx')
  320. test:assertEquals(bottomRightY, bry, 'check bbox methods match bry')
  321. test:assertEquals(topLeftX, topLeftY, 'check bbox tl 1')
  322. test:assertRange(topLeftY, -0.3, -0.2, 'check bbox tl 2')
  323. test:assertEquals(bottomRightX, bottomRightY, 'check bbox br 1')
  324. test:assertRange(bottomRightX, 10.3, 10.4, 'check bbox br 2')
  325. -- check density
  326. test:assertEquals(1, shape1:getDensity(), 'check def density')
  327. shape1:setDensity(5)
  328. test:assertEquals(5, shape1:getDensity(), 'check set density')
  329. -- check mass
  330. local x1, y1, mass1, inertia1 = shape1:getMassData()
  331. test:assertRange(x1, 5, 5.1, 'check shape mass pos x')
  332. test:assertRange(y1, 5, 5.1, 'check shape mass pos y')
  333. test:assertRange(mass1, 0.5, 0.6, 'check mass at 1 density')
  334. test:assertRange(inertia1, 0, 0.1, 'check intertia at 1 density')
  335. local x2, y2, mass2, inertia2 = shape1:computeMass(1000)
  336. test:assertRange(mass2, 111, 112, 'check mass at 1000 density')
  337. test:assertRange(inertia2, 7407, 7408, 'check intertia at 1000 density')
  338. -- check friction
  339. test:assertRange(shape1:getFriction(), 0.2, 0.3, 'check def friction')
  340. shape1:setFriction(1)
  341. test:assertEquals(1, shape1:getFriction(), 'check set friction')
  342. -- check restitution
  343. test:assertEquals(0, shape1:getRestitution(), 'check def restitution')
  344. shape1:setRestitution(0.5)
  345. test:assertRange(shape1:getRestitution(), 0.5, 0.6, 'check set restitution')
  346. -- check points
  347. local bodyp = love.physics.newBody(world, 0, 0, 'dynamic')
  348. local shape2 = love.physics.newRectangleShape(bodyp, 5, 5, 10, 10)
  349. test:assertTrue(shape2:testPoint(5, 5), 'check point 5,5')
  350. test:assertTrue(shape2:testPoint(10, 10, 0, 15, 15), 'check point 15,15 after translate 10,10')
  351. test:assertFalse(shape2:testPoint(5, 5, 90, 10, 10), 'check point 10,10 after translate 5,5,90')
  352. test:assertFalse(shape2:testPoint(10, 10, 90, 5, 5), 'check point 5,5 after translate 10,10,90')
  353. test:assertFalse(shape2:testPoint(15, 15), 'check point 15,15')
  354. -- check ray cast
  355. local xn1, yn1, fraction1 = shape2:rayCast(-20, -20, 20, 20, 100, 0, 0, 0, 1)
  356. test:assertNotEquals(nil, xn1, 'check ray 1 x')
  357. test:assertNotEquals(nil, xn1, 'check ray 1 y')
  358. local xn2, yn2, fraction2 = shape2:rayCast(10, 10, -150, -150, 100, 0, 0, 0, 1)
  359. test:assertEquals(nil, xn2, 'check ray 2 x')
  360. test:assertEquals(nil, yn2, 'check ray 2 y')
  361. -- check filtering
  362. test:assertEquals(nil, shape2:getMask(), 'check no mask')
  363. shape2:setMask(1, 2, 3)
  364. test:assertEquals(3, #{shape2:getMask()}, 'check set mask')
  365. test:assertEquals(0, shape2:getGroupIndex(), 'check no index')
  366. shape2:setGroupIndex(-1)
  367. test:assertEquals(-1, shape2:getGroupIndex(), 'check set index')
  368. local cat, mask, group = shape2:getFilterData()
  369. test:assertEquals(1, cat, 'check filter cat')
  370. test:assertEquals(65528, mask, 'check filter mask')
  371. test:assertEquals(-1, group, 'check filter group')
  372. -- check destroyed
  373. shape1:destroy()
  374. test:assertTrue(shape1:isDestroyed(), 'check destroyed')
  375. shape2:destroy()
  376. -- run some collision checks using filters, setup new shapes
  377. local body2 = love.physics.newBody(world, 5, 5, 'dynamic')
  378. local shape3 = love.physics.newRectangleShape(body1, 0, 0, 10, 10)
  379. local shape4 = love.physics.newRectangleShape(body2, 0, 0, 10, 10)
  380. local collisions = 0
  381. world:setCallbacks(
  382. function() collisions = collisions + 1 end,
  383. function() end,
  384. function() end,
  385. function() end
  386. )
  387. -- same positive group do collide
  388. shape3:setGroupIndex(1)
  389. shape4:setGroupIndex(1)
  390. world:update(1)
  391. test:assertEquals(1, collisions, 'check positive group collide')
  392. -- check negative group dont collide
  393. shape3:setGroupIndex(-1)
  394. shape4:setGroupIndex(-1)
  395. body2:setPosition(20, 20); world:update(1); body2:setPosition(0, 0); world:update(1)
  396. test:assertEquals(1, collisions, 'check negative group collide')
  397. -- check masks do collide
  398. shape3:setGroupIndex(0)
  399. shape4:setGroupIndex(0)
  400. shape3:setCategory(2)
  401. shape4:setMask(3)
  402. body2:setPosition(20, 20); world:update(1); body2:setPosition(0, 0); world:update(1)
  403. test:assertEquals(2, collisions, 'check mask collide')
  404. -- check masks not colliding
  405. shape3:setCategory(2)
  406. shape4:setMask(2, 4, 6)
  407. body2:setPosition(20, 20); world:update(1); body2:setPosition(0, 0); world:update(1)
  408. test:assertEquals(2, collisions, 'check mask not collide')
  409. end
  410. -- World (love.physics.newWorld)
  411. love.test.physics.World = function(test)
  412. -- create new world
  413. local world = love.physics.newWorld(0, 0, false)
  414. local body1 = love.physics.newBody(world, 0, 0, 'dynamic')
  415. local rectangle1 = love.physics.newRectangleShape(body1, 0, 0, 10, 10)
  416. test:assertObject(world)
  417. -- check bodies in world
  418. test:assertEquals(1, #world:getBodies(), 'check 1 body')
  419. test:assertEquals(0, world:getBodies()[1]:getX(), 'check body prop x')
  420. test:assertEquals(0, world:getBodies()[1]:getY(), 'check body prop y')
  421. world:translateOrigin(-10, -10) -- check affects bodies
  422. test:assertRange(world:getBodies()[1]:getX(), 9, 11, 'check body prop change x')
  423. test:assertRange(world:getBodies()[1]:getY(), 9, 11, 'check body prop change y')
  424. test:assertEquals(1, world:getBodyCount(), 'check 1 body count')
  425. -- check world status
  426. test:assertFalse(world:isLocked(), 'check not updating')
  427. test:assertFalse(world:isSleepingAllowed(), 'check no sleep (till brooklyn)')
  428. world:setSleepingAllowed(true)
  429. test:assertTrue(world:isSleepingAllowed(), 'check can sleep')
  430. -- check world objects
  431. test:assertEquals(0, #world:getJoints(), 'check no joints')
  432. test:assertEquals(0, world:getJointCount(), 'check no joints count')
  433. test:assertEquals(0, world:getGravity(), 'check def gravity')
  434. test:assertEquals(0, #world:getContacts(), 'check no contacts')
  435. test:assertEquals(0, world:getContactCount(), 'check no contact count')
  436. -- check callbacks are called
  437. local beginContact, endContact, preSolve, postSolve = world:getCallbacks()
  438. test:assertEquals(nil, beginContact, 'check no begin contact callback')
  439. test:assertEquals(nil, endContact, 'check no end contact callback')
  440. test:assertEquals(nil, preSolve, 'check no pre solve callback')
  441. test:assertEquals(nil, postSolve, 'check no post solve callback')
  442. local beginContactCheck = false
  443. local endContactCheck = false
  444. local preSolveCheck = false
  445. local postSolveCheck = false
  446. local collisions = 0
  447. world:setCallbacks(
  448. function() beginContactCheck = true; collisions = collisions + 1 end,
  449. function() endContactCheck = true end,
  450. function() preSolveCheck = true end,
  451. function() postSolveCheck = true end
  452. )
  453. -- setup so we can collide stuff to call the callbacks
  454. local body2 = love.physics.newBody(world, 10, 10, 'dynamic')
  455. local rectangle2 = love.physics.newRectangleShape(body2, 0, 0, 10, 10)
  456. test:assertFalse(beginContactCheck, 'check world didnt update after adding body')
  457. world:update(1)
  458. test:assertTrue(beginContactCheck, 'check contact start')
  459. test:assertTrue(preSolveCheck, 'check pre solve')
  460. test:assertTrue(postSolveCheck, 'check post solve')
  461. body2:setPosition(100, 100)
  462. world:update(1)
  463. test:assertTrue(endContactCheck, 'check contact end')
  464. -- check point checking
  465. local shapes = 0
  466. world:queryShapesInArea(0, 0, 10, 10, function(x)
  467. shapes = shapes + 1
  468. end)
  469. test:assertEquals(1, shapes, 'check shapes in area')
  470. -- check raycast
  471. world:rayCast(0, 0, 200, 200, function(x)
  472. shapes = shapes + 1
  473. return 1
  474. end)
  475. test:assertEquals(3, shapes, 'check shapes in raycast')
  476. -- change collision logic
  477. test:assertEquals(nil, world:getContactFilter(), 'check def filter')
  478. world:update(1)
  479. world:setContactFilter(function(s1, s2)
  480. return false -- nothing collides
  481. end)
  482. body2:setPosition(10, 10)
  483. world:update(1)
  484. test:assertEquals(1, collisions, 'check collision logic change')
  485. -- check gravity
  486. world:setGravity(1, 1)
  487. test:assertEquals(1, world:getGravity(), 'check grav change')
  488. -- check destruction
  489. test:assertFalse(world:isDestroyed(), 'check not destroyed')
  490. world:destroy()
  491. test:assertTrue(world:isDestroyed(), 'check world destroyed')
  492. end
  493. --------------------------------------------------------------------------------
  494. --------------------------------------------------------------------------------
  495. ------------------------------------METHODS-------------------------------------
  496. --------------------------------------------------------------------------------
  497. --------------------------------------------------------------------------------
  498. -- love.physics.getDistance
  499. love.test.physics.getDistance = function(test)
  500. -- setup two fixtues to check
  501. local world = love.physics.newWorld(0, 0, false)
  502. local body = love.physics.newBody(world, 10, 10, 'static')
  503. local shape1 = love.physics.newEdgeShape(body, 0, 0, 5, 5)
  504. local shape2 = love.physics.newEdgeShape(body, 10, 10, 15, 15)
  505. -- check distance between them
  506. test:assertRange(love.physics.getDistance(shape1, shape2), 6, 7, 'check distance matches')
  507. end
  508. -- love.physics.getMeter
  509. love.test.physics.getMeter = function(test)
  510. -- check value set is returned
  511. love.physics.setMeter(30)
  512. test:assertEquals(30, love.physics.getMeter(), 'check meter matches')
  513. end
  514. -- love.physics.newBody
  515. -- @NOTE this is just basic nil checking, objs have their own test method
  516. love.test.physics.newBody = function(test)
  517. local world = love.physics.newWorld(1, 1, true)
  518. local body = love.physics.newBody(world, 10, 10, 'static')
  519. test:assertObject(body)
  520. end
  521. -- love.physics.newChainShape
  522. -- @NOTE this is just basic nil checking, objs have their own test method
  523. love.test.physics.newChainShape = function(test)
  524. local world = love.physics.newWorld(1, 1, true)
  525. local body = love.physics.newBody(world, 10, 10, 'static')
  526. test:assertObject(love.physics.newChainShape(body, true, 0, 0, 1, 0, 1, 1, 0, 1))
  527. end
  528. -- love.physics.newCircleShape
  529. -- @NOTE this is just basic nil checking, objs have their own test method
  530. love.test.physics.newCircleShape = function(test)
  531. local world = love.physics.newWorld(1, 1, true)
  532. local body = love.physics.newBody(world, 10, 10, 'static')
  533. test:assertObject(love.physics.newCircleShape(body, 10))
  534. end
  535. -- love.physics.newDistanceJoint
  536. -- @NOTE this is just basic nil checking, objs have their own test method
  537. love.test.physics.newDistanceJoint = function(test)
  538. local world = love.physics.newWorld(1, 1, true)
  539. local body1 = love.physics.newBody(world, 10, 10, 'static')
  540. local body2 = love.physics.newBody(world, 20, 20, 'static')
  541. local obj = love.physics.newDistanceJoint(body1, body2, 10, 10, 20, 20, true)
  542. test:assertObject(obj)
  543. end
  544. -- love.physics.newEdgeShape
  545. -- @NOTE this is just basic nil checking, objs have their own test method
  546. love.test.physics.newEdgeShape = function(test)
  547. local world = love.physics.newWorld(1, 1, true)
  548. local body = love.physics.newBody(world, 10, 10, 'static')
  549. local obj = love.physics.newEdgeShape(body, 0, 0, 10, 10)
  550. test:assertObject(obj)
  551. end
  552. -- love.physics.newFrictionJoint
  553. -- @NOTE this is just basic nil checking, objs have their own test method
  554. love.test.physics.newFrictionJoint = function(test)
  555. local world = love.physics.newWorld(1, 1, true)
  556. local body1 = love.physics.newBody(world, 10, 10, 'static')
  557. local body2 = love.physics.newBody(world, 20, 20, 'static')
  558. local obj = love.physics.newFrictionJoint(body1, body2, 15, 15, true)
  559. test:assertObject(obj)
  560. end
  561. -- love.physics.newGearJoint
  562. -- @NOTE this is just basic nil checking, objs have their own test method
  563. love.test.physics.newGearJoint = function(test)
  564. local world = love.physics.newWorld(1, 1, true)
  565. local body1 = love.physics.newBody(world, 10, 10, 'dynamic')
  566. local body2 = love.physics.newBody(world, 20, 20, 'dynamic')
  567. local body3 = love.physics.newBody(world, 30, 30, 'dynamic')
  568. local body4 = love.physics.newBody(world, 40, 40, 'dynamic')
  569. local joint1 = love.physics.newPrismaticJoint(body1, body2, 10, 10, 20, 20, true)
  570. local joint2 = love.physics.newPrismaticJoint(body3, body4, 30, 30, 40, 40, true)
  571. local obj = love.physics.newGearJoint(joint1, joint2, 1, true)
  572. test:assertObject(obj)
  573. end
  574. -- love.physics.newMotorJoint
  575. -- @NOTE this is just basic nil checking, objs have their own test method
  576. love.test.physics.newMotorJoint = 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.newMotorJoint(body1, body2, 1)
  581. test:assertObject(obj)
  582. end
  583. -- love.physics.newMouseJoint
  584. -- @NOTE this is just basic nil checking, objs have their own test method
  585. love.test.physics.newMouseJoint = function(test)
  586. local world = love.physics.newWorld(1, 1, true)
  587. local body = love.physics.newBody(world, 10, 10, 'static')
  588. local obj = love.physics.newMouseJoint(body, 10, 10)
  589. test:assertObject(obj)
  590. end
  591. -- love.physics.newPolygonShape
  592. -- @NOTE this is just basic nil checking, objs have their own test method
  593. love.test.physics.newPolygonShape = function(test)
  594. local world = love.physics.newWorld(1, 1, true)
  595. local body = love.physics.newBody(world, 10, 10, 'static')
  596. local obj = love.physics.newPolygonShape(body, {0, 0, 2, 3, 2, 1, 3, 1, 5, 1})
  597. test:assertObject(obj)
  598. end
  599. -- love.physics.newPrismaticJoint
  600. -- @NOTE this is just basic nil checking, objs have their own test method
  601. love.test.physics.newPrismaticJoint = function(test)
  602. local world = love.physics.newWorld(1, 1, true)
  603. local body1 = love.physics.newBody(world, 10, 10, 'static')
  604. local body2 = love.physics.newBody(world, 20, 20, 'static')
  605. local obj = love.physics.newPrismaticJoint(body1, body2, 10, 10, 20, 20, true)
  606. test:assertObject(obj)
  607. end
  608. -- love.physics.newPulleyJoint
  609. -- @NOTE this is just basic nil checking, objs have their own test method
  610. love.test.physics.newPulleyJoint = function(test)
  611. local world = love.physics.newWorld(1, 1, true)
  612. local body1 = love.physics.newBody(world, 10, 10, 'static')
  613. local body2 = love.physics.newBody(world, 20, 20, 'static')
  614. local obj = love.physics.newPulleyJoint(body1, body2, 10, 10, 20, 20, 15, 15, 25, 25, 1, true)
  615. test:assertObject(obj)
  616. end
  617. -- love.physics.newRectangleShape
  618. -- @NOTE this is just basic nil checking, objs have their own test method
  619. love.test.physics.newRectangleShape = function(test)
  620. local world = love.physics.newWorld(1, 1, true)
  621. local body = love.physics.newBody(world, 10, 10, 'static')
  622. local shape1 = love.physics.newRectangleShape(body, 10, 20)
  623. local shape2 = love.physics.newRectangleShape(body, 10, 10, 40, 30, 10)
  624. test:assertObject(shape1)
  625. test:assertObject(shape2)
  626. end
  627. -- love.physics.newRevoluteJoint
  628. -- @NOTE this is just basic nil checking, objs have their own test method
  629. love.test.physics.newRevoluteJoint = function(test)
  630. local world = love.physics.newWorld(1, 1, true)
  631. local body1 = love.physics.newBody(world, 10, 10, 'static')
  632. local body2 = love.physics.newBody(world, 20, 20, 'static')
  633. local obj = love.physics.newRevoluteJoint(body1, body2, 10, 10, true)
  634. test:assertObject(obj)
  635. end
  636. -- love.physics.newRopeJoint
  637. -- @NOTE this is just basic nil checking, objs have their own test method
  638. love.test.physics.newRopeJoint = function(test)
  639. local world = love.physics.newWorld(1, 1, true)
  640. local body1 = love.physics.newBody(world, 10, 10, 'static')
  641. local body2 = love.physics.newBody(world, 20, 20, 'static')
  642. local obj = love.physics.newRopeJoint(body1, body2, 10, 10, 20, 20, 50, true)
  643. test:assertObject(obj)
  644. end
  645. -- love.physics.newWeldJoint
  646. -- @NOTE this is just basic nil checking, objs have their own test method
  647. love.test.physics.newWeldJoint = function(test)
  648. local world = love.physics.newWorld(1, 1, true)
  649. local body1 = love.physics.newBody(world, 10, 10, 'static')
  650. local body2 = love.physics.newBody(world, 20, 20, 'static')
  651. local obj = love.physics.newWeldJoint(body1, body2, 10, 10, true)
  652. test:assertObject(obj)
  653. end
  654. -- love.physics.newWheelJoint
  655. -- @NOTE this is just basic nil checking, objs have their own test method
  656. love.test.physics.newWheelJoint = function(test)
  657. local world = love.physics.newWorld(1, 1, true)
  658. local body1 = love.physics.newBody(world, 10, 10, 'static')
  659. local body2 = love.physics.newBody(world, 20, 20, 'static')
  660. local obj = love.physics.newWheelJoint(body1, body2, 10, 10, 5, 5, true)
  661. test:assertObject(obj)
  662. end
  663. -- love.physics.newWorld
  664. -- @NOTE this is just basic nil checking, objs have their own test method
  665. love.test.physics.newWorld = function(test)
  666. local world = love.physics.newWorld(1, 1, true)
  667. test:assertObject(world)
  668. end
  669. -- love.physics.setMeter
  670. love.test.physics.setMeter = function(test)
  671. -- set initial meter
  672. local world = love.physics.newWorld(1, 1, true)
  673. love.physics.setMeter(30)
  674. local body = love.physics.newBody(world, 300, 300, "dynamic")
  675. -- check changing meter changes pos value relatively
  676. love.physics.setMeter(10)
  677. local x, y = body:getPosition()
  678. test:assertEquals(100, x, 'check pos x')
  679. test:assertEquals(100, y, 'check pos y')
  680. end