After a long hiatus I am back to try and fix some of the last few bugs in Spacefall, and I ran into one issue in particular:
Does anyone know how to passively find the polar and azimuthal angles of the direction a character is facing, analogous to how position is found: local x,y,z = GetWorldPosition(GetCharacterUnit(player)) ? I wonder if there is a LUA function which does just this.
If not, has anyone ever written code to actively calculate the direction a character is facing? I imagine it could be done by recording the character's original position, teleporting the character a distance forward, recording the character's new position, and then quickly teleporting the character back. Using trigonometry, one should be able to compute those two angles. I could do it myself, but would prefer to avoid reinventing the wheel if possible.
If I do have to write code myself, I will make it accessible to anyone interested. I imagine people could have some uses of knowing if two characters are facing each other, or if one character kills another from behind, or something else.
EDIT: Well I came up with a method that kinda works at getting the phi (azimuthal) angle, since players can't normally change their polar angle:
Code: Select all
local x1,y1,z1 = GetWorldPosition(GetCharacterUnit(player))
SetEntityMatrix(GetCharacterUnit(players[i]),CreateMatrix(0,0,0,0,0,0,-1,GetEntityMatrix(GetCharacterUnit(player)))) -- move the player "forward" to see how his position changes
local x2,y2,z2 = GetWorldPosition(GetCharacterUnit(player))
local phi = math.acos((x2-x1)/math.sqrt(((x2-x1)*(x2-x1))+((z2-z1)*(z2-z1)))) -- yay for lua's math functions
SetEntityMatrix(GetCharacterUnit(players[i]),CreateMatrix(0,0,0,0,0,0,-1,GetEntityMatrix(GetCharacterUnit(player)))) -- move the player back quickly so he doesn't notice anything



