camera.lua 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. require "core/lua/class"
  2. local wkey = false
  3. local skey = false
  4. local akey = false
  5. local dkey = false
  6. FPSCamera = class(FPSCamera)
  7. function FPSCamera:init(world, unit)
  8. self._world = world
  9. self._unit = unit
  10. self._sg = World.scene_graph(world)
  11. self._movement_speed = 20
  12. self._rotation_speed = 0.14
  13. end
  14. function FPSCamera:unit()
  15. return self._unit;
  16. end
  17. function FPSCamera:camera()
  18. return World.camera_instance(self._world, self._unit)
  19. end
  20. function FPSCamera:update(dt, dx, dy)
  21. if Keyboard.pressed(Keyboard.button_id("w")) then wkey = true end
  22. if Keyboard.pressed(Keyboard.button_id("s")) then skey = true end
  23. if Keyboard.pressed(Keyboard.button_id("a")) then akey = true end
  24. if Keyboard.pressed(Keyboard.button_id("d")) then dkey = true end
  25. if Keyboard.released(Keyboard.button_id("w")) then wkey = false end
  26. if Keyboard.released(Keyboard.button_id("s")) then skey = false end
  27. if Keyboard.released(Keyboard.button_id("a")) then akey = false end
  28. if Keyboard.released(Keyboard.button_id("d")) then dkey = false end
  29. local camera = self:camera()
  30. local tr = SceneGraph.instance(self._sg, self._unit)
  31. local camera_local_pose = SceneGraph.local_pose(self._sg, tr)
  32. local camera_right_vector = Matrix4x4.x(camera_local_pose)
  33. local camera_position = Matrix4x4.translation(camera_local_pose)
  34. local camera_rotation = Matrix4x4.rotation(camera_local_pose)
  35. local view_dir = Matrix4x4.z(camera_local_pose)
  36. -- Rotation
  37. if dx ~= 0 or dy ~= 0 then
  38. local rotation_speed = self._rotation_speed * dt
  39. local rotation_around_world_up = Quaternion(Vector3(0, 1, 0), dx * rotation_speed)
  40. local rotation_around_camera_right = Quaternion(camera_right_vector, dy * rotation_speed)
  41. local rotation = Quaternion.multiply(rotation_around_world_up, rotation_around_camera_right)
  42. local old_rotation = Matrix4x4.from_quaternion(camera_rotation)
  43. local delta_rotation = Matrix4x4.from_quaternion(rotation)
  44. local new_rotation = Matrix4x4.multiply(old_rotation, delta_rotation)
  45. Matrix4x4.set_translation(new_rotation, camera_position)
  46. -- Fixme
  47. SceneGraph.set_local_pose(self._sg, tr, new_rotation)
  48. end
  49. -- Translation
  50. local translation_speed = self._movement_speed * dt
  51. if wkey then camera_position = camera_position + view_dir * translation_speed end
  52. if skey then camera_position = camera_position - view_dir * translation_speed end
  53. if akey then camera_position = camera_position - camera_right_vector * translation_speed end
  54. if dkey then camera_position = camera_position + camera_right_vector * translation_speed end
  55. SceneGraph.set_local_position(self._sg, tr, camera_position)
  56. end