Page 1 of 1

Lua rotations and angles [Solved]

Posted: Sun Feb 08, 2015 7:24 pm
by razac920
Hi everyone,

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
Phi here is the angle, in radians, that the projection of the units direction onto the xz plane makes with the x axis, to get technical. For some reason, in this game the y axis, not the z axis, is the "vertical direction".

Re: Lua rotations and angles [Solved]

Posted: Thu Feb 12, 2015 5:16 am
by [RDH]Zerted
I was going to say "Doesn't GetEntityMatrix() give you that info", but looking into past topics the answer seems to be we're still not sure.

At least I'll recommend teleporting some invisible object to an offset of the player. That way the player won't jitter around. It'd be weird if you teleported them into or away from a shot. Also I wouldn't count on the angle calculation to tell you much at the point of death. Maybe a grenade got them, the person was turning to run, it takes time for a missile to fly, etc..

Y is normally vertical on computers. Think of Z as the depth into the screen. GUI components are put at different Z levels to move them forward or behind other GUI components.

Re: Lua rotations and angles [Solved]

Posted: Wed Mar 11, 2015 12:08 pm
by jedimoose32
I found this while looking at some other lua-related stuff today: Frisbeetarian lists what the values of GetEntityMatrix and SetEntityMatrix do.
Zerted, I see you were active in that topic. Sky_216 seemed to indicate that Frisbeetarian's results worked for him. Have we since discovered that this isn't the case? Just curious.

Re: Lua rotations and angles [Solved]

Posted: Thu Mar 12, 2015 12:02 am
by [RDH]Zerted
In that topic the result of GetEntityMatrix is only used as a parameter to SetEntityMatrix or CreateMatrix. I've never seen someone take apart the result into x, y, z, etc... values.

Re: Lua rotations and angles [Solved]

Posted: Thu Mar 12, 2015 12:09 am
by jedimoose32
I see. So we're able to specify the input to SetEntityMatrix but we still can't pick apart the output of GetEntityMatrix. Too bad. Good thing razac knows his trig. :)