-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.lua
More file actions
121 lines (99 loc) · 3.5 KB
/
Copy pathcamera.lua
File metadata and controls
121 lines (99 loc) · 3.5 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
local Camera = {}
Camera.__index = Camera
local rad = math.rad
local cos, sin = math.cos, math.sin
local function vec3_normalize(x, y, z)
local len = math.sqrt(x*x + y*y + z*z)
if len < 1e-6 then return 0, 0, 0 end
return x / len, y / len, z / len
end
local function vec3_cross(ax, ay, az, bx, by, bz)
return ay*bz - az*by, az*bx - ax*bz, ax*by - ay*bx
end
function Camera.new(opts)
opts = opts or {}
local self = setmetatable({}, Camera)
self.posX = opts.x or 0.0
self.posY = opts.y or 0.0
self.posZ = opts.z or 3.0
self.yaw = opts.yaw or -90.0
self.pitch = opts.pitch or 0.0
self.moveSpeed = opts.moveSpeed or 3.0
self.sprintMult = opts.sprintMult or 2.5
self.mouseSens = opts.mouseSens or 0.10
self.upX, self.upY, self.upZ = 0.0, 1.0, 0.0
self:_updateVectors()
return self
end
function Camera:_updateVectors()
local yawR, pitchR = rad(self.yaw), rad(self.pitch)
local fx = cos(yawR) * cos(pitchR)
local fy = sin(pitchR)
local fz = sin(yawR) * cos(pitchR)
self.frontX, self.frontY, self.frontZ = vec3_normalize(fx, fy, fz)
self.rightX, self.rightY, self.rightZ = vec3_normalize(
vec3_cross(self.frontX, self.frontY, self.frontZ, 0, 1, 0)
)
end
function Camera:updateLook(input)
self.yaw = self.yaw + input.mouseDX * self.mouseSens
self.pitch = self.pitch - input.mouseDY * self.mouseSens
if self.pitch > 89.0 then self.pitch = 89.0 end
if self.pitch < -89.0 then self.pitch = -89.0 end
self:_updateVectors()
end
function Camera:update(input, dt)
self.yaw = self.yaw + input.mouseDX * self.mouseSens
self.pitch = self.pitch - input.mouseDY * self.mouseSens
if self.pitch > 89.0 then self.pitch = 89.0 end
if self.pitch < -89.0 then self.pitch = -89.0 end
self:_updateVectors()
local speed = self.moveSpeed
if input:isDown("LEFT_SHIFT") then
speed = speed * self.sprintMult
end
local d = speed * dt
if input:isDown("W") then
self.posX = self.posX + self.frontX * d
self.posY = self.posY + self.frontY * d
self.posZ = self.posZ + self.frontZ * d
end
if input:isDown("S") then
self.posX = self.posX - self.frontX * d
self.posY = self.posY - self.frontY * d
self.posZ = self.posZ - self.frontZ * d
end
if input:isDown("A") then
self.posX = self.posX - self.rightX * d
self.posY = self.posY - self.rightY * d
self.posZ = self.posZ - self.rightZ * d
end
if input:isDown("D") then
self.posX = self.posX + self.rightX * d
self.posY = self.posY + self.rightY * d
self.posZ = self.posZ + self.rightZ * d
end
if input:isDown("SPACE") then
self.posY = self.posY + d
end
if input:isDown("LEFT_CONTROL") then
self.posY = self.posY - d
end
end
function Camera:getViewMatrix()
local eyeX, eyeY, eyeZ = self.posX, self.posY, self.posZ
local cx, cy, cz = eyeX + self.frontX, eyeY + self.frontY, eyeZ + self.frontZ
local fx, fy, fz = self.frontX, self.frontY, self.frontZ
local sx, sy, sz = vec3_normalize(vec3_cross(fx, fy, fz, self.upX, self.upY, self.upZ))
local ux, uy, uz = vec3_cross(sx, sy, sz, fx, fy, fz)
return {
sx, ux, -fx, 0,
sy, uy, -fy, 0,
sz, uz, -fz, 0,
-(sx*eyeX + sy*eyeY + sz*eyeZ),
-(ux*eyeX + uy*eyeY + uz*eyeZ),
(fx*eyeX + fy*eyeY + fz*eyeZ),
1,
}
end
return Camera