What is the result of vector.direction ()?

Post Reply
User avatar
burli
Member
Posts: 1643
Joined: Fri Apr 10, 2015 13:18

What is the result of vector.direction ()?

by burli » Post

I don't understand what's the result of vector.direction (p1,p2). Can someone explain this for me?

User avatar
AccidentallyRhine
Member
Posts: 252
Joined: Sun Aug 02, 2015 05:43

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

by AccidentallyRhine » Post

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?

User avatar
burli
Member
Posts: 1643
Joined: Fri Apr 10, 2015 13:18

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

by burli » Post

http://dev.minetest.net/vector doesn't have anything to say about it.
That is my problem

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
}

User avatar
Hybrid Dog
Member
Posts: 2834
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

by Hybrid Dog » Post

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

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…

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

User avatar
burli
Member
Posts: 1643
Joined: Fri Apr 10, 2015 13:18

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

by burli » Post

duane uses this function in his valleys helper 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
it is also used in the identify mod

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

User avatar
Hybrid Dog
Member
Posts: 2834
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

by Hybrid Dog » Post

l guess duane doesn't know what it does and isn't experienced with vectors
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)

[…]
or more streamlined:

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

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

User avatar
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 ()?

by rubenwardy » Post

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|
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

User avatar
Hybrid Dog
Member
Posts: 2834
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

by Hybrid Dog » Post

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?

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

User avatar
burli
Member
Posts: 1643
Joined: Fri Apr 10, 2015 13:18

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

by burli » Post

I'm not sure, but I think this function calculates a vector between two positions

User avatar
Ferk
Member
Posts: 337
Joined: Tue Aug 18, 2015 17:18
GitHub: Ferk

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

by Ferk » Post

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:

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.
{ ☠ Dungeontest ☠ , ᗧ••myarcade•• }

User avatar
Hybrid Dog
Member
Posts: 2834
Joined: Thu Nov 01, 2012 12:46
GitHub: HybridDog

by Hybrid Dog » Post

https://github.com/minetest/minetest/co ... 642775dR52

ShadowNinja may know more about it.

‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪‮
‮‪

Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests