-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvec3_v3.p8
131 lines (107 loc) · 2.04 KB
/
vec3_v3.p8
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
function round(n)
return flr(n+0.5)
end
function lerp(a, b, t)
return (1-t)*a + t*b
end
function damp(a, b, rem)
return lerp(a, b, 1-rem^.0167)
end
function rotate(x,y,angle)
local nx=x*cos(angle)+y*sin(angle)
local ny=-x*sin(angle)+y*cos(angle)
return nx,ny
end
vec3 = {}
vec3.__index = vec3
function vec3_new(x, y, z)
return setmetatable({
x = x or 0,
y = y or 0,
z = z or 0,
}, vec3)
end
function vec3.zero(v)
v.x, v.y, v.z = 0, 0, 0
return v
end
function vec3.mul(v, c)
v.x *= c
v.y *= c
v.z *= c
return v
end
function vec3.assign(a, b)
a.x = b.x
a.y = b.y
a.z = b.z
return a
end
function vec3.add(a, b)
a.x += b.x
a.y += b.y
a.z += b.z
return a
end
function vec3.sub(a, b)
a.x -= b.x
a.y -= b.y
a.z -= b.z
return a
end
function vec3.tostr(v)
return v.x .. ',' .. v.y .. ',' .. v.z
end
function vec3.debug(v)
print(v:tostr())
end
function vec3.damp(a, b, rem)
a.x, a.y, a.z = damp(a.x, b.x, rem), damp(a.y, b.y, rem), damp(a.z, b.z, rem)
return a
end
function vec3.dupe(v)
return vec3_new(v.x, v.y, v.z)
end
function vec3.round(v)
v.x = flr(v.x+.5)
v.y = flr(v.y+.5)
v.z = flr(v.z+.5)
return v
end
function vec3.normalize(v)
local d = v:mag()
if d==0 then return v end
v.x /= d
v.y /= d
v.z /= d
return v
end
function vec3.mag(a)
return a:dist_between(vec3_new())
end
function vec3.dist_between(a, b)
-- scale inputs down by 6 bits
local dx=(a.x-b.x)/64
local dy=(a.y-b.y)/64
local dz=(a.z-b.z)/64
-- get distance squared
local dsq=dx*dx + dy*dy + dz*dz
-- in case of overflow/wrap
if(dsq<0) then return 32767.99999 end
-- scale output back up by 6 bits
return sqrt(dsq)*64
end
function vec3.rotate(v, angle)
v.x, v.y = rotate(v.x, v.y, angle)
return v
end
function vec3.lerp(v1, v2, t)
v1.x = lerp(v1.x, v2.x, t)
v1.y = lerp(v1.y, v2.y, t)
v1.z = lerp(v1.z, v2.z, t)
return v1
end
function grab_inputs()
i_left, i_right, i_up, i_down, i_z, i_x = btn(0), btn(1), btn(2), btn(3), btn(4), btn(5)
p_left, p_right, p_up, p_down, p_z, p_x = btnp(0), btnp(1), btnp(2), btnp(3), btnp(4), btnp(5)
end