raycast.lua 2.2 KB

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