• Inverse of math.tan()
    12 replies, posted
Hello, I am relatively experienced with lua, but for some reson, I cannot find a way to inverse the math.tan function. I want to find the relative angle of attack on EntityTakeDAmage to get rid of those ugly trapazoids and replace them with damage indicators around the crosshairs, but to do so, I need the angles relative to the person being inflicted. [code] function GM:EntityTakeDamage(ent, attacker, inflictor, damage) if ent:IsPlayer() then local vpos = ent:GetPos() local apos = attacker:GetPos() local vang = ent:EyeAngles() -- used later to add to the world angle we should find local distx = vpos.x - apos.x --adj local disty = vpos.y - apos.y -- opp local ratio = disty / distx -- this is what we need to plug into the inverse tangent function to get the degrees. local raddegree = math.tan(ratio) ^ -1 raddegree = math.Rad2Deg( raddegree ) local dataangle = math.fmod(raddegree, 360)-- simulates arc tangent data = dataangle + vang.yaw ent:ChatPrint("Angle of attack = "..data.." deg.") end --continue with funciton end [/code] Using trig with lua is a little confusing for me. Is there any alternative?
math.pale()? But really, I don't think there is anything.
Check out the math library: [url]http://wiki.garrysmod.com/?title=Math[/url]
I've searched. I don't think there is a way to do it with any inverse of a trig function. There might be a way though afor loop. It would only have to go up to 360. Anyone have ideas? Thank's in advance.
tan(a)^-1 (1/tan(a)) is definitely not equal to arctan(a). The ^-1 is just a maths notation that means arctan. I think you are looking for Math.atan2(disty, distx).
^-1 should be the inverse of the function. and 1/tan(a) is the cot, not the arc tan. Arc tan is defined at math.tan(a) +- math.pi() * x where x is the number of times the period is repeated and math.pi() is the general period of an unmodified tan function in radians. That is for graphs, not triangles. [editline]05:02PM[/editline] [code] local vpos = ent:GetPos() local apos = attacker:GetPos() local vang = ent:EyeAngles() -- used later to add to the world angle we should find local distx = vpos.x - apos.x --adj local disty = vpos.y - apos.y -- opp local test = math.atan2(disty, distx) test = math.Rad2Deg( test ) -- the angle from the origin(world) data = test + vang.yaw --make it relative to the player. ent:ChatPrint("Angle of attack = "..data.." deg.") [/code]
[url=http://wiki.garrysmod.com/?title=Math.atan]math.atan()[/url]?
tan(a)^-1 == cot(a) != atan(a)
Yes. Arc tangent is much different than cot. This seems to be working better. [code] local vpos = ent:GetPos() local apos = attacker:GetPos() local eyeang = ent:EyeAngles() local eyaw = eyeang.y if eyaw > 0 then bearing = 360 - eyaw else bearing = math.abs(eyaw) end local distx = vpos.x - apos.x --adj local disty = vpos.y - apos.y -- opp local test = math.atan2(disty, distx) test = math.Rad2Deg( test ) -- the angle from the origin(world) data = test + bearing --make it relative to the player. data = data - 180 -- for some reason, it's rotated 180 deg. ent:ChatPrint("Angle of attack = "..math.abs(data).." deg.") [/code]
If you didn't need to get the angle of attack, you could just do it easily like this: [lua] local pos = attacker:GetPos():ToScreen() if pos.x > ScrW() then // Draw stuff to the right elseif pos.x < 0 then // Draw stuff to the left end[/lua]
Well, I'm converting this angle to a basig 0- 360 deg angle used todraw and fade a textured damage indicator in the HUD. I alreadu have a tadial compass, so i will also use the degrees to add warnings on the compass.
Are we talking functional inverse or multiplicative inverse? tan(x): Functional inverse is atan(x) (or tan-1(x) which in most calculators LOOKS like the multiplicative inverse but it really denotes the functional inverse). And the multiplicative inverse is cos(x) / sin(x)
functional inverse.
Sorry, you need to Log In to post a reply to this thread.