Page 1 of 1

[S] Transform Global Directions to Player Local directions ?

PostPosted: Sat Dec 24, 2016 09:07
by IKRadulov
How to transform Global minetest directions on "x" and "z" to local directions ? I know there are two object functions that I can get the yaw and lookdir of the player . But since I am not good in mathemathics : I don't understand how to do it with "get_look_dir()" couse it returns a unit vector witch I don't know how to handle . I have an idea how to do it with "get_look_yaw()" but minetest debug console tells me different values for yaw then the function "get_look_yaw()".
My concept is to move player forward programaticly dependent on the direction he faces , becouse when I move on the "x" axis with "moveto()" function I will be going in the same direction no matter where the player is facing .

Re: [S] Transform Global Directions to Player Local directio

PostPosted: Mon Jan 02, 2017 14:45
by IKRadulov
Well I solved it after thinking a lot and code and test . Here is the solution with player yaw :

Where :
pos.x is forward
-pos.x is backward
pos.z and -pos.z are left and right

function move(player,pos)
local pi = math.pi
local player_yaw = player:get_look_yaw()
local player_pos = player:getpos()
local pos_sub = 0

if player_yaw > 0 then
-- On local left
if player_yaw > 0.59 and player_yaw < 2.49 then
-- ; Z direction
pos_sub = pos.z
pos.z = pos.x
pos.x = pos_sub
elseif player_yaw > 3.81 and player_yaw < 5.68 then
-- ; -Z direction
pos_sub = pos.z
pos.z = -pos.x
pos.x = pos_sub
elseif player_yaw >=2.50 and player_yaw <= 3.80 then
-- ; -X direction
pos.x = -pos.x
end
else
-- On local right
if player_yaw < -0.59 and player_yaw > -2.49 then
-- ; -Z direction
pos_sub = pos.z
pos.z = -pos.x
pos.x = pos_sub
elseif player_yaw < -3.81 and player_yaw > -5.68 then
-- ; Z direction
pos_sub = pos.z
pos.z = pos.x
pos.x = pos_sub
elseif player_yaw <= -2.50 and player_yaw > -3.80 then
-- ; -X direction
pos.x = -pos.x
end
end

player:moveto( { x = player_pos.x + pos.x , y = player_pos.y + pos.y , z = player_pos.z + pos.z } )
end

Re: [S] Transform Global Directions to Player Local directio

PostPosted: Mon Jan 02, 2017 14:49
by rubenwardy
You should normalise the vector then times it by the speed / distance

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 v = vector.new(lookdirection)
v = vector.normalize(v)
v = vector.mul(v, 1.23)

player:moveto({x = player_pos.x + v.x, y = player_pos.y + v.y, z = player_pos.z + v.z })


If you don't want the player to move upwards, place

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
v.y = 0


between vector.new and vector.normalize.

vector.normalize makes the length of the vector 1 whilst keeping the direction the same
Timesing a normalized vector by a constant makes the length of the new vector the constant

So the above code moves the player by 1.23 in the direction they're looking

Re: [S] Transform Global Directions to Player Local directio

PostPosted: Mon Jan 02, 2017 16:01
by IKRadulov
I don`t understand . Is lookdir for get_look_dir() function ? I don`t realy understand high level mathematics . I will have to read more about vectors and normalization to understand.this and how to apply it in my code . also wouldnt it move the player on same axis if I was to look away from x axis

Re: [S] Transform Global Directions to Player Local directio

PostPosted: Mon Jan 02, 2017 16:59
by rubenwardy
IKRadulov wrote:Is lookdir for get_look_dir() function?


yes

IKRadulov wrote:wouldnt it move the player on same axis if I was to look away from x axis


It moves in the direction the player is facing. So if they're facing north west, they'll move north west

Re: [S] Transform Global Directions to Player Local directio

PostPosted: Mon Jan 02, 2017 17:41
by IKRadulov
Interesting . Thank you very much

Re: [S] Transform Global Directions to Player Local directio

PostPosted: Thu Jan 12, 2017 15:58
by Wuzzy
WARNING:
get_look_yaw() and get_look_pitch() are DEPRECATED since 0.4.15 because they are known to be buggy. They will probably be removed in the next version.

The new methods are get_look_horizontal() and get_look_vertical(). Look them up in lua_api.txt.