[S] Transform Global Directions to Player Local directions ?

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

[S] Transform Global Directions to Player Local directions ?

by IKRadulov » Sat Dec 24, 2016 09:07

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 » Mon Jan 02, 2017 14:45

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
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

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

by rubenwardy » Mon Jan 02, 2017 14:49

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
Last edited by rubenwardy on Mon Jan 02, 2017 17:00, edited 1 time in total.
 

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

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

by IKRadulov » Mon Jan 02, 2017 16:01

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
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

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

by rubenwardy » Mon Jan 02, 2017 16:59

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
 

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

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

by IKRadulov » Mon Jan 02, 2017 17:41

Interesting . Thank you very much
 

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

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

by Wuzzy » Thu Jan 12, 2017 15:58

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.
I'm creating MineClone 2, a Minecraft clone for Minetest.
I made the Help modpack, adding in-game help to Minetest.
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 9 guests

cron