[S] Transform Global Directions to Player Local directions ?

Post Reply
IKRadulov
Member
Posts: 19
Joined: Thu Nov 10, 2016 11:02

[S] Transform Global Directions to Player Local directions ?

by IKRadulov » Post

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 .

IKRadulov
Member
Posts: 19
Joined: Thu Nov 10, 2016 11:02

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

by IKRadulov » Post

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

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: [S] Transform Global Directions to Player Local directio

by rubenwardy » Post

You should normalise the vector then times it by the speed / distance

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

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
Last edited by rubenwardy on Mon Jan 02, 2017 17:00, edited 1 time in total.
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

IKRadulov
Member
Posts: 19
Joined: Thu Nov 10, 2016 11:02

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

by IKRadulov » Post

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

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: [S] Transform Global Directions to Player Local directio

by rubenwardy » Post

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

Hello profile reader

IKRadulov
Member
Posts: 19
Joined: Thu Nov 10, 2016 11:02

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

by IKRadulov » Post

Interesting . Thank you very much

User avatar
Wuzzy
Member
Posts: 4803
Joined: Mon Sep 24, 2012 15:01
GitHub: Wuzzy2
IRC: Wuzzy
In-game: Wuzzy
Contact:

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

by Wuzzy » Post

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests