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 cannot 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.combine(a, b, func)
123 function vector.distance(a, b)
127 return math.sqrt(x * x + y * y + z * z)
130 function vector.direction(pos1, pos2)
131 return vector.subtract(pos2, pos1):normalize()
134 function vector.angle(a, b)
135 local dotp = vector.dot(a, b)
136 local cp = vector.cross(a, b)
137 local crossplen = vector.length(cp)
138 return math.atan2(crossplen, dotp)
141 function vector.dot(a, b)
142 return a.x * b.x + a.y * b.y + a.z * b.z
145 function vector.cross(a, b)
147 a.y * b.z - a.z * b.y,
148 a.z * b.x - a.x * b.z,
149 a.x * b.y - a.y * b.x
153 function metatable.__unm(v)
154 return fast_new(-v.x, -v.y, -v.z)
157 -- add, sub, mul, div operations
159 function vector.add(a, b)
160 if type(b) == "table" then
174 function metatable.__add(a, b)
182 function vector.subtract(a, b)
183 if type(b) == "table" then
197 function metatable.__sub(a, b)
205 function vector.multiply(a, b)
206 if type(b) == "table" then
220 function metatable.__mul(a, b)
221 if type(a) == "table" then
236 function vector.divide(a, b)
237 if type(b) == "table" then
251 function metatable.__div(a, b)
252 -- scalar/vector makes no sense
262 function vector.offset(v, x, y, z)
270 function vector.sort(a, b)
271 return fast_new(math.min(a.x, b.x), math.min(a.y, b.y), math.min(a.z, b.z)),
272 fast_new(math.max(a.x, b.x), math.max(a.y, b.y), math.max(a.z, b.z))
275 function vector.check(v)
276 return getmetatable(v) == metatable
279 local function sin(x)
280 if x % math.pi == 0 then
287 local function cos(x)
288 if x % math.pi == math.pi / 2 then
295 function vector.rotate_around_axis(v, axis, angle)
296 local cosangle = cos(angle)
297 local sinangle = sin(angle)
298 axis = vector.normalize(axis)
299 -- https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
300 local dot_axis = vector.multiply(axis, vector.dot(axis, v))
301 local cross = vector.cross(v, axis)
303 cross.x * sinangle + (v.x - dot_axis.x) * cosangle + dot_axis.x,
304 cross.y * sinangle + (v.y - dot_axis.y) * cosangle + dot_axis.y,
305 cross.z * sinangle + (v.z - dot_axis.z) * cosangle + dot_axis.z
309 function vector.rotate(v, rot)
310 local sinpitch = sin(-rot.x)
311 local sinyaw = sin(-rot.y)
312 local sinroll = sin(-rot.z)
313 local cospitch = cos(rot.x)
314 local cosyaw = cos(rot.y)
315 local cosroll = math.cos(rot.z)
316 -- Rotation matrix that applies yaw, pitch and roll
319 sinyaw * sinpitch * sinroll + cosyaw * cosroll,
320 sinyaw * sinpitch * cosroll - cosyaw * sinroll,
329 cosyaw * sinpitch * sinroll - sinyaw * cosroll,
330 cosyaw * sinpitch * cosroll + sinyaw * sinroll,
334 -- Compute matrix multiplication: `matrix` * `v`
336 matrix[1][1] * v.x + matrix[1][2] * v.y + matrix[1][3] * v.z,
337 matrix[2][1] * v.x + matrix[2][2] * v.y + matrix[2][3] * v.z,
338 matrix[3][1] * v.x + matrix[3][2] * v.y + matrix[3][3] * v.z
342 function vector.dir_to_rotation(forward, up)
343 forward = vector.normalize(forward)
344 local rot = vector.new(math.asin(forward.y), -math.atan2(forward.x, forward.z), 0)
348 assert(vector.dot(forward, up) < 0.000001,
349 "Invalid vectors passed to vector.dir_to_rotation().")
350 up = vector.normalize(up)
351 -- Calculate vector pointing up with roll = 0, just based on forward vector.
352 local forwup = vector.rotate(vector.new(0, 1, 0), rot)
353 -- 'forwup' and 'up' are now in a plane with 'forward' as normal.
354 -- The angle between them is the absolute of the roll value we're looking for.
355 rot.z = vector.angle(forwup, up)
357 -- Since vector.angle never returns a negative value or a value greater
358 -- than math.pi, rot.z has to be inverted sometimes.
359 -- To determine whether this is the case, we rotate the up vector back around
360 -- the forward vector and check if it worked out.
361 local back = vector.rotate_around_axis(up, forward, -rot.z)
363 -- We don't use vector.equals for this because of floating point imprecision.
364 if (back.x - forwup.x) * (back.x - forwup.x) +
365 (back.y - forwup.y) * (back.y - forwup.y) +
366 (back.z - forwup.z) * (back.z - forwup.z) > 0.0000001 then
372 if rawget(_G, "core") and core.set_read_vector and core.set_push_vector then
373 local function read_vector(v)
376 core.set_read_vector(read_vector)
377 core.set_read_vector = nil
379 if rawget(_G, "jit") then
380 -- This is necessary to prevent trace aborts.
381 local function push_vector(x, y, z)
382 return (fast_new(x, y, z))
384 core.set_push_vector(push_vector)
386 core.set_push_vector(fast_new)
388 core.set_push_vector = nil