-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcamera.lua
More file actions
65 lines (56 loc) · 1.56 KB
/
Copy pathcamera.lua
File metadata and controls
65 lines (56 loc) · 1.56 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
require "physics"
require "render"
require "Vector"
camera = {
pos = Vector(0, 0, 0.6),
dir = Vector(),
plane = Vector(),
}
function camera:setangle(r)
self.dir:set(1, 0):rotate2d(r)
self.plane:set(0, 1):rotate2d(r)
end
camera:setangle(0)
function camera:getangle()
return self.dir:angle2d()
end
local temp = Vector()
function camera:getscreenpoint(x, y, z)
local dist = temp:set(x, y, z):sub(self.pos):projectscalar(self.dir)
local scanx = temp:set(x, y, z):sub(self.pos):projectscalar(self.plane)
scanx = ((scanx / (dist * self.plane:len()) + 1) / 2) * screen.width
local screeny = self.pos.z - z
return dist, scanx, screeny
end
function camera.visiblepredicate(obj)
return type(obj) == "table" and not obj.invisible
end
function camera.transparentpredicate(obj)
return type(obj) == "table" and obj.transparent
end
local function drawraycasthit(start, dir, pos, dist, obj, hitindex, axis, sign, i, j, scanx, distfactor)
dist = dist * distfactor
local info = {}
info.x, info.y, info.z = pos.x, pos.y, 0
info.dist = dist
info.scanx = scanx
info.i, info.j = i, j
info.axis = axis
info.sign = sign
render.render(obj, info)
end
local dir, pos = Vector(), Vector()
function camera:render()
local inc = 2 / screen.width
local i = -1
local scanx = 0
pos:set(self.pos)
pos.z = 0
while i < 1 do
dir:set(self.plane):scale(i):add(self.dir):norm()
local distfactor = dir:projectscalar(self.dir)
physics.Domain.current:raycast(pos, dir, nil, drawraycasthit, self.visiblepredicate, self.transparentpredicate, scanx, distfactor)
i = i + inc
scanx = scanx + 1
end
end