• Getting the angle of the ground under the player
    11 replies, posted
I really suck with vectors. What I need to do is get the angle of the ground under the player, so that it returns 0 when the player is on flat ground and if the player is on a slope then the angle of the slope (preferably relative to the player, so that it returns a negative number when going down the slope)
[lua]SurfaceAngle = util.Trace(LocalPlayer():GetPos(),Vector(0,0,-512),LocalPlayer()).Normal:Angle()[/lua]
[lua] local tr = util.QuickTrace(ply:GetPos(), Vector(0,0,-512), ply) local groundang = tr.Normal:Angle() [/lua] just outputs 90, 0, 0 [editline]8th November 2010[/editline] [QUOTE=thejjokerr;25937144]-snip- What I tried to do was calculate a [b]2d angle[/b] using [b][url=http://wiki.garrysmod.com/?title=Math.cos]Math.cos [img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/url][/b] and Pythagoras.[/QUOTE] That's what I need
Try this one: [lua] local tr = util.QuickTrace(ply:GetPos(), Vector(0,0,-512), ply) local groundang = Angle(math.NormalizeAngle(tr.HitNormal:Angle().p), math.NormalizeAngle(tr.HitNormal:Angle().y), math.NormalizeAngle(tr.HitNormal:Angle().r)) [/lua]
what have I done!!! [lua] local groundangw = (Angle(math.NormalizeAngle(tr.HitNormal:Angle().p) , math.NormalizeAngle(tr.HitNormal:Angle().y), math.NormalizeAngle(tr.HitNormal:Angle().r)) + Angle(0, math.NormalizeAngle(ply:EyeAngles().y), 0)) -- thanx Loures local groundangl = ((math.abs(math.NormalizeAngle(groundangw.p))) - 90) * (math.abs((math.NormalizeAngle(groundangw.y)) / 180 * 2) - 1) [/lua] groundangl outputs the slope angle relative to where the player is facing, which is exactly what I wanted. It's really complicated but it works perfect, thanks for all of the help guys!.
Assuming you have the variables eyeAngles and groundNormal [lua] eyeAngles.Pitch = 0 eyeAngles.Roll = 0 eyeAngles = eyeAngles:Forward() local elevation = math.acos(eyeAngles:Dot(groundNormal))*180/math.pi-90 [/lua] elevation is positive when going up a steep and negative when going down (stretching from 90 to -90) Now go learn some vector math and you'll be able to solve these problems on your own:v:
[QUOTE=ralle105;25939099]Assuming you have the variables eyeAngles and groundNormal [lua] eyeAngles.Pitch = 0 eyeAngles.Roll = 0 eyeAngles = eyeAngles:Forward() local elevation = math.acos(eyeAngles:Dot(groundNormal))*180/math.pi-90 [/lua] elevation is positive when going up a steep and negative when going down (stretching from 90 to -90) Now go learn some vector math and you'll be able to solve these problems on your own:v:[/QUOTE] [lua] local tr = util.QuickTrace(self:GetPos(), Vector(0,0,-512), self) local eyeAngles = Angle(0, self:EyeAngles(), 0):Forward() local groundNormal = tr.HitNormal local elevation = math.acos(eyeAngles:Dot(groundNormal))*180/math.pi-90 [/lua] outputs 0 on 50% of slopes
Take another look at your eyeAngles line.
[lua] local tr = util.QuickTrace(self:GetPos(), Vector(0,0,-512), self) eyeAngles = self:EyeAngles() eyeAngles.Pitch = 0 eyeAngles.Roll = 0 eyeAngles = eyeAngles:Forward() groundNormal = tr.HitNormal local elevation = math.acos(eyeAngles:Dot(groundNormal))*180/math.pi-90 [/lua] fork year [editline]8th November 2010[/editline] [QUOTE=ralle105;25939813]Take another look at your eyeAngles line.[/QUOTE] forgot the .y, I'm retarded
I think this is what I'm going to stick with: [lua] local tr = util.QuickTrace(self:GetPos(), Vector(0,0,-512), self) eyeAngles = self:GetVelocity():GetNormalized():Angle() eyeAngles.Pitch = 0 eyeAngles.Roll = 0 eyeAngles = eyeAngles:Forward() groundNormal = tr.HitNormal local elevation = math.acos(eyeAngles:Dot(groundNormal))*180/math.pi-90 [/lua]
Sorry, you need to Log In to post a reply to this thread.