3 Note: The vector.*-functions must be able to accept old vectors that had no metatables
7 local setmetatable = setmetatable
12 vector.metatable = metatable
14 local xyz = {"x", "y", "z"}
16 -- only called when rawget(v, key) returns nil
17 function metatable.__index(v, key)
18 return rawget(v, xyz[key]) or vector[key]
21 -- only called when rawget(v, key) returns nil
22 function metatable.__newindex(v, key, value)
23 rawset(v, xyz[key] or key, value)
28 local function fast_new(x, y, z)
29 return setmetatable({x = x, y = y, z = z}, metatable)
32 function vector.new(a, b, c)
34 return fast_new(a, b, c)
37 -- deprecated, use vector.copy and vector.zero directly
38 if type(a) == "table" then
41 assert(not a, "Invalid arguments for vector.new()")
46 function vector.zero()
47 return fast_new(0, 0, 0)
50 function vector.copy(v)
51 assert(v.x and v.y and v.z, "Invalid vector passed to vector.copy()")
52 return fast_new(v.x, v.y, v.z)
55 function vector.from_string(s, init)
56 local x, y, z, np = string.match(s, "^%s*%(%s*([^%s,]+)%s*[,%s]%s*([^%s,]+)%s*[,%s]" ..
57 "%s*([^%s,]+)%s*[,%s]?%s*%)()", init)
61 if not (x and y and z) then
64 return fast_new(x, y, z), np
67 function vector.to_string(v)
68 return string.format("(%g, %g, %g)", v.x, v.y, v.z)
70 metatable.__tostring = vector.to_string
72 function vector.equals(a, b)
77 metatable.__eq = vector.equals
81 function vector.length(v)
82 return math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
84 -- Note: we can not use __len because it is already used for primitive table length
86 function vector.normalize(v)
87 local len = vector.length(v)
89 return fast_new(0, 0, 0)
91 return vector.divide(v, len)
95 function vector.floor(v)
96 return vector.apply(v, math.floor)
99 function vector.round(v)
107 function vector.apply(v, func)
115 function vector.distance(a, b)
119 return math.sqrt(x * x + y * y + z * z)
122 function vector.direction(pos1, pos2)
123 return vector.subtract(pos2, pos1):normalize()
126 function vector.angle(a, b)
127 local dotp = vector.dot(a, b)
128 local cp = vector.cross(a, b)
129 local crossplen = vector.length(cp)
130 return math.atan2(crossplen, dotp)
133 function vector.dot(a, b)
134 return a.x * b.x + a.y * b.y + a.z * b.z
137 function vector.cross(a, b)
139 a.y * b.z - a.z * b.y,
140 a.z * b.x - a.x * b.z,
141 a.x * b.y - a.y * b.x
145 function metatable.__unm(v)
146 return fast_new(-v.x, -v.y, -v.z)
149 -- add, sub, mul, div operations
151 function vector.add(a, b)
152 if type(b) == "table" then
166 function metatable.__add(a, b)
174 function vector.subtract(a, b)
175 if type(b) == "table" then
189 function metatable.__sub(a, b)
197 function vector.multiply(a, b)
198 if type(b) == "table" then
212 function metatable.__mul(a, b)
213 if type(a) == "table" then
228 function vector.divide(a, b)
229 if type(b) == "table" then
243 function metatable.__div(a, b)
244 -- scalar/vector makes no sense
254 function vector.offset(v, x, y, z)
262 function vector.sort(a, b)
263 return fast_new(math.min(a.x, b.x), math.min(a.y, b.y), math.min(a.z, b.z)),
264 fast_new(math.max(a.x, b.x), math.max(a.y, b.y), math.max(a.z, b.z))
267 function vector.check(v)
268 return getmetatable(v) == metatable
271 local function sin(x)
272 if x % math.pi == 0 then
279 local function cos(x)
280 if x % math.pi == math.pi / 2 then
287 function vector.rotate_around_axis(v, axis, angle)
288 local cosangle = cos(angle)
289 local sinangle = sin(angle)
290 axis = vector.normalize(axis)
291 -- https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
292 local dot_axis = vector.multiply(axis, vector.dot(axis, v))
293 local cross = vector.cross(v, axis)
295 cross.x * sinangle + (v.x - dot_axis.x) * cosangle + dot_axis.x,
296 cross.y * sinangle + (v.y - dot_axis.y) * cosangle + dot_axis.y,
297 cross.z * sinangle + (v.z - dot_axis.z) * cosangle + dot_axis.z
301 function vector.rotate(v, rot)
302 local sinpitch = sin(-rot.x)
303 local sinyaw = sin(-rot.y)
304 local sinroll = sin(-rot.z)
305 local cospitch = cos(rot.x)
306 local cosyaw = cos(rot.y)
307 local cosroll = math.cos(rot.z)
308 -- Rotation matrix that applies yaw, pitch and roll
311 sinyaw * sinpitch * sinroll + cosyaw * cosroll,
312 sinyaw * sinpitch * cosroll - cosyaw * sinroll,
321 cosyaw * sinpitch * sinroll - sinyaw * cosroll,
322 cosyaw * sinpitch * cosroll + sinyaw * sinroll,
326 -- Compute matrix multiplication: `matrix` * `v`
328 matrix[1][1] * v.x + matrix[1][2] * v.y + matrix[1][3] * v.z,
329 matrix[2][1] * v.x + matrix[2][2] * v.y + matrix[2][3] * v.z,
330 matrix[3][1] * v.x + matrix[3][2] * v.y + matrix[3][3] * v.z
334 function vector.dir_to_rotation(forward, up)
335 forward = vector.normalize(forward)
336 local rot = vector.new(math.asin(forward.y), -math.atan2(forward.x, forward.z), 0)
340 assert(vector.dot(forward, up) < 0.000001,
341 "Invalid vectors passed to vector.dir_to_rotation().")
342 up = vector.normalize(up)
343 -- Calculate vector pointing up with roll = 0, just based on forward vector.
344 local forwup = vector.rotate(vector.new(0, 1, 0), rot)
345 -- 'forwup' and 'up' are now in a plane with 'forward' as normal.
346 -- The angle between them is the absolute of the roll value we're looking for.
347 rot.z = vector.angle(forwup, up)
349 -- Since vector.angle never returns a negative value or a value greater
350 -- than math.pi, rot.z has to be inverted sometimes.
351 -- To determine wether this is the case, we rotate the up vector back around
352 -- the forward vector and check if it worked out.
353 local back = vector.rotate_around_axis(up, forward, -rot.z)
355 -- We don't use vector.equals for this because of floating point imprecision.
356 if (back.x - forwup.x) * (back.x - forwup.x) +
357 (back.y - forwup.y) * (back.y - forwup.y) +
358 (back.z - forwup.z) * (back.z - forwup.z) > 0.0000001 then