/
CameraControls.lua
354 lines (284 loc) · 9.76 KB
/
CameraControls.lua
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
--[=[
Interface between user input and camera controls
@class CameraControls
]=]
local require = require(script.Parent.loader).load(script)
local ContextActionService = game:GetService("ContextActionService")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Maid = require("Maid")
local GamepadRotateModel = require("GamepadRotateModel")
local InputObjectUtils = require("InputObjectUtils")
-- Stolen directly from ROBLOX's core scripts.
-- Looks like a simple integrator.
-- Called (zoom, zoomScale, 1) returns zoom
local function rk4Integrator(position, velocity, t)
local direction = velocity < 0 and -1 or 1
local function acceleration(p, _)
local accel = direction * math.max(1, (p / 3.3) + 0.5)
return accel
end
local p1 = position
local v1 = velocity
local a1 = acceleration(p1, v1)
local p2 = p1 + v1 * (t / 2)
local v2 = v1 + a1 * (t / 2)
local a2 = acceleration(p2, v2)
local p3 = p1 + v2 * (t / 2)
local v3 = v1 + a2 * (t / 2)
local a3 = acceleration(p3, v3)
local p4 = p1 + v3 * t
local v4 = v1 + a3 * t
local a4 = acceleration(p4, v4)
local positionResult = position + (v1 + 2 * v2 + 2 * v3 + v4) * (t / 6)
local velocityResult = velocity + (a1 + 2 * a2 + 2 * a3 + a4) * (t / 6)
return positionResult, velocityResult
end
local CameraControls = {}
CameraControls.__index = CameraControls
CameraControls.ClassName = "CameraControls"
CameraControls.MOUSE_SENSITIVITY = Vector2.new(math.pi*4, math.pi*1.9)
CameraControls._dragBeginTypes = { Enum.UserInputType.MouseButton2, Enum.UserInputType.Touch }
function CameraControls.new(zoomCamera, rotatedCamera)
local self = setmetatable({}, CameraControls)
self._enabled = false
self._key = tostring(self) .. "CameraControls"
-- Destroyed below
self._gamepadRotateModel = GamepadRotateModel.new()
if zoomCamera then
self:SetZoomedCamera(zoomCamera)
end
if rotatedCamera then
self:SetRotatedCamera(rotatedCamera)
end
return self
end
--[=[
Sets the gamepad rotation acceleration
@param acceleration number
]=]
function CameraControls:SetGamepadRotationAcceleration(acceleration)
assert(type(acceleration) == "number", "Bad acceleration")
self._gamepadRotateModel:SetAcceleration(acceleration)
end
function CameraControls:GetKey()
return self._key
end
function CameraControls:IsEnabled()
return self._enabled
end
--[=[
Enables the control.
]=]
function CameraControls:Enable()
if self._enabled then
return
end
assert(not self._maid, "Maid already defined")
self._enabled = true
self._maid = Maid.new()
self._maid:GiveTask(self._gamepadRotateModel.IsRotating.Changed:Connect(function()
if self._gamepadRotateModel.IsRotating.Value then
self:_handleGamepadRotateStart()
else
self:_handleGamepadRotateStop()
end
end))
ContextActionService:BindAction(self._key, function(_, _, inputObject)
if inputObject.UserInputType == Enum.UserInputType.MouseWheel then
self:_handleMouseWheel(inputObject)
end
end, false, Enum.UserInputType.MouseWheel)
ContextActionService:BindAction(self._key .. "Drag", function(_, userInputState, inputObject)
if userInputState == Enum.UserInputState.Begin then
self:BeginDrag(inputObject)
end
end, false, unpack(self._dragBeginTypes))
ContextActionService:BindAction(self._key .. "Rotate", function(_, _, inputObject)
self:_handleThumbstickInput(inputObject)
end, false, Enum.KeyCode.Thumbstick2)
self._maid:GiveTask(UserInputService.TouchPinch:Connect(function(_, scale, velocity, userInputState)
self:_handleTouchPinch(scale, velocity, userInputState)
end))
self._maid:GiveTask(function()
ContextActionService:UnbindAction(self._key)
ContextActionService:UnbindAction(self._key .. "Drag")
ContextActionService:UnbindAction(self._key .. "Rotate")
end)
end
--[=[
Disables the control.
]=]
function CameraControls:Disable()
if not self._enabled then
return
end
self._enabled = false
self._maid:DoCleaning()
self._maid = nil
self._lastMousePosition = nil
end
function CameraControls:BeginDrag(beginInputObject)
if not self._rotatedCamera then
self._maid._dragMaid = nil
return
end
local maid = Maid.new()
self._lastMousePosition = beginInputObject.Position
local isMouse = InputObjectUtils.isMouseUserInputType(beginInputObject.UserInputType)
if isMouse then
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
end
maid:GiveTask(UserInputService.InputEnded:Connect(function(inputObject, _)
if inputObject == beginInputObject then
self:_endDrag()
end
end))
maid:GiveTask(UserInputService.InputChanged:Connect(function(inputObject)
if InputObjectUtils.isMouseUserInputType(inputObject.UserInputType) or inputObject == beginInputObject then
self:_handleMouseMovement(inputObject)
end
end))
if self._rotatedCamera.ClassName == "SmoothRotatedCamera" then
self._rotVelocityTracker = self:_getVelocityTracker(0.05, Vector2.zero)
end
self._maid._dragMaid = maid
end
function CameraControls:SetZoomedCamera(zoomedCamera)
self._zoomedCamera = assert(zoomedCamera, "Bad zoomedCamera")
self._startZoomScale = self._zoomedCamera.Zoom
return self
end
function CameraControls:SetRotatedCamera(rotatedCamera)
self._rotatedCamera = assert(rotatedCamera, "Bad rotatedCamera")
return self
end
-- This code was the same algorithm used by Roblox. It makes it so you can zoom easier at further distances.
function CameraControls:_handleMouseWheel(inputObject)
if self._zoomedCamera then
local delta = math.clamp(-inputObject.Position.Z, -1, 1)*1.4
local zoom = rk4Integrator(self._zoomedCamera.TargetZoom, delta, 1)
self._zoomedCamera.TargetZoom = zoom
end
if self._rotatedCamera then
if self._rotatedCamera.ClassName == "PushCamera" then
self._rotatedCamera:StopRotateBack()
end
end
end
function CameraControls:_handleTouchPinch(scale, velocity, userInputState)
if self._zoomedCamera then
if userInputState == Enum.UserInputState.Begin then
self._startZoomScale = self._zoomedCamera.Zoom
self._zoomedCamera.Zoom = self._startZoomScale*1/scale
elseif userInputState == Enum.UserInputState.End then
self._zoomedCamera.Zoom = self._startZoomScale*1/scale
self._zoomedCamera.TargetZoom = self._zoomedCamera.Zoom + -velocity/5
elseif userInputState == Enum.UserInputState.Change then
if self._startZoomScale then
self._zoomedCamera.TargetZoom = self._startZoomScale*1/scale
self._zoomedCamera.Zoom = self._zoomedCamera.TargetZoom
else
warn("[CameraControls._handleTouchPinch] - No self._startZoomScale")
end
end
end
end
-- This is also a Roblox algorithm. Not sure why screen resolution is locked like it is.
function CameraControls:_mouseTranslationToAngle(translationVector)
local xTheta = (translationVector.x / 1920)
local yTheta = (translationVector.y / 1200)
return Vector2.new(xTheta, yTheta)
end
function CameraControls:_getVelocityTracker(strength, startVelocity)
strength = strength or 1
local lastUpdate = tick()
local velocity = startVelocity
return {
Update = function(_, delta)
local elapsed = tick() - lastUpdate
lastUpdate = tick()
velocity = velocity / (2^(elapsed/strength)) + (delta / (0.0001 + elapsed)) * strength
end;
GetVelocity = function(this)
this:Update(startVelocity*0)
return velocity
end;
}
end
function CameraControls:_handleMouseMovement(inputObject)
if self._lastMousePosition then
if self._rotatedCamera then
-- This calculation may seem weird, but either .Position updates (if it's locked), or .Delta updates (if it's not).
local delta
if InputObjectUtils.isMouseUserInputType(inputObject.UserInputType) then
delta = -inputObject.Delta + self._lastMousePosition - inputObject.Position
else
delta = -inputObject.Delta
end
local deltaAngle = self:_mouseTranslationToAngle(delta) * self.MOUSE_SENSITIVITY
self._rotatedCamera:RotateXY(deltaAngle)
if self._rotVelocityTracker then
self._rotVelocityTracker:Update(deltaAngle)
end
end
self._lastMousePosition = inputObject.Position
end
end
function CameraControls:_handleThumbstickInput(inputObject)
self._gamepadRotateModel:HandleThumbstickInput(inputObject)
end
function CameraControls:_applyRotVelocityTracker(rotVelocityTracker)
if self._rotatedCamera then
local position = self._rotatedCamera.AngleXZ
local velocity = rotVelocityTracker:GetVelocity().X
local newVelocityTarget = position + velocity
local target = self._rotatedCamera.TargetAngleXZ
if math.abs(newVelocityTarget - position) > math.abs(target - position) then
self._rotatedCamera.TargetAngleXZ = newVelocityTarget
end
self._rotatedCamera:SnapIntoBounds()
end
end
function CameraControls:_endDrag()
if self._rotVelocityTracker then
self:_applyRotVelocityTracker(self._rotVelocityTracker)
self._rotVelocityTracker = nil
end
self._lastMousePosition = nil
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
self._maid._dragMaid = nil
end
function CameraControls:_handleGamepadRotateStop()
if self._rotVelocityTracker then
self:_applyRotVelocityTracker(self._rotVelocityTracker)
self._rotVelocityTracker = nil
end
self._maid._dragMaid = nil
end
function CameraControls:_handleGamepadRotateStart()
if not self._rotatedCamera then
self._maid._dragMaid = nil
return
end
local maid = Maid.new()
if self._rotatedCamera.ClassName == "SmoothRotatedCamera" then
self._rotVelocityTracker = self:_getVelocityTracker(0.05, Vector2.zero)
end
maid:GiveTask(RunService.Stepped:Connect(function()
local deltaAngle = 0.1*self._gamepadRotateModel:GetThumbstickDeltaAngle()
if self._rotatedCamera then
self._rotatedCamera:RotateXY(deltaAngle)
end
if self._rotVelocityTracker then
self._rotVelocityTracker:Update(deltaAngle)
end
end))
self._maid._dragMaid = maid
end
function CameraControls:Destroy()
self._gamepadRotateModel:Destroy()
self:Disable()
setmetatable(self, nil)
end
return CameraControls