What is the result of vector.direction ()?
What is the result of vector.direction ()?
I don't understand what's the result of vector.direction (p1,p2). Can someone explain this for me?
- AccidentallyRhine
- Member
- Posts: 252
- Joined: Sun Aug 02, 2015 05:43
Re: What is the result of vector.direction ()?
Hmm, http://dev.minetest.net/vector doesn't have anything to say about it. Can you print p1 and p2 to console just to see what the values are?
Re: What is the result of vector.direction ()?
That is my problemhttp://dev.minetest.net/vector doesn't have anything to say about it.
Code: Select all
print("Vector",dump(vector.direction({x=1,y=2,z=3},{x=4,y=5,z=6})))
Vector {
y = 0.11111111111111,
x = 0.11111111111111,
z = 0.11111111111111
}
Code: Select all
print("Vector",dump(vector.direction({x=1,y=1,z=1},{x=1,y=1,z=1})))
Vector {
y = nan,
x = nan,
z = nan
}
Code: Select all
print("Vector",dump(vector.direction({x=1,y=1,z=1},{x=2,y=2,z=2})))
Vector {
y = 1,
x = 1,
z = 1
}
Code: Select all
print("Vector",dump(vector.direction({x=5,y=3,z=4},{x=1,y=5,z=7})))
Vector {
y = 0.5,
x = -1,
z = 0.75
}
- Hybrid Dog
- Member
- Posts: 2836
- Joined: Thu Nov 01, 2012 12:46
- GitHub: HybridDog
There's the code:
https://github.com/minetest/minetest/bl ... or.lua#L57
Obviously it firstly subtracts pos1 from pos2,
then it gets the abs values of the numbers
after that it divides every number by the biggest abs one, if there is not one biggest but two or three, it divides two or three times
l assume it's supposed to do the same as vector.normalize(vector.subtract(pos2, pos1))
but it does something completely different, it doesn't return a unit vector,
maybe it's supposed to return a vector with coordinates which are inside [-1;1] and at least one of them equal to -1 or 1
but this only works if each number's abs is different because there "if" is used and not "elseif", so if the numbers are e.g. {x=3,y=3,z=3}, instead of returning {x=1,y=1,z=1} it returns {x=1/3^2,y=1/3^2,z=1/3^2} (1/3^2 = 1/9 = 0.111…; see your test result) because it divides by the biggest abs number three times instead of just one time
l don't know what the result of vector.direction is good for…
https://github.com/minetest/minetest/bl ... or.lua#L57
Obviously it firstly subtracts pos1 from pos2,
then it gets the abs values of the numbers
after that it divides every number by the biggest abs one, if there is not one biggest but two or three, it divides two or three times
l assume it's supposed to do the same as vector.normalize(vector.subtract(pos2, pos1))
but it does something completely different, it doesn't return a unit vector,
maybe it's supposed to return a vector with coordinates which are inside [-1;1] and at least one of them equal to -1 or 1
but this only works if each number's abs is different because there "if" is used and not "elseif", so if the numbers are e.g. {x=3,y=3,z=3}, instead of returning {x=1,y=1,z=1} it returns {x=1/3^2,y=1/3^2,z=1/3^2} (1/3^2 = 1/9 = 0.111…; see your test result) because it divides by the biggest abs number three times instead of just one time
Code: Select all
print("Vector",dump(vector.direction({x=1,y=2,z=3},{x=4,y=5,z=6})))
print("Vector",dump(vector.direction({x=0,y=0,z=0},{x=3,y=3,z=3})))
print("Vector2",dump(vector.normalize{x=3,y=3,z=3}))
print("Vector2",dump{x=3/math.sqrt(3^2+3^2+3^2),y=3/math.sqrt(3^2+3^2+3^2),z=3/math.sqrt(3^2+3^2+3^2)})
print("Vector2",dump{x=1/math.sqrt(3),y=1/math.sqrt(3),z=1/math.sqrt(3)})
Re: What is the result of vector.direction ()?
duane uses this function in his valleys helper mod
it is also used in the identify mod
Code: Select all
local function calc_velocity(pos1, pos2, old_vel, power)
local vel = vector.direction(pos1, pos2)
vel = vector.normalize(vel)
vel = vector.multiply(vel, power)
-- Divide by distance
local dist = vector.distance(pos1, pos2)
dist = math.max(dist, 1)
vel = vector.divide(vel, dist)
-- Add old velocity
vel = vector.add(vel, old_vel)
return vel
end
Code: Select all
local function node_contents_within_reach(center, direction, position)
local cluster = node_cuboids(center)
local i, j = box_distance_minimum(direction, position, cluster)
local reach = point_interval(node_ray_intersection(vector.direction(center, position), center), position)
local value = false
if not (i == 0) then
if (reach.x < 4.0) and (reach.y < 4.0) and (reach.z < 4.0) then
if (math.abs(vector.distance(box_side_intersection(direction, position, cluster[i], j), position)) < 5.0) then
value = true
end
end
elseif (i == 0) then
value = true
end
return value
end
- Hybrid Dog
- Member
- Posts: 2836
- Joined: Thu Nov 01, 2012 12:46
- GitHub: HybridDog
l guess duane doesn't know what it does and isn't experienced with vectors
this should work better
or more streamlined:
lf l understand it correctly, the identify mod uses it for a ray iteration function, so a function which calculates positions which "follow" a line,
the technic mining laser uses a ray iteration function,
as far as l know there are lots of ways to do such a rayIter function and lots of bad and wrong working ways,
l made one myself (https://github.com/HybridDog/vector_ext ... t.lua#L100), it's fairly simple but it tests each (of the 3 or less) position if it fits best, so it's not perfect
this should work better
Code: Select all
local function calc_velocity(pos1, pos2, old_vel, power)
local vel = vector.subtract(pos2, pos1)
vel = vector.normalize(vel)
vel = vector.multiply(vel, power)
[…]
Code: Select all
local function calc_velocity(pos1, pos2, old_vel, power)
local vel = vector.subtract(pos2, pos1)
vel = vector.multiply(vel, power / vector.length(vel))
[…]
the technic mining laser uses a ray iteration function,
as far as l know there are lots of ways to do such a rayIter function and lots of bad and wrong working ways,
l made one myself (https://github.com/HybridDog/vector_ext ... t.lua#L100), it's fairly simple but it tests each (of the 3 or less) position if it fits best, so it's not perfect
- rubenwardy
- Moderator
- Posts: 6978
- Joined: Tue Jun 12, 2012 18:11
- GitHub: rubenwardy
- IRC: rubenwardy
- In-game: rubenwardy
- Location: Bristol, United Kingdom
- Contact:
Re: What is the result of vector.direction ()?
Whatever this function does, it doesn't look like it calculates a direction. In linear algebra, a direction is usually:
Code: Select all
normalise(to - from)
where
normalise(A) = A / |A|
- Hybrid Dog
- Member
- Posts: 2836
- Joined: Thu Nov 01, 2012 12:46
- GitHub: HybridDog
Re: What is the result of vector.direction ()?
I'm not sure, but I think this function calculates a vector between two positions
Re: What is the result of vector.direction ()?
I don't think so, for that you only need to subtract the initial position from the final position, vector.subtract(b, a) would return the vector from point a to point b.burli wrote:I'm not sure, but I think this function calculates a vector between two positions
Just as Hybrid Dog and Ruberwardy said, the unitary vector between two points would simply be:
Code: Select all
function vector.direction(a, b)
return vector.normalize(vector.subtract(b, a))
end
- Hybrid Dog
- Member
- Posts: 2836
- Joined: Thu Nov 01, 2012 12:46
- GitHub: HybridDog
Who is online
Users browsing this forum: flowingpoint, rudzik8 and 16 guests