summaryrefslogtreecommitdiff
path: root/demo/FPSCamera.lua
blob: 5a9fabf5b13ac7023bd6b8cea94ad5bcfd228047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
local Vector = require('Vector')
local Matrix = require('Matrix')
local Camera = require('Camera')

local camera = Camera.new(nil,
                          Vector.Vec3.new(),
                          Vector.Vec3.new(),
                          Vector.Vec3.new{1,1,1},
                          math.rad(90), 640/480, 0.1, 1000)

camera.pitch = 0
camera.yaw = 0

camera.sensitivity = 0.1

camera.movement_speed = 1

function camera:update(dt)
   movement = Vector.Vec3.new()
   if honey.input.key.is_down(honey.input.key.w) then
      movement:add(self.basis.z, movement)
   end
   if honey.input.key.is_down(honey.input.key.a) then
      movement:add(self.basis.x, movement)
   end
   if honey.input.key.is_down(honey.input.key.s) then
      movement:sub(self.basis.z, movement)
   end
   if honey.input.key.is_down(honey.input.key.d) then
      movement:sub(self.basis.x, movement)
   end

   movement:setAt(1, 0)
   movement:normalize()

   if honey.input.key.is_down(honey.input.key.left_shift) then
      movement:add(Vector.Vec3.Y_UNIT, movement)
   end
   if honey.input.key.is_down(honey.input.key.left_control) then
      movement:sub(Vector.Vec3.Y_UNIT, movement)
   end
   movement:muls(self.movement_speed*dt, movement)
   self:translate(movement)
   
   self:updateTransform()
   self:updateView()
end

camera.mouse_pos = {}
camera.mouse_pos.x = 0
camera.mouse_pos.y = 0

honey.input.mouse.bind_movement(
   function(xpos, ypos)
      local dx = xpos - camera.mouse_pos.x
      local dy = ypos - camera.mouse_pos.y

      camera.mouse_pos = { x=xpos, y=ypos }

      camera.pitch = camera.pitch + camera.sensitivity * dy
      camera.yaw = camera.yaw - camera.sensitivity * dx

      if camera.pitch > 89.9 then camera.pitch = 89.9 end
      if camera.pitch < -89.9 then camera.pitch = -89.9 end

      camera.rotation:setAt(0, math.rad(camera.pitch))
      camera.rotation:setAt(1, math.rad(camera.yaw))
   end
)

honey.input.mouse.set_mode(honey.input.mouse.mode.disabled)

return camera