mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-23 23:53:44 +01:00
Vector3: add move_towards
This commit is contained in:
parent
773e6354ad
commit
133cae587c
17
.tests/Vector3/move_towards.test.lua
Normal file
17
.tests/Vector3/move_towards.test.lua
Normal file
@ -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()
|
||||
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
|
||||
|
||||
|
||||
-- ██████ ██████ ███████ ██████ █████ ████████ ██████ ██████
|
||||
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
|
||||
|
Loading…
Reference in New Issue
Block a user