-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcamera.lua
More file actions
74 lines (63 loc) · 2.05 KB
/
camera.lua
File metadata and controls
74 lines (63 loc) · 2.05 KB
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
74
local cpml = require("libraries.cpml")
---constants
local YAW = -90.0;
local PITCH = 0.0;
local SPEED = 2.5;
local SENSITIVITY = 0.1;
local ZOOM = 45.0;
---@class Camera
---@field pos cpml.vec3 position
---@field front cpml.vec3 front vector
---@field up cpml.vec3 up vector
---@field worldUp cpml.vec3 world up vector
---@field yaw number yaw rotation
---@field pitch number pitch rotation
local Camera = {
up
}
Camera.new = function(pos, yaw, pitch)
local self = {}
self.pos = pos or cpml.vec3.new(0,1,0)
self.yaw = yaw or YAW
self.pitch = pitch or PITCH
self.worldUp = cpml.vec3.new(0,1,0)
self.front = cpml.vec3.new(0,0,-1)
self = setmetatable(self,Camera)
self:updateCameraVectors()
return self
end
Camera.__index = Camera
local tempViewMat = cpml.mat4.new({0,0,0,0,0,0,0,0,0})
function Camera:getViewMatrix()
return cpml.mat4.look_at(tempViewMat, self.pos, self.pos:add(self.front), self.up)
end
function Camera:getNearPoint(near)
return self.pos + self.front:scale(near)
end
function Camera:updateCameraVectors()
local front = cpml.vec3.new(
math.cos(math.rad(self.yaw))*math.cos(math.rad(self.pitch)),
math.sin(math.rad(self.pitch)),
math.sin(math.rad(self.yaw))*math.cos(math.rad(self.pitch))
)
self.front = front:normalize()
self.right = (self.front:cross(self.worldUp)):normalize()
self.up = (self.right:cross(self.front)):normalize()
end
function Camera:update(dt,moveX, moveY, rotX, rotY)
-- movement
self.pos = self.pos:add(self.front:scale(-SPEED*moveY*dt))
self.pos = self.pos:add(self.right:scale(SPEED*moveX*dt))
-- rotation
self.yaw = self.yaw + rotX
self.pitch = cpml.utils.clamp(self.pitch - rotY,-89,89)
self:updateCameraVectors()
end
function Camera:draw2D()
local f = self.pos:add(self.front:scale(3))
love.graphics.setColor(1,0,1)
love.graphics.circle("fill",self.pos.x, self.pos.z, 3)
love.graphics.setColor(1,1,1)
love.graphics.line(self.pos.x, self.pos.z, f.x, f.z)
end
return Camera