[solved]What value does player:get_look_horizontal() return?

User avatar
addi
Member
 
Posts: 605
Joined: Thu Sep 20, 2012 03:16

[solved]What value does player:get_look_horizontal() return?

by addi » Sat Jan 21, 2017 08:29

According to lua api.txt I assume it returns the rotation in radians.
so the value ranges from 0 rad to 2*PI rad that is 0° to 360° Degree while rotation is counter-clockwise.
to transform rad into degree I used math.deg(rot).
since I need the roataion clocwise I used 360-math.deg(rot).
So the full part reads like this:

local look_dir = 360-math.deg(player:get_look_horizontal())
I stored the look dir of the players in a mysql database, to make the players visible on a map.
After a day I took a look into the database and saw the players with rotation values much more then 360 Degree.
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
jerry    711
Psycho_Girl    678
schnico    678
puppy101    631
Jausta    625
x-man    519
Frisk    512
Joker    494
Xnorxor    436
pilouptichou    425
SpursFan    406
adoniram    383
maximus    367
Geater681    360
Hotaru    360
Shavon300    360
424346466    360
Julian2AJ    360
evelina    360
Jare    360
Tequila902    360
natstya    360
000000005555555    360
VeRnO    360
Lleopinopalo    360
addi    358
Delacuesta638    357
penguins    355
roza    355


So what value does player:get_look_horizontal() return and what range does it have?
I could not reproduce the behavior of the > 360° players with my client, so does only older clients report a wrong rotation to the server?
Last edited by addi on Wed Jan 25, 2017 14:28, edited 1 time in total.
 

User avatar
stu
Member
 
Posts: 737
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11

Re: What value does player:get_look_horizontal() return?

by stu » Sat Jan 21, 2017 19:18

I seem to recall there were some changes made to this fairly recently, however, I should think that doing modulo 360 on the result of `math.deg` would fix the issue with older clients.
 

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

Re: What value does player:get_look_horizontal() return?

by Wuzzy » Sun Jan 22, 2017 16:01

Can you verify 100% that get_look_horizontal itself returns a value outside of 0 to 2*math.pi in Minetest 0.4.15? Because if yes, that's an obvious bug and I will start to cry inside. ;-(
I'm creating MineClone 2, a Minecraft clone for Minetest.
I made the Help modpack, adding in-game help to Minetest.
 

User avatar
addi
Member
 
Posts: 605
Joined: Thu Sep 20, 2012 03:16

Re: What value does player:get_look_horizontal() return?

by addi » Sun Jan 22, 2017 18:52

I did some further tests and printing out the rotation directly into chat. It seems it ranges from 0 to -2PI! To get something usefull I first had to do math.abs(player:get_look_dir())
I think this should be at least noted somewhere! The mathematical correct way would be 0 to 2PI from x-Axis counter-clockwise. not shure if the behaviour I describe the expected or probably broken behavior.

With the knowledge of the negative value the calculation seemed to work correctly for most cases eg. 360--360 = 720. but It does still not explain why there are some values still smaller then 360° :-/

Not shure if get_look_horizontal() less buggy then the deprecated get_yaw() but at least it lacks on some important documentation.

the function local look_dir = 360-math.deg(math.abs(player:get_look_horizontal())) seems to work correctly right now (look_dir is from 0° to 360°clockwise) Ill complain here again if something unexpected happens again.
 

Byakuren
Member
 
Posts: 441
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri

Re: What value does player:get_look_horizontal() return?

by Byakuren » Mon Jan 23, 2017 23:23

The issue was that when the server received the yaw value from the player, it was using the modulo360f function which will keep negative values negative (and I guess the client must have been using negative yaw values as well).

I suspect that the issue existed before my changes since I think I cut-and-pasted that section, but I don't know for sure. Either way, I put a PR up that fixes this.
Every time a mod API is left undocumented, a koala dies.
 

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

Re: What value does player:get_look_horizontal() return?

by Wuzzy » Tue Jan 24, 2017 00:03

I'm creating MineClone 2, a Minecraft clone for Minetest.
I made the Help modpack, adding in-game help to Minetest.
 

sofar
Member
 
Posts: 781
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: What value does player:get_look_horizontal() return?

by sofar » Tue Jan 24, 2017 00:13

I believe the rotation values can wrap outside 0-2*pi just fine, and the core and engine will not have any issues with it.

If a player starts out looking north, and rotates "right" 10 degrees, then mathematically his angle is -10 degrees. If he then spins around 360 degrees clockwise more, he will be at -370 degrees. Perfectly reasonable. In the same fashion, if a player turns a full circle counterclockwise from 0 degrees, he'll be at 720 degrees rotation.

What the problem was with the old interface that the output of get_look_yaw wasn't the input of set_look_yaw, and so if you "saved" the yaw of a player, and then restored it, the player would rotate 90 degrees for no reason. This was the reason the old interface was deprecated: it forced mod writers to always convert the values.

The new function resolves this problem. You can set the new angle to anything that fits in there, but it shouldn't matter what value it is unless you want to print it out to a user, or convert to "NESW" indicator or something similar.

In that case, all you should do is either limit the value to [0-2pi) or (-pi, pi] or whatever value you find logical.

while rad >= 2* math.pi do
rad = rad - (2 * math.pi)
end
while rad < 0 do
rad = rad + (2 * math.pi)
end
 

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

Re: What value does player:get_look_horizontal() return?

by Wuzzy » Tue Jan 24, 2017 01:09

The thing is, as I stated in my issue, the angle goes from positive to negative at a completely arbitrary point.
After that, it wraps from 0 to +2pi or -2pi to 0, depending on which “side” you are.
Also, the angle does never go beyond +2pi and -2pi. So your “absolute angle” argument does not apply. It just adds another pointless inconvenience. Why not just 0 to +2pi like the rest of the world already agrees on?

If you don't understand what is happening here, just try a simple mod which permanently outputs the get_look_horizontal value for the player and turn around a couple of times, in both directions. You will understand.
IMO this behaviour stinks and also doesn't make much sense to me.
I agree, the new function is definitely an improvement but still a bit WTF.
And yes, I am aware of the simple maths. Its about getting a less WTF output out of these functions. That's all I want.

But worst of all, get_look_horizontal is as poorly documented as get_look_yaw. The returned value range is NOT properly described. This needs to be fixed regardless if you want to change/fix get_look_horizontal or not.
I'm creating MineClone 2, a Minecraft clone for Minetest.
I made the Help modpack, adding in-game help to Minetest.
 

sofar
Member
 
Posts: 781
Joined: Fri Jan 16, 2015 07:31
GitHub: sofar
IRC: sofar
In-game: sofar

Re: What value does player:get_look_horizontal() return?

by sofar » Tue Jan 24, 2017 02:39

Wuzzy wrote:If you don't understand what is happening here, just try a simple mod which permanently outputs the get_look_horizontal value for the player and turn around a couple of times, in both directions. You will understand.
IMO this behaviour stinks and also doesn't make much sense to me..


Unless the value is inconsistent, or incorrect, your complaint is just about mathematical geometry behaving in the way that it does.

In radians, 0 == 2pi == 4pi == -2pi etc. And pi == -pi, and 1/2pi == -3/2pi. No matter where it flaps, it's the same.

I would argue that fixing the output to be clamped is nice, but not mathematically necessary, and therefore suboptimal, as it requires more computation.

Maybe that's being overly pedantic. Math is very unpersonal.
 

User avatar
addi
Member
 
Posts: 605
Joined: Thu Sep 20, 2012 03:16

Re: What value does player:get_look_horizontal() return?

by addi » Tue Jan 24, 2017 08:48

Here is a example mod to make this odd (probably wrong) behavior visible:

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 players = {}

local format= [[rot: %f
math.abs(rot): %f
math.deg(rot): %f
math.deg(math.abs(rot)): %f]]

local function updatehud(player)
    local playername = player:get_player_name();
    local rot = player:get_look_horizontal();
    local abs_rot = math.abs(rot)
    local rot_deg = math.deg(rot)
    local abs_rot_deg = math.deg(abs_rot)
    if not players[playername] then
        players[playername] = player:hud_add({
            text = format:format(rot, abs_rot, rot_deg, abs_rot_deg),
            hud_elem_type = "text",
            position = {x=1,y=0},
            number = 0xFFFFFF,
            alignment = {x=-1,y=1},
            offset = {x=-22, y=20},
        })
    else
        player:hud_change(players[playername], "text", format:format(rot, abs_rot, rot_deg, abs_rot_deg))
    end
end
minetest.register_on_leaveplayer(function(playername)
    players[playername] = nil;
end);

minetest.register_globalstep(function(dtime)
    local conn_players = minetest.get_connected_players();
    for i=1, #conn_players do
        updatehud(conn_players[i])
    end
end)
 

User avatar
stu
Member
 
Posts: 737
Joined: Sat Feb 02, 2013 02:51
GitHub: stujones11

Re: What value does player:get_look_horizontal() return?

by stu » Tue Jan 24, 2017 18:39

I have no idea why you are using `abs` here that looks wrong to me. The following code should give consistent results.
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
deg = (360 + math.deg(yaw)) % 360


Edit: I see you want it to be clockwise, in that case this should do. Posting from a mobile so unable to test right now but hopefully you get the idea.

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
deg = (360 + math.deg(-yaw)) % 360


Unless you have a specific need to use Euler degrees then I would suggest that you work directly with radians or unit vectors wherever possible.
 

User avatar
addi
Member
 
Posts: 605
Joined: Thu Sep 20, 2012 03:16

Re: What value does player:get_look_horizontal() return?

by addi » Wed Jan 25, 2017 14:26

Thanks for you response. I just used the math.abs() to make the negative numbers positive. I didnt know earlier that there where also positive numbers possible, so this was a shortcut for normalization.
As this patch got merged now the rotation is now correctly in the range [0, 2PI) so such tricks are not required anymore :)
To make it clockwise the calculation is now 360-math.deg(player:get_look_horizontal())
Thanks again for your response! And thanks to all other too that did response here or helped to get rid of that bug!
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 6 guests

cron