-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.lua
232 lines (203 loc) · 7.08 KB
/
vector.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
local module = {
_version = "vector.lua v2019.14.12",
_description = "a simple vector library for Lua based on the PVector class from processing",
_url = "https://github.com/themousery/vector.lua",
_license = [[
Copyright (c) 2018 themousery
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
}
-- create the module
local vector = {}
vector.__index = vector
-- get a random function from Love2d or base lua, in that order.
local rand = math.random
if love and love.math then rand = love.math.random end
-- makes a new vector
local function new(x,y,z)
return setmetatable({x=x or 0, y=y or 0, z=z or 0}, vector)
end
-- makes a new vector from an angle
local function fromAngle(theta)
return new(math.cos(theta), -math.sin(theta))
end
-- makes a vector with a random direction
local function random()
return fromAngle(rand() * math.pi*2)
end
-- check if an object is a vector
local function isvector(t)
return getmetatable(t) == vector
end
-- set the values of the vector to something new
function vector:set(x,y,z)
if isvector(x) then self.x, self.y, self.z = x.x, x.y,x.z;return end
self.x, self.y,self.z = x or self.x, y or self.y, z or self.z
return self
end
-- replace the values of a vector with the values of another vector
function vector:replace(v)
assert(isvector(v), "replace: wrong argument type: (expected <vector>, got "..type(v)..")")
self.x, self.y,self.z = v.x, v.y,v.z
return self
end
-- returns a copy of a vector
function vector:clone()
return new(self.x, self.y,self.z)
end
-- get the magnitude of a vector
function vector:getmag()
return math.sqrt(self.x^2 + self.y^2+self.z^2)
end
-- get the magnitude squared of a vector
function vector:magSq()
return self.x^2 + self.y^2 + self.z^2
end
-- set the magnitude of a vector
function vector:setmag(mag)
self:norm()
local v = self * mag
self:replace(v)
return self
end
-- meta function to make vectors negative
-- ex: (negative) -vector(5,6) is the same as vector(-5,-6)
function vector.__unm(v)
return new(-v.x, -v.y,-v.z)
end
-- meta function to add vectors together
-- ex: (vector(5,6) + vector(6,5)) is the same as vector(11,11)
function vector.__add(a,b)
assert(isvector(a) and isvector(b), "add: wrong argument types: (expected <vector> and <vector>)")
return new(a.x+b.x, a.y+b.y,a.z+b.z)
end
-- meta function to subtract vectors
function vector.__sub(a,b)
assert(isvector(a) and isvector(b), "sub: wrong argument types: (expected <vector> and <vector>)")
return new(a.x-b.x, a.y-b.y,a.z-b.z)
end
-- meta function to multiply vectors
function vector.__mul(a,b)
if type(a) == 'number' then
return new(a * b.x, a * b.y,a*b.z)
elseif type(b) == 'number' then
return new(a.x * b, a.y * b,a.z * b)
else
assert(isvector(a) and isvector(b), "mul: wrong argument types: (expected <vector> or <number>)")
return new(a.x*b.x, a.y*b.y,a.z*b.z)
end
end
-- meta function to divide vectors
function vector.__div(a,b)
if type(a) == 'number' then
return new(a / b.x, a / b.y,a/b.z)
elseif type(b) == 'number' then
return new(a.x / b, a.y / b,a.z / b)
else
assert(isvector(a) and isvector(b), "mul: wrong argument types: (expected <vector> or <number>)")
return new(a.x/b.x, a.y/b.y,a.z/b.z)
end
end
-- meta function to check if vectors have the same values
function vector.__eq(a,b)
assert(isvector(a) and isvector(b), "eq: wrong argument types (expected <vector> and <vector>)")
return a.x==b.x and a.y==b.y and a.z ==b.z
end
-- meta function to change how vectors appear as string
-- ex: print(vector(2,8)) - this prints '(2,8)'
function vector:__tostring()
return "("..self.x..", "..self.y..", "..self.z..")"
end
-- get the distance between two vectors
function vector.dist(a,b)
assert(isvector(a) and isvector(b), "dist: wrong argument types (expected <vector> and <vector>)")
return math.sqrt((a.x-b.x)^2 + (a.y-b.y)^2 + (a.z-b.z)^2)
end
-- return the dot product of the vector
function vector:dot(v)
assert(isvector(v), "dot: wrong argument type (expected <vector>)")
return self.x * v.x + self.y * v.y
end
-- normalize the vector (give it a magnitude of 1)
function vector:norm()
local m = self:getmag()
if m~=0 then
self:replace(self / m)
end
return self
end
-- limit the vector to a certain amount
function vector:limit(max)
assert(type(max) == 'number', "limit: wrong argument type (expected <number>)")
local mSq = self:magSq()
if mSq > max^2 then
self:setmag(max)
end
return self
end
-- Clamp each axis between max and min's corresponding axis
function vector:clamp(min, max)
assert(isvector(min) and isvector(max), "clamp: wrong argument type (expected <vector>) and <vector>")
local x = math.min( math.max( self.x, min.x ), max.x )
local y = math.min( math.max( self.y, min.y ), max.y )
local z = math.min( math.max( self.z, min.z ), max.z )
self:set(x,y,z)
return self
end
-- get the heading (direction) of a vector
function vector:heading()
return -math.atan2(self.y, self.x)
end
-- rotate a vector clockwise by a certain number of radians
function vector:rotate(theta)
local s = math.sin(theta)
local c = math.cos(theta)
local v = new(
(c * self.x) + (s * self.y),
-(s * self.x) + (c * self.y))
self:replace(v)
return self
end
-- return x and y of vector as a regular array
function vector:array()
return {self.x, self.y,self.z}
end
-- return x and y of vector, unpacked from table
function vector:unpack()
return self.x, self.y,self.z
end
function vector:floor()
local floor = math.floor
return self:set(floor(self.x),floor(self.y),floor(self.z))
end
function vector:ceil()
local ceil = math.ceil
return self:set(ceil(self.x),ceil(self.y),ceil(self.z))
end
function vector:specialCeil()
local sign = math.sign
local ceil = math.ceil
local nx,ny,nz = sign(self.x),sign(self.y),sign(self.z)
local x,y,z = ceil(self.x*nx),ceil(self.y*ny),ceil(self.z*nz)
return self:set(x*nx,y*ny,z*nz)
end
-- pack up and return module
module.new = new
module.random = random
module.fromAngle = fromAngle
module.isvector = isvector
return setmetatable(module, {__call = function(_,...) return new(...) end})