raycast.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. return {
  2. tag = 'worldBasics',
  3. summary = 'Cast a ray through the World, calling a function for each hit.',
  4. description = [[
  5. Casts a ray through the World, calling a function every time the ray intersects with a Shape.
  6. ]],
  7. arguments = {
  8. x1 = {
  9. type = 'number',
  10. description = 'The x coordinate of the starting position of the ray.',
  11. },
  12. y1 = {
  13. type = 'number',
  14. description = 'The y coordinate of the starting position of the ray.',
  15. },
  16. z1 = {
  17. type = 'number',
  18. description = 'The z coordinate of the starting position of the ray.',
  19. },
  20. x2 = {
  21. type = 'number',
  22. description = 'The x coordinate of the ending position of the ray.',
  23. },
  24. y2 = {
  25. type = 'number',
  26. description = 'The y coordinate of the ending position of the ray.',
  27. },
  28. z2 = {
  29. type = 'number',
  30. description = 'The z coordinate of the ending position of the ray.',
  31. },
  32. start = {
  33. type = 'Vec3',
  34. description = 'The starting position of the ray.'
  35. },
  36. ['end'] = {
  37. type = 'Vec3',
  38. description = 'The end position of the ray.'
  39. },
  40. callback = {
  41. type = 'function',
  42. arguments = {
  43. {
  44. name = 'shape',
  45. type = 'Shape'
  46. },
  47. {
  48. name = 'x',
  49. type = 'number'
  50. },
  51. {
  52. name = 'y',
  53. type = 'number'
  54. },
  55. {
  56. name = 'z',
  57. type = 'number'
  58. },
  59. {
  60. name = 'nx',
  61. type = 'number'
  62. },
  63. {
  64. name = 'ny',
  65. type = 'number'
  66. },
  67. {
  68. name = 'nz',
  69. type = 'number'
  70. }
  71. },
  72. returns = {},
  73. description = [[
  74. The function to call when an intersection is detected. It can return `false` to cancel
  75. checks against additional shapes.
  76. ]]
  77. }
  78. },
  79. returns = {},
  80. variants = {
  81. {
  82. arguments = { 'x1', 'y1', 'z1', 'x2', 'y2', 'z2', 'callback' },
  83. returns = {}
  84. },
  85. {
  86. arguments = { 'start', 'end', 'callback' },
  87. returns = {}
  88. }
  89. },
  90. notes = [[
  91. The callback is passed the shape that was hit, the hit position (in world coordinates), and the
  92. normal vector of the hit.
  93. ]],
  94. example = [[
  95. function lovr.load()
  96. world = lovr.physics.newWorld()
  97. world:newSphereCollider(0, 0, 0, 2)
  98. -- Cast a ray through the sphere
  99. local x1, y1, z1 = .5, 3, 0
  100. local x2, y2, z2 = -.5, -2, 0
  101. world:raycast(x1, y1, z1, x2, y2, z2, function(shape, x, y, z, nx, ny, nz)
  102. print('Collision detected!', shape, x, y, z, nx, ny, nz)
  103. end)
  104. end
  105. ]],
  106. related = {
  107. 'World:raycastAny',
  108. 'World:raycastClosest',
  109. 'World:queryBox',
  110. 'World:querySphere'
  111. }
  112. }