Vector3: add move_towards

This commit is contained in:
Starbeamrainbowlabs 2021-06-26 15:39:47 +01:00
parent 773e6354ad
commit 133cae587c
No known key found for this signature in database
GPG Key ID: 1BE5172E637709C2
2 changed files with 25 additions and 0 deletions

@ -0,0 +1,17 @@
local Vector3 = require("worldeditadditions.utils.vector3")
-- To find these numbers, in Javascript:
-- function t(x) { return Math.sqrt((x*x)*3); }
-- for(let i = 0; i < 1000000000; i++) { let r = t(i); if(Math.floor(r) === r) console.log(`i ${i}, r ${r}`); }
describe("Vector3.move_towards", function()
it("should work with a positive vector", function()
local a = Vector3.new(3, 4, 5)
local b = Vector3.new(10, 10, 10)
assert.are.same(
Vector3.new(5.0022714374157439821, 5.7162326606420661435, 6.4301938838683883048),
a:move_towards(b, 3)
)
end)
end)

@ -175,6 +175,14 @@ function Vector3.limit_to(a, length)
return a:clone() return a:clone()
end end
--- Return a vector that is amount distance towards b from a.
-- @param a Vector3 The vector to move from.
-- @param b Vector3 The vector to move towards.
-- @param amount number The amount to move.
function Vector3.move_towards(a, b, amount)
return a + (b - a):limit_to(amount)
end
-- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████ -- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██