cameras/followcamera.lua
45678910111213141516171819
FollowCamera = Class(function(self, inst)
self.inst = inst
self.target = nil
self:SetDefault()
self:Snap()
self.time_since_zoom = nil
self.currentpos = Vector3(0,0,0)
end)
function FollowCamera:SetDefault()
self.targetpos = Vector3(0,0,0)
self.targetoffset = Vector3(0,1.5,0)
if self.headingtarget == nil then
45678910111213141516171819
FollowCamera = Class(function(self, inst)
self.inst = inst
self.target = nil
self.currentpos = Vector3(0,0,0)
self.distance = 30
self:SetDefault()
self:Snap()
self.time_since_zoom = nil
end)
function FollowCamera:SetDefault()
self.targetpos = Vector3(0,0,0)
--self.currentpos = Vector3(0,0,0)
self.targetoffset = Vector3(0,1.5,0)
if self.headingtarget == nil then
30313233343536
self.mindist = 15
self.maxdist = 50 --40
self.mindistpitch = 30
self.maxdistpitch = 60--60
self.paused = false
30313233343536
self.mindist = 15
self.maxdist = 50 --40
self.mindistpitch = 30
self.maxdistpitch = 60--60
self.paused = false
474849505152
self.distancetarget = 25
end
end
function FollowCamera:GetRightVec()
47484950515253545556
self.distancetarget = 25
end
if self.target then
self:SetTarget(self.target)
end
end
function FollowCamera:GetRightVec()
84858687888990
end
function FollowCamera:SetOffset(offset)
self.targetoffset = offset
end
function FollowCamera:GetDistance()
88899091929394
end
function FollowCamera:SetOffset(offset)
self.targetoffset.x, self.targetoffset.y, self.targetoffset.z = offset.x, offset.y, offset.z
end
function FollowCamera:GetDistance()
102103104105106107108109110111112113114115116117118119120121122123124125126127128129
function FollowCamera:SetTarget(inst)
self.target = inst
self.targetpos.x, self.targetpos.y, self.targetpos.z = self.target.Transform:GetWorldPosition()
self.currentpos.x, self.currentpos.y, self.currentpos.z = self.target.Transform:GetWorldPosition()
end
function FollowCamera:Apply()
local dir = Vector3(0,0,0)
dir.x = -math.cos(self.pitch*DEGREES)*math.cos(self.heading*DEGREES)
dir.y = -math.sin(self.pitch*DEGREES)
dir.z = -math.cos(self.pitch*DEGREES)*math.sin(self.heading*DEGREES)
local pos = dir*(-self.distance) + self.currentpos
local right = Vector3(math.cos((self.heading+90)*DEGREES), 0, math.sin((self.heading+90)*DEGREES))
local up = dir:Cross(right)
TheSim:SetCameraPos(pos.x,pos.y,pos.z)
TheSim:SetCameraDir(dir.x,dir.y,dir.z)
TheSim:SetCameraUp(up.x, up.y, up.z)
TheSim:SetCameraFOV(self.fov)
local listenpos = dir*(-self.distance*.1) + self.currentpos
TheSim:SetListener(listenpos.x, listenpos.y, listenpos.z, dir.x, dir.y, dir.z, up.x, up.y, up.z)
end
106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
function FollowCamera:SetTarget(inst)
self.target = inst
self.targetpos.x, self.targetpos.y, self.targetpos.z = self.target.Transform:GetWorldPosition()
--self.currentpos.x, self.currentpos.y, self.currentpos.z = self.target.Transform:GetWorldPosition()
end
function FollowCamera:Apply()
--dir
local dx = -math.cos(self.pitch*DEGREES)*math.cos(self.heading*DEGREES)
local dy = -math.sin(self.pitch*DEGREES)
local dz = -math.cos(self.pitch*DEGREES)*math.sin(self.heading*DEGREES)
--pos
local px = dx*(-self.distance) + self.currentpos.x
local py = dy*(-self.distance) + self.currentpos.y
local pz = dz*(-self.distance) + self.currentpos.z
--right
local rx = math.cos((self.heading+90)*DEGREES)
local ry = 0
local rz = math.sin((self.heading+90)*DEGREES)
--up
local ux, uy, uz = dy * rz - dz * ry,
dz * rx - dx * rz,
dx * ry - dy * rx
TheSim:SetCameraPos(px,py,pz)
TheSim:SetCameraDir(dx, dy, dz)
TheSim:SetCameraUp(ux, uy, uz)
TheSim:SetCameraFOV(self.fov)
--local listenpos = dir*(-self.distance*.1) + self.currentpos
--listen dist
local lx = dx*(-self.distance*.1) + self.currentpos.x
local ly = dy*(-self.distance*.1) + self.currentpos.y
local lz = dz*(-self.distance*.1) + self.currentpos.z
--print (lx, ly, lz, self.distance)
TheSim:SetListener(lx, ly, lz, dx, dy, dz, ux, uy, uz)
end
166167168169170171172173174175
if self.target then
self.targetpos = Vector3(self.target.Transform:GetWorldPosition()) + self.targetoffset
else
self.targetpos = self.targetoffset
end
self.currentpos = self.targetpos
self.heading = self.headingtarget
self.distance = self.distancetarget
188189190191192193194195196197
if self.target then
self.targetpos = Vector3(self.target.Transform:GetWorldPosition()) + self.targetoffset
else
self.targetpos.x,self.targetpos.y,self.targetpos.z = self.targetoffset.x,self.targetoffset.y,self.targetoffset.z
end
self.currentpos.x, self.currentpos.y, self.currentpos.z = self.targetpos.x, self.targetpos.y, self.targetpos.z
self.heading = self.headingtarget
self.distance = self.distancetarget
184185186187188189190
end
function FollowCamera:SetCustomLocation(loc)
self.targetpos = loc
end
function FollowCamera:Update(dt)
206207208209210211212
end
function FollowCamera:SetCustomLocation(loc)
self.targetpos.x,self.targetpos.y,self.targetpos.z = loc.x,loc.y,loc.z
end
function FollowCamera:Update(dt)
195196197198199200201
if self.cutscene then
self.currentpos = lerp(self.currentpos, self.targetpos + self.targetoffset, dt*self.pangain)
if self.shake then
217218219220221222223224225
if self.cutscene then
self.currentpos.x = lerp(self.currentpos.x, self.targetpos.x + self.targetoffset.x, dt*self.pangain)
self.currentpos.y = lerp(self.currentpos.y, self.targetpos.y + self.targetoffset.y, dt*self.pangain)
self.currentpos.z = lerp(self.currentpos.z, self.targetpos.z + self.targetoffset.z, dt*self.pangain)
if self.shake then
203204205206207208209
if shakeOffset then
local upOffset = Vector3(0, shakeOffset.y, 0)
local rightOffset = self:GetRightVec() * shakeOffset.x
self.currentpos = self.currentpos + upOffset + rightOffset
else
self.shake = nil
end
227228229230231232233234235
if shakeOffset then
local upOffset = Vector3(0, shakeOffset.y, 0)
local rightOffset = self:GetRightVec() * shakeOffset.x
self.currentpos.x = self.currentpos.x + upOffset.x + rightOffset.x
self.currentpos.y = self.currentpos.y + upOffset.y + rightOffset.y
self.currentpos.z = self.currentpos.z + upOffset.z + rightOffset.z
else
self.shake = nil
end
230231232233234235236237238239240241
end
if self.target then
self.targetpos = Vector3(self.target.Transform:GetWorldPosition()) + self.targetoffset
else
self.targetpos = self.targetoffset
end
self.currentpos = lerp(self.currentpos, self.targetpos, dt*self.pangain)
if self.shake then
local shakeOffset = self.shake:Update(dt)
256257258259260261262263264265266267268269270271272273274275276
end
if self.target then
--self.targetpos = Vector3(self.target.Transform:GetWorldPosition()) + self.targetoffset
local x, y, z = self.target.Transform:GetWorldPosition()
self.targetpos.x = x + self.targetoffset.x
self.targetpos.y = y + self.targetoffset.y
self.targetpos.z = z + self.targetoffset.z
else
self.targetpos.x, self.targetpos.y, self.targetpos.z = self.targetoffset.x, self.targetoffset.y, self.targetoffset.z
end
self.currentpos.x = lerp(self.currentpos.x, self.targetpos.x, dt*self.pangain)
self.currentpos.y = lerp(self.currentpos.y, self.targetpos.y, dt*self.pangain)
self.currentpos.z = lerp(self.currentpos.z, self.targetpos.z, dt*self.pangain)
--self.currentpos = lerp(self.currentpos, self.targetpos, dt*self.pangain)
if self.shake then
local shakeOffset = self.shake:Update(dt)
242243244245246247248
if shakeOffset then
local upOffset = Vector3(0, shakeOffset.y, 0)
local rightOffset = self:GetRightVec() * shakeOffset.x
self.currentpos = self.currentpos + upOffset + rightOffset
else
self.shake = nil
end
277278279280281282283284285
if shakeOffset then
local upOffset = Vector3(0, shakeOffset.y, 0)
local rightOffset = self:GetRightVec() * shakeOffset.x
self.currentpos.x = self.currentpos.x + upOffset.x + rightOffset.x
self.currentpos.y = self.currentpos.y + upOffset.y + rightOffset.y
self.currentpos.z = self.currentpos.z + upOffset.z + rightOffset.z
else
self.shake = nil
end