Page 1 of 1

What is the result of vector.direction ()?

PostPosted: Wed Jun 15, 2016 04:53
by burli
I don't understand what's the result of vector.direction (p1,p2). Can someone explain this for me?

Re: What is the result of vector.direction ()?

PostPosted: Tue Jun 21, 2016 05:50
by AnxiousInfusion
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 ()?

PostPosted: Tue Jun 21, 2016 06:05
by burli
http://dev.minetest.net/vector doesn't have anything to say about it.

That is my problem

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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
}


Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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
}



Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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
}


Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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
}

PostPosted: Tue Jun 21, 2016 09:20
by Hybrid Dog
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

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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)})


l don't know what the result of vector.direction is good for…

Re: What is the result of vector.direction ()?

PostPosted: Tue Jun 21, 2016 09:35
by burli
duane uses this function in his valleys helper mod

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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


it is also used in the identify mod

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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

PostPosted: Wed Jun 22, 2016 10:32
by Hybrid Dog
l guess duane doesn't know what it does and isn't experienced with vectors
this should work better
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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)

[…]


or more streamlined:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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))

[…]


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

Re: What is the result of vector.direction ()?

PostPosted: Wed Jun 22, 2016 16:03
by rubenwardy
Whatever this function does, it doesn't look like it calculates a direction. In linear algebra, a direction is usually:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
normalise(to - from)
where
    normalise(A) = A / |A|

PostPosted: Thu Jun 23, 2016 17:10
by Hybrid Dog
rubenwardy, so a "direction" is always a unit vector?

And the direction from pos to pos (resulting in the zero vector) is an isotropic vector?

Re: What is the result of vector.direction ()?

PostPosted: Fri Jul 15, 2016 17:37
by burli
I'm not sure, but I think this function calculates a vector between two positions

Re: What is the result of vector.direction ()?

PostPosted: Fri Jul 15, 2016 19:40
by Ferk
burli wrote:I'm not sure, but I think this function calculates a vector between two positions

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.

Just as Hybrid Dog and Ruberwardy said, the unitary vector between two points would simply be:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
function vector.direction(a, b)
    return vector.normalize(vector.subtract(b, a))
end

And if instead of 2 points you have 2 vectors that you want to merge into one to get the resulting direction when combined, you would rather just use vector.add and then (if needed) normalize it.

PostPosted: Sat Jul 16, 2016 07:33
by Hybrid Dog