camera.lua 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. local flux = require "libs.flux"
  2. local camera = lovr.math.newMat4()
  3. local defaultY = 22
  4. local target = { x = 0, y = defaultY, z = 0 }
  5. local scene = { width = 10, size = 10 }
  6. function loadCamera(sceneImport)
  7. scene = sceneImport
  8. end
  9. function aimCameraXZ(x,y, z)
  10. target.x = x or 0
  11. target.y = y or 0
  12. target.z = z or 0
  13. end
  14. function updateCamera(dt)
  15. flux.update(dt)
  16. camera:identity()
  17. camera:translate(lovr.math.vec3(target.x, 0, target.z))
  18. camera:rotate(math.rad(180), 0, 1, 0)
  19. camera:rotate(math.rad(45), 0, -1, 0)
  20. camera:rotate(math.rad(-60), 1, 0, 0)
  21. camera:translate(lovr.math.vec3(0, target.y, 0))
  22. camera:target(vec3(camera), vec3(target.x, 0, target.z), vec3(0, 1, 0))
  23. end
  24. function drawCamera(pass)
  25. local width, height = lovr.system.getWindowDimensions()
  26. local ratio = width / height
  27. local fov = math.rad(20)
  28. local aspect = ratio* 1.13
  29. local near = 0.01
  30. local far = 0
  31. local light_projection = lovr.math.mat4():perspective(fov, aspect,near,far)
  32. pass:setProjection(1, light_projection)
  33. pass:setViewPose(1, camera)
  34. end
  35. function moveCamera(key)
  36. local size = scene.size
  37. local newPos = nil
  38. local distanceFromEdge = 4
  39. if key == "a" or key == "left" then
  40. newPos = { x = -size / 2+distanceFromEdge, y = defaultY, z = -size / 2 +distanceFromEdge}
  41. end
  42. if key == "d" or key == "right" then
  43. newPos = { x = size / 2 -distanceFromEdge, y = defaultY, z = size / 2 -distanceFromEdge }
  44. end
  45. if key == "w" or key == "up" then
  46. newPos = { x = size / 2 -distanceFromEdge, y = defaultY, z = -size / 2 +distanceFromEdge}
  47. end
  48. if key == "s" or key == "down" then
  49. newPos = { x = -size / 2 +distanceFromEdge, y = defaultY, z = size / 2 -distanceFromEdge }
  50. end
  51. if key == "space" then
  52. newPos = { x = 0, y = defaultY, z = 0 }
  53. end
  54. if key == "q" then
  55. newPos = { x = 0, y = size * 3, z = 0 }
  56. end
  57. if newPos then
  58. local speed = 44
  59. local distance = vec3(target.x, target.y, target.z):distance(newPos.x,newPos.y,newPos.z)
  60. flux.to(target, speed/distance, newPos)
  61. end
  62. end